Example #1
0
def test_LDL_first_missing(ft_mvar, theta_mvar_diffuse, Yt_mvar, Xt_mvar):
    """
    Test when first measurement is missing
    """
    kf = Filter(ft_mvar, Yt_mvar, Xt_mvar, for_smoother=True)
    kf.init_attr(theta_mvar_diffuse)

    Y_t, H_t, D_t, R_t, L_t, L_inv = kf._LDL(1)
    assert kf.n_t[1] == 2

    R_t_move = np.array([[4, 3, 2], [3, 6, 1], [2, 1, 3]])
    L_t_expected, R_t_expected, _ = linalg.ldl(R_t_move)
    L_inv_expected, _ = linalg.lapack.dtrtri(L_t_expected, lower=True)
    np.testing.assert_array_equal(L_t, L_t_expected)
    np.testing.assert_array_equal(R_t, R_t_expected)

    Y_t_expected = linalg.pinv(L_t_expected).dot(
        np.array([2.2, 3, 0]).reshape(-1, 1))
    np.testing.assert_array_almost_equal(Y_t, Y_t_expected)

    H_t_expected = L_inv_expected.dot(np.array([2, 2.4, 1]).reshape(-1, 1))
    np.testing.assert_array_almost_equal(H_t, H_t_expected)

    expected_partitioned_index = np.array([1, 2, 0])
    np.testing.assert_array_equal(kf.partitioned_index[1],
                                  expected_partitioned_index)
Example #2
0
def test_LDL(ft_mvar, theta_mvar_diffuse, Yt_mvar, Xt_mvar):
    """
    Test normal run
    """
    kf = Filter(ft_mvar, Yt_mvar, Xt_mvar, for_smoother=True)
    kf.init_attr(theta_mvar_diffuse)

    Y_t, H_t, D_t, R_t, L_t, L_inv = kf._LDL(0)
    assert kf.n_t[0] == 3

    R_t_move = np.array([[3, 2, 1], [2, 4, 3], [1, 3, 6]])
    L_t_expected, R_t_expected, _ = linalg.ldl(R_t_move)
    L_inv_expected, _ = linalg.lapack.dtrtri(L_t_expected, lower=True)
    np.testing.assert_array_equal(L_t, L_t_expected)
    np.testing.assert_array_equal(R_t, R_t_expected)

    Y_t_expected = linalg.pinv(L_t_expected).dot(
        np.array([1, 2, 2.1]).reshape(-1, 1))
    np.testing.assert_array_almost_equal(Y_t, Y_t_expected)

    H_t_expected = L_inv_expected.dot(np.array([1, 2, 2.4]).reshape(-1, 1))
    np.testing.assert_array_almost_equal(H_t, H_t_expected)

    expected_partitioned_index = np.array([0, 1, 2])
    np.testing.assert_array_equal(kf.partitioned_index[0],
                                  expected_partitioned_index)
Example #3
0
def test_G_explosive_root(f_arma32):
    """
    Test how to handle explosive root in Smoother
    """
    # Initialize the model
    x = 1  # used to calculate stationary mean
    model = BCM()
    model.set_f(f_arma32)
    theta_intend = np.array([0.1, 0.2, 0.25])
    theta_test = np.array([0.59358299, 0.91708496, 0.54634604])
    T = 250
    my_ft = lambda theta, T, **kwargs: ft(theta, f_arma32, T, **kwargs)
    x_col = ['const']
    Xt = pd.DataFrame({x_col[0]: x * np.ones(T)})

    # Build simulated data
    df, y_col, xi_col = model.simulated_data(input_theta=theta_intend, Xt=Xt)
    Xt = df_to_tensor(df, x_col)
    Yt = df_to_tensor(df, y_col)
    kf = Filter(my_ft, Yt, Xt, for_smoother=True)
    kf.fit(theta_test)
    ks = Smoother()
    ks.fit(kf)
    result = ks.G(theta_test)
    assert not np.isnan(result)
