From d6c7e71df4a00fa0e9c4cba2d3d9ae7f4b352117 Mon Sep 17 00:00:00 2001 From: WeiLiuu <103909074+WeiLiuu@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:42:15 +0800 Subject: [PATCH] BUG: Choose the scale of each fit in the original data units --- arch/tests/univariate/test_rescale.py | 27 ++++++++++++++++++++++++++- arch/univariate/base.py | 5 +++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/arch/tests/univariate/test_rescale.py b/arch/tests/univariate/test_rescale.py index a3fd62a058..e94b100c98 100644 --- a/arch/tests/univariate/test_rescale.py +++ b/arch/tests/univariate/test_rescale.py @@ -3,7 +3,7 @@ import pandas as pd import pytest -from arch.univariate import GARCH, Normal, ZeroMean +from arch.univariate import ARX, GARCH, Normal, ZeroMean @pytest.fixture(scope="module") @@ -54,3 +54,28 @@ def test_rescale_fit(small_data, std_data): small_fcast = small_res.forecast(start=0) direct_fcast = direct_res.forecast(start=0) assert_allclose(small_fcast.variance, direct_fcast.variance) + + +@pytest.mark.parametrize( + ("amplitude", "scale"), [(0.02, 100.0), (2.0, 1.0), (200.0, 0.1)] +) +def test_rescale_refit_window(amplitude, scale): + rng = np.random.default_rng(1243) + y = rng.standard_normal(400) + y[:200] *= 0.0002 + y[200:] *= amplitude + model = ARX(y, lags=1, rescale=True) + first = model.fit(last_obs=200, disp="off") + assert first.scale == 10000.0 + result = model.fit(first_obs=200, disp="off") + + # OLS on the second window in the units selected for this window. + window = scale * y[200:] + x = np.column_stack((np.ones(window.size - 1), window[:-1])) + params = np.linalg.lstsq(x, window[1:], rcond=None)[0] + sigma2 = np.mean((window[1:] - x @ params) ** 2) + assert result.scale == scale + assert_allclose(result.params, [*params, sigma2]) + forecast = result.forecast() + assert_allclose(forecast.mean.iloc[-1, 0], params[0] + params[1] * window[-1]) + assert_allclose(forecast.variance.iloc[-1, 0], sigma2) diff --git a/arch/univariate/base.py b/arch/univariate/base.py index 649c249012..edeb3c2544 100644 --- a/arch/univariate/base.py +++ b/arch/univariate/base.py @@ -1123,6 +1123,11 @@ def fit( v.closed_form and d.num_params == 0 and isinstance(v, ConstantVariance) ) + if self.scale != 1.0: + # Choose the scale of each estimation sample in the original units. + self.scale = 1.0 + self._y = to_array_1d(self._y_series.to_numpy(dtype=float, copy=True)) + self._scale_changed() self._adjust_sample(first_obs, last_obs) resids = self.resids(self.starting_values())