Skip to content

Commit

Permalink
Rolling window forecast with rolling demean bashtage#633
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincyi committed Mar 15, 2023
1 parent 274143e commit 88a118f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
17 changes: 13 additions & 4 deletions arch/univariate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ def fix(
params: Sequence[float] | ArrayLike1D,
first_obs: int | DateLike | None = None,
last_obs: int | DateLike | None = None,
demean: bool = False,
) -> ARCHModelFixedResult:
"""
Allows an ARCHModelFixedResult to be constructed from fixed parameters.
Expand All @@ -481,6 +482,8 @@ def fix(
First observation to use when fixing model
last_obs : {int, str, datetime, Timestamp}
Last observation to use when fixing model
demean: bool
Indicate to demean the fitting data y. Default is `False`.
Returns
-------
Expand All @@ -493,7 +496,7 @@ def fix(
"""
v = self.volatility

self._adjust_sample(first_obs, last_obs)
self._adjust_sample(first_obs, last_obs, demean)
resids = np.asarray(self.resids(self.starting_values()), dtype=float)
sigma2 = np.zeros_like(resids)
backcast = v.backcast(resids)
Expand Down Expand Up @@ -536,7 +539,10 @@ def fix(

@abstractmethod
def _adjust_sample(
self, first_obs: int | DateLike | None, last_obs: int | DateLike | None
self,
first_obs: int | DateLike | None,
last_obs: int | DateLike | None,
demean: bool,
) -> None:
"""
Performs sample adjustment for estimation
Expand All @@ -547,6 +553,8 @@ def _adjust_sample(
First observation to use when estimating model
last_obs : {int, str, datetime, datetime64, Timestamp}
Last observation to use when estimating model
demean: bool
Perform demean on fitting data y
Notes
-----
Expand All @@ -565,6 +573,7 @@ def fit(
tol: float | None = None,
options: dict[str, Any] | None = None,
backcast: None | float | Float64Array = None,
demean: bool = False,
) -> ARCHModelResult:
r"""
Estimate model parameters
Expand Down Expand Up @@ -627,14 +636,14 @@ def fit(
v.closed_form and d.num_params == 0 and isinstance(v, ConstantVariance)
)

self._adjust_sample(first_obs, last_obs)
self._adjust_sample(first_obs, last_obs, demean)

resids = np.asarray(self.resids(self.starting_values()), dtype=float)
self._check_scale(resids)
if self.scale != 1.0:
# Scale changed, rescale data and reset model
self._y = cast(np.ndarray, self.scale * np.asarray(self._y_original))
self._adjust_sample(first_obs, last_obs)
self._adjust_sample(first_obs, last_obs, demean)
resids = np.asarray(self.resids(self.starting_values()), dtype=float)

if backcast is None:
Expand Down
3 changes: 3 additions & 0 deletions arch/univariate/mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ def _adjust_sample(
self,
first_obs: None | int | DateLike,
last_obs: None | int | DateLike,
demean: bool,
) -> None:
index = self._y_series.index
_first_obs_index = cutoff_to_index(first_obs, index, 0)
Expand All @@ -681,6 +682,8 @@ def _adjust_sample(
raise ValueError("first_obs and last_obs produce in an empty array.")
self._fit_indices = [_first_obs_index, _last_obs_index]
self._fit_y = self._y[_first_obs_index:_last_obs_index]
if demean:
self._fit_y = np.mean(self._fit_y)
reg = self.regressors
self._fit_regressors = reg[_first_obs_index:_last_obs_index]
self.volatility.start, self.volatility.stop = self._fit_indices
Expand Down

0 comments on commit 88a118f

Please sign in to comment.