Example #4
0
def test_E_chi2(ft_ll_mvar_diffuse, theta_ll_mvar_diffuse,
                Yt_mvar_diffuse_smooth):
    """
    Test normal run 
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_smooth, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)
    ks = Smoother()
    ks.fit(kf)

    theta_test = theta_ll_mvar_diffuse + 1
    Mt = ft_ll_mvar_diffuse(theta_test, ks.T)

    # Test first missing
    t = 2
    chi2 = ks._E_chi2(Mt, t)
    Ht = Mt['Ht'][t][1]
    Dt = Mt['Dt'][t][1]
    e_chi = ks.Yt[t][0] - Ht.dot(ks.xi_t_T[t]) - \
            Dt.dot(ks.Xt[t])
    e_chi2 = e_chi.dot(e_chi.T) + Ht.dot(ks.P_t_T[t]).dot(Ht.T)
    np.testing.assert_array_equal(e_chi2, chi2)

    # # Test all present
    t = 3
    chi2 = ks._E_chi2(Mt, t)
    Ht = Mt['Ht'][t]
    Dt = Mt['Dt'][t]
    e_chi = ks.Yt[t] - Ht.dot(ks.xi_t_T[t]) - \
            Dt.dot(ks.Xt[t])
    e_chi2 = e_chi.dot(e_chi.T) + Ht.dot(ks.P_t_T[t]).dot(Ht.T)
    np.testing.assert_array_equal(e_chi2, chi2)
Example #5
0
def test_init_attr_diffuse(ft_mvar, theta_mvar_diffuse, Yt_mvar, Xt_mvar):
    """
    Test if init_attr for diffuse
    """
    kf = Filter(ft_mvar, Yt_mvar, Xt_mvar, for_smoother=True)
    kf.init_attr(theta_mvar_diffuse)
    assert kf.q == 1 and \
            len(kf.L0_t[0]) == Yt_mvar[0].shape[0]
Example #6
0
def test_init_attr_input(ft_mvar, theta_mvar, Yt_mvar, Xt_mvar):
    """
    Test normal run
    """
    kf = Filter(ft_mvar, Yt_mvar, Xt_mvar, for_smoother=True)
    kf.init_attr(theta_mvar)
    assert len(kf.L_star_t) == len(Yt_mvar) and \
            len(kf.L_star_t[0]) == Yt_mvar[0].shape[0]
Example #7
0
def test_get_filtered_val_missing(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                                  theta_ll_mvar_diffuse):
    """
    Test missing measurements handling
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)

    Yt_filtered, Yt_filtered_cov, _, _ = kf.get_filtered_val()
    np.testing.assert_array_equal(kf.Ht[2].dot(kf.xi_t[2][0]), Yt_filtered[2])
Example #8
0
def test_get_filtered_val_not_filtered(ft_ll_mvar_1d, Yt_mvar_diffuse_missing):
    """
    Test error message when fit is not run
    """
    kf = Filter(ft_ll_mvar_1d, Yt_mvar_diffuse_missing, for_smoother=True)
    with pytest.raises(TypeError) as error:
        kf.get_filtered_val()
    expected_result = 'The Kalman filter object is not fitted yet'
    result = str(error.value)
    assert result == expected_result
Example #9
0
def test_get_filtered_state_not_fitted(ft_ll_mvar_diffuse,
                                       Yt_mvar_diffuse_missing,
                                       theta_ll_mvar_diffuse):
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)

    t = 6
    with pytest.raises(ValueError) as error:
        result = kf.get_filtered_state(t)
    expected_result = 'Kalman filter is not fitted yet'
    result = str(error.value)
    assert expected_result == result
