Variance Ratio Test [pig]

In Finance, people usually assume the price follows a random walk or more precisely geometric Brownian motion. In 1988, Lo and MacKinlay came up with the variance ratio test to refute the random walk hypothesis and efficient market hypothesis. The variance ratio test is a simple test for market efficiency, autocorrelation, and whether price follows a random walk.

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 volatility . 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)
Release Notes: Added vrt1 to vrt5 sampling period manual enter input function.
Skript med en öppen källkod

I sann TradingView-anda har författaren till detta skript publicerat det som öppen källkod, så att handlare kan förstå och verifiera det. Skål för författaren! Du kan använda den gratis men återanvändning av den här koden i en publikation regleras av våra Ordningsregler. Du kan göra den till favorit för att använda den i ett diagram.

Vill du använda det här skriptet i ett diagram?
Our Website🅱🅰🐷:

Sign up on our website to get access to our indicators.

Join Our Discord for further discussions:


Life after BAPIG:

( •_•)>⌐■-■

+9 Svara
Impressive! your skills kill!
+8 Svara
balipour ricmos
@ricmos, thanks
"Lazy" a bit harsh I'd say. People also have lives bruh lol. We do the math, so you don't have to (or need too). But, Also these things will allow you to still get down and dirty with the math if you're anything like us.
Good luck trading , your tools couldn't be any better. This is a movement, Thank you all.
+7 Svara
@balipour, one hurdle I face when reading an exhaustive text is floundering for definitions, wikipedia is great because they offer links to words that are liable to trip you up. Most people just glaze over and ask are we there yet (you know what I mean), but I love learning and understanding it +this is the stuff I live for.
+1 Svara
balipour UnknownUnicorn6941191
@UnknownUnicorn6941191, no worries bro. we get it.
balipour balipour
@balipour, first part im referring to our brilliant team. I can see some stuff has rubbed off a bit though. lol
+1 Svara
balipour balipour
The description on its own is amazing, not to mention the indicator.

Awesome work bapig team! Thank you!
+6 Svara
balipour SemiQuasi
@SemiQuasi, it's great when a great coder gets it. Thank you.
+1 Svara