|
| 1 | +"""Test TVP forecaster. |
| 2 | +
|
| 3 | +Tests include convergence properties described in Durbin & Koopman, 2012. |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from aeon.forecasting._tvp import TVPForecaster |
| 10 | + |
| 11 | + |
| 12 | +def test_direct(): |
| 13 | + """Test aeon TVP Forecaster equivalent to statsmodels.""" |
| 14 | + expected = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]) |
| 15 | + tvp = TVPForecaster(window=5, horizon=1, var=0.01, beta_var=0.01) |
| 16 | + p = tvp.forecast(expected) |
| 17 | + p2 = tvp.direct_forecast(expected, prediction_horizon=5) |
| 18 | + assert p == p2[0] |
| 19 | + |
| 20 | + |
| 21 | +def test_static_ar1_convergence_to_ols(): |
| 22 | + """Test TVPForecaster converges to the OLS solution for a static AR(1) process.""" |
| 23 | + # Simulate AR(1) data with constant parameters |
| 24 | + rng = np.random.RandomState(0) |
| 25 | + true_phi = 0.6 |
| 26 | + true_intercept = 2.0 |
| 27 | + noise_std = 0.5 |
| 28 | + n = 500 |
| 29 | + y = np.zeros(n) |
| 30 | + # Initialize y[0] near the steady-state mean to avoid startup bias |
| 31 | + y[0] = true_intercept / (1 - true_phi) |
| 32 | + for t in range(1, n): |
| 33 | + y[t] = true_intercept + true_phi * y[t - 1] + rng.normal(0, noise_std) |
| 34 | + # Fit with beta_var=0 (no parameter drift) and observation variance = noise_var |
| 35 | + forecaster = TVPForecaster(window=1, horizon=1, var=noise_std**2, beta_var=0.0) |
| 36 | + forecaster.fit(y) |
| 37 | + beta_est = forecaster._beta # [intercept, phi] estimated |
| 38 | + # Compute static OLS estimates for comparison |
| 39 | + X = np.vstack( |
| 40 | + [np.ones(n - 1), y[: n - 1]] |
| 41 | + ).T # regress y[t] on [1, y[t-1]] for t=1..n-1 |
| 42 | + y_resp = y[1:] |
| 43 | + beta_ols, *_ = np.linalg.lstsq(X, y_resp, rcond=None) |
| 44 | + # The TVP forecaster (with no drift) should converge to OLS estimates |
| 45 | + assert beta_est.shape == (2,) |
| 46 | + # Check that estimated parameters are close to OLS solution |
| 47 | + assert np.allclose(beta_est, beta_ols, atol=0.1) |
| 48 | + # Also check they are close to true parameters |
| 49 | + assert abs(beta_est[0] - true_intercept) < 0.2 |
| 50 | + assert abs(beta_est[1] - true_phi) < 0.1 |
| 51 | + |
| 52 | + |
| 53 | +def test_tvp_adapts_to_changing_coefficient(): |
| 54 | + """Test TVP adapts its parameters when the true AR(1) coefficient changes.""" |
| 55 | + rng = np.random.RandomState(42) |
| 56 | + # Piecewise AR(1): phi changes from 0.2 to 0.8 at t=100, intercept remains 1.0 |
| 57 | + n = 200 |
| 58 | + phi1, phi2 = 0.2, 0.8 |
| 59 | + intercept = 1.0 |
| 60 | + noise_std = 0.05 |
| 61 | + y = np.zeros(n) |
| 62 | + # Start near the mean of first regime |
| 63 | + y[0] = intercept / (1 - phi1) |
| 64 | + # First half (t=1 to 99) with phi1 |
| 65 | + for t in range(1, 100): |
| 66 | + y[t] = intercept + phi1 * y[t - 1] + rng.normal(0, noise_std) |
| 67 | + # Second half (t=100 to 199) with phi2 |
| 68 | + for t in range(100, n): |
| 69 | + y[t] = intercept + phi2 * y[t - 1] + rng.normal(0, noise_std) |
| 70 | + # Fit TVPForecaster with nonzero beta_var to allow parameter drift |
| 71 | + forecaster = TVPForecaster(window=1, horizon=1, var=noise_std**2, beta_var=0.1) |
| 72 | + forecaster.fit(y) |
| 73 | + beta_final = forecaster._beta |
| 74 | + # Compute OLS on first and second half segments for reference |
| 75 | + X1 = np.vstack([np.ones(99), y[:99]]).T |
| 76 | + y1 = y[1:100] |
| 77 | + beta1_ols, *_ = np.linalg.lstsq(X1, y1, rcond=None) |
| 78 | + # use points 100..198 to predict 101..199 |
| 79 | + X2 = np.vstack([np.ones(n - 101), y[100 : n - 1]]).T |
| 80 | + y2 = y[101:n] |
| 81 | + beta2_ols, *_ = np.linalg.lstsq(X2, y2, rcond=None) |
| 82 | + # The final estimated phi should be much closer to phi2 than phi1 |
| 83 | + estimated_intercept, estimated_phi = beta_final[0], beta_final[1] |
| 84 | + # Validate that phi coefficient increased towards phi2 |
| 85 | + assert estimated_phi > 0.5 # moved well above the initial ~0.2 |
| 86 | + assert abs(estimated_phi - phi2) < 0.1 # close to the new true phi |
| 87 | + # Validate intercept remains reasonable (around true intercept) |
| 88 | + assert abs(estimated_intercept - intercept) < 0.5 |
| 89 | + # Check that final phi is closer to second-half OLS estimate than first-half |
| 90 | + assert abs(estimated_phi - beta2_ols[1]) < abs(estimated_phi - beta1_ols[1]) |
0 commit comments