本文整理汇总了Python中sympy.stats.density函数的典型用法代码示例。如果您正苦于以下问题:Python density函数的具体用法?Python density怎么用?Python density使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了density函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rademacher
def test_rademacher():
X = Rademacher('X')
assert E(X) == 0
assert variance(X) == 1
assert density(X)[-1] == S.Half
assert density(X)[1] == S.Half
示例2: test_discreteuniform
def test_discreteuniform():
# Symbolic
a, b, c, t = symbols('a b c t')
X = DiscreteUniform('X', [a, b, c])
assert E(X) == (a + b + c)/3
assert simplify(variance(X)
- ((a**2 + b**2 + c**2)/3 - (a/3 + b/3 + c/3)**2)) == 0
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')
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 dict(density(Die('D', 6)).items()) == \
dict(density(DiscreteUniform('U', range(1, 7))).items())
assert characteristic_function(X)(t) == exp(I*a*t)/3 + exp(I*b*t)/3 + exp(I*c*t)/3
assert moment_generating_function(X)(t) == exp(a*t)/3 + exp(b*t)/3 + exp(c*t)/3
示例3: test_lognormal
def test_lognormal():
mean = Symbol('mu', real=True, finite=True)
std = Symbol('sigma', positive=True, real=True, finite=True)
X = LogNormal('x', mean, std)
# The sympy integrator can't do this too well
#assert E(X) == exp(mean+std**2/2)
#assert variance(X) == (exp(std**2)-1) * exp(2*mean + std**2)
# Right now, only density function and sampling works
# Test sampling: Only e^mean in sample std of 0
for i in range(3):
X = LogNormal('x', i, 0)
assert S(sample(X)) == N(exp(i))
# The sympy integrator can't do this too well
#assert E(X) ==
mu = Symbol("mu", real=True)
sigma = Symbol("sigma", positive=True)
X = LogNormal('x', mu, sigma)
assert density(X)(x) == (sqrt(2)*exp(-(-mu + log(x))**2
/(2*sigma**2))/(2*x*sqrt(pi)*sigma))
X = LogNormal('x', 0, 1) # Mean 0, standard deviation 1
assert density(X)(x) == sqrt(2)*exp(-log(x)**2/2)/(2*x*sqrt(pi))
示例4: test_multivariate_laplace
def test_multivariate_laplace():
from sympy.stats.crv_types import Laplace
raises(ValueError, lambda: Laplace('T', [1, 2], [[1, 2], [2, 1]]))
L = Laplace('L', [1, 0], [[1, 2], [0, 1]])
assert density(L)(2, 3) == exp(2)*besselk(0, sqrt(3))/pi
L1 = Laplace('L1', [1, 2], [[x, 0], [0, y]])
assert density(L1)(0, 1) == \
exp(2/y)*besselk(0, sqrt((2 + 4/y + 1/x)/y))/(pi*sqrt(x*y))
示例5: test_difficult_univariate
def test_difficult_univariate():
""" Since using solve in place of deltaintegrate we're able to perform
substantially more complex density computations on single continuous random
variables """
x = Normal('x', 0, 1)
assert density(x**3)
assert density(exp(x**2))
assert density(log(x))
示例6: test_MultivariateTDist
def test_MultivariateTDist():
from sympy.stats.joint_rv_types import MultivariateT
t1 = MultivariateT('T', [0, 0], [[1, 0], [0, 1]], 2)
assert(density(t1))(1, 1) == 1/(8*pi)
assert integrate(density(t1)(x, y), (x, -oo, oo), \
(y, -oo, oo)).evalf() == 1
raises(ValueError, lambda: MultivariateT('T', [1, 2], [[1, 1], [1, -1]], 1))
t2 = MultivariateT('t2', [1, 2], [[x, 0], [0, y]], 1)
assert density(t2)(1, 2) == 1/(2*pi*sqrt(x*y))
示例7: test_rademacher
def test_rademacher():
X = Rademacher('X')
t = Symbol('t')
assert E(X) == 0
assert variance(X) == 1
assert density(X)[-1] == S.Half
assert density(X)[1] == S.Half
assert characteristic_function(X)(t) == exp(I*t)/2 + exp(-I*t)/2
assert moment_generating_function(X)(t) == exp(t) / 2 + exp(-t) / 2
示例8: test_conditional_1d
def test_conditional_1d():
X = Normal('x', 0, 1)
Y = given(X, X >= 0)
assert density(Y) == 2 * density(X)
assert Y.pspace.domain.set == Interval(0, oo)
assert E(Y) == sqrt(2) / sqrt(pi)
assert E(X**2) == E(Y**2)
示例9: test_cauchy
def test_cauchy():
x0 = Symbol("x0")
gamma = Symbol("gamma", positive=True)
X = Cauchy('x', x0, gamma)
assert density(X)(x) == 1/(pi*gamma*(1 + (x - x0)**2/gamma**2))
assert cdf(X)(x) == atan((x - x0)/gamma)/pi + S.Half
assert diff(cdf(X)(x), x) == density(X)(x)
gamma = Symbol("gamma", positive=False)
raises(ValueError, lambda: Cauchy('x', x0, gamma))
示例10: test_Normal
def test_Normal():
m = Normal('A', [1, 2], [[1, 0], [0, 1]])
assert density(m)(1, 2) == 1/(2*pi)
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
N1 = Normal('N1', [1, 2], [[x, 0], [0, y]])
assert str(density(N1)(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]]))
示例11: test_bernoulli
def test_bernoulli():
p, a, b = symbols('p a b')
X = Bernoulli('B', p, a, b)
assert E(X) == a*p + b*(-p + 1)
assert density(X)[a] == p
assert density(X)[b] == 1 - p
X = Bernoulli('B', p, 1, 0)
assert E(X) == p
assert simplify(variance(X)) == p*(1 - p)
E(a*X + b) == a*E(X) + b
variance(a*X + b) == a**2 * variance(X)
示例12: test_bernoulli
def test_bernoulli():
p, a, b = symbols("p a b")
X = Bernoulli(p, a, b, symbol="B")
assert E(X) == a * p + b * (-p + 1)
assert density(X)[a] == p
assert density(X)[b] == 1 - p
X = Bernoulli(p, 1, 0, symbol="B")
assert E(X) == p
assert variance(X) == -p ** 2 + p
E(a * X + b) == a * E(X) + b
variance(a * X + b) == a ** 2 * variance(X)
示例13: test_coins
def test_coins():
C, D = Coin("C"), Coin("D")
H, T = symbols("H, T")
assert P(Eq(C, D)) == S.Half
assert density(Tuple(C, D)) == {(H, H): S.One / 4, (H, T): S.One / 4, (T, H): S.One / 4, (T, T): S.One / 4}
assert dict(density(C).items()) == {H: S.Half, T: S.Half}
F = Coin("F", S.One / 10)
assert P(Eq(F, H)) == S(1) / 10
d = pspace(C).domain
assert d.as_boolean() == Or(Eq(C.symbol, H), Eq(C.symbol, T))
raises(ValueError, lambda: P(C > D)) # Can't intelligently compare H to T
示例14: test_coins
def test_coins():
C, D = Coin(), Coin()
H, T = sorted(density(C).keys())
assert P(Eq(C, D)) == S.Half
assert density(Tuple(C, D)) == {(H, H): S.One / 4, (H, T): S.One / 4, (T, H): S.One / 4, (T, T): S.One / 4}
assert density(C) == {H: S.Half, T: S.Half}
E = Coin(S.One / 10)
assert P(Eq(E, H)) == S(1) / 10
d = pspace(C).domain
assert d.as_boolean() == Or(Eq(C.symbol, H), Eq(C.symbol, T))
raises(ValueError, lambda: P(C > D)) # Can't intelligently compare H to T
示例15: test_uniformsum
def test_uniformsum():
n = Symbol("n", integer=True)
_k = Symbol("k")
X = UniformSum('x', n)
assert density(X)(x) == (Sum((-1)**_k*(-_k + x)**(n - 1)
*binomial(n, _k), (_k, 0, floor(x)))/factorial(n - 1))