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
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
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.)
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:])
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:])
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
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
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))