Time Series Reversibility | Algorithmic Trading Indicators in Python

neurotrader · Intermediate ·💰 FinTech & AI for Finance Professionals ·3y ago

About this lesson

I show two measures for time series reversibility. A time series is reversible when the statistical properties are preserved when series is reversed. High values of these measure indicate irreversibility. Irreversibility gives us an implicit measure for nonlinear dynamics in the price/input. The two measures are quite different computationally but have similar outputs. One is based on horizontal visibility graphs while the other is based on permutation/ordinal patterns. Code: https://github.com/neurotrader888/TimeSeriesReversibility Links https://en.wikipedia.org/wiki/Logistic_map https://en.wikipedia.org/wiki/Time_reversibility https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence Citations Yang, P., Shang, P. Relative asynchronous index: a new measure for time series irreversibility. Nonlinear Dyn 93, 1545–1557 (2018). https://doi.org/10.1007/s11071-018-4275-1 Zanin M, Rodríguez-González A, Menasalvas Ruiz E, Papo D. Assessing Time Series Reversibility through Permutation Patterns. Entropy. 2018; 20(9):665. https://doi.org/10.3390/e20090665 Zanin M, Papo D. Algorithmic Approaches for Assessing Irreversibility in Time Series: Review and Comparison. Entropy (Basel). 2021 Nov 8;23(11):1474. doi: 10.3390/e23111474. PMID: 34828172; PMCID: PMC8622570. The content covered on this channel is NOT to be considered as any financial or investment advice. Past results are not necessarily indicative of future results. This content is purely for education/entertainment.

Full Transcript

