File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22from datetime import datetime
33
44import holidays
5+ import numpy as np
56import pandas as pd
67from statsmodels .tsa .statespace .sarimax import SARIMAX
78
@@ -94,7 +95,21 @@ def get_exog_flags(index: pd.DatetimeIndex) -> pd.DataFrame:
9495
9596 results = pd .DataFrame (index = forecast_index )
9697 results ["mean" ] = forecast .predicted_mean
97- results ["se" ] = forecast .var_pred_mean ** 0.5
98+
99+ # SE estimation: take the max of three sources to prevent overconfident bounds.
100+ # 1. Model SE (var_pred_mean): can be artificially small when AR/MA nearly cancel
101+ # 2. Residual SE: the model's actual 1-step prediction errors (after Kalman burn-in)
102+ # 3. Raw diff SE: std of first-differences of the original data — captures inherent
103+ # point-to-point variability that the model may underestimate
104+ model_se = forecast .var_pred_mean ** 0.5
105+ order_sum = model .k_ar + model .k_diff + model .k_ma
106+ burn_in = max (order_sum , 3 )
107+ usable_residuals = fitted_model .resid .iloc [burn_in :]
108+ resid_se = usable_residuals .std () if len (usable_residuals ) >= 5 else 0.0
109+ raw_diffs = np .diff (history .iloc [:, 0 ].values )
110+ raw_diff_se = np .std (raw_diffs , ddof = 1 ) if len (raw_diffs ) > 1 else 0.0
111+ results ["se" ] = np .maximum (model_se , max (resid_se , raw_diff_se ))
112+
98113 return results
99114
100115
You can’t perform that action at this time.
0 commit comments