def test_exponential(): rate = Symbol('lambda', positive=True, real=True, finite=True) X = Exponential('x', rate) assert E(X) == 1/rate assert variance(X) == 1/rate**2 assert skewness(X) == 2 assert skewness(X) == smoment(X, 3) assert smoment(2*X, 4) == smoment(X, 4) assert moment(X, 3) == 3*2*1/rate**3 assert P(X > 0) == S(1) assert P(X > 1) == exp(-rate) assert P(X > 10) == exp(-10*rate) assert where(X <= 1).set == Interval(0, 1)
def test_weibull(): a, b = symbols('a b', positive=True) X = Weibull('x', a, b) assert simplify(E(X)) == simplify(a * gamma(1 + 1/b)) assert simplify(variance(X)) == simplify(a**2 * gamma(1 + 2/b) - E(X)**2) assert simplify(skewness(X)) == (2*gamma(1 + 1/b)**3 - 3*gamma(1 + 1/b)*gamma(1 + 2/b) + gamma(1 + 3/b))/(-gamma(1 + 1/b)**2 + gamma(1 + 2/b))**(S(3)/2)
def test_multiple_normal(): X, Y = Normal('x', 0, 1), Normal('y', 0, 1) assert E(X + Y) == 0 assert variance(X + Y) == 2 assert variance(X + X) == 4 assert covariance(X, Y) == 0 assert covariance(2*X + Y, -X) == -2*variance(X) assert skewness(X) == 0 assert skewness(X + Y) == 0 assert correlation(X, Y) == 0 assert correlation(X, X + Y) == correlation(X, X - Y) assert moment(X, 2) == 1 assert cmoment(X, 3) == 0 assert moment(X + Y, 4) == 12 assert cmoment(X, 2) == variance(X) assert smoment(X*X, 2) == 1 assert smoment(X + Y, 3) == skewness(X + Y) assert E(X, Eq(X + Y, 0)) == 0 assert variance(X, Eq(X + Y, 0)) == S.Half
def test_exponential(): rate = Symbol('lambda', positive=True, real=True, bounded=True) X = Exponential('x', rate) assert E(X) == 1/rate assert variance(X) == 1/rate**2 assert skewness(X) == 2 assert P(X > 0) == S(1) assert P(X > 1) == exp(-rate) assert P(X > 10) == exp(-10*rate) assert where(X <= 1).set == Interval(0, 1)
def test_binomial_symbolic(): n = 10 # Because we're using for loops, can't do symbolic n p = symbols('p', positive=True) X = Binomial('X', n, p) assert simplify(E(X)) == n*p == simplify(moment(X, 1)) assert simplify(variance(X)) == n*p*(1 - p) == simplify(cmoment(X, 2)) assert cancel((skewness(X) - (1-2*p)/sqrt(n*p*(1-p)))) == 0 # Test ability to change success/failure winnings H, T = symbols('H T') Y = Binomial('Y', n, p, succ=H, fail=T) assert simplify(E(Y) - (n*(H*p + T*(1 - p)))) == 0
def test_binomial_symbolic(): n = 10 # Because we're using for loops, can't do symbolic n p = symbols("p", positive=True) X = Binomial("X", n, p) assert simplify(E(X)) == n * p == simplify(moment(X, 1)) assert simplify(variance(X)) == n * p * (1 - p) == simplify(cmoment(X, 2)) assert factor(simplify(skewness(X))) == factor((1 - 2 * p) / sqrt(n * p * (1 - p))) # Test ability to change success/failure winnings H, T = symbols("H T") Y = Binomial("Y", n, p, succ=H, fail=T) assert simplify(E(Y)) == simplify(n * (H * p + T * (1 - p)))
def test_binomial_numeric(): nvals = range(5) pvals = [0, S(1)/4, S.Half, S(3)/4, 1] for n in nvals: for p in pvals: X = Binomial('X', n, p) assert E(X) == n*p assert variance(X) == n*p*(1 - p) if n > 0 and 0 < p < 1: assert skewness(X) == (1 - 2*p)/sqrt(n*p*(1 - p)) for k in range(n + 1): assert P(Eq(X, k)) == binomial(n, k)*p**k*(1 - p)**(n - k)
def test_multiple_normal(): X, Y = Normal('x', 0, 1), Normal('y', 0, 1) p = Symbol("p", positive=True) assert E(X + Y) == 0 assert variance(X + Y) == 2 assert variance(X + X) == 4 assert covariance(X, Y) == 0 assert covariance(2*X + Y, -X) == -2*variance(X) assert skewness(X) == 0 assert skewness(X + Y) == 0 assert correlation(X, Y) == 0 assert correlation(X, X + Y) == correlation(X, X - Y) assert moment(X, 2) == 1 assert cmoment(X, 3) == 0 assert moment(X + Y, 4) == 12 assert cmoment(X, 2) == variance(X) assert smoment(X*X, 2) == 1 assert smoment(X + Y, 3) == skewness(X + Y) assert E(X, Eq(X + Y, 0)) == 0 assert variance(X, Eq(X + Y, 0)) == S.Half assert quantile(X)(p) == sqrt(2)*erfinv(2*p - S.One)
def test_binomial_numeric(): nvals = range(5) pvals = [0, S(1) / 4, S.Half, S(3) / 4, 1] for n in nvals: for p in pvals: X = Binomial("X", n, p) assert Eq(E(X), n * p) assert Eq(variance(X), n * p * (1 - p)) if n > 0 and 0 < p < 1: assert Eq(skewness(X), (1 - 2 * p) / sqrt(n * p * (1 - p))) for k in range(n + 1): assert Eq(P(Eq(X, k)), binomial(n, k) * p ** k * (1 - p) ** (n - k))
def test_dice(): # TODO: Make iid method! X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6) a, b, t, p = symbols('a b t p') assert E(X) == 3 + S.Half assert variance(X) == S(35)/12 assert E(X + Y) == 7 assert E(X + X) == 7 assert E(a*X + b) == a*E(X) + b assert variance(X + Y) == variance(X) + variance(Y) == cmoment(X + Y, 2) assert variance(X + X) == 4 * variance(X) == cmoment(X + X, 2) assert cmoment(X, 0) == 1 assert cmoment(4*X, 3) == 64*cmoment(X, 3) assert covariance(X, Y) == S.Zero assert covariance(X, X + Y) == variance(X) assert density(Eq(cos(X*S.Pi), 1))[True] == S.Half assert correlation(X, Y) == 0 assert correlation(X, Y) == correlation(Y, X) assert smoment(X + Y, 3) == skewness(X + Y) assert smoment(X, 0) == 1 assert P(X > 3) == S.Half assert P(2*X > 6) == S.Half assert P(X > Y) == S(5)/12 assert P(Eq(X, Y)) == P(Eq(X, 1)) assert E(X, X > 3) == 5 == moment(X, 1, 0, X > 3) assert E(X, Y > 3) == E(X) == moment(X, 1, 0, Y > 3) assert E(X + Y, Eq(X, Y)) == E(2*X) assert moment(X, 0) == 1 assert moment(5*X, 2) == 25*moment(X, 2) assert quantile(X)(p) == Piecewise((nan, (p > S.One) | (p < S(0))),\ (S.One, p <= S(1)/6), (S(2), p <= S(1)/3), (S(3), p <= S.Half),\ (S(4), p <= S(2)/3), (S(5), p <= S(5)/6), (S(6), p <= S.One)) assert P(X > 3, X > 3) == S.One assert P(X > Y, Eq(Y, 6)) == S.Zero assert P(Eq(X + Y, 12)) == S.One/36 assert P(Eq(X + Y, 12), Eq(X, 6)) == S.One/6 assert density(X + Y) == density(Y + Z) != density(X + X) d = density(2*X + Y**Z) assert d[S(22)] == S.One/108 and d[S(4100)] == S.One/216 and S(3130) not in d assert pspace(X).domain.as_boolean() == Or( *[Eq(X.symbol, i) for i in [1, 2, 3, 4, 5, 6]]) assert where(X > 3).set == FiniteSet(4, 5, 6) assert characteristic_function(X)(t) == exp(6*I*t)/6 + exp(5*I*t)/6 + exp(4*I*t)/6 + exp(3*I*t)/6 + exp(2*I*t)/6 + exp(I*t)/6 assert moment_generating_function(X)(t) == exp(6*t)/6 + exp(5*t)/6 + exp(4*t)/6 + exp(3*t)/6 + exp(2*t)/6 + exp(t)/6
def test_hypergeometric_numeric(): for N in range(1, 5): for m in range(0, N + 1): for n in range(1, N + 1): X = Hypergeometric('X', N, m, n) N, m, n = map(sympify, (N, m, n)) assert sum(density(X).values()) == 1 assert E(X) == n * m / N if N > 1: assert variance(X) == n*(m/N)*(N - m)/N*(N - n)/(N - 1) # Only test for skewness when defined if N > 2 and 0 < m < N and n < N: assert skewness(X) == simplify((N - 2*m)*sqrt(N - 1)*(N - 2*n) / (sqrt(n*m*(N - m)*(N - n))*(N - 2)))
def test_binomial_symbolic(): n = 2 # Because we're using for loops, can't do symbolic n p = symbols('p', positive=True) X = Binomial('X', n, p) t = Symbol('t') assert simplify(E(X)) == n*p == simplify(moment(X, 1)) assert simplify(variance(X)) == n*p*(1 - p) == simplify(cmoment(X, 2)) assert cancel((skewness(X) - (1 - 2*p)/sqrt(n*p*(1 - p)))) == 0 assert characteristic_function(X)(t) == p ** 2 * exp(2 * I * t) + 2 * p * (-p + 1) * exp(I * t) + (-p + 1) ** 2 assert moment_generating_function(X)(t) == p ** 2 * exp(2 * t) + 2 * p * (-p + 1) * exp(t) + (-p + 1) ** 2 # Test ability to change success/failure winnings H, T = symbols('H T') Y = Binomial('Y', n, p, succ=H, fail=T) assert simplify(E(Y) - (n*(H*p + T*(1 - p)))) == 0
def test_dice(): # TODO: Make iid method! X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6) a, b = symbols('a b') assert E(X) == 3 + S.Half assert variance(X) == S(35)/12 assert E(X + Y) == 7 assert E(X + X) == 7 assert E(a*X + b) == a*E(X) + b assert variance(X + Y) == variance(X) + variance(Y) == cmoment(X + Y, 2) assert variance(X + X) == 4 * variance(X) == cmoment(X + X, 2) assert cmoment(X, 0) == 1 assert cmoment(4*X, 3) == 64*cmoment(X, 3) assert covariance(X, Y) == S.Zero assert covariance(X, X + Y) == variance(X) assert density(Eq(cos(X*S.Pi), 1))[True] == S.Half assert correlation(X, Y) == 0 assert correlation(X, Y) == correlation(Y, X) assert smoment(X + Y, 3) == skewness(X + Y) assert smoment(X, 0) == 1 assert P(X > 3) == S.Half assert P(2*X > 6) == S.Half assert P(X > Y) == S(5)/12 assert P(Eq(X, Y)) == P(Eq(X, 1)) assert E(X, X > 3) == 5 == moment(X, 1, 0, X > 3) assert E(X, Y > 3) == E(X) == moment(X, 1, 0, Y > 3) assert E(X + Y, Eq(X, Y)) == E(2*X) assert moment(X, 0) == 1 assert moment(5*X, 2) == 25*moment(X, 2) assert P(X > 3, X > 3) == S.One assert P(X > Y, Eq(Y, 6)) == S.Zero assert P(Eq(X + Y, 12)) == S.One/36 assert P(Eq(X + Y, 12), Eq(X, 6)) == S.One/6 assert density(X + Y) == density(Y + Z) != density(X + X) d = density(2*X + Y**Z) assert d[S(22)] == S.One/108 and d[S(4100)] == S.One/216 and S(3130) not in d assert pspace(X).domain.as_boolean() == Or( *[Eq(X.symbol, i) for i in [1, 2, 3, 4, 5, 6]]) assert where(X > 3).set == FiniteSet(4, 5, 6)
def test_gamma(): k = Symbol("k", positive=True) theta = Symbol("theta", positive=True) X = Gamma('x', k, theta) assert density(X)(x) == x**(k - 1)*theta**(-k)*exp(-x/theta)/gamma(k) assert cdf(X, meijerg=True)(z) == Piecewise( (-k*lowergamma(k, 0)/gamma(k + 1) + k*lowergamma(k, z/theta)/gamma(k + 1), z >= 0), (0, True)) # assert simplify(variance(X)) == k*theta**2 # handled numerically below assert E(X) == moment(X, 1) k, theta = symbols('k theta', real=True, finite=True, positive=True) X = Gamma('x', k, theta) assert E(X) == k*theta assert variance(X) == k*theta**2 assert simplify(skewness(X)) == 2/sqrt(k)
def __init__(self): mu1 = Symbol('mu1', positive=True, real=True, bounded=True) s1 = Symbol('s1', positive=True, real=True, bounded=True) mu2 = Symbol('mu2', positive=True, real=True, bounded=True) s2 = Symbol('s2', positive=True, real=True, bounded=True) N1 = Normal('N1', mu1, s1) N2 = Normal('N2', mu2, s2) NN = N1 * N2 self.MeanNN = E(NN) self.VarNN = variance(NN) self.StdevNN = SQRT(self.VarNN) self.SkewNN = skewness(NN) self.meanNN = lambdify([mu1, s1, mu2, s2], self.MeanNN) self.varNN = lambdify([mu1, s1, mu2, s2], self.VarNN) self.stdevNN = lambdify([mu1, s1, mu2, s2], self.StdevNN) self.skewNN = lambdify([mu1, s1, mu2, s2], self.SkewNN)
def test_discreteuniform(): # Symbolic a, b, c = symbols("a b c") X = DiscreteUniform([a, b, c]) assert E(X) == (a + b + c) / 3 assert variance(X) == (a ** 2 + b ** 2 + c ** 2) / 3 - (a / 3 + b / 3 + c / 3) ** 2 assert P(Eq(X, a)) == P(Eq(X, b)) == P(Eq(X, c)) == S("1/3") Y = DiscreteUniform(range(-5, 5)) # Numeric assert E(Y) == S("-1/2") assert variance(Y) == S("33/4") assert skewness(Y) == 0 for x in range(-5, 5): assert P(Eq(Y, x)) == S("1/10") assert P(Y <= x) == S(x + 6) / 10 assert P(Y >= x) == S(5 - x) / 10 assert density(Die(6)) == density(DiscreteUniform(range(1, 7)))
def test_discreteuniform(): # Symbolic a, b, c = symbols('a b c') X = DiscreteUniform('X', [a,b,c]) assert E(X) == (a+b+c)/3 assert variance(X) == (a**2+b**2+c**2)/3 - (a/3+b/3+c/3)**2 assert P(Eq(X, a)) == P(Eq(X, b)) == P(Eq(X, c)) == S('1/3') Y = DiscreteUniform('Y', range(-5, 5)) # Numeric assert E(Y) == S('-1/2') assert variance(Y) == S('33/4') assert skewness(Y) == 0 for x in range(-5, 5): assert P(Eq(Y, x)) == S('1/10') assert P(Y <= x) == S(x+6)/10 assert P(Y >= x) == S(5-x)/10 assert density(Die('D', 6)) == density(DiscreteUniform('U', range(1,7)))
def __init__(self, n): mu = symbols('mu0:%d' % n, positive=True, real=True, bounded=True) s = symbols('s0:%d' % n, positive=True, real=True, bounded=True) N = [] for i in range(n): N.append(Normal('N%d' % i, mu[i], s[i])) NN = N[-1] for i in range(n - 1): NN *= N[i] self.DistributionNN = NN self.MeanNN = E(NN) self.VarNN = variance(NN) self.StdevNN = SQRT(self.VarNN) self.SkewNN = skewness(NN) self.meanNN = lambdify([mu, s], self.MeanNN) self.varNN = lambdify([mu, s], self.VarNN) self.stdevNN = lambdify([mu, s], self.StdevNN) self.skewNN = lambdify([mu, s], self.SkewNN)
def test_dice(): # TODO: Make iid method! X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6) a, b, t, p = symbols('a b t p') assert E(X) == 3 + S.Half assert variance(X) == S(35) / 12 assert E(X + Y) == 7 assert E(X + X) == 7 assert E(a * X + b) == a * E(X) + b assert variance(X + Y) == variance(X) + variance(Y) == cmoment(X + Y, 2) assert variance(X + X) == 4 * variance(X) == cmoment(X + X, 2) assert cmoment(X, 0) == 1 assert cmoment(4 * X, 3) == 64 * cmoment(X, 3) assert covariance(X, Y) == S.Zero assert covariance(X, X + Y) == variance(X) assert density(Eq(cos(X * S.Pi), 1))[True] == S.Half assert correlation(X, Y) == 0 assert correlation(X, Y) == correlation(Y, X) assert smoment(X + Y, 3) == skewness(X + Y) assert smoment(X + Y, 4) == kurtosis(X + Y) assert smoment(X, 0) == 1 assert P(X > 3) == S.Half assert P(2 * X > 6) == S.Half assert P(X > Y) == S(5) / 12 assert P(Eq(X, Y)) == P(Eq(X, 1)) assert E(X, X > 3) == 5 == moment(X, 1, 0, X > 3) assert E(X, Y > 3) == E(X) == moment(X, 1, 0, Y > 3) assert E(X + Y, Eq(X, Y)) == E(2 * X) assert moment(X, 0) == 1 assert moment(5 * X, 2) == 25 * moment(X, 2) assert quantile(X)(p) == Piecewise((nan, (p > S.One) | (p < S(0))),\ (S.One, p <= S(1)/6), (S(2), p <= S(1)/3), (S(3), p <= S.Half),\ (S(4), p <= S(2)/3), (S(5), p <= S(5)/6), (S(6), p <= S.One)) assert P(X > 3, X > 3) == S.One assert P(X > Y, Eq(Y, 6)) == S.Zero assert P(Eq(X + Y, 12)) == S.One / 36 assert P(Eq(X + Y, 12), Eq(X, 6)) == S.One / 6 assert density(X + Y) == density(Y + Z) != density(X + X) d = density(2 * X + Y**Z) assert d[S(22)] == S.One / 108 and d[S(4100)] == S.One / 216 and S( 3130) not in d assert pspace(X).domain.as_boolean() == Or( *[Eq(X.symbol, i) for i in [1, 2, 3, 4, 5, 6]]) assert where(X > 3).set == FiniteSet(4, 5, 6) assert characteristic_function(X)(t) == exp(6 * I * t) / 6 + exp( 5 * I * t) / 6 + exp(4 * I * t) / 6 + exp(3 * I * t) / 6 + exp( 2 * I * t) / 6 + exp(I * t) / 6 assert moment_generating_function(X)( t) == exp(6 * t) / 6 + exp(5 * t) / 6 + exp(4 * t) / 6 + exp( 3 * t) / 6 + exp(2 * t) / 6 + exp(t) / 6 # Bayes test for die BayesTest(X > 3, X + Y < 5) BayesTest(Eq(X - Y, Z), Z > Y) BayesTest(X > 3, X > 2) # arg test for die raises(ValueError, lambda: Die('X', -1)) # issue 8105: negative sides. raises(ValueError, lambda: Die('X', 0)) raises(ValueError, lambda: Die('X', 1.5)) # issue 8103: non integer sides. # symbolic test for die n, k = symbols('n, k', positive=True) D = Die('D', n) dens = density(D).dict assert dens == Density(DieDistribution(n)) assert set(dens.subs(n, 4).doit().keys()) == set([1, 2, 3, 4]) assert set(dens.subs(n, 4).doit().values()) == set([S(1) / 4]) k = Dummy('k', integer=True) assert E(D).dummy_eq(Sum(Piecewise((k / n, k <= n), (0, True)), (k, 1, n))) assert variance(D).subs(n, 6).doit() == S(35) / 12 ki = Dummy('ki') cumuf = cdf(D)(k) assert cumuf.dummy_eq( Sum(Piecewise((1 / n, (ki >= 1) & (ki <= n)), (0, True)), (ki, 1, k))) assert cumuf.subs({n: 6, k: 2}).doit() == S(1) / 3 t = Dummy('t') cf = characteristic_function(D)(t) assert cf.dummy_eq( Sum(Piecewise((exp(ki * I * t) / n, (ki >= 1) & (ki <= n)), (0, True)), (ki, 1, n))) assert cf.subs( n, 3).doit() == exp(3 * I * t) / 3 + exp(2 * I * t) / 3 + exp(I * t) / 3 mgf = moment_generating_function(D)(t) assert mgf.dummy_eq( Sum(Piecewise((exp(ki * t) / n, (ki >= 1) & (ki <= n)), (0, True)), (ki, 1, n))) assert mgf.subs(n, 3).doit() == exp(3 * t) / 3 + exp(2 * t) / 3 + exp(t) / 3