-
Notifications
You must be signed in to change notification settings - Fork 1
/
predictARIMA.py
176 lines (141 loc) · 4.6 KB
/
predictARIMA.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
176
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
def lag_view(x, order):
y = x.copy()
x = np.array([y[-(i + order):][:order] for i in range(y.shape[0])])
x = np.stack(x)[::-1][order - 1: -1]
y = y[order:]
return x, y
def ma_process(eps, theta):
theta = np.array([1] + list(theta))[::-1][:, None]
eps_q, _ = lag_view(eps, len(theta))
return eps_q @ theta
def ar_process(eps, phi):
phi = np.r_[1, phi][::-1]
ar = eps.copy()
offset = len(phi)
for i in range(offset, ar.shape[0]):
ar[i - 1] = ar[i - offset: i] @ phi
return ar
def least_squares(x, y):
return np.linalg.inv((x.T @ x)) @ (x.T @ y)
class LinearModel:
def __init__(self, fit_intercept=True):
self.fit_intercept = fit_intercept
self.beta = None
self.intercept_ = None
self.coef_ = None
def _prepare_features(self, x):
if self.fit_intercept:
x = np.hstack((np.ones((x.shape[0], 1)), x))
return x
def fit(self, x, y):
x = self._prepare_features(x)
self.beta = least_squares(x, y)
if self.fit_intercept:
self.intercept_ = self.beta[0]
self.coef_ = self.beta[1:]
else:
self.coef_ = self.beta
def predict(self, x):
x = self._prepare_features(x)
return x @ self.beta
def fit_predict(self, x, y):
self.fit(x, y)
return self.predict(x)
def difference(x, d=1):
if d == 0:
return x
else:
x = np.r_[x[0], np.diff(x)]
return difference(x, d - 1)
def undo_difference(x, d=1):
if d == 1:
return np.cumsum(x)
else:
x = np.cumsum(x)
return undo_difference(x, d - 1)
class ARIMA(LinearModel):
def __init__(self, q, d, p):
super().__init__(True)
self.p = p
self.d = d
self.q = q
self.ar = None
self.resid = None
def prepare_features(self, x):
if self.d > 0:
x = difference(x, self.d)
ar_features = None
ma_features = None
if self.q > 0:
if self.ar is None:
self.ar = ARIMA(0, 0, self.p)
self.ar.fit_predict(x)
eps = self.ar.resid
eps[0] = 0
ma_features, _ = lag_view(np.r_[np.zeros(self.q), eps], self.q)
if self.p > 0:
ar_features = lag_view(np.r_[np.zeros(self.p), x], self.p)[0]
if ar_features is not None and ma_features is not None:
n = min(len(ar_features), len(ma_features))
ar_features = ar_features[:n]
ma_features = ma_features[:n]
features = np.hstack((ar_features, ma_features))
elif ma_features is not None:
n = len(ma_features)
features = ma_features[:n]
else:
n = len(ar_features)
features = ar_features[:n]
return features, x[:n]
def fit(self, x):
features, x = self.prepare_features(x)
super().fit(features, x)
return features
def fit_predict(self, x):
features = self.fit(x)
return self.predict(x, prepared=(features))
def predict(self, x, **kwargs):
features = kwargs.get('prepared', None)
if features is None:
features, x = self.prepare_features(x)
y = super().predict(features)
self.resid = x - y
return self.return_output(y)
def return_output(self, x):
if self.d > 0:
x = undo_difference(x, self.d)
return x
def forecast(self, x, n):
features, x = self.prepare_features(x)
y = super().predict(features)
y = np.r_[y, np.zeros(n)]
for i in range(n):
feat = np.r_[y[-(self.p + n) + i: -n + i], np.zeros(self.q)]
y[x.shape[0] + i] = super().predict(feat[None, :])
return self.return_output(y)
############################################################################################
class predictARIMA():
def getSS(self):
data = pd.read_csv('sih-2022/data/daily.csv')
q = 1
d = 0
p = 3
y = data.Price
m = ARIMA(q, d, p)
m.fit(y)
pred = m.forecast(y, n=1)
return pred[-1]
def getMS(self, steps):
data = pd.read_csv('sih-2022/data/daily.csv')
q = 1
d = 0
p = 3
y = data.Price
m = ARIMA(q, d, p)
m.fit(y)
pred = m.forecast(y, n=steps)
return pred[len(data.Price):]