-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_series_tests.py
104 lines (81 loc) · 2.21 KB
/
time_series_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
This file implements two functions used for time series tests:
Ljung_Box_Q_test and Check_Stationarity
"""
import numpy as np
import scipy.stats as sts
import matplotlib.pyplot as plt
def autocov(z, j):
"""
autocovariance function of j-th order
z: data series (array)
j: order
"""
n = len(z)
z_mean = np.mean(z)
gamma = 0.0
for t in range(j,n):
gamma += (z[t] - z_mean) * (z[t-j] - z_mean)
gamma = gamma/n
return gamma
def autocorr(z, j):
""""
autocorrelation function of j-th order
z: data series (array)
j: order
"""
gamma_0 = autocov(z,0)
gamma_j = autocov(z,j)
rho = gamma_j / gamma_0
return rho
# Correlogram
def correlogram(z, max_lag=40, plot=False):
# z: data series
# max_lag: number of lags
correlogram = []
for j in range(max_lag+1):
rho_j = autocorr(z,j)
correlogram.append(rho_j)
# stderr
N = len(z)
stderr = 1 / np.sqrt(N)
# plot correlogram
if plot:
plt.figure(figsize=(20,8))
plt.plot(range(max_lag+1), correlogram, "o-")
plt.axhline(2*stderr, ls="--", label='two std errs')
plt.axhline(-2*stderr, ls="--")
plt.title("Correlogram", fontsize=20)
plt.xlabel("Lag")
plt.ylabel("sample correlation coefficient")
plt.legend(fontsize=20)
plt.show()
return correlogram, stderr
def Ljung_Box_Q_test(z,j):
"""
Computes the Ljung-Box Q statistic
and the p-value basd on the Q test
See reference: Fumio Hayashi (2011) Econometrics Ch. 2.10
z: data series (array)
j: order
"""
n = len(z)
q = 0.0
for i in range(1,j+1):
q += autocorr(z,i)**2 / (n-i)
q = q * n *(n+2)
# Q-stat asympototically converges to chi-square distribution
p = 1 - sts.chi2.cdf(q, j)
return q, p
from statsmodels.tsa.stattools import adfuller
def Check_Stationarity(z):
"""
Performs Augmented Dickey-Fuller's test
z: data series (array)
"""
test_result = adfuller(z)
print("ADF Statistic : %f \n" %test_result[0])
print("p-value : %f \n" %test_result[1])
print("Critical values are: \n")
print(test_result[4])
return test_result