Exemple #1
0
def test_lds(T=1000, D=1, N=10, U=3):
    args = make_lds_parameters(T, D, N, U)

    # Test the standard Kalman filter
    from pylds.lds_messages_interface import kalman_filter as kalman_filter_ref
    ll1, filtered_mus1, filtered_Sigmas1 = kalman_filter_ref(*args)
    ll2, filtered_mus2, filtered_Sigmas2 = kalman_filter(*args)
    assert np.allclose(ll1, ll2)
    assert np.allclose(filtered_mus1, filtered_mus2)
    assert np.allclose(filtered_Sigmas1, filtered_Sigmas2)

    # Sample
    xs = kalman_sample(*args)

    # Test the standard Kalman smoother
    from pylds.lds_messages_interface import E_step as kalman_smoother_ref
    ll1, smoothed_mus1, smoothed_Sigmas1, ExnxT1 = kalman_smoother_ref(*args)
    ll2, smoothed_mus2, smoothed_Sigmas2, ExxnT2 = kalman_smoother(*args)
    assert np.allclose(ll1, ll2)
    assert np.allclose(smoothed_mus1, smoothed_mus2)
    assert np.allclose(smoothed_Sigmas1, smoothed_Sigmas2)
    assert np.allclose(ExnxT1, np.swapaxes(ExxnT2, 1, 2))

    # Test the info form filter
    info_args = convert_mean_to_info_args(*args)
    from pylds.lds_messages_interface import kalman_info_filter as kalman_info_filter_ref
    log_Z1, filtered_Js1, filtered_hs1 = kalman_info_filter_ref(*info_args)
    log_Z2, filtered_Js2, filtered_hs2 = kalman_info_filter(*info_args)
    assert np.allclose(log_Z1, log_Z2)
    assert np.allclose(filtered_Js1, filtered_Js2)
    assert np.allclose(filtered_hs1, filtered_hs2)

    # Test the info form smoother
    _, smoothed_mus3, smoothed_Sigmas3, ExxnT3 = kalman_info_smoother(
        *info_args)
    assert np.allclose(smoothed_mus1, smoothed_mus3)
    assert np.allclose(smoothed_Sigmas1, smoothed_Sigmas3)
    assert np.allclose(ExxnT2, ExxnT3)

    # Plot_the samples vs the smoother
    xs = kalman_info_sample(*info_args)
Exemple #2
0
def test_info_sample(T=100, D=3, N=10, U=3):
    args = make_lds_parameters(T, D, N, U)

    # Test the standard Kalman filter
    from pylds.lds_messages_interface import E_step as kalman_smoother_ref
    ll1, smoothed_mus1, smoothed_Sigmas1, ExnxT1 = kalman_smoother_ref(*args)

    # Plot_the samples vs the smoother
    info_args = convert_mean_to_info_args(*args)
    xs = [kalman_info_sample(*info_args) for _ in range(4)]

    import matplotlib.pyplot as plt
    for i in range(D):
        plt.subplot(D, 1, i+1)
        plt.fill_between(np.arange(T),
                         smoothed_mus1[:, i] - 2 * np.sqrt(smoothed_Sigmas1[:, i, i]),
                         smoothed_mus1[:, i] + 2 * np.sqrt(smoothed_Sigmas1[:, i, i]),
                         color='k', alpha=0.25)
        plt.plot(smoothed_mus1[:, i], '-k', lw=2)
        for x in xs:
            plt.plot(x[:, i])
    plt.show()