def test_lognormal_distribution(moment): num_samples = 100000 inputs = OrderedDict(batch=bint(10)) loc = random_tensor(inputs) scale = random_tensor(inputs).exp() log_measure = dist.LogNormal(loc, scale)(value='x') probe = Variable('x', reals())**moment with monte_carlo_interpretation(particle=bint(num_samples)): with xfail_if_not_implemented(): actual = Integrate(log_measure, probe, frozenset(['x'])) samples = backend_dist.LogNormal(loc, scale).sample((num_samples, )) expected = (samples**moment).mean(0) assert_close(actual.data, expected, atol=1e-2, rtol=1e-2)
def test_integrate_gaussian(int_inputs, real_inputs): int_inputs = OrderedDict(sorted(int_inputs.items())) real_inputs = OrderedDict(sorted(real_inputs.items())) inputs = int_inputs.copy() inputs.update(real_inputs) log_measure = random_gaussian(inputs) integrand = random_gaussian(inputs) reduced_vars = frozenset(real_inputs) with monte_carlo_interpretation(particle=bint(10000)): approx = Integrate(log_measure, integrand, reduced_vars) assert isinstance(approx, Tensor) exact = Integrate(log_measure, integrand, reduced_vars) assert isinstance(exact, Tensor) assert_close(approx, exact, atol=0.1, rtol=0.1)
def test_reduce_moment_matching_moments(): x = Variable('x', reals(2)) gaussian = random_gaussian( OrderedDict([('i', bint(2)), ('j', bint(3)), ('x', reals(2))])) with interpretation(moment_matching): approx = gaussian.reduce(ops.logaddexp, 'j') with monte_carlo_interpretation(s=bint(100000)): actual = Integrate(approx, Number(1.), 'x') expected = Integrate(gaussian, Number(1.), {'j', 'x'}) assert_close(actual, expected, atol=1e-3, rtol=1e-3) actual = Integrate(approx, x, 'x') expected = Integrate(gaussian, x, {'j', 'x'}) assert_close(actual, expected, atol=1e-2, rtol=1e-2) actual = Integrate(approx, x * x, 'x') expected = Integrate(gaussian, x * x, {'j', 'x'}) assert_close(actual, expected, atol=1e-2, rtol=1e-2)