Skip to content

Commit cb41040

Browse files
committed
whenxuan: fix the visualization for the forecasting of emd
1 parent f6dd902 commit cb41040

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

‎examples/emd/emd_forecasting.py‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ def mape(y_true, y_pred, eps=1e-8):
9999
return float(np.mean(np.abs((y_true - y_pred) / denom)) * 100.0)
100100

101101

102+
def join_forecast(t_hist, y_hist, t_fc, y_fc):
103+
"""Prepend the last history sample so the forecast line meets the train tail."""
104+
t_hist = np.asarray(t_hist, dtype=float).ravel()
105+
y_hist = np.asarray(y_hist, dtype=float).ravel()
106+
t_fc = np.asarray(t_fc, dtype=float).ravel()
107+
y_fc = np.asarray(y_fc, dtype=float).ravel()
108+
return (
109+
np.concatenate([t_hist[-1:], t_fc]),
110+
np.concatenate([y_hist[-1:], y_fc]),
111+
)
112+
113+
102114
def make_lagged(y, lags):
103115
y = np.asarray(y, dtype=float)
104116
X, t = [], []
@@ -238,7 +250,8 @@ def emd_hybrid_forecast(
238250

239251
fig, ax = plt.subplots(figsize=(10, 3.0))
240252
ax.plot(t_train, train, "k", lw=1.0, label="train")
241-
ax.plot(t_test, test, "C3", lw=1.2, label="test (to forecast)")
253+
t_te, y_te = join_forecast(t_train, train, t_test, test)
254+
ax.plot(t_te, y_te, "C3", lw=1.2, label="test (to forecast)")
242255
ax.axvline(t_train[-1], color="0.5", ls="--", lw=0.9)
243256
ax.legend(fontsize=9)
244257
ax.set_title("Train / test split")
@@ -303,14 +316,18 @@ def emd_hybrid_forecast(
303316
axs[i].plot(
304317
t_train[-80:], comps_train[i][-80:], color="0.4", lw=1.0, label="train (tail)"
305318
)
306-
axs[i].plot(
307-
t_test, result_ar.component_forecasts[i], "C3", lw=1.2, label="forecast"
319+
t_fc, y_fc = join_forecast(
320+
t_train, comps_train[i], t_test, result_ar.component_forecasts[i]
308321
)
322+
axs[i].plot(t_fc, y_fc, "C3", lw=1.2, label="forecast")
309323
axs[i].axvline(t_train[-1], color="0.5", ls="--", lw=0.8)
310324
axs[i].set_ylabel(labels[i])
311325
axs[i].legend(fontsize=7, loc="upper right")
326+
axs[-1].plot(t_train[-80:], train[-80:], color="0.4", lw=1.0, label="train (tail)")
312327
axs[-1].plot(t_test, test, "k", lw=1.2, label="true test")
313-
axs[-1].plot(t_test, result_ar.yhat, "C3", lw=1.3, label="EMD-AR aggregate")
328+
t_fc, y_fc = join_forecast(t_train, train, t_test, result_ar.yhat)
329+
axs[-1].plot(t_fc, y_fc, "C3", lw=1.3, label="EMD-AR aggregate")
330+
axs[-1].axvline(t_train[-1], color="0.5", ls="--", lw=0.8)
314331
axs[-1].legend(fontsize=8)
315332
axs[-1].set_ylabel("x")
316333
axs[-1].set_xlabel("Time index")

0 commit comments

Comments
 (0)