Doc: Example in Parametric-VaR
Alan-Hung opened this issue · 1 comments
Alan-Hung commented
Issue description:
The example use the codes:
res = am.fit(disp="off", last_obs="2017-12-31")
-forecasts = res.forecast(start="2018-1-1", reindex=False)
-cond_mean = forecasts.mean["2018":]
-cond_var = forecasts.variance["2018":]
q = am.distribution.ppf([0.01, 0.05], res.params[-2:])
-value_at_risk = -cond_mean.values - np.sqrt(cond_var).values * q[None, :]
-rets_2018 = returns["2018":].copy()
for idx in value_at_risk.index:
if rets_2018[idx] > -value_at_risk.loc[idx, "5%"]:
c.append("#000000")
The rets_2018 starts from 2018-01-02, but value_at_risk is evaluated from "one-step ahead" forecast starts from 2018-01-02. This leads to misalignment in date during comparison.
Modification:
I think it could be modified as followings.
- Remove the start date in forecast, since the last_obs has been set.
- Using .iloc instead of index in comparison.
res = am.fit(disp="off", last_obs="2017-12-31")
+forecasts = res.forecast(reindex=False)
+cond_mean = forecasts.mean
+cond_var = forecasts.variance
q = am.distribution.ppf([0.01, 0.05], res.params[-2:])
value_at_risk = -cond_mean.values - np.sqrt(cond_var).values * q[None, :]
rets_2018 = returns["2018":].copy()
+for idx in range(len(rets_2018)):
+ if rets_2018.iloc[idx] > -value_at_risk["5%"].iloc[idx]:
c.append("#000000")
bashtage commented
Thanks for reporting - I agree this is off by one. One simpler solution is to use align="target"
which then lets the rest of the code go through.
Fixed in the next release.