def _test_gauss(slice_sample_fn, prng):
    # sample from N(0,1)
    pdf = log_normal(0., 1.)

    def sampler(x0, niters):
        x = x0
        for _ in xrange(niters):
            x = slice_sample_fn(pdf, x, 2.0, prng)
            yield x

    bins = np.linspace(-3, 3, 1000)
    smoothing = 1e-5

    actual_samples = np.random.normal(size=10000)
    actual_hist = hist(actual_samples, bins) + smoothing
    actual_hist /= actual_hist.sum()

    slice_samples = np.array(list(sampler(1.0, 10000)))
    slice_hist = hist(slice_samples, bins) + smoothing
    slice_hist /= slice_hist.sum()

    # KL-divergence
    kldiff = KL_approx(actual_hist, slice_hist, bins[1] - bins[0])
    print 'KL:', kldiff

    # statistical distance
    maxdiff = np.abs(actual_hist - slice_hist).max()
    print 'maxdiff:', maxdiff

    assert kldiff <= 0.005
    assert maxdiff <= 0.005
def test_nich_hp_mu():
    N = 1000
    Y = np.array([(x, ) for x in np.random.uniform(low=-10, high=10, size=N)],
                 dtype=[('', np.float32)])
    view = numpy_dataview(Y)
    grid_min, grid_max, grid_n = -5., 5., 100
    _test_scalar_hp_inference(view, log_normal(0., 1.), 0.1, grid_min,
                              grid_max, grid_n, nich, 'mu')
def test_nich_hp_mu():
    N = 1000
    Y = np.array([(x,) for x in np.random.uniform(low=-10, high=10, size=N)],
                 dtype=[('', np.float32)])
    view = numpy_dataview(Y)
    grid_min, grid_max, grid_n = -5., 5., 100
    _test_scalar_hp_inference(view,
                              log_normal(0., 1.),
                              0.1,
                              grid_min,
                              grid_max,
                              grid_n,
                              nich,
                              'mu')
Beispiel #4
0
def test_log_normal():
    from microscopes.common.scalar_functions import log_normal
    from scipy.stats import norm
    import math
    mu, sigma2 = 1.5, 3.2
    x = 6.3
    ours = log_normal(mu, sigma2)
    assert_almost_equals(
        ours(x), norm.logpdf(x, loc=mu, scale=math.sqrt(sigma2)))
    ours1 = pickle.loads(pickle.dumps(ours))
    assert_almost_equals(ours._mu, ours1._mu)
    assert_almost_equals(ours._sigma2, ours1._sigma2)
    assert_almost_equals(
        ours1(x), norm.logpdf(x, loc=mu, scale=math.sqrt(sigma2)))
    ours1 = copy.copy(ours)
    assert_almost_equals(ours._mu, ours1._mu)
    assert_almost_equals(ours._sigma2, ours1._sigma2)
    assert_almost_equals(
        ours1(x), norm.logpdf(x, loc=mu, scale=math.sqrt(sigma2)))
Beispiel #5
0
def test_log_normal():
    from microscopes.common.scalar_functions import log_normal
    from scipy.stats import norm
    import math
    mu, sigma2 = 1.5, 3.2
    x = 6.3
    ours = log_normal(mu, sigma2)
    assert_almost_equals(ours(x),
                         norm.logpdf(x, loc=mu, scale=math.sqrt(sigma2)))
    ours1 = pickle.loads(pickle.dumps(ours))
    assert_almost_equals(ours._mu, ours1._mu)
    assert_almost_equals(ours._sigma2, ours1._sigma2)
    assert_almost_equals(ours1(x),
                         norm.logpdf(x, loc=mu, scale=math.sqrt(sigma2)))
    ours1 = copy.copy(ours)
    assert_almost_equals(ours._mu, ours1._mu)
    assert_almost_equals(ours._sigma2, ours1._sigma2)
    assert_almost_equals(ours1(x),
                         norm.logpdf(x, loc=mu, scale=math.sqrt(sigma2)))