Example #10
0
def test_get_filtered_y_no_xi(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                              theta_ll_mvar_diffuse):
    """
    Test df without xi
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)

    Yt_filtered, Yt_filtered_cov, xi_t, P_t = kf.get_filtered_val(is_xi=False)
    assert np.isnan(xi_t[-1])
    assert np.isnan(P_t[-1])
Example #11
0
def test_get_filtered_state_max(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                                theta_ll_mvar_diffuse):
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)

    t = 6
    with pytest.raises(ValueError) as error:
        result = kf.get_filtered_state(t)
    expected_result = 'Maximum t allowed is 4'
    result = str(error.value)
    assert expected_result == result
Example #12
0
def test_get_filtered_val_all_xi(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                                 theta_ll_mvar_diffuse):
    """
    Test df with all xi
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)

    Yt_filtered, Yt_filtered_cov, xi_t, P_t = kf.get_filtered_val()
    np.testing.assert_array_equal(xi_t[2], kf.xi_t[2][0])
    np.testing.assert_array_equal(P_t[2], np.nan * np.ones(P_t[2].shape))
    np.testing.assert_array_equal(P_t[3], kf.P_star_t[3][0])
Example #13
0
def test_get_filtered_state_diffuse(ft_ll_mvar_diffuse,
                                    Yt_mvar_diffuse_missing,
                                    theta_ll_mvar_diffuse):
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)

    t = 1
    with pytest.raises(ValueError) as error:
        result = kf.get_filtered_state(t)
    expected_result = 'Diffuse state at time 1'
    result = str(error.value)
    assert expected_result == result
Example #14
0
def test_get_smoothed_val_all_xi(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                                 theta_ll_mvar_diffuse):
    """
    Test df with all xi
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)
    ks = Smoother()
    ks.fit(kf)

    y_t_T, yP_t_T, xi_T, P_T = ks.get_smoothed_val()
    np.testing.assert_array_equal(xi_T[2], ks.xi_t_T[2])
    np.testing.assert_array_equal(P_T[2], ks.P_t_T[2])
    np.testing.assert_array_equal(P_T[3], ks.P_t_T[3])

    Mt = ft_ll_mvar_diffuse(theta_ll_mvar_diffuse, 4)
    Rt = Mt['Rt'][0]
    Dt = Mt['Dt'][0]
    Ht = Mt['Ht'][0]

    # Test smoothed y
    R2_0 = np.array([[0.5 - 0.4 / 0.6 * 0.4]])
    B0 = 0.4 / 0.6
    delta_H0 = Ht[0:1] - B0 * Ht[1:]
    eps0 = B0 * (Yt_mvar_diffuse_missing[0][1] - Dt[1:].dot(ks.Xt[0]) -
                 Ht[1:].dot(ks.xi_t_T[0]))
    y0 = Ht[0:1].dot(ks.xi_t_T[0]) + Dt[0:1].dot(ks.Xt[0]) + eps0

    yP_0 = (delta_H0.dot(ks.P_t_T[0]).dot(delta_H0.T) + R2_0).item()

    R2_2 = np.array([[0.6 - 0.4 / 0.5 * 0.4]])
    B2 = 0.4 / 0.5
    delta_H2 = Ht[1] - B2 * Ht[0]
    eps2 = B2 * (Yt_mvar_diffuse_missing[2][0] - Dt[:1].dot(ks.Xt[2]) -
                 Ht[:1].dot(ks.xi_t_T[2]))
    y2 = Ht[1:].dot(ks.xi_t_T[2]) + Dt[1:].dot(ks.Xt[2]) + eps2
    yP_2 = (delta_H2.dot(ks.P_t_T[2]).dot(delta_H2.T) + R2_2).item()
    expected_y_t_T = [
        np.array([[y0], [2]]),
        Ht.dot(ks.xi_t_T[1] + Dt.dot(ks.Xt[1])),
        np.array([[2.5], [y2]]),
        np.array([[3], [5]])
    ]
    expected_Pcov_T = [
        np.array([[yP_0, 0], [0, 0]]),
        ks.Ht[1].dot(ks.P_t_T[1]).dot(ks.Ht[1].T) + ks.Rt[1],
        np.array([[0, 0], [0, yP_2]]),
        np.zeros([2, 2])
    ]
    for t in range(4):
        np.testing.assert_array_almost_equal(yP_t_T[t], expected_Pcov_T[t])
        np.testing.assert_array_almost_equal(y_t_T[t], expected_y_t_T[t])
Example #15
0
def test_joseph_form(ft_ar1, theta_ar1, Yt_1d):
    """
    Test normal run
    """

    kf = Filter(ft_ar1, Yt_1d, for_smoother=True)
    L = np.array([[2, 3], [4, 5]])
    P = np.array([[3, 4], [4, 5]])
    KRK = np.ones([2, 2])
    result = kf._joseph_form(L, P, KRK=KRK)
    expected_result = np.array([[106, 188], [188, 334]])

    np.testing.assert_array_equal(result, expected_result)
Example #16
0
def test_init_attr_smoother_not_for_smoother(ft_rw_1, theta_rw, Yt_1d, Xt_1d):
    """
    Test error message Kalman filter not for smoother
    """
    kf = Filter(ft_rw_1, Yt_1d, Xt_1d, for_smoother=False)
    kf.fit(theta_rw)
    ks = Smoother()
    with pytest.raises(TypeError) as error:
        ks.init_attr_smoother(kf)
    msg = str(error.value)
    e_msg = 'The Kalman filter object is not for smoothers'

    assert msg == e_msg
Example #17
0
def test_init_attr_smoother_diffuse(ft_ll_mvar_diffuse, theta_ll_mvar_diffuse,
                                    Yt_mvar_diffuse_missing):
    """
    Test initialization for diffuse smoother
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)
    ks = Smoother()
    ks.init_attr_smoother(kf)
    e_r1 = np.zeros([2, 1])
    e_N1 = np.zeros([2, 2])
    np.testing.assert_array_equal(e_r1, ks.r1_t[ks.t_q - 1])
    np.testing.assert_array_equal(e_N1, ks.N1_t[ks.t_q - 1])
    assert ks.t_q == 3
