-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathADFuller.py
175 lines (139 loc) · 5.57 KB
/
ADFuller.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#coding:utf-8
"""Augmented Dickey-Fuller test implemented using Pytorch"""
import math
import torch
from statsmodels.tsa.adfvalues import mackinnonp, mackinnoncrit
def ad_fuller(series, maxlag=None):
"""Get series and return the p-value and the t-stat of the coefficient"""
if maxlag is None:
n = int((len(series) - 1) ** (1./3))
elif maxlag < 1:
n = 1
else:
n = maxlag
# Putting the X values on a Tensor with Double as type
X = torch.tensor(series)
X = X.type(torch.DoubleTensor)
# Generating the lagged tensor to calculate the difference
X_1 = X.narrow(0, 1, X.shape[0] - 1)
# Re-sizing the x values to get the difference
X = X.narrow(0, 0, X.shape[0] - 1)
dX = X_1 - X
# Generating the lagged difference tensors
# and concatenating the lagged tensors into a single one
for i in range(1, n + 1):
lagged_n = dX.narrow(0, n - i, (dX.shape[0] - n))
lagged_reshape = torch.reshape(lagged_n, (lagged_n.shape[0], 1))
if i == 1:
lagged_tensors = lagged_reshape
else:
lagged_tensors = torch.cat((lagged_tensors, lagged_reshape), 1)
# Reshaping the X and the difference tensor
# to match the dimension of the lagged ones
X = X.narrow(0, 0, X.shape[0] - n)
dX = dX.narrow(0, n, dX.shape[0] - n)
dX = torch.reshape(dX, (dX.shape[0], 1))
# Concatenating the lagged tensors to the X one
# and adding a column full of ones for the Linear Regression
X = torch.cat((torch.reshape(X, (X.shape[0], 1)), lagged_tensors), 1)
ones_columns = torch.ones((X.shape[0], 1))
X_ = torch.cat((X, torch.ones_like(ones_columns, dtype=torch.float64)), 1)
nobs = X_.shape[0]
# Xb = y -> Xt.X.b = Xt.y -> b = (Xt.X)^-1.Xt.y
coeff = torch.mm(torch.mm(torch.inverse(
torch.mm(torch.t(X_), X_)), torch.t(X_)), dX)
std_error = get_std_error(X_, dX, coeff)
coeff_std_err = get_coeff_std_error(X_, std_error, coeff)[0]
t_stat = coeff[0]/coeff_std_err
p_value = mackinnonp(t_stat.item(), regression="c", N=1)
critvalues = mackinnoncrit(N=1, regression="c", nobs=nobs)
critvalues = {
"1%" : critvalues[0],
"5%" : critvalues[1],
"10%" : critvalues[2]
}
return t_stat.item(), p_value, n, nobs, critvalues
def get_coeff_std_error(X, std_error, p):
"""Receive the regression standard error
and calculate for the coefficient p"""
std_coeff = []
for i in range(len(p)):
s = torch.inverse(torch.mm(torch.t(X), X))[i][i] * (std_error ** 2)
s = math.sqrt(s.item())
std_coeff.append(s)
return std_coeff
def get_std_error(X, label, p):
"""Get the regression standard error"""
std_error = 0
y_new = torch.mm(X, p)
for i in range(len(X)):
diff = (label[i][0] - y_new[i][0]) ** 2
std_error += diff.item()
std_error = math.sqrt(std_error/X.shape[0])
return std_error
def test_shape(series, maxlag=None):
"""Get series and return the p-value and the t-stat of the coefficient"""
if maxlag is None:
n = int(12 * ((len(series)/100) ** (1./4)))
elif maxlag < 1:
n = 1
else:
n = maxlag
# Putting the X values on a Tensor with Double as type
X = torch.tensor(series)
X = X.type(torch.DoubleTensor)
# Generating the lagged tensor to calculate the difference
X_1 = X.narrow(0, 1, X.shape[0] - 1)
# Re-sizing the x values to get the difference
X = X.narrow(0, 0, X.shape[0] - 1)
dX = X_1 - X
expanded_dX = toeplitz_like(dX, n)
X = torch.cat((X.narrow(0, 0, expanded_dX.shape[0]).unsqueeze(1), expanded_dX), dim=1)
'''# Generating the lagged difference tensors
# and concatenating the lagged tensors into a single one
for i in range(1, n + 1):
lagged_n = dX.narrow(0, n - i, (dX.shape[0] - n))
lagged_reshape = torch.reshape(lagged_n, (lagged_n.shape[0], 1))
if i == 1:
lagged_tensors = lagged_reshape
else:
lagged_tensors = torch.cat((lagged_tensors, lagged_reshape), 1)
# Reshaping the X and the difference tensor
# to match the dimension of the lagged ones
X = X.narrow(0, 0, X.shape[0] - n)
X = torch.cat((torch.reshape(X, (X.shape[0], 1)), lagged_tensors), 1)'''
dX = dX.narrow(0, n, dX.shape[0] - n).unsqueeze(0).t()
print(dX.shape)
ones_columns = torch.ones((X.shape[0], 1))
X_ = torch.cat((X, torch.ones_like(ones_columns, dtype=torch.float64)), 1)
nobs = X_.shape[0]
# Xb = y -> Xt.X.b = Xt.y -> b = (Xt.X)^-1.Xt.y
coeff = torch.mm(torch.mm(torch.inverse(
torch.mm(torch.t(X_), X_)), torch.t(X_)), dX)
std_error = get_std_error(X_, dX, coeff)
coeff_std_err = get_coeff_std_error(X_, std_error, coeff)[0]
t_stat = coeff[0]/coeff_std_err
p_value = mackinnonp(t_stat.item(), regression="c", N=1)
critvalues = mackinnoncrit(N=1, regression="c", nobs=nobs)
critvalues = {
"1%" : critvalues[0],
"5%" : critvalues[1],
"10%" : critvalues[2]
}
return t_stat.item(), p_value, n, nobs, critvalues
def toeplitz(v):
c = v.view(-1)
vals = torch.cat((torch.flip(c, [0]), c[1:]))
a = torch.arange(c.shape[0]).unsqueeze(0).t()
b = torch.arange(c.shape[0] - 1, -1, step=-1).unsqueeze(0)
indx = a + b
return vals[indx]
def toeplitz_like(x, n):
r = x
stop = x.shape[0] - 1
if n < stop:
stop = n
else:
stop = 2
r = toeplitz(r)
return r.narrow(1, 0, stop).narrow(0, stop - 1, r.shape[0] - stop)