From 7bebff672f030762f653686dfefe976d3063f2f5 Mon Sep 17 00:00:00 2001 From: Cyber-Marty <15370813896@163.com> Date: Mon, 23 Nov 2026 11:13:24 +0800 Subject: [PATCH] BUG: Use full vector initial_value in HARCH and MIDASHyperbolic simulate --- arch/tests/univariate/test_volatility.py | 35 ++++++++++++++++++++++++ arch/univariate/volatility.py | 4 +-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/arch/tests/univariate/test_volatility.py b/arch/tests/univariate/test_volatility.py index ad5dcbc960..c3bb4c9972 100644 --- a/arch/tests/univariate/test_volatility.py +++ b/arch/tests/univariate/test_volatility.py @@ -372,6 +372,25 @@ def test_harch(setup, initial_value): assert str(hex(id(harch))) in txt +def test_harch_simulate_vector_initial_value(): + harch = HARCH(lags=[1, 3]) + params = np.array([0.1, 0.2, 0.3]) + iv = np.array([1.0, 4.0, 9.0]) + _, sigma2 = harch.simulate( + params, + 5, + rng=np.zeros, + burn=0, + initial_value=iv, + ) + assert_allclose(sigma2[:3], iv) + expected_lag1 = iv[2] + expected_lag3 = np.mean(iv) + assert_allclose( + sigma2[3], params[0] + params[1] * expected_lag1 + params[2] * expected_lag3 + ) + + def test_constant_variance(setup): cv = ConstantVariance() @@ -1242,6 +1261,22 @@ def test_midas_symmetric(setup, initial_value): midas.simulate(parameters, setup.t, rng.simulate([])) +def test_midas_simulate_vector_initial_value(): + midas = MIDASHyperbolic(m=3) + params = np.array([0.1, 0.4, 0.5]) + iv = np.array([1.0, 4.0, 9.0]) + _, sigma2 = midas.simulate( + params, + 5, + rng=np.zeros, + burn=0, + initial_value=iv, + ) + weights = midas._weights(params) + expected_first = params[0] + params[1] * np.dot(weights, iv[::-1]) + assert_allclose(sigma2[0], expected_first) + + def test_midas_asymmetric(setup): midas = MIDASHyperbolic(33, asym=True) diff --git a/arch/univariate/volatility.py b/arch/univariate/volatility.py index 90969a96e6..054e22d7f4 100644 --- a/arch/univariate/volatility.py +++ b/arch/univariate/volatility.py @@ -2145,7 +2145,7 @@ def simulate( data[:max_lag] = np.sqrt(initial_value) # The squared data, to avoid squaring it once for each term of each sum data2 = np.empty(nobs + burn) - data2[:max_lag] = data[0] ** 2.0 + data2[:max_lag] = data[:max_lag] ** 2.0 # The weight and the lag of each term that is added to omega, in order. # The terms are added one at a time, in this order, using a cumulative sum weights = np.concatenate( @@ -2460,7 +2460,7 @@ def simulate( data[:m] = np.sqrt(initial_value) # The squared data, to avoid squaring it once for each term of each sum data2 = np.empty(nobs + burn) - data2[:m] = data[0] ** 2.0 + data2[:m] = data[:m] ** 2.0 # Coefficient on the first m values, which are initial values init_coef = aw + 0.5 * gw # The m terms that are added to omega are added one at a time, in order of