def test_Compound_Distribution(): X = Normal('X', 2, 4) N = NormalDistribution(X, 4) C = CompoundDistribution(N) assert C.is_Continuous assert C.set == Interval(-oo, oo) assert C.pdf(x, evaluate=True).simplify() == exp(-x**2/64 + x/16 - S(1)/16)/(8*sqrt(pi)) assert not isinstance(CompoundDistribution(NormalDistribution(2, 3)), CompoundDistribution) M = MultivariateNormalDistribution([1, 2], [[2, 1], [1, 2]]) raises(NotImplementedError, lambda: CompoundDistribution(M)) X = Beta('X', 2, 4) B = BernoulliDistribution(X, 1, 0) C = CompoundDistribution(B) assert C.is_Finite assert C.set == {0, 1} y = symbols('y', negative=False, integer=True) assert C.pdf(y, evaluate=True) == Piecewise((S(1)/(30*beta(2, 4)), Eq(y, 0)), (S(1)/(60*beta(2, 4)), Eq(y, 1)), (0, True)) k, t, z = symbols('k t z', positive=True, real=True) G = Gamma('G', k, t) X = PoissonDistribution(G) C = CompoundDistribution(X) assert C.is_Discrete assert C.set == S.Naturals0 assert C.pdf(z, evaluate=True).simplify() == t**z*(t + 1)**(-k - z)*gamma(k \ + z)/(gamma(k)*gamma(z + 1))
def test_Normal(): m = Normal('A', [1, 2], [[1, 0], [0, 1]]) A = MultivariateNormal('A', [1, 2], [[1, 0], [0, 1]]) assert m == A assert density(m)(1, 2) == 1 / (2 * pi) assert m.pspace.distribution.set == ProductSet(S.Reals, S.Reals) raises(ValueError, lambda: m[2]) raises (ValueError,\ lambda: Normal('M',[1, 2], [[0, 0], [0, 1]])) n = Normal('B', [1, 2, 3], [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) p = Normal('C', Matrix([1, 2]), Matrix([[1, 0], [0, 1]])) assert density(m)(x, y) == density(p)(x, y) assert marginal_distribution(n, 0, 1)(1, 2) == 1 / (2 * pi) raises(ValueError, lambda: marginal_distribution(m)) assert integrate(density(m)(x, y), (x, -oo, oo), (y, -oo, oo)).evalf() == 1 N = Normal('N', [1, 2], [[x, 0], [0, y]]) assert density(N)(0, 0) == exp(-2 / y - 1 / (2 * x)) / (2 * pi * sqrt(x * y)) raises(ValueError, lambda: Normal('M', [1, 2], [[1, 1], [1, -1]])) # symbolic n = symbols('n', natural=True) mu = MatrixSymbol('mu', n, 1) sigma = MatrixSymbol('sigma', n, n) X = Normal('X', mu, sigma) assert density(X) == MultivariateNormalDistribution(mu, sigma) raises(NotImplementedError, lambda: median(m))
def test_Normal(): m = Normal('A', [1, 2], [[1, 0], [0, 1]]) A = MultivariateNormal('A', [1, 2], [[1, 0], [0, 1]]) assert m == A assert density(m)(1, 2) == 1/(2*pi) assert m.pspace.distribution.set == ProductSet(S.Reals, S.Reals) raises (ValueError, lambda:m[2]) n = Normal('B', [1, 2, 3], [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) p = Normal('C', Matrix([1, 2]), Matrix([[1, 0], [0, 1]])) assert density(m)(x, y) == density(p)(x, y) assert marginal_distribution(n, 0, 1)(1, 2) == 1/(2*pi) raises(ValueError, lambda: marginal_distribution(m)) assert integrate(density(m)(x, y), (x, -oo, oo), (y, -oo, oo)).evalf() == 1 N = Normal('N', [1, 2], [[x, 0], [0, y]]) assert density(N)(0, 0) == exp(-((4*x + y)/(2*x*y)))/(2*pi*sqrt(x*y)) raises (ValueError, lambda: Normal('M', [1, 2], [[1, 1], [1, -1]])) # symbolic n = symbols('n', integer=True, positive=True) mu = MatrixSymbol('mu', n, 1) sigma = MatrixSymbol('sigma', n, n) X = Normal('X', mu, sigma) assert density(X) == MultivariateNormalDistribution(mu, sigma) raises (NotImplementedError, lambda: median(m)) # Below tests should work after issue #17267 is resolved # assert E(X) == mu # assert variance(X) == sigma # test symbolic multivariate normal densities n = 3 Sg = MatrixSymbol('Sg', n, n) mu = MatrixSymbol('mu', n, 1) obs = MatrixSymbol('obs', n, 1) X = MultivariateNormal('X', mu, Sg) density_X = density(X) eval_a = density_X(obs).subs({Sg: eye(3), mu: Matrix([0, 0, 0]), obs: Matrix([0, 0, 0])}).doit() eval_b = density_X(0, 0, 0).subs({Sg: eye(3), mu: Matrix([0, 0, 0])}).doit() assert eval_a == sqrt(2)/(4*pi**Rational(3/2)) assert eval_b == sqrt(2)/(4*pi**Rational(3/2)) n = symbols('n', integer=True, positive=True) Sg = MatrixSymbol('Sg', n, n) mu = MatrixSymbol('mu', n, 1) obs = MatrixSymbol('obs', n, 1) X = MultivariateNormal('X', mu, Sg) density_X_at_obs = density(X)(obs) expected_density = MatrixElement( exp((S(1)/2) * (mu.T - obs.T) * Sg**(-1) * (-mu + obs)) / \ sqrt((2*pi)**n * Determinant(Sg)), 0, 0) assert density_X_at_obs == expected_density
def test_compound_pspace(): X = Normal('X', 2, 4) Y = Normal('Y', 3, 6) assert not isinstance(Y.pspace, CompoundPSpace) N = NormalDistribution(1, 2) D = PoissonDistribution(3) B = BernoulliDistribution(0.2, 1, 0) pspace1 = CompoundPSpace('N', N) pspace2 = CompoundPSpace('D', D) pspace3 = CompoundPSpace('B', B) assert not isinstance(pspace1, CompoundPSpace) assert not isinstance(pspace2, CompoundPSpace) assert not isinstance(pspace3, CompoundPSpace) M = MultivariateNormalDistribution([1, 2], [[2, 1], [1, 2]]) raises(ValueError, lambda: CompoundPSpace('M', M)) Y = Normal('Y', X, 6) assert isinstance(Y.pspace, CompoundPSpace) assert Y.pspace.distribution == CompoundDistribution(NormalDistribution(X, 6)) assert Y.pspace.domain.set == Interval(-oo, oo)
def test_Normal(): m = Normal("A", [1, 2], [[1, 0], [0, 1]]) assert density(m)(1, 2) == 1 / (2 * pi) raises(ValueError, lambda: m[2]) raises(ValueError, lambda: Normal("M", [1, 2], [[0, 0], [0, 1]])) n = Normal("B", [1, 2, 3], [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) p = Normal("C", Matrix([1, 2]), Matrix([[1, 0], [0, 1]])) assert density(m)(x, y) == density(p)(x, y) assert marginal_distribution(n, 0, 1)(1, 2) == 1 / (2 * pi) assert integrate(density(m)(x, y), (x, -oo, oo), (y, -oo, oo)).evalf() == 1 N = Normal("N", [1, 2], [[x, 0], [0, y]]) assert density(N)(0, 0) == exp(-2 / y - 1 / (2 * x)) / (2 * pi * sqrt(x * y)) raises(ValueError, lambda: Normal("M", [1, 2], [[1, 1], [1, -1]])) # symbolic n = symbols("n", natural=True) mu = MatrixSymbol("mu", n, 1) sigma = MatrixSymbol("sigma", n, n) X = Normal("X", mu, sigma) assert density(X) == MultivariateNormalDistribution(mu, sigma)