def test_innovations_filter_pandas(reset_randomstate): ma = np.array([-0.9, 0.5]) acovf = np.array([1 + (ma**2).sum(), ma[0] + ma[1] * ma[0], ma[1]]) theta, _ = innovations_algo(acovf, nobs=10) endog = np.random.randn(10) endog_pd = pd.Series(endog, index=pd.date_range('2000-01-01', periods=10)) resid = innovations_filter(endog, theta) resid_pd = innovations_filter(endog_pd, theta) assert_allclose(resid, resid_pd.values) assert_index_equal(endog_pd.index, resid_pd.index)
def test_innovations_filter_pandas(reset_randomstate): ma = np.array([-0.9, 0.5]) acovf = np.array([1 + (ma ** 2).sum(), ma[0] + ma[1] * ma[0], ma[1]]) theta, _ = innovations_algo(acovf, nobs=10) endog = np.random.randn(10) endog_pd = pd.Series(endog, index=pd.date_range('2000-01-01', periods=10)) resid = innovations_filter(endog, theta) resid_pd = innovations_filter(endog_pd, theta) assert_allclose(resid, resid_pd.values) assert_index_equal(endog_pd.index, resid_pd.index)
def test_innovations_algo_filter_kalman_filter(reset_randomstate): # Test the innovations algorithm and filter against the Kalman filter # for exact likelihood evaluation of an ARMA process ar_params = np.array([0.5]) ma_params = np.array([0.2]) # TODO could generalize to sigma2 != 1, if desired, after #5324 is merged # and there is a sigma2 argument to arma_acovf # (but maybe this is not really necessary for the point of this test) sigma2 = 1 endog = np.random.normal(size=10) # Innovations algorithm approach acovf = arma_acovf(np.r_[1, -ar_params], np.r_[1, ma_params], nobs=len(endog)) theta, v = innovations_algo(acovf) u = innovations_filter(endog, theta) llf_obs = -0.5 * u ** 2 / (sigma2 * v) - 0.5 * np.log(2 * np.pi * v) # Kalman filter apparoach mod = SARIMAX(endog, order=(len(ar_params), 0, len(ma_params))) res = mod.filter(np.r_[ar_params, ma_params, sigma2]) # Test that the two approaches are identical atol = 1e-6 if PLATFORM_WIN else 0.0 assert_allclose(u, res.forecasts_error[0], rtol=1e-6, atol=atol) assert_allclose(theta[1:, 0], res.filter_results.kalman_gain[0, 0, :-1], atol=atol) assert_allclose(llf_obs, res.llf_obs, atol=atol)
def test_innovations_algo_filter_kalman_filter(reset_randomstate): # Test the innovations algorithm and filter against the Kalman filter # for exact likelihood evaluation of an ARMA process ar_params = np.array([0.5]) ma_params = np.array([0.2]) # TODO could generalize to sigma2 != 1, if desired, after #5324 is merged # and there is a sigma2 argument to arma_acovf # (but maybe this is not really necessary for the point of this test) sigma2 = 1 endog = np.random.normal(size=10) # Innovations algorithm approach acovf = arma_acovf(np.r_[1, -ar_params], np.r_[1, ma_params], nobs=len(endog)) theta, v = innovations_algo(acovf) u = innovations_filter(endog, theta) llf_obs = -0.5 * u**2 / (sigma2 * v) - 0.5 * np.log(2 * np.pi * v) # Kalman filter apparoach mod = SARIMAX(endog, order=(len(ar_params), 0, len(ma_params))) res = mod.filter(np.r_[ar_params, ma_params, sigma2]) # Test that the two approaches are identical atol = 1e-6 if PLATFORM_WIN else 0.0 assert_allclose(u, res.forecasts_error[0], atol=atol) assert_allclose(theta[1:, 0], res.filter_results.kalman_gain[0, 0, :-1], atol=atol) assert_allclose(llf_obs, res.llf_obs, atol=atol)
def test_innovations_filter_brockwell_davis(reset_randomstate): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, _ = innovations_algo(acovf, nobs=4) e = np.random.randn(5) endog = e[1:] + ma * e[:-1] resid = innovations_filter(endog, theta) expected = [endog[0]] for i in range(1, 4): expected.append(endog[i] - theta[i, 0] * expected[-1]) expected = np.array(expected) assert_allclose(resid, expected)
def test_innovations_filter_errors(): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, _ = innovations_algo(acovf, nobs=4) with pytest.raises(ValueError): innovations_filter(np.empty((2, 2)), theta) with pytest.raises(ValueError): innovations_filter(np.empty(4), theta[:-1]) with pytest.raises(ValueError): innovations_filter(pd.DataFrame(np.empty((1, 4))), theta)