test_discrete_rv.py 11 KB

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