in this video we will measure the time reversibility of financial time series using bitcoin price as an example time reversibility as a concept has applications in physics and various areas of math but here we'll focus on time irreversibility as a feature or indicator for financial time series broadly speaking a Time series is reversible if its statistical properties are the same when it's reversed time irreversibility can manifest in many different ways and there have been several different methods devised to measure it in this video we'll look at two of them but first let's look at a reversible and irreversible Series this is a sine wave sine waves are reversible in a moment we'll go over an irreversibility measure called the relative asynchronous index for now just know this measure ranges from zero to one zero meaning the input series is reversible on this sine wave the relative asynchronous index measures 0.003 indicating the series is reversible here's another series generated from a logistic map the logistic map is built from this recurrence relation it's often used as a simple example of chaotic Behavior arising from non-linear Dynamic equations with this series the relative asynchronous index measures 0.15 using irreversibility measures with a rolling window on market prices we can find times where the price is showing signs of non-linear Dynamics or chaotic Behavior perhaps certain trading strategies will perform Better or Worse during these times let's now go over the relative asynchronous Index this measure makes use of the horizontal visibility graph I covered visibility graphs and the adjacency Matrix in my previous video so I will assume you're already familiar here is an example series and its horizontal visibility graphs adjacency Matrix the first step is counting the number of outgoing links on each node an outgoing link from a node is a link to a node at a future time for example node 2 has 4 total links but only three of them are outgoing because there are three links to a node at a future time node 5 the last node has zero outgoing links because there are no nodes in the future for it to link to counting the outgoing links is straightforward with the adjacency Matrix for each row we sum the values that are to the right of the diagonal node 0 has one outgoing link node one has one outgoing link node two has three outgoing links and node three has one outgoing link and so on these are the outgoing link counts for this visibility graph the next step is to reverse the series and do the same thing here's the visibility graph of the regular series and the Reversed series side by side we count the outgoing links in the exact same way using the Reversed series visibility graph this leaves us with two arrays of outgoing link counts one for the forward series and one for the reverse series with these two series we compute the asynchronous index between them the asynchronous index takes two arrays and returns a number it is not necessarily symmetric so the asynchronous index from A to B is not necessarily equal to the asynchronous index from B to a this is is not a hard rule as they can be equal but often they are not to compute the asynchronous index between two arrays first we find the permutation that serves the first array and ascending order here's that permutation these are the indices of the sorted array the lowest value in the array is on index five the second lowest is on index 0 and so on the asynchronous index counts how many times the second array is decreasing where the first array is increasing we use these sorted indices from the first array on the second array to get the count let's look at the code for the asynchronous index we have this function that takes two arrays the two arrays need to have the same length so we have this assertion here we find the permutation that sorts the first array this normalization constant is the maximum amount of ways the two arrays could differ we use it to normalize the final count we keep track of the amount of times the two arrays have different orderings in this variable in version n then we move on to these tested Loops which will check each pair of values in the second array note that J will always be larger than I we find the difference between each pair of values in the second array using the sorted indices from the first array if the two arrays are synchronized having the same order the difference should always be zero or less if they are out of sync the difference will be greater than zero in that case we increase the counts we divide the final count by the normalization constant and return now let's look at the code for the relative asynchronous Index this function takes in a single array or time series we create two visibility graphs one for the regular series and one for the Reversed Order series we build them using the array and the Reversed array this numpy function flip reverses the order of the input then we get the adjacency Matrix of both the visibility graphs now we count the number of outgoing links for both of the graphs we create arrays to hold the number of outgoing Links at each node we Loop through each row of the adjacency Matrix then get the sum of the links to the right of the diagonal this row indexer will get the values from the diagonal to the end of the row now we have the number of outgoing links for the forward and reversed series then we compute the asynchronous index between the outgoing links in both directions from forward to reverse and reverse to forward the relative asynchronous index is the ratio between the minimum and maximum of these values so the ratio will always be one or less we take the logarithm of this ratio and flip the sign This Way the final value will have a minimum possible value of 0 and a maximum possible value of one there's one more short function to go over for computing the relative asynchronous index in a rolling window this function takes an array of closing prices or something else and a look back window we create an output array for the indicator values then we Loop through each value in the array we get the recent window of values as specified by the look back we passed these recent values into the relative asynchronous index function and add the returned value to the array for the current index here is the relative asynchronous index on daily Bitcoin data with a rolling window of one year plotted in Orange this indicator tends to have a high amount of noise so I added some smoothing to it I used a seven period exponential moving average plotted in red this plot isn't necessarily practical for building a trading system but it gives us an idea where the price has historically been more irreversible during the bubble of 2021 we saw much higher readings and throughout 2022 we see much lower readings the paper proposing the relative asynchronous index shows its behavior on a variety of stock indices during the 2008 financial crisis the paper is worth looking at to see Its Behavior in that scenario as well I found this indicator when calculated on hourly data to be effective for filtering mean reversion trades typically when reversibility is lower some of my mean reversion systems tend to do better in my experience meaner version strategies generally did better in 2022 than Trend following strategies and the lower values throughout 2022 align with this observation anyways we'll look at more trouts later let's move on to another measure for irreversibility this next measure is from this paper the measure compares the distribution of permutation patterns also known as ordinal patterns from the forward series and reversed series conveniently I have a video about ordinal patterns already in that video we compute permutation entropy of financial time series this reversibility computation is quite similar I'm going to assume you've watched the ordinal patterns video already just like in the permutation entropy video we find the probability distribution of ordinal patterns for the series then we reverse the series and find the probability distribution of ordinal patterns for the Reversed series The reversibility Measure is the Callback liebler Divergence between the two distributions of ordinal patterns let's look at the code for this measure we have this function which takes an array the m input needs to be fairly long for this calculation to be stable I'd say at least 60 but preferably more we get the Reversed array using the numpy split we use the ordinal patterns function which we went over in the ordinal patterns video to symbolize the series I have it hard-coded to use an embedding dimension of three this is because any more than three would require a massive look back window because the used embedding dimension of three the first two values in The Returned array will be nand so we cut them off I also convert the resulting array to integers we do the same for the Reversed array we use the numpy function bin count to count the number of occurrences of each ordinal pattern we divide these counts by the sides of the input to get probabilities then we compute the relative entropy also known as the Callback liebler Divergence between the two distributions the relative entropy computation will not work if there's a 0 and either of the distributions so we check for it and if there is we return Nan this is why we need the input length to be fairly long we need enough data so each pattern is represented here is the function to compute this measure with a rolling window we take an input and a window size we increase the Look Back by two this is because the hard-coded embedding dimension of 3 needs the extra two pieces of data to be calculated we Loop through the array we get the recent window of data then we pass that recent data to the reversibility function because this function can return Nan we need to handle that in some way so we check for an and value if there is one we just use the last value for reversibility this shouldn't happen often so if you notice large streaks of repeating values in the indicator then a longer look back window is required here is daily Bitcoin data with the ordinal pattern reversibility measure in Orange and the smoothed relative asynchronous index in red both have a look back of one year the two measures have a fair amount of correlation which isn't surprising as they are both attempting to measure the same thing but they do differ at times this ordinal pattern based measure seems to have a bit less noise than the relative asynchronous index when applied in a rolling window there's is no smoothing applied to the orange series here are the same indicators on the hourly time frame with a look back of 168 or one week I found this indicator useful in a meta labeling setup for a mean reversion system these indicators will probably be most useful to those of you utilizing machine learning and you're trading as these reversibility measures are hard to interpret manually to those of you who are utilizing machine learning I imagine you have a large collection of features or indicators that you scan as I'm sure you know a common problem is that many market indicators tend to have a high correlation with each other at least for me these reversibility measures have almost zero correlation with the other features I typically look at so these indicators might be a breath of fresh air for your feature collection on the off chance you find these measures very potent for your particular strategies you may want to check out this paper it is a survey of different methods for measuring irreversibility of Time series the two methods I've shown in this video are certainly not the only two anyways that's it for this one thank you for watching