Example #18
0
def test_get_smoothed_y_selected_xi(ft_ll_mvar_diffuse,
                                    Yt_mvar_diffuse_missing,
                                    theta_ll_mvar_diffuse):
    """
    Test df with selected xi
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)
    ks = Smoother()
    ks.fit(kf)

    _, _, xi_T, P_T = ks.get_smoothed_val(xi_col=[1])
    np.testing.assert_array_equal(xi_T[2], ks.xi_t_T[2][[1]])
    np.testing.assert_array_equal(P_T[2], ks.P_t_T[2][[1]][:, [1]])
Example #19
0
def test_over_ride_error_input(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                               theta_ll_mvar_diffuse):
    """
    Raises exception when init_state wrong input
    """
    init_val = {
        'P_star_t': np.zeros([2]),
        'xi_t': 100 * np.ones([2, 1]),
        'q': 0
    }
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    with pytest.raises(ValueError) as error:
        kf.fit(theta_ll_mvar_diffuse, init_state=init_val)
    expected_result = 'User-specified P_star_t does not have 2 dimensions'
    result = str(error.value)
    assert expected_result == result
Example #20
0
def test_get_filtered_state_T(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                              theta_ll_mvar_diffuse):
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)

    t = kf.T
    result = kf.get_filtered_state(t)
    expected_result = {
        'P_star_t': kf.P_T1,
        'P_inf_t': np.zeros([2, 2]),
        'xi_t': kf.xi_T1,
        'q': 0
    }
    for i in ['P_star_t', 'P_inf_t', 'xi_t']:
        np.testing.assert_array_equal(result[i], expected_result[i])
    assert result['q'] == expected_result['q']
Example #21
0
def test_init_attr_smoother(ft_rw_1, theta_rw, Yt_1d, Xt_1d):
    """
    Test normal run
    """
    kf = Filter(ft_rw_1, Yt_1d, Xt_1d, for_smoother=True)
    kf.fit(theta_rw)
    ks = Smoother()
    ks.init_attr_smoother(kf)

    e_r0 = np.zeros([1, 1])
    e_N0 = np.zeros([1, 1])

    r0 = ks.r0_t[kf.T - 1]
    N0 = ks.N0_t[kf.T - 1]
    np.testing.assert_array_equal(e_r0, r0)
    np.testing.assert_array_equal(e_N0, N0)
    assert ks.r1_t.shape[0] == 0
Example #22
0
def test_sequential_update_mvar_missing_first(ft_ar2_mvar_kw, theta_ar2_mvar,
                                              Yt_ar2_mvar, Xt_ar2_mvar):
    """
    Test normal run in multi-variate case missing middle measurements
    """
    t = 3
    kf = Filter(ft_ar2_mvar_kw, Yt_ar2_mvar, Xt_ar2_mvar, for_smoother=True)
    kf.init_attr(theta_ar2_mvar)
    for t_ in range(t + 1):
        kf._sequential_update(t_)
    Mt = kf.ft(kf.theta, kf.T, x_0=Xt_ar2_mvar[0])

    Ht = Mt['Ht'][t][[1, 2]]
    Bt = Mt['Bt'][t]
    Dt = Mt['Dt'][t][[1, 2]]
    Ft = Mt['Ft'][t]
    Qt = Mt['Qt'][t]
    Rt = Mt['Rt'][t][[1, 2]][:, [1, 2]]
    Upsilon = Ht.dot(kf.P_star_t[t][0]).dot(Ht.T) + Rt
    K = kf.P_star_t[t][0].dot(Ht.T).dot(linalg.pinvh(Upsilon))
    v = kf.Yt[t][[0, 1]] - Ht.dot(kf.xi_t[t][0]) - Dt.dot(kf.Xt[t])

    expected_xi_t_nt = kf.xi_t[t][0] + K.dot(v)
    P_t_0 = kf.P_star_t[t][0]
    P_t_t = P_t_0 - P_t_0.dot(Ht.T).dot(
        linalg.pinvh(Upsilon)).dot(Ht).dot(P_t_0)
    expected_P_t_nt = P_t_t
    np.testing.assert_array_almost_equal(expected_P_t_nt,
                                         kf.P_star_t[t][kf.n_t[t]])
    np.testing.assert_array_almost_equal(expected_xi_t_nt,
                                         kf.xi_t[t][kf.n_t[t]])
Example #23
0
def test_sequential_update_mvar_full_obs(ft_ar2_mvar_kw, theta_ar2_mvar,
                                         Yt_ar2_mvar, Xt_ar2_mvar):
    """
    Test normal run in multi-variate case full measurements
    """
    t = 0
    kf = Filter(ft_ar2_mvar_kw, Yt_ar2_mvar, Xt_ar2_mvar, for_smoother=True)
    kf.init_attr(theta_ar2_mvar)
    kf._sequential_update(t)
    Mt = kf.ft(kf.theta, kf.T, x_0=Xt_ar2_mvar[0])

    Ht = Mt['Ht'][t]
    Bt = Mt['Bt'][t]
    Dt = Mt['Dt'][t]
    Ft = Mt['Ft'][t]
    Qt = Mt['Qt'][t]
    Rt = Mt['Rt'][t]
    Upsilon = Ht.dot(kf.P_star_t[t][0]).dot(Ht.T) + Rt
    K = kf.P_star_t[t][0].dot(Mt['Ht'][t].T).dot(linalg.pinvh(Upsilon))
    v = kf.Yt[t] - Ht.dot(kf.xi_t[t][0]) - Dt.dot(kf.Xt[t])

    expected_xi_t1_0 = Ft.dot(kf.xi_t[t][0] + K.dot(v)) + Bt.dot(kf.Xt[t])
    P_t_0 = kf.P_star_t[t][0]
    P_t_t = P_t_0 - P_t_0.dot(Ht.T).dot(
        linalg.pinvh(Upsilon)).dot(Ht).dot(P_t_0)
    expected_P_t1_0 = Ft.dot(P_t_t).dot(Ft.T) + Qt
    np.testing.assert_array_almost_equal(expected_P_t1_0,
                                         kf.P_star_t[t + 1][0])
    np.testing.assert_array_almost_equal(expected_xi_t1_0, kf.xi_t[t + 1][0])
Example #24
0
def test_get_filtered_state_t(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                              theta_ll_mvar_diffuse):
    """
    get values at t < self.T
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)

    t = kf.T - 1
    result = kf.get_filtered_state(t)
    expected_result = {
        'P_star_t': kf.P_star_t[t][0],
        'P_inf_t': kf.P_inf_t[t][0],
        'xi_t': kf.xi_t[t][0],
        'q': 0
    }
    for i in ['P_star_t', 'P_inf_t', 'xi_t']:
        np.testing.assert_array_equal(result[i], expected_result[i])
    assert result['q'] == expected_result['q']
