In random walks, variables should be iid (independently and identically distributed). Therefore, the variance should be constantly scaled throughout the samples. For example, variance sampled in four-bar should be close to four times the variance sampled in one bar. The variance ratio test tests if the variances are scaled constantly.
First, we calculate one period sampled variance as the basis variance of the test. Then we compute variance sampled by 2 bar, 4 bar, 8 bar, 16 bar and 32 bar. We use power of 2 as the sample period here, but any multiple periods are fine as the variance scaling all applies.
After we calculated all the sample variance, we scale the higher sampled period variance in terms of basis variance (one-period sampled variance). We can't actually divide K from the K period sampled variance directly here to scale the variance because it's biased. The following chart shows unbiased scaling variances. After we finished scaling, the higher period and lower period variance should have pretty close values. The closer price is to random walk, the smaller difference they will have.
The variance ratio is just the higher period sampled scaled variance / lower period sampled variance. Because their values are close, the ratio should be around 1. And the variance ratio test will test how different the ratio is from 1. Lo and Mackinley suggest (variance ratio - 1) follows a standard normal distribution with mean 0 and standard deviation σ.
We can get σ from sample size t and period K. σ^2 = 2*(2k-1)* ( k-1 )/3k*T(The formula we use here doesn't take into account heteroscedasticity). Once we know the standard deviation. We can get the z score from dividing σ from vr -1.
We can apply the standard normal test statistics on the z scores. The user can choose the critical value based on the significance. Default is 5% and critical value 1.96. This means when the z score is above 1.96 or below -1.96, the variance ratio is significantly different from 1 and we can reject the random walk hypothesis. And if the z score is between 1.96 and -1.96. We are 95% confident that the price follows a random walk.
As we have different scaled period, some periods might have a significant a z score some might not. Generally if one of the z score is significant we reject the RWH. When all the period is displayed, you can see the different behavior of short term and long term variance. Shorter period like 2 period sample is likely to trend, longer period is likely to show mean reversion. This is very clear when it's applied in a long lookback period. We also recommend users to use lookback period longer than 2*K. Eg: 4-period VRT requires 8 bars of lookback.
When it's applied to 1000 lookback, price(log returns) shows mean reversion behaviour in the long run. (All sample periods show red)
When "Show Multiple Variance Ratio" is ticked off. The indicator shows only one line of variance ratio. And the sampling period can be adjusted in "Period Multiplier". Flash in the background suggests when price doesn't follow a random walk.
In order to test how useful is the variance ratio test, I used the VRT on the random walk data I simulated based on geometric Brownian motion. The data I simulated has a 0.001 drift and constant . It's not a true random walk because I used a pseudo-random number generator in the script, not true random numbers. But It's pretty close. The result shows VRT is between the critical value most of the time which suggests it follows a random walk.
For people that are too lazy to read math. Here is how to use this indicator:
Red means possible negative autocorrelation, mean reversion.
Blue means possible positive autocorrelation, trend.
(2 Period Variance ratio is the 1st order autocorrelation)
(We suggest users open indicator name labels and values on the side so they can see which period variance ratio is significant. )
Above or below critical levels means price does not follow a random walk. Between critical levels means price follows a random walk.
(Main purpose of this test)
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.