-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mts_timeseries_2.py
209 lines (178 loc) · 7.37 KB
/
mts_timeseries_2.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import subprocess
import sys
import nnetsauce as ns
import numpy as np
import pandas as pd
from scipy import stats
from sklearn import datasets, metrics
from sklearn import linear_model
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.ensemble import RandomForestRegressor
from time import time
print(f"\n ----- Running: {os.path.basename(__file__)}... ----- \n")
subprocess.check_call([sys.executable, "-m", "pip", "install", "statsmodels"])
from statsmodels.stats.diagnostic import acorr_ljungbox
np.random.seed(1235)
# url = "https://raw.githubusercontent.com/selva86/datasets/master/Raotbl6.csv"
# url = "/Users/t/Documents/datasets/time_series/multivariate/Raotbl6.csv"
url = "https://github.com/ritvikmath/Time-Series-Analysis/raw/master/ice_cream_vs_heater.csv"
df = pd.read_csv(url)
#df.set_index('date', inplace=True)
df.set_index('Month', inplace=True)
df.index.rename('date')
print(df.shape)
print(198*0.8)
# df_train = df.iloc[0:97,]
# df_test = df.iloc[97:123,]
df_train = df.iloc[0:158,]
df_test = df.iloc[158:198,]
print(df_train.head())
print(df_train.tail())
print(df_test.head())
print(df_test.tail())
print(f"\n 1. fit BayesianRidge: ------- \n")
regr = linear_model.BayesianRidge()
obj_MTS = ns.MTS(regr, lags = 1, n_hidden_features=5, verbose = 1)
obj_MTS.fit(df_train.values)
print("\n")
print(obj_MTS.predict(h=5, return_std=True))
# print(f" stats.describe(obj_MTS.residuals_, axis=0, bias=False) \n {stats.describe(obj_MTS.residuals_, axis=0, bias=False)} ")
# print([acorr_ljungbox(obj_MTS.residuals_[:,i], boxpierce=True, auto_lag=True, return_df=True) for i in range(obj_MTS.residuals_.shape[1])])
print(f"\n 2. fit ARDRegression: ------- \n")
regr2 = linear_model.ARDRegression()
obj_MTS2 = ns.MTS(regr2, lags = 1, n_hidden_features=5,
replications=10, kernel='gaussian',
seed=2324, verbose = 1)
start = time()
obj_MTS2.fit(df_train.values)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(obj_MTS2.get_params())
print("\n\n")
print(obj_MTS2.kde_)
print("\n\n")
print(obj_MTS2.predict(h=5, return_std=True))
print("\n\n")
print(f"------- obj_MTS2.residuals_: {obj_MTS2.residuals_}")
print("\n\n")
print(f"\n 3. fit ElasticNet: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="kde")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(obj_MTS3.kde_)
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 4. fit ElasticNet SCP: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="scp-kde")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(obj_MTS3.kde_)
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 4. fit ElasticNet SCP2: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="scp2-kde")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(obj_MTS3.kde_)
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 3. fit ElasticNet bootstrap: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="bootstrap")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 4. fit ElasticNet SCP bootstrap: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="scp-bootstrap")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 4. fit ElasticNet SCP2 bootstrap: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="scp2-bootstrap")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 3. fit ElasticNet block bootstrap: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="block-bootstrap")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 4. fit ElasticNet SCP block bootstrap: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="scp-block-bootstrap")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")
print(f"\n 4. fit ElasticNet SCP2 block bootstrap: ------- \n")
regr3 = linear_model.ElasticNet()
obj_MTS3 = ns.MTS(regr3, lags = 3, n_hidden_features=7,
replications=10, kernel='gaussian',
seed=24, verbose = 1, type_pi="scp2-block-bootstrap")
start = time()
obj_MTS3.fit(df_train)
print(f"Elapsed {time()-start} s")
print("\n\n")
print(f"obj_MTS3.predict(h=5): {obj_MTS3.predict(h=5)}")
print(f" Predictive simulations #1 {obj_MTS3.sims_[0]}")
print(f" Predictive simulations #2 {obj_MTS3.sims_[1]}")
print(f" Predictive simulations #3 {obj_MTS3.sims_[2]}")