Original Description

I show two measures for time series reversibility. A time series is reversible when the statistical properties are preserved when series is reversed. High values of these measure indicate irreversibility. Irreversibility gives us an implicit measure for nonlinear dynamics in the price/input. The two measures are quite different computationally but have similar outputs. One is based on horizontal visibility graphs while the other is based on permutation/ordinal patterns. Code: https://github.com/neurotrader888/TimeSeriesReversibility Links https://en.wikipedia.org/wiki/Logistic_map https://en.wikipedia.org/wiki/Time_reversibility https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence Citations Yang, P., Shang, P. Relative asynchronous index: a new measure for time series irreversibility. Nonlinear Dyn 93, 1545–1557 (2018). https://doi.org/10.1007/s11071-018-4275-1 Zanin M, Rodríguez-González A, Menasalvas Ruiz E, Papo D. Assessing Time Series Reversibility through Permutation Patterns. Entropy. 2018; 20(9):665. https://doi.org/10.3390/e20090665 Zanin M, Papo D. Algorithmic Approaches for Assessing Irreversibility in Time Series: Review and Comparison. Entropy (Basel). 2021 Nov 8;23(11):1474. doi: 10.3390/e23111474. PMID: 34828172; PMCID: PMC8622570. The content covered on this channel is NOT to be considered as any financial or investment advice. Past results are not necessarily indicative of future results. This content is purely for education/entertainment.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

📰
This region wants to build Africa’s most connected fintech ecosystem
WAEMU aims to create Africa's most connected fintech ecosystem, addressing interoperability issues, and key stakeholders discuss the future of fintech in the region
TechCabal
📰
Why South African banks still charge for instant payments
South African banks' instant payment fees spark debate on customer transaction costs and pricing strategies
TechCabal
📰
Accrue targets African businesses with stablecoin-powered cross-border banking platform
Accrue's stablecoin-powered platform facilitates cross-border banking for African businesses, reflecting a shift in fintech approaches to international commerce
TechCabal
📰
Why “faster payments” is the wrong frame for stablecoins.
Learn why 'faster payments' is an outdated concept for stablecoins and how to reframe the conversation around risk management
Medium · Startup
Up next
🏕️Best Retirement Planning Software for Individuals Analyzed
Modest Money - Reviews & Coupons
Watch →