Example #1
0
def test_histogram_2d():
    X = np.arange(100).reshape(-1, 2)
    h = Histogram(bins=[5, 3])
    h.fit(X)
    assert h.ndim == 2
    assert h.histogram_.shape[0] == 5 + 2
    assert h.histogram_.shape[1] == 3 + 2
Example #2
0
def test_histogram_2d():
    X = np.arange(100).reshape(-1, 2)
    h = Histogram(bins=[5, 3], random_state=1)
    h.fit(X)
    assert h.ndim == 2
    assert h.histogram_.shape[0] == 5 + 2
    assert h.histogram_.shape[1] == 3 + 2
Example #3
0
def test_likelihood_free_mixture():
    p1 = Normal(random_state=1)
    p2 = Normal(mu=2.0, random_state=1)
    h1 = Histogram(bins=50).fit(p1.rvs(10000))
    h2 = Histogram(bins=50).fit(p2.rvs(10000))
    m1 = Mixture(components=[p1, p2])
    m2 = Mixture(components=[h1, h2])

    # Check whether pdf, nnlf and cdf have been overriden
    assert isinstance(m1.pdf, theano.compile.function_module.Function)
    assert isinstance(m1.nnlf, theano.compile.function_module.Function)
    assert isinstance(m1.cdf, theano.compile.function_module.Function)
    assert isinstance(m2.pdf, types.MethodType)
    assert isinstance(m2.nnlf, types.MethodType)
    assert isinstance(m2.cdf, types.MethodType)

    # Compare pdfs
    rng = check_random_state(1)
    X = rng.rand(100, 1) * 10 - 5
    assert np.mean(np.abs(m1.pdf(X) - m2.pdf(X))) < 0.05

    # Test sampling
    X = m2.rvs(10)
    assert X.shape == (10, 1)

    # Check errors
    assert_raises(NotImplementedError, m2.fit, X)
Example #4
0
def test_histogram_sample_weight():
    X = np.arange(11).reshape(-1, 1)
    w = np.ones(len(X)) / len(X)

    h1 = Histogram(bins=11)
    h1.fit(X)
    h2 = Histogram(bins=11)
    h2.fit(X, sample_weight=w)

    assert_array_almost_equal(h1.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]]),
                              h2.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]]))

    assert_raises(ValueError, h1.fit, X, sample_weight=w[1:])
Example #5
0
def test_histogram_variable_width():
    X = np.arange(11).reshape(-1, 1)
    h = Histogram(bins=11, variable_width=True)
    h.fit(X)
    assert_array_almost_equal(h.pdf([[1.0], [2.0], [8.0]]), [0.1, 0.1, 0.1])

    h = Histogram(bins=3, variable_width=True)
    h.fit(X)
    integral = h.histogram_ * (h.edges_[0][1:] - h.edges_[0][:-1])
    integral = integral[1:-1].sum()
    assert_almost_equal(integral, 1.)
Example #6
0
def test_histogram_sample_weight():
    X = np.arange(11).reshape(-1, 1)
    w = np.ones(len(X)) / len(X)

    h1 = Histogram(bins=11)
    h1.fit(X)
    h2 = Histogram(bins=11)
    h2.fit(X, sample_weight=w)

    assert_array_almost_equal(
        h1.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]]), h2.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]])
    )

    assert_raises(ValueError, h1.fit, X, sample_weight=w[1:])
Example #7
0
def test_join_non_theano():
    h0 = Histogram(interpolation="linear", bins=30)
    h1 = Histogram(interpolation="linear", bins=30)
    h2 = Histogram(interpolation="linear", bins=30)

    h0.fit(Normal(mu=0).rvs(10000, random_state=0))
    h1.fit(Normal(mu=1).rvs(10000, random_state=1))
    h2.fit(Normal(mu=2).rvs(10000, random_state=2))

    p = Join(components=[h0, h1, h2])
    assert p.ndim == 3
    assert len(p.parameters_) == 0

    X = p.rvs(10000, random_state=1)
    assert X.shape == (10000, 3)
    assert np.abs(np.mean(X[:, 0]) - 0.) < 0.05
    assert np.abs(np.mean(X[:, 1]) - 1.) < 0.05
    assert np.abs(np.mean(X[:, 2]) - 2.) < 0.05
    assert_array_almost_equal(-np.log(p.pdf(X)), p.nll(X))
Example #8
0
def test_histogram():
    X = np.arange(11).reshape(-1, 1)
    h = Histogram(bins=11)
    h.fit(X)

    assert_array_almost_equal(h.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]]),
                              [0.1, 0.1, 0.1, 0., 0.])

    assert_array_almost_equal(
        h.nll([[0.0], [1.0], [10.0], [-0.5], [10.5]]),
        -np.log(h.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]])))

    X = h.rvs(10000, random_state=1)
    assert np.abs(np.mean(X) - 5.0) < 0.05
    assert X.min() >= 0.0
    assert X.max() <= 10.0
Example #9
0
def test_histogram():
    X = np.arange(11).reshape(-1, 1)
    h = Histogram(bins=11, random_state=1)
    h.fit(X)

    assert_array_almost_equal(h.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]]), [0.1, 0.1, 0.1, 0.0, 0.0])

    assert_array_almost_equal(
        h.nnlf([[0.0], [1.0], [10.0], [-0.5], [10.5]]), -np.log(h.pdf([[0.0], [1.0], [10.0], [-0.5], [10.5]]))
    )

    X = h.rvs(10000)
    assert np.abs(np.mean(X) - 5.0) < 0.05
    assert X.min() >= 0.0
    assert X.max() <= 10.0
Example #10
0
def test_join_non_theano():
    h0 = Histogram(interpolation="linear", bins=30)
    h1 = Histogram(interpolation="linear", bins=30)
    h2 = Histogram(interpolation="linear", bins=30)

    h0.fit(Normal(mu=0).rvs(10000, random_state=0))
    h1.fit(Normal(mu=1).rvs(10000, random_state=1))
    h2.fit(Normal(mu=2).rvs(10000, random_state=2))

    p = Join(components=[h0, h1, h2])
    assert p.ndim == 3
    assert len(p.parameters_) == 0

    X = p.rvs(10000, random_state=1)
    assert X.shape == (10000, 3)
    assert np.abs(np.mean(X[:, 0]) - 0.) < 0.05
    assert np.abs(np.mean(X[:, 1]) - 1.) < 0.05
    assert np.abs(np.mean(X[:, 2]) - 2.) < 0.05
    assert_array_almost_equal(-np.log(p.pdf(X)), p.nll(X))