Skip to content

Commit 6cce109

Browse files
align Ridge2Regressor with R version 0.8.0
1 parent fb943f9 commit 6cce109

File tree

2 files changed

+87
-31
lines changed

2 files changed

+87
-31
lines changed

ahead/Ridge2/Ridge2Regressor.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ class Ridge2Regressor():
4444
4545
type_pi: a string;
4646
Type of prediction interval (currently "gaussian",
47-
or "bootstrap" or "blockbootstrap")
47+
"bootstrap", (circular) "blockbootstrap", "movingblockbootstrap", or "rvinecopula")
4848
4949
block_length: an integer
50-
length of block for multivariate circular block bootstrap (`type_pi == blockbootstrap`)
50+
length of block for multivariate block bootstrap (`type_pi == blockbootstrap` or
51+
`type_pi == movingblockbootstrap`)
52+
53+
margins: a string;
54+
distribution of residuals' marginals for `type_pi == rvinecopula`: "empirical" (default),
55+
"gaussian"
5156
5257
B: an integer;
53-
Number of bootstrap replications for `type_pi == bootstrap` or `type_pi == blockbootstrap`
58+
Number of bootstrap replications for `type_pi == bootstrap`, "blockbootstrap",
59+
"movingblockbootstrap", or "rvinecopula"
5460
5561
type_aggregation: a string;
5662
Type of aggregation, ONLY for bootstrapping; either "mean" or "median"
@@ -140,6 +146,7 @@ def __init__(
140146
dropout = 0,
141147
type_pi = "gaussian",
142148
block_length = 3, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
149+
margins = "empirical",
143150
B = 100,
144151
type_aggregation = "mean",
145152
centers = 2,
@@ -161,6 +168,7 @@ def __init__(
161168
self.dropout = dropout
162169
self.type_pi = type_pi
163170
self.block_length = block_length
171+
self.margins = margins
164172
self.B = B
165173
self.type_aggregation = type_aggregation
166174
self.centers = centers # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
@@ -209,29 +217,28 @@ def forecast(self, df, xreg = None):
209217

210218
if xreg is None:
211219

212-
with cv.localconverter(default_converter + config.NONE_CONVERTER):
213-
self.fcast_ = config.AHEAD_PACKAGE.ridge2f(
214-
y,
215-
xreg = xreg,
216-
h=self.h,
217-
level=self.level,
218-
lags=self.lags,
219-
nb_hidden=self.nb_hidden,
220-
nodes_sim=self.nodes_sim,
221-
activ=self.activation,
222-
a=self.a,
223-
lambda_1=self.lambda_1,
224-
lambda_2=self.lambda_2,
225-
dropout=self.dropout,
226-
type_pi=self.type_pi,
227-
block_length=self.block_length, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
228-
B=self.B,
229-
type_aggregation = self.type_aggregation,
230-
centers = self.centers, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
231-
type_clustering = self.type_clustering,
232-
cl=self.cl,
233-
seed=self.seed,
234-
)
220+
self.fcast_ = config.AHEAD_PACKAGE.ridge2f(
221+
y,
222+
h=self.h,
223+
level=self.level,
224+
lags=self.lags,
225+
nb_hidden=self.nb_hidden,
226+
nodes_sim=self.nodes_sim,
227+
activ=self.activation,
228+
a=self.a,
229+
lambda_1=self.lambda_1,
230+
lambda_2=self.lambda_2,
231+
dropout=self.dropout,
232+
type_pi=self.type_pi,
233+
margins=self.margins,
234+
block_length=self.block_length, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
235+
B=self.B,
236+
type_aggregation = self.type_aggregation,
237+
centers = self.centers, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
238+
type_clustering = self.type_clustering,
239+
cl=self.cl,
240+
seed=self.seed,
241+
)
235242

236243
else: # xreg is not None:
237244

@@ -263,6 +270,7 @@ def forecast(self, df, xreg = None):
263270
lambda_2=self.lambda_2,
264271
dropout=self.dropout,
265272
type_pi=self.type_pi,
273+
margins=self.margins,
266274
block_length=self.block_length, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
267275
B=self.B,
268276
type_aggregation = self.type_aggregation,
@@ -295,7 +303,8 @@ def forecast(self, df, xreg = None):
295303
for i in range(n_series)
296304
)
297305

298-
if self.type_pi == "bootstrap" or self.type_pi == "blockbootstrap":
306+
if self.type_pi in ("bootstrap", "blockbootstrap",
307+
"movingblockbootstrap", "rvinecopula"):
299308
self.sims_ = tuple(
300309
np.asarray(self.fcast_.rx2["sims"][i]) for i in range(self.B)
301310
)

examples/ridge2regressor.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
print("\n")
3939
print(d1.ranges_[2])
4040
print("\n")
41-
plot(d1, selected_series="series1")
41+
#plot(d1, selected_series="series1")
4242

4343
print("Example 2 -----")
4444
d2 = Ridge2Regressor(h = h, date_formatting = "original")
@@ -58,7 +58,7 @@
5858
print("\n")
5959
print(d2.ranges_[2])
6060
print("\n")
61-
plot(d2, selected_series="series1")
61+
#plot(d2, selected_series="series1")
6262
print("\n")
6363

6464
print("Example 3 -----")
@@ -94,7 +94,7 @@
9494
print(d3.lower_)
9595
print(d3.upper_)
9696
print("\n")
97-
plot(d3, selected_series="series1")
97+
#plot(d3, selected_series="series1")
9898
print("\n")
9999

100100
print("Example 4 -----")
@@ -141,7 +141,7 @@
141141
print(d4.lower_)
142142
print(d4.upper_)
143143
print("\n")
144-
plot(d4, selected_series="series1")
144+
#plot(d4, selected_series="series1")
145145
print("\n")
146146

147147
# print("Example 5 -----")
@@ -191,3 +191,50 @@
191191
# plot(d5, selected_series="series1")
192192
# print("\n")
193193

194+
195+
print("Example 6 -----")
196+
197+
d6 = Ridge2Regressor(h = 7, date_formatting = "original",
198+
type_pi="rvinecopula",
199+
margins="empirical",
200+
B=10, seed=1)
201+
202+
start = time()
203+
d6.forecast(df, xreg = xreg)
204+
print(f"Elapsed: {time()-start} \n")
205+
206+
print("\n mean: \n")
207+
print(d6.mean_)
208+
209+
print("\n lower: \n")
210+
print(d6.lower_)
211+
212+
print("\n upper: \n")
213+
print(d6.upper_)
214+
215+
print("\n sims: \n")
216+
print(d6.sims_)
217+
print("\n")
218+
219+
print("Example 7 -----")
220+
221+
d7 = Ridge2Regressor(h = 7, date_formatting = "original",
222+
type_pi="rvinecopula",
223+
margins="gaussian",
224+
B=8, seed=1)
225+
226+
start = time()
227+
d7.forecast(df)
228+
print(f"Elapsed: {time()-start} \n")
229+
230+
print("\n mean: \n")
231+
print(d7.mean_)
232+
233+
print("\n lower: \n")
234+
print(d7.lower_)
235+
236+
print("\n upper: \n")
237+
print(d7.upper_)
238+
239+
print("\n sims: \n")
240+
print(d6.sims_)

0 commit comments

Comments
 (0)