Пример #1
0
def test_sample_gaussian():
    """
    Test sample generation from gmm.sample_gaussian where covariance
    is diagonal, spherical and full
    """
    
    n_dim, n_samples = 2, 300
    axis = 1
    mu = np.random.randint(10) * np.random.rand(n_dim)
    cv = (np.random.rand(n_dim) + 1.0) ** 2

    samples = gmm.sample_gaussian(mu, cv, cvtype='diag', n=n_samples)

    assert np.allclose(samples.mean(axis), mu, atol=0.3)
    assert np.allclose(samples.var(axis),  cv, atol=0.5)

    # the same for spherical covariances
    cv = (np.random.rand() + 1.0) ** 2
    samples = gmm.sample_gaussian(mu, cv, cvtype='spherical', n=n_samples)

    assert np.allclose(samples.mean(axis), mu, atol=0.3)
    assert np.allclose(samples.var(axis),  np.repeat(cv, n_dim), atol=0.5)

    # and for full covariances
    A = np.random.randn(n_dim, n_dim)
    cv = np.dot(A.T, A) + np.eye(n_dim)
    samples = gmm.sample_gaussian(mu, cv, cvtype='full', n=n_samples)
    assert np.allclose(samples.mean(axis), mu, atol=0.3)
    assert np.allclose(np.cov(samples), cv, atol=0.7)
Пример #2
0
    def _test_sample_gaussian_diag(self, ndim, n=10000):
        mu = np.random.randint(10) * np.random.rand(ndim)
        cv = (np.random.rand(ndim) + 1.0) ** 2

        samples = gmm.sample_gaussian(mu, cv, cvtype="diag", n=n)

        if ndim > 1:
            axis = 1
        else:
            axis = None
        assert_array_almost_equal(samples.mean(axis), mu, decimal=1)
        assert_array_almost_equal(samples.var(axis), cv, decimal=1)
Пример #3
0
    def _test_sample_gaussian_full(self, ndim, n=10000):
        mu = np.random.randint(10) * np.random.rand(ndim)
        cv = _generate_random_spd_matrix(ndim)

        samples = gmm.sample_gaussian(mu, cv, cvtype="full", n=n)

        if ndim > 1:
            axis = 1
        else:
            axis = None
        assert_array_almost_equal(samples.mean(axis), mu, decimal=1)
        assert_array_almost_equal(np.cov(samples), cv, decimal=1)