def helper(homog):
     n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10, 50)
     lds = rand_lds(n, p, None if homog else T)
     states, data = generate_data(T, *lds)
     natparam = lds_standard_to_natparam(*lds)
     E_stats = natural_lds_Estep(natparam, data)
     assert allclose(sub(add(natparam, E_stats), E_stats), natparam)
Ejemplo n.º 2
0
 def helper(homog):
     n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10,50)
     lds = rand_lds(n, p, None if homog else T)
     states, data = generate_data(T, *lds)
     natparam = lds_standard_to_natparam(*lds)
     E_stats = natural_lds_Estep(natparam, data)
     assert allclose(sub(add(natparam, E_stats), E_stats), natparam)
Ejemplo n.º 3
0
def test_E_step_inhomog():
    def compare_E_step(lds, data):
        natparam = lds_standard_to_natparam(*lds)
        E_init_stats, E_pairwise_stats, E_node_stats = natural_lds_Estep(natparam, data)
        E_init_stats2, E_pairwise_stats2, E_node_stats2 = pylds_E_step_inhomog(lds, data)

        assert all(map(np.allclose, E_init_stats, E_init_stats2))
        assert all(map(np.allclose, E_pairwise_stats, E_pairwise_stats2))
        assert all(map(np.allclose, E_node_stats, E_node_stats2))

    for _ in xrange(10):
        n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10,50)
        lds = rand_lds(n, p, T)
        states, data = generate_data(T, *lds)

        yield compare_E_step, lds, data
Ejemplo n.º 4
0
def test_filter():
    def check_filter(data, lds):
        (filtered_mus, filtered_sigmas), loglike = filter_forward(data, *lds)
        filtered_mus2, filtered_sigmas2 = dense_filter(data, lds)
        loglike2 = dense_loglike(data, lds)

        assert all(map(np.allclose, filtered_mus, filtered_mus2))
        assert all(map(np.allclose, filtered_sigmas, filtered_sigmas2))
        assert np.isclose(loglike, loglike2)

    for _ in xrange(10):
        n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10, 20)
        lds = rand_lds(n, p)
        _, data = generate_data(T, *lds)

        yield check_filter, data, lds
def test_E_step_inhomog():
    def compare_E_step(lds, data):
        natparam = lds_standard_to_natparam(*lds)
        E_init_stats, E_pairwise_stats, E_node_stats = natural_lds_Estep(
            natparam, data)
        E_init_stats2, E_pairwise_stats2, E_node_stats2 = pylds_E_step_inhomog(
            lds, data)

        assert all(map(np.allclose, E_init_stats, E_init_stats2))
        assert all(map(np.allclose, E_pairwise_stats, E_pairwise_stats2))
        assert all(map(np.allclose, E_node_stats, E_node_stats2))

    for _ in xrange(10):
        n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10, 50)
        lds = rand_lds(n, p, T)
        states, data = generate_data(T, *lds)

        yield compare_E_step, lds, data
Ejemplo n.º 6
0
def test_filters():
    def compare_filters(lds, data):
        (filtered_mus, filtered_sigmas), loglike = filter_forward(data, *lds)

        messages, lognorm = natural_filter_forward(lds_standard_to_natparam(*lds), data)
        prediction_messages, filter_messages = uninterleave(messages)
        natural_filtered_mus, natural_filtered_sigmas = zip(*map(natural_to_mean, filter_messages))

        assert all(map(np.allclose, filtered_mus, natural_filtered_mus))
        assert all(map(np.allclose, filtered_sigmas, natural_filtered_sigmas))
        assert np.isclose(loglike, lognorm)

    for _ in xrange(10):
        n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10,50)
        lds = rand_lds(n, p)
        states, data = generate_data(T, *lds)

        yield compare_filters, lds, data
def test_filters():
    def compare_filters(lds, data):
        (filtered_mus, filtered_sigmas), loglike = filter_forward(data, *lds)

        messages, lognorm = natural_filter_forward(
            lds_standard_to_natparam(*lds), data)
        prediction_messages, filter_messages = uninterleave(messages)
        natural_filtered_mus, natural_filtered_sigmas = zip(
            *map(natural_to_mean, filter_messages))

        assert all(map(np.allclose, filtered_mus, natural_filtered_mus))
        assert all(map(np.allclose, filtered_sigmas, natural_filtered_sigmas))
        assert np.isclose(loglike, lognorm)

    for _ in xrange(10):
        n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10, 50)
        lds = rand_lds(n, p)
        states, data = generate_data(T, *lds)

        yield compare_filters, lds, data
def test_general_inference():
    def get_general_node_params(x, lds):
        T, p = x.shape
        C, sigma_obs = lds[-2:]

        J, Jzx, Jxx, logZ = pair_mean_to_natural(C, sigma_obs)
        h = np.einsum('tzx,tx->tz', Jzx, x)
        logZ += np.einsum('ti,tij,tj->t', x, Jxx,
                          x) - p / 2. * np.log(2 * np.pi)

        return J, h, logZ

    def compare_E_step(lds, data):
        natparam = init_params, pair_params, node_params = lds_standard_to_natparam(
            *lds)
        general_node_params = get_general_node_params(data, lds)
        C, sigma_obs = lds[-2:]
        sample, E_stats, lognorm = natural_lds_inference(natparam, data)
        sample2, E_stats2, lognorm2 = natural_lds_inference_general(
            (init_params, pair_params), general_node_params)
        sample3, E_stats3, lognorm3 = natural_lds_inference_general_nosaving(
            (init_params, pair_params), general_node_params)
        sample4, E_stats4, lognorm4 = natural_lds_inference_general_autograd(
            (init_params, pair_params), general_node_params)

        assert allclose(E_stats[:-1], E_stats2[:-1])
        assert allclose(E_stats2, E_stats3)
        assert allclose(E_stats2, E_stats4)
        assert np.isclose(lognorm, lognorm2)
        assert np.isclose(lognorm, lognorm3)
        assert np.isclose(lognorm, lognorm4)

    for _ in xrange(10):
        n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10, 50)
        lds = rand_lds(n, p, T)
        states, data = generate_data(T, *lds)

        yield compare_E_step, lds, data
Ejemplo n.º 9
0
def test_general_inference():
    def get_general_node_params(x, lds):
        T, p = x.shape
        C, sigma_obs = lds[-2:]

        J, Jzx, Jxx, logZ = pair_mean_to_natural(C, sigma_obs)
        h = np.einsum('tzx,tx->tz', Jzx, x)
        logZ += np.einsum('ti,tij,tj->t', x, Jxx, x) - p/2.*np.log(2*np.pi)

        return J, h, logZ

    def compare_E_step(lds, data):
        natparam = init_params, pair_params, node_params = lds_standard_to_natparam(*lds)
        general_node_params = get_general_node_params(data, lds)
        C, sigma_obs = lds[-2:]
        sample, E_stats, lognorm = natural_lds_inference(natparam, data)
        sample2, E_stats2, lognorm2 = natural_lds_inference_general(
            (init_params, pair_params), general_node_params)
        sample3, E_stats3, lognorm3 = natural_lds_inference_general_nosaving(
            (init_params, pair_params), general_node_params)
        sample4, E_stats4, lognorm4 = natural_lds_inference_general_autograd(
            (init_params, pair_params), general_node_params)

        assert allclose(E_stats[:-1], E_stats2[:-1])
        assert allclose(E_stats2, E_stats3)
        assert allclose(E_stats2, E_stats4)
        assert np.isclose(lognorm, lognorm2)
        assert np.isclose(lognorm, lognorm3)
        assert np.isclose(lognorm, lognorm4)

    for _ in xrange(10):
        n, p, T = npr.randint(1, 5), npr.randint(1, 5), npr.randint(10,50)
        lds = rand_lds(n, p, T)
        states, data = generate_data(T, *lds)

        yield compare_E_step, lds, data
Ejemplo n.º 10
0
def generate_test_model_and_data(n, p, T):
    lds = rand_lds(n, p)
    _, data = generate_data(T, *lds)
    return lds, data
Ejemplo n.º 11
0
def generate_test_model_and_data(n, p, T):
    lds = rand_lds(n, p)
    _, data = generate_data(T, *lds)
    return lds, data