AI Summary
This video explores autocorrelation as a tool for time series analysis, using Python and Y-Finance to analyze Apple stock returns and trading volume. It demonstrates how to calculate autocorrelation for different lags and interprets the results to assess predictability and volatility clustering.
Chapters
Autocorrelation measures how a variable relates to its past values using lagged values, e.g., correlation between current return and return from five rows ago.
Positive autocorrelation means high values follow high values, negative means high follows low, and zero indicates no correlation.
Using Apple stock data from 2022 to 2025, returns are computed from close prices. Autocorrelation is obtained with a single line: df['return'].autocorr(lag=1).
Lag 1 autocorrelation is 0.015, lag 5 is 0.026. These values are close to zero, indicating weak autocorrelation and limited predictive power for returns.
Using absolute returns, lag 1 autocorrelation is around 0.14, about 8 times larger, suggesting volatility clustering. This is more useful for volatility modeling than for predicting returns.
Relative volume is calculated as current volume divided by 30-day rolling mean. Lag 1 autocorrelation is 0.36, indicating strong persistence, while lag 5 drops to 0.014, showing rapid decay.
Volume autocorrelation can help detect news-driven activity or unusual trading patterns, as volume tends to persist for a day but not five days.
Autocorrelation analysis reveals that stock returns are weakly predictable, but absolute returns and relative volume show stronger patterns useful for volatility and activity detection. This technique provides a practical way to assess market behavior using Python.
Mentioned in this Video
Tutorial Checklist
💡 Key Takeaways
Autocorrelation Defined
Provides a clear, foundational definition that sets the stage for the entire analysis.
00:19Weak Return Predictability
Empirical result showing returns have low autocorrelation, challenging the idea of easy prediction.
01:45Volatility Clustering
Absolute returns show 8x higher autocorrelation, highlighting a key pattern in financial markets.
02:54Volume Persistence
Relative volume shows strong short-term persistence (0.36) but decays quickly, useful for detecting unusual activity.
03:50Full Transcript
[00:00] Can yesterday's market behaviour tell us anything about tomorrow? Today we'll explore autocorrelation, one of the most important tools in time series analysis, and using Python with Y-Finance to see what stock returns and trading volume can actually predict.
[00:19] So let's get right into it. Autocorrelation is a measure of how a variable it relations to its past values. So this is done with lagged values. It is the correlation between
[00:32] for example the return and then the return from five rows ago. So what does this value mean? A positive autocorrelation means that a higher value on one value tends to follow a high value
[00:47] and low values tend to follow low values. Negative means a higher variable will tend to follow lower values, and then a zero means there's not really a correlation detected.
[01:00] Let's work this out in Python code now. We will first take our data frame of the Apple stocks from the start of 2022 to 2025 Now from last time we make the return instead of close as relative values are generally better to
[01:17] use. Now let's get our autocorrelation. We get this with one simple line. df return the value we want the autocorrelation for, .autocall and then give
[01:31] the lag in brackets. Let's say lag equals 1. So this will see if there is a correlation between a current return value and the one before it. Let's also take another auto correlation
[01:45] of 5 too and print them out to have a look. Lag 1 has a 0.015 autocorrelation and a 0.026 five. So therefore, there appears to be a more correlation between current rows and
[02:02] five rows than just the row before it. However, overall, these values are pretty close to zero So they weak And therefore we can get too much information from it Generally returns show a weak autocorrelation in finance So this doesn show that a very predictive nature Compared to if we had a high autocorrelation it would mean that when the
[02:25] return is quite high, it tends to stay quite high, making it easier to predict. But generally, just how life goes, this shows that finance isn't very predictable in this regard.
[02:37] What about a difference matrix? Take the absolute return with simple df.return.abs. Let's take our lag1 and lag5 once again. These values are quite a lot bigger than previously, around
[02:54] 0.14, about 8 times the size, so slightly more positive correlation. This means that often larger movements in return seem to cluster together. Now this metric says a lot more
[03:08] about volatility or risk rather than the predictive nature we are looking for. This means that return autocorrelation may not be used in models, however the absolute return we can
[03:20] for sure find useful when it comes to volatility modelling which are just as important What is another important metric to test The relative volume We can get this with the current volume divided by the rolling volume mean
[03:35] let's say 30 days. So to this write in Python, df relative volume equals df volume divided by df volume dot rolling 30 dot mean. Let's print this autocorrelation for lags one and five.
[03:50] And as we can see, we get 0.36 for lag 1, which is pretty strong for finance, and this means that the volume increase or decrease tends to stay the same for the next day.
[04:02] However, lag 5 gave a low 0.014. This is quickly reduced, so we could make some decent predictions on volume that the next day's volume will tend to be the same, but completely irrelevant in about 5 days time.
[04:20] We could use this details to detect news driven activity or detecting unusual activity. And then that is everything we are going through for today's video. If you found this video helpful, please like the video and subscribe to support the channel.
[04:35] And I hope to see you in another video.