Skip to content

Instantly share code, notes, and snippets.

@jcorrius
Created September 18, 2018 15:19
Show Gist options
  • Save jcorrius/56b4983ca059e69f2d2df38a3a05e225 to your computer and use it in GitHub Desktop.
Save jcorrius/56b4983ca059e69f2d2df38a3a05e225 to your computer and use it in GitHub Desktop.
Variance Ratio Test
import numpy as np
def variance_ratio(ts, lag = 2):
"""
Returns the variance ratio test result
"""
# make sure we are working with an array, convert if necessary
ts = np.asarray(ts)
# Apply the formula to calculate the test
n = len(ts)
mu = sum(ts[1:n]-ts[:n-1])/n;
m=(n-lag+1)*(1-lag/n);
b=sum(np.square(ts[1:n]-ts[:n-1]-mu))/(n-1)
t=sum(np.square(ts[lag:n]-ts[:n-lag]-lag*mu))/m
return t/(lag*b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment