test_discrete_rv.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. from sympy.concrete.summations import Sum
  2. from sympy.core.numbers import (I, Rational, oo, pi)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import Symbol
  5. from sympy.functions.elementary.complexes import (im, re)
  6. from sympy.functions.elementary.exponential import log
  7. from sympy.functions.elementary.integers import floor
  8. from sympy.functions.elementary.miscellaneous import sqrt
  9. from sympy.functions.elementary.piecewise import Piecewise
  10. from sympy.functions.special.bessel import besseli
  11. from sympy.functions.special.beta_functions import beta
  12. from sympy.functions.special.zeta_functions import zeta
  13. from sympy.sets.sets import FiniteSet
  14. from sympy.simplify.simplify import simplify
  15. from sympy.utilities.lambdify import lambdify
  16. from sympy.core.relational import Eq, Ne
  17. from sympy.functions.elementary.exponential import exp
  18. from sympy.logic.boolalg import Or
  19. from sympy.sets.fancysets import Range
  20. from sympy.stats import (P, E, variance, density, characteristic_function,
  21. where, moment_generating_function, skewness, cdf,
  22. kurtosis, coskewness)
  23. from sympy.stats.drv_types import (PoissonDistribution, GeometricDistribution,
  24. FlorySchulz, Poisson, Geometric, Hermite, Logarithmic,
  25. NegativeBinomial, Skellam, YuleSimon, Zeta,
  26. DiscreteRV)
  27. from sympy.testing.pytest import slow, nocache_fail, raises
  28. from sympy.stats.symbolic_probability import Expectation
  29. x = Symbol('x')
  30. def test_PoissonDistribution():
  31. l = 3
  32. p = PoissonDistribution(l)
  33. assert abs(p.cdf(10).evalf() - 1) < .001
  34. assert abs(p.cdf(10.4).evalf() - 1) < .001
  35. assert p.expectation(x, x) == l
  36. assert p.expectation(x**2, x) - p.expectation(x, x)**2 == l
  37. def test_Poisson():
  38. l = 3
  39. x = Poisson('x', l)
  40. assert E(x) == l
  41. assert E(2*x) == 2*l
  42. assert variance(x) == l
  43. assert density(x) == PoissonDistribution(l)
  44. assert isinstance(E(x, evaluate=False), Expectation)
  45. assert isinstance(E(2*x, evaluate=False), Expectation)
  46. # issue 8248
  47. assert x.pspace.compute_expectation(1) == 1
  48. def test_FlorySchulz():
  49. a = Symbol("a")
  50. z = Symbol("z")
  51. x = FlorySchulz('x', a)
  52. assert E(x) == (2 - a)/a
  53. assert (variance(x) - 2*(1 - a)/a**2).simplify() == S(0)
  54. assert density(x)(z) == a**2*z*(1 - a)**(z - 1)
  55. @slow
  56. def test_GeometricDistribution():
  57. p = S.One / 5
  58. d = GeometricDistribution(p)
  59. assert d.expectation(x, x) == 1/p
  60. assert d.expectation(x**2, x) - d.expectation(x, x)**2 == (1-p)/p**2
  61. assert abs(d.cdf(20000).evalf() - 1) < .001
  62. assert abs(d.cdf(20000.8).evalf() - 1) < .001
  63. G = Geometric('G', p=S(1)/4)
  64. assert cdf(G)(S(7)/2) == P(G <= S(7)/2)
  65. X = Geometric('X', Rational(1, 5))
  66. Y = Geometric('Y', Rational(3, 10))
  67. assert coskewness(X, X + Y, X + 2*Y).simplify() == sqrt(230)*Rational(81, 1150)
  68. def test_Hermite():
  69. a1 = Symbol("a1", positive=True)
  70. a2 = Symbol("a2", negative=True)
  71. raises(ValueError, lambda: Hermite("H", a1, a2))
  72. a1 = Symbol("a1", negative=True)
  73. a2 = Symbol("a2", positive=True)
  74. raises(ValueError, lambda: Hermite("H", a1, a2))
  75. a1 = Symbol("a1", positive=True)
  76. x = Symbol("x")
  77. H = Hermite("H", a1, a2)
  78. assert moment_generating_function(H)(x) == exp(a1*(exp(x) - 1)
  79. + a2*(exp(2*x) - 1))
  80. assert characteristic_function(H)(x) == exp(a1*(exp(I*x) - 1)
  81. + a2*(exp(2*I*x) - 1))
  82. assert E(H) == a1 + 2*a2
  83. H = Hermite("H", a1=5, a2=4)
  84. assert density(H)(2) == 33*exp(-9)/2
  85. assert E(H) == 13
  86. assert variance(H) == 21
  87. assert kurtosis(H) == Rational(464,147)
  88. assert skewness(H) == 37*sqrt(21)/441
  89. def test_Logarithmic():
  90. p = S.Half
  91. x = Logarithmic('x', p)
  92. assert E(x) == -p / ((1 - p) * log(1 - p))
  93. assert variance(x) == -1/log(2)**2 + 2/log(2)
  94. assert E(2*x**2 + 3*x + 4) == 4 + 7 / log(2)
  95. assert isinstance(E(x, evaluate=False), Expectation)
  96. @nocache_fail
  97. def test_negative_binomial():
  98. r = 5
  99. p = S.One / 3
  100. x = NegativeBinomial('x', r, p)
  101. assert E(x) == p*r / (1-p)
  102. # This hangs when run with the cache disabled:
  103. assert variance(x) == p*r / (1-p)**2
  104. assert E(x**5 + 2*x + 3) == Rational(9207, 4)
  105. assert isinstance(E(x, evaluate=False), Expectation)
  106. def test_skellam():
  107. mu1 = Symbol('mu1')
  108. mu2 = Symbol('mu2')
  109. z = Symbol('z')
  110. X = Skellam('x', mu1, mu2)
  111. assert density(X)(z) == (mu1/mu2)**(z/2) * \
  112. exp(-mu1 - mu2)*besseli(z, 2*sqrt(mu1*mu2))
  113. assert skewness(X).expand() == mu1/(mu1*sqrt(mu1 + mu2) + mu2 *
  114. sqrt(mu1 + mu2)) - mu2/(mu1*sqrt(mu1 + mu2) + mu2*sqrt(mu1 + mu2))
  115. assert variance(X).expand() == mu1 + mu2
  116. assert E(X) == mu1 - mu2
  117. assert characteristic_function(X)(z) == exp(
  118. mu1*exp(I*z) - mu1 - mu2 + mu2*exp(-I*z))
  119. assert moment_generating_function(X)(z) == exp(
  120. mu1*exp(z) - mu1 - mu2 + mu2*exp(-z))
  121. def test_yule_simon():
  122. from sympy.core.singleton import S
  123. rho = S(3)
  124. x = YuleSimon('x', rho)
  125. assert simplify(E(x)) == rho / (rho - 1)
  126. assert simplify(variance(x)) == rho**2 / ((rho - 1)**2 * (rho - 2))
  127. assert isinstance(E(x, evaluate=False), Expectation)
  128. # To test the cdf function
  129. assert cdf(x)(x) == Piecewise((-beta(floor(x), 4)*floor(x) + 1, x >= 1), (0, True))
  130. def test_zeta():
  131. s = S(5)
  132. x = Zeta('x', s)
  133. assert E(x) == zeta(s-1) / zeta(s)
  134. assert simplify(variance(x)) == (
  135. zeta(s) * zeta(s-2) - zeta(s-1)**2) / zeta(s)**2
  136. def test_discrete_probability():
  137. X = Geometric('X', Rational(1, 5))
  138. Y = Poisson('Y', 4)
  139. G = Geometric('e', x)
  140. assert P(Eq(X, 3)) == Rational(16, 125)
  141. assert P(X < 3) == Rational(9, 25)
  142. assert P(X > 3) == Rational(64, 125)
  143. assert P(X >= 3) == Rational(16, 25)
  144. assert P(X <= 3) == Rational(61, 125)
  145. assert P(Ne(X, 3)) == Rational(109, 125)
  146. assert P(Eq(Y, 3)) == 32*exp(-4)/3
  147. assert P(Y < 3) == 13*exp(-4)
  148. assert P(Y > 3).equals(32*(Rational(-71, 32) + 3*exp(4)/32)*exp(-4)/3)
  149. assert P(Y >= 3).equals(32*(Rational(-39, 32) + 3*exp(4)/32)*exp(-4)/3)
  150. assert P(Y <= 3) == 71*exp(-4)/3
  151. assert P(Ne(Y, 3)).equals(
  152. 13*exp(-4) + 32*(Rational(-71, 32) + 3*exp(4)/32)*exp(-4)/3)
  153. assert P(X < S.Infinity) is S.One
  154. assert P(X > S.Infinity) is S.Zero
  155. assert P(G < 3) == x*(2-x)
  156. assert P(Eq(G, 3)) == x*(-x + 1)**2
  157. def test_DiscreteRV():
  158. p = S(1)/2
  159. x = Symbol('x', integer=True, positive=True)
  160. pdf = p*(1 - p)**(x - 1) # pdf of Geometric Distribution
  161. D = DiscreteRV(x, pdf, set=S.Naturals, check=True)
  162. assert E(D) == E(Geometric('G', S(1)/2)) == 2
  163. assert P(D > 3) == S(1)/8
  164. assert D.pspace.domain.set == S.Naturals
  165. raises(ValueError, lambda: DiscreteRV(x, x, FiniteSet(*range(4)), check=True))
  166. # purposeful invalid pmf but it should not raise since check=False
  167. # see test_drv_types.test_ContinuousRV for explanation
  168. X = DiscreteRV(x, 1/x, S.Naturals)
  169. assert P(X < 2) == 1
  170. assert E(X) == oo
  171. def test_precomputed_characteristic_functions():
  172. import mpmath
  173. def test_cf(dist, support_lower_limit, support_upper_limit):
  174. pdf = density(dist)
  175. t = S('t')
  176. x = S('x')
  177. # first function is the hardcoded CF of the distribution
  178. cf1 = lambdify([t], characteristic_function(dist)(t), 'mpmath')
  179. # second function is the Fourier transform of the density function
  180. f = lambdify([x, t], pdf(x)*exp(I*x*t), 'mpmath')
  181. cf2 = lambda t: mpmath.nsum(lambda x: f(x, t), [
  182. support_lower_limit, support_upper_limit], maxdegree=10)
  183. # compare the two functions at various points
  184. for test_point in [2, 5, 8, 11]:
  185. n1 = cf1(test_point)
  186. n2 = cf2(test_point)
  187. assert abs(re(n1) - re(n2)) < 1e-12
  188. assert abs(im(n1) - im(n2)) < 1e-12
  189. test_cf(Geometric('g', Rational(1, 3)), 1, mpmath.inf)
  190. test_cf(Logarithmic('l', Rational(1, 5)), 1, mpmath.inf)
  191. test_cf(NegativeBinomial('n', 5, Rational(1, 7)), 0, mpmath.inf)
  192. test_cf(Poisson('p', 5), 0, mpmath.inf)
  193. test_cf(YuleSimon('y', 5), 1, mpmath.inf)
  194. test_cf(Zeta('z', 5), 1, mpmath.inf)
  195. def test_moment_generating_functions():
  196. t = S('t')
  197. geometric_mgf = moment_generating_function(Geometric('g', S.Half))(t)
  198. assert geometric_mgf.diff(t).subs(t, 0) == 2
  199. logarithmic_mgf = moment_generating_function(Logarithmic('l', S.Half))(t)
  200. assert logarithmic_mgf.diff(t).subs(t, 0) == 1/log(2)
  201. negative_binomial_mgf = moment_generating_function(
  202. NegativeBinomial('n', 5, Rational(1, 3)))(t)
  203. assert negative_binomial_mgf.diff(t).subs(t, 0) == Rational(5, 2)
  204. poisson_mgf = moment_generating_function(Poisson('p', 5))(t)
  205. assert poisson_mgf.diff(t).subs(t, 0) == 5
  206. skellam_mgf = moment_generating_function(Skellam('s', 1, 1))(t)
  207. assert skellam_mgf.diff(t).subs(
  208. t, 2) == (-exp(-2) + exp(2))*exp(-2 + exp(-2) + exp(2))
  209. yule_simon_mgf = moment_generating_function(YuleSimon('y', 3))(t)
  210. assert simplify(yule_simon_mgf.diff(t).subs(t, 0)) == Rational(3, 2)
  211. zeta_mgf = moment_generating_function(Zeta('z', 5))(t)
  212. assert zeta_mgf.diff(t).subs(t, 0) == pi**4/(90*zeta(5))
  213. def test_Or():
  214. X = Geometric('X', S.Half)
  215. assert P(Or(X < 3, X > 4)) == Rational(13, 16)
  216. assert P(Or(X > 2, X > 1)) == P(X > 1)
  217. assert P(Or(X >= 3, X < 3)) == 1
  218. def test_where():
  219. X = Geometric('X', Rational(1, 5))
  220. Y = Poisson('Y', 4)
  221. assert where(X**2 > 4).set == Range(3, S.Infinity, 1)
  222. assert where(X**2 >= 4).set == Range(2, S.Infinity, 1)
  223. assert where(Y**2 < 9).set == Range(0, 3, 1)
  224. assert where(Y**2 <= 9).set == Range(0, 4, 1)
  225. def test_conditional():
  226. X = Geometric('X', Rational(2, 3))
  227. Y = Poisson('Y', 3)
  228. assert P(X > 2, X > 3) == 1
  229. assert P(X > 3, X > 2) == Rational(1, 3)
  230. assert P(Y > 2, Y < 2) == 0
  231. assert P(Eq(Y, 3), Y >= 0) == 9*exp(-3)/2
  232. assert P(Eq(Y, 3), Eq(Y, 2)) == 0
  233. assert P(X < 2, Eq(X, 2)) == 0
  234. assert P(X > 2, Eq(X, 3)) == 1
  235. def test_product_spaces():
  236. X1 = Geometric('X1', S.Half)
  237. X2 = Geometric('X2', Rational(1, 3))
  238. assert str(P(X1 + X2 < 3).rewrite(Sum)) == (
  239. "Sum(Piecewise((1/(4*2**n), n >= -1), (0, True)), (n, -oo, -1))/3")
  240. assert str(P(X1 + X2 > 3).rewrite(Sum)) == (
  241. 'Sum(Piecewise((2**(X2 - n - 2)*(3/2)**(1 - X2)/6, '
  242. 'X2 - n <= 2), (0, True)), (X2, 1, oo), (n, 1, oo))')
  243. assert P(Eq(X1 + X2, 3)) == Rational(1, 12)