Skip to content

Commit 94b49d6

Browse files
add plotting function Pt.6
1 parent 8f3c518 commit 94b49d6

File tree

5 files changed

+78
-54
lines changed

5 files changed

+78
-54
lines changed

ahead/Ridge2/Ridge2Regressor.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22
import rpy2.robjects.conversion as cv
3-
from rpy2.robjects import default_converter, FloatVector, numpy2ri, r
3+
from rpy2.robjects import default_converter, FloatVector, ListVector, numpy2ri, r
44
from .. import config
55
from ..utils import multivariate as mv
66
from ..utils import unimultivariate as umv
@@ -139,7 +139,7 @@ def __init__(
139139
lambda_2 = 0.1,
140140
dropout = 0,
141141
type_pi = "gaussian",
142-
block_length = 3, # can be NULL, but in R
142+
block_length = 3, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
143143
B = 100,
144144
type_aggregation = "mean",
145145
centers = 2,
@@ -163,7 +163,7 @@ def __init__(
163163
self.block_length = block_length
164164
self.B = B
165165
self.type_aggregation = type_aggregation
166-
self.centers = centers # can be NULL, but in R
166+
self.centers = centers # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
167167
self.type_clustering = type_clustering
168168
self.cl = cl
169169
self.date_formatting = date_formatting
@@ -224,10 +224,10 @@ def forecast(self, df, xreg = None):
224224
lambda_2=self.lambda_2,
225225
dropout=self.dropout,
226226
type_pi=self.type_pi,
227-
block_length=self.block_length, # can be NULL, but in R
227+
block_length=self.block_length, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
228228
B=self.B,
229229
type_aggregation = self.type_aggregation,
230-
centers = self.centers, # can be NULL, but in R
230+
centers = self.centers, # can be NULL, but in R (use 0 in R instead of NULL for v0.7.0)
231231
type_clustering = self.type_clustering,
232232
cl=self.cl,
233233
seed=self.seed,
@@ -242,14 +242,16 @@ def forecast(self, df, xreg = None):
242242

243243
is_matrix_xreg = (len(self.xreg_.shape) > 1)
244244

245-
numpy2ri.activate()
245+
numpy2ri.activate()
246+
247+
xreg_ = r.matrix(FloatVector(self.xreg_.flatten()),
248+
byrow = True, nrow = self.xreg_.shape[0],
249+
ncol = self.xreg_.shape[1]) if is_matrix_xreg else r.matrix(FloatVector(self.xreg_.flatten()),
250+
byrow=True, nrow=self.xreg_.shape[0], ncol=1)
246251

247252
self.fcast_ = config.AHEAD_PACKAGE.ridge2f(
248253
y,
249-
xreg = r.matrix(FloatVector(self.xreg_.flatten()),
250-
byrow=True, nrow=self.xreg_.shape[0],
251-
ncol=self.xreg_.shape[1]) if is_matrix_xreg else r.matrix(FloatVector(self.xreg_.flatten()),
252-
byrow=True, nrow=self.xreg_.shape[0], ncol=1),
254+
xreg = xreg_,
253255
h=self.h,
254256
level=self.level,
255257
lags=self.lags,
@@ -268,9 +270,7 @@ def forecast(self, df, xreg = None):
268270
type_clustering = self.type_clustering,
269271
cl=self.cl,
270272
seed=self.seed,
271-
)
272-
273-
print(f"self.fcast_.rx2['x']: {self.fcast_.rx2['x']}")
273+
)
274274

275275
# result -----
276276

ahead/plot/plot.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
from .. import config
22

33
def plot(obj, selected_series = None):
4-
5-
print(f" \n obj.fcast_: {obj.fcast_} \n ")
6-
7-
if selected_series is not None:
8-
try:
9-
config.PLOT_AHEAD(obj.fcast_, selected_series)
10-
except:
11-
pass
4+
if selected_series is not None:
5+
return(config.PLOT_AHEAD(obj.fcast_, selected_series))
126
else:
13-
config.PLOT_BASE(obj.fcast_)
7+
return(config.PLOT_BASE(obj.fcast_))

ahead/utils/multivariate.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import rpy2.robjects as robjects
66
import rpy2.robjects.packages as rpackages
77
from rpy2.robjects.packages import importr
8-
from rpy2.robjects import FloatVector
8+
from rpy2.robjects import FloatVector, r
99
from datetime import datetime
1010
from rpy2.robjects.vectors import StrVector
1111
from .unimultivariate import get_frequency
@@ -21,10 +21,14 @@ def compute_y_mts(df, df_frequency):
2121
input_series_tolist = input_series.tolist()
2222
xx = [item for sublist in input_series_tolist for item in sublist]
2323

24-
return stats.ts(
25-
base.matrix(FloatVector(xx), byrow=True, nrow=len(input_series_tolist)),
26-
frequency=get_frequency(df_frequency)
27-
)
24+
ts = r.matrix(FloatVector(xx),
25+
byrow = True,
26+
nrow = len(input_series_tolist),
27+
ncol = df.shape[1])
28+
29+
ts.colnames = StrVector(df.columns.tolist())
30+
31+
return stats.ts(ts, frequency=get_frequency(df_frequency))
2832

2933

3034
def format_multivariate_forecast(

ahead/utils/univariate.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ def compute_y_ts(df, df_frequency):
1717

1818
input_series = df.to_numpy()
1919

20-
return stats.ts(
20+
ts = stats.ts(
2121
FloatVector(input_series.flatten()),
2222
frequency=get_frequency(df_frequency),
2323
)
2424

25+
ts.colnames = StrVector(df.columns.tolist())
26+
27+
return ts
28+
2529

2630
def format_univariate_forecast(date_formatting, output_dates, horizon, fcast):
2731

examples/ridge2regressor.py

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@
102102
d4 = Ridge2Regressor(h = h, date_formatting = "original",
103103
type_pi="bootstrap", B=3, seed=1)
104104

105-
xreg = np.asarray(range(1, df.shape[0] + 1),
106-
dtype='float64')
105+
# Data frame containing the time series
106+
dataset = {
107+
'date' : ['2001-01-01', '2001-02-01', '2001-03-01', '2001-04-01', '2001-05-01'],
108+
'series1' : [34, 30, 35.6, 33.3, 38.1],
109+
'series2' : [4, 5.5, 5.6, 6.3, 5.1],
110+
'series3' : [100, 100.5, 100.6, 100.2, 100.1]}
111+
df = pd.DataFrame(dataset).set_index('date')
112+
113+
xreg = np.asarray(range(1, df.shape[0] + 1), dtype='float64')
107114

108115
print(f"\n xreg: {xreg} \n")
109116

@@ -134,38 +141,53 @@
134141
print(d4.lower_)
135142
print(d4.upper_)
136143
print("\n")
137-
plot(d4, selected_series="y1")
144+
plot(d4, selected_series="series1")
138145
print("\n")
139146

140-
print("Example 5 -----")
147+
# print("Example 5 -----")
141148

142-
d5 = Ridge2Regressor(h = h, date_formatting = "original",
143-
type_pi="blockbootstrap", B=5)
149+
# d5 = Ridge2Regressor(h = h, date_formatting = "original",
150+
# type_pi="bootstrap", B=3, seed=1)
144151

145-
xreg = np.column_stack((range(df.shape[0]),
146-
[0.2, 0.5, 0.4, 0.3, 0.1]))
152+
# # Data frame containing the time series
153+
# dataset = {
154+
# 'date' : ['2001-01-01', '2001-02-01', '2001-03-01', '2001-04-01', '2001-05-01'],
155+
# 'series1' : [34, 30, 35.6, 33.3, 38.1],
156+
# 'series2' : [4, 5.5, 5.6, 6.3, 5.1],
157+
# 'series3' : [100, 100.5, 100.6, 100.2, 100.1]}
158+
# df = pd.DataFrame(dataset).set_index('date')
147159

148-
start = time()
149-
d5.forecast(df, xreg = xreg)
150-
print(f"Elapsed: {time()-start} \n")
160+
# xreg = np.asarray(range(1, df.shape[0] + 1), dtype='float64')
151161

152-
print("\n mean ---------- \n")
153-
print(d5.fcast_.rx2['mean']) # R output
154-
print(d5.mean_) # Python output
162+
# print(f"\n xreg: {xreg} \n")
155163

156-
print("\n lower ---------- \n")
157-
print(d5.fcast_.rx2['lower']) # R output
158-
print(d5.lower_) # Python output
164+
# start = time()
165+
# d5.forecast(df, xreg = xreg)
166+
# print(f"Elapsed: {time()-start} \n")
159167

160-
print("\n upper ---------- \n")
161-
print(d5.fcast_.rx2['upper']) # R output
162-
print(d5.upper_) # Python output
168+
# print(d5.fcast_.rx2['mean'])
169+
# print(d5.averages_[1])
170+
# print(np.asarray(d5.fcast_.rx2['mean']))
163171

164-
print("\n d5.averages_ ---------- \n")
165-
print(d5.averages_)
172+
# print(d5.fcast_.rx2['sims'][0])
173+
# res = np.asarray(d5.fcast_.rx2['sims'][1])
174+
# print(res)
175+
# print(res.shape)
166176

167-
print("\n d5.ranges_ ---------- \n")
168-
print(d5.ranges_)
177+
# print("\n result_dfs_: \n")
178+
# print(d5.result_dfs_)
179+
180+
# print("\n sims_: \n")
181+
# print(d5.sims_)
182+
183+
# print("\n output_dates_: \n")
184+
# print(d5.output_dates_)
185+
186+
# print("\n mean, lower, upper as numpy arrays: \n")
187+
# print(d5.mean_)
188+
# print(d5.lower_)
189+
# print(d5.upper_)
190+
# print("\n")
191+
# plot(d5, selected_series="series1")
192+
# print("\n")
169193

170-
print("\n")
171-
plot(d5, selected_series="y1")

0 commit comments

Comments
 (0)