Example #25
0
def test_get_smoothed_val_not_smoothed(ft_ll_mvar_1d, Yt_1d):
    """
    Test error message when fit is not run
    """
    kf = Filter(ft_ll_mvar_1d, Yt_1d, for_smoother=True)
    ks = Smoother()
    with pytest.raises(TypeError) as error:
        ks.get_smoothed_val()
    expected_result = 'The Kalman smoother object is not fitted yet'
    result = str(error.value)
    assert result == expected_result
Example #26
0
def test_E_delta2(ft_ll_mvar_diffuse, theta_ll_mvar_diffuse,
                  Yt_mvar_diffuse_smooth):
    """
    Test normal run 
    """
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_smooth, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse)
    ks = Smoother()
    ks.fit(kf)

    t = 2
    theta_test = [i + 0.1 for i in theta_ll_mvar_diffuse]
    Mt = ft_ll_mvar_diffuse(theta_test, ks.T)
    delta2 = ks._E_delta2(Mt, t)

    nt = ks.xi_t_T[t] - Mt['Ft'][t-1].dot(ks.xi_t_T[t-1]) - \
            Mt['Bt'][t-1].dot(ks.Xt[t-1])
    e_delta2 = nt.dot(nt.T) + ks.P_t_T[t] + Mt['Ft'][t - 1].dot(
        ks.P_t_T[t - 1]).dot(Mt['Ft'][t - 1].T) - Mt['Ft'][t - 1].dot(
            ks.Pcov_t_t1[t - 1]) - (ks.Pcov_t_t1[t - 1].T).dot(
                Mt['Ft'][t - 1].T)
    np.testing.assert_array_almost_equal(e_delta2, delta2)
Example #27
0
def test_over_ride(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing,
                   theta_ll_mvar_diffuse):
    """
    Force a diffuse kalman filter to become regular filter
    """
    init_val = {
        'P_star_t': np.zeros([2, 2]),
        'xi_t': 100 * np.ones([2, 1]),
        'q': 0
    }
    kf = Filter(ft_ll_mvar_diffuse, Yt_mvar_diffuse_missing, for_smoother=True)
    kf.fit(theta_ll_mvar_diffuse, init_state=init_val)

    t = 0
    result = kf.get_filtered_state(t)
    expected_result = {
        'P_star_t': np.zeros([2, 2]),
        'P_inf_t': np.zeros([2, 2]),
        'xi_t': 100 * np.ones([2, 1]),
        'q': 0
    }
    for i in ['P_star_t', 'P_inf_t', 'xi_t']:
        np.testing.assert_array_equal(result[i], expected_result[i])
    assert result['q'] == expected_result['q']
Example #28
0
def test_sequential_update_diffuse_ll_1d(ft_ll_1d_diffuse, theta_ll_1d_diffuse,
                                         Yt_1d_full):
    """
    Test local linear models from chapter 5 of Koopman and Durbin (2012)
    """
    t = 3
    kf = Filter(ft_ll_1d_diffuse, Yt_1d_full, for_smoother=True)
    kf.init_attr(theta_ll_1d_diffuse)
    for t_ in range(t):
        kf._sequential_update_diffuse(t_)

    # Test period 0 result
    q1 = theta_ll_1d_diffuse[0] / theta_ll_1d_diffuse[2]
    q2 = theta_ll_1d_diffuse[1] / theta_ll_1d_diffuse[2]
    e_P_inf_t1_0 = np.ones([2, 2])
    e_P_star_t1_0 = np.array([[1 + q1, 0], [0, q2]]) * theta_ll_1d_diffuse[2]
    e_xi_t1_0 = np.array([[Yt_1d_full[0][0]], [0]])

    np.testing.assert_array_almost_equal(e_P_inf_t1_0, kf.P_inf_t[1][0])
    np.testing.assert_array_almost_equal(e_xi_t1_0, kf.xi_t[1][0])
    np.testing.assert_array_almost_equal(e_P_star_t1_0, kf.P_star_t[1][0])

    # Test period 1 result
    e_P_inf_t1_0 = np.zeros([2, 2])
    e_P_star_t1_0 = np.array([[5 + 2 * q1 + q2, 3 + q1 + q2],
                              [3 + q1 + q2, 2 + q1 + 2 * q2]]) * \
                                      theta_ll_1d_diffuse[2]
    y2 = Yt_1d_full[1][0][0]
    y1 = Yt_1d_full[0][0][0]
    e_xi_t1_0 = np.array([[2 * y2 - y1], [y2 - y1]])

    np.testing.assert_array_almost_equal(e_P_inf_t1_0, kf.P_inf_t[2][0])
    np.testing.assert_array_almost_equal(e_xi_t1_0, kf.xi_t[2][0])
    np.testing.assert_array_almost_equal(e_P_star_t1_0, kf.P_star_t[2][0])

    # Test period 2 result, should return same result as _sequential_update()
    P_inf_t1_0 = kf.P_inf_t[3][0].copy()
    P_star_t1_0 = kf.P_star_t[3][0].copy()
    xi_t1_0 = kf.xi_t[3][0].copy()

    kf._sequential_update(2)
    np.testing.assert_array_almost_equal(P_inf_t1_0, np.zeros([2, 2]))
    np.testing.assert_array_almost_equal(xi_t1_0, kf.xi_t[3][0])
    np.testing.assert_array_almost_equal(P_star_t1_0, kf.P_star_t[3][0])
Example #29
0
def test_sequential_update_diffuse_update_multiple_q(ft_q,
                                                     theta_ll_mvar_diffuse,
                                                     Yt_q):
    """
    For ll model, only measurements across time can reduce rank of P_inf_t
    """
    t = 1
    kf = Filter(ft_q, Yt_q, for_smoother=True)
    kf.init_attr(theta_ll_mvar_diffuse)
    kf._sequential_update_diffuse(0)
    assert kf.q == 0
Example #30
0
def test_sequential_update_diffuse_missing(ft_rw_1_diffuse, theta_rw,
                                           Yt_1d_missing, Xt_1d):
    """
    Test first missing
    """
    t = 0
    kf = Filter(ft_rw_1_diffuse, Yt_1d_missing, Xt_1d, for_smoother=True)
    kf.init_attr(theta_rw)
    for t_ in range(t + 1):
        kf._sequential_update_diffuse(t_)
    e_P_inf_t1_0 = np.array([[1]])
    e_xi_t1_0 = np.array([[0]])
    np.testing.assert_array_almost_equal(e_P_inf_t1_0, kf.P_inf_t[t + 1][0])
    np.testing.assert_array_almost_equal(e_xi_t1_0, kf.xi_t[t + 1][0])