m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
12 KiB

7 months ago
  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 (Dummy, 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, ignore_warnings
  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 variance(x) == l
  42. assert density(x) == PoissonDistribution(l)
  43. with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed
  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. with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed
  96. assert isinstance(E(x, evaluate=False), Expectation)
  97. @nocache_fail
  98. def test_negative_binomial():
  99. r = 5
  100. p = S.One / 3
  101. x = NegativeBinomial('x', r, p)
  102. assert E(x) == p*r / (1-p)
  103. # This hangs when run with the cache disabled:
  104. assert variance(x) == p*r / (1-p)**2
  105. assert E(x**5 + 2*x + 3) == Rational(9207, 4)
  106. with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed
  107. assert isinstance(E(x, evaluate=False), Expectation)
  108. def test_skellam():
  109. mu1 = Symbol('mu1')
  110. mu2 = Symbol('mu2')
  111. z = Symbol('z')
  112. X = Skellam('x', mu1, mu2)
  113. assert density(X)(z) == (mu1/mu2)**(z/2) * \
  114. exp(-mu1 - mu2)*besseli(z, 2*sqrt(mu1*mu2))
  115. assert skewness(X).expand() == mu1/(mu1*sqrt(mu1 + mu2) + mu2 *
  116. sqrt(mu1 + mu2)) - mu2/(mu1*sqrt(mu1 + mu2) + mu2*sqrt(mu1 + mu2))
  117. assert variance(X).expand() == mu1 + mu2
  118. assert E(X) == mu1 - mu2
  119. assert characteristic_function(X)(z) == exp(
  120. mu1*exp(I*z) - mu1 - mu2 + mu2*exp(-I*z))
  121. assert moment_generating_function(X)(z) == exp(
  122. mu1*exp(z) - mu1 - mu2 + mu2*exp(-z))
  123. def test_yule_simon():
  124. from sympy.core.singleton import S
  125. rho = S(3)
  126. x = YuleSimon('x', rho)
  127. assert simplify(E(x)) == rho / (rho - 1)
  128. assert simplify(variance(x)) == rho**2 / ((rho - 1)**2 * (rho - 2))
  129. with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed
  130. assert isinstance(E(x, evaluate=False), Expectation)
  131. # To test the cdf function
  132. assert cdf(x)(x) == Piecewise((-beta(floor(x), 4)*floor(x) + 1, x >= 1), (0, True))
  133. def test_zeta():
  134. s = S(5)
  135. x = Zeta('x', s)
  136. assert E(x) == zeta(s-1) / zeta(s)
  137. assert simplify(variance(x)) == (
  138. zeta(s) * zeta(s-2) - zeta(s-1)**2) / zeta(s)**2
  139. def test_discrete_probability():
  140. X = Geometric('X', Rational(1, 5))
  141. Y = Poisson('Y', 4)
  142. G = Geometric('e', x)
  143. assert P(Eq(X, 3)) == Rational(16, 125)
  144. assert P(X < 3) == Rational(9, 25)
  145. assert P(X > 3) == Rational(64, 125)
  146. assert P(X >= 3) == Rational(16, 25)
  147. assert P(X <= 3) == Rational(61, 125)
  148. assert P(Ne(X, 3)) == Rational(109, 125)
  149. assert P(Eq(Y, 3)) == 32*exp(-4)/3
  150. assert P(Y < 3) == 13*exp(-4)
  151. assert P(Y > 3).equals(32*(Rational(-71, 32) + 3*exp(4)/32)*exp(-4)/3)
  152. assert P(Y >= 3).equals(32*(Rational(-39, 32) + 3*exp(4)/32)*exp(-4)/3)
  153. assert P(Y <= 3) == 71*exp(-4)/3
  154. assert P(Ne(Y, 3)).equals(
  155. 13*exp(-4) + 32*(Rational(-71, 32) + 3*exp(4)/32)*exp(-4)/3)
  156. assert P(X < S.Infinity) is S.One
  157. assert P(X > S.Infinity) is S.Zero
  158. assert P(G < 3) == x*(2-x)
  159. assert P(Eq(G, 3)) == x*(-x + 1)**2
  160. def test_DiscreteRV():
  161. p = S(1)/2
  162. x = Symbol('x', integer=True, positive=True)
  163. pdf = p*(1 - p)**(x - 1) # pdf of Geometric Distribution
  164. D = DiscreteRV(x, pdf, set=S.Naturals, check=True)
  165. assert E(D) == E(Geometric('G', S(1)/2)) == 2
  166. assert P(D > 3) == S(1)/8
  167. assert D.pspace.domain.set == S.Naturals
  168. raises(ValueError, lambda: DiscreteRV(x, x, FiniteSet(*range(4)), check=True))
  169. # purposeful invalid pmf but it should not raise since check=False
  170. # see test_drv_types.test_ContinuousRV for explanation
  171. X = DiscreteRV(x, 1/x, S.Naturals)
  172. assert P(X < 2) == 1
  173. assert E(X) == oo
  174. def test_precomputed_characteristic_functions():
  175. import mpmath
  176. def test_cf(dist, support_lower_limit, support_upper_limit):
  177. pdf = density(dist)
  178. t = S('t')
  179. x = S('x')
  180. # first function is the hardcoded CF of the distribution
  181. cf1 = lambdify([t], characteristic_function(dist)(t), 'mpmath')
  182. # second function is the Fourier transform of the density function
  183. f = lambdify([x, t], pdf(x)*exp(I*x*t), 'mpmath')
  184. cf2 = lambda t: mpmath.nsum(lambda x: f(x, t), [
  185. support_lower_limit, support_upper_limit], maxdegree=10)
  186. # compare the two functions at various points
  187. for test_point in [2, 5, 8, 11]:
  188. n1 = cf1(test_point)
  189. n2 = cf2(test_point)
  190. assert abs(re(n1) - re(n2)) < 1e-12
  191. assert abs(im(n1) - im(n2)) < 1e-12
  192. test_cf(Geometric('g', Rational(1, 3)), 1, mpmath.inf)
  193. test_cf(Logarithmic('l', Rational(1, 5)), 1, mpmath.inf)
  194. test_cf(NegativeBinomial('n', 5, Rational(1, 7)), 0, mpmath.inf)
  195. test_cf(Poisson('p', 5), 0, mpmath.inf)
  196. test_cf(YuleSimon('y', 5), 1, mpmath.inf)
  197. test_cf(Zeta('z', 5), 1, mpmath.inf)
  198. def test_moment_generating_functions():
  199. t = S('t')
  200. geometric_mgf = moment_generating_function(Geometric('g', S.Half))(t)
  201. assert geometric_mgf.diff(t).subs(t, 0) == 2
  202. logarithmic_mgf = moment_generating_function(Logarithmic('l', S.Half))(t)
  203. assert logarithmic_mgf.diff(t).subs(t, 0) == 1/log(2)
  204. negative_binomial_mgf = moment_generating_function(
  205. NegativeBinomial('n', 5, Rational(1, 3)))(t)
  206. assert negative_binomial_mgf.diff(t).subs(t, 0) == Rational(5, 2)
  207. poisson_mgf = moment_generating_function(Poisson('p', 5))(t)
  208. assert poisson_mgf.diff(t).subs(t, 0) == 5
  209. skellam_mgf = moment_generating_function(Skellam('s', 1, 1))(t)
  210. assert skellam_mgf.diff(t).subs(
  211. t, 2) == (-exp(-2) + exp(2))*exp(-2 + exp(-2) + exp(2))
  212. yule_simon_mgf = moment_generating_function(YuleSimon('y', 3))(t)
  213. assert simplify(yule_simon_mgf.diff(t).subs(t, 0)) == Rational(3, 2)
  214. zeta_mgf = moment_generating_function(Zeta('z', 5))(t)
  215. assert zeta_mgf.diff(t).subs(t, 0) == pi**4/(90*zeta(5))
  216. def test_Or():
  217. X = Geometric('X', S.Half)
  218. assert P(Or(X < 3, X > 4)) == Rational(13, 16)
  219. assert P(Or(X > 2, X > 1)) == P(X > 1)
  220. assert P(Or(X >= 3, X < 3)) == 1
  221. def test_where():
  222. X = Geometric('X', Rational(1, 5))
  223. Y = Poisson('Y', 4)
  224. assert where(X**2 > 4).set == Range(3, S.Infinity, 1)
  225. assert where(X**2 >= 4).set == Range(2, S.Infinity, 1)
  226. assert where(Y**2 < 9).set == Range(0, 3, 1)
  227. assert where(Y**2 <= 9).set == Range(0, 4, 1)
  228. def test_conditional():
  229. X = Geometric('X', Rational(2, 3))
  230. Y = Poisson('Y', 3)
  231. assert P(X > 2, X > 3) == 1
  232. assert P(X > 3, X > 2) == Rational(1, 3)
  233. assert P(Y > 2, Y < 2) == 0
  234. assert P(Eq(Y, 3), Y >= 0) == 9*exp(-3)/2
  235. assert P(Eq(Y, 3), Eq(Y, 2)) == 0
  236. assert P(X < 2, Eq(X, 2)) == 0
  237. assert P(X > 2, Eq(X, 3)) == 1
  238. def test_product_spaces():
  239. X1 = Geometric('X1', S.Half)
  240. X2 = Geometric('X2', Rational(1, 3))
  241. #assert str(P(X1 + X2 < 3, evaluate=False)) == """Sum(Piecewise((2**(X2 - n - 2)*(2/3)**(X2 - 1)/6, """\
  242. # + """(-X2 + n + 3 >= 1) & (-X2 + n + 3 < oo)), (0, True)), (X2, 1, oo), (n, -oo, -1))"""
  243. n = Dummy('n')
  244. with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed
  245. assert P(X1 + X2 < 3, evaluate=False).rewrite(Sum).dummy_eq(Sum(Piecewise((2**(-n)/4,
  246. n + 2 >= 1), (0, True)), (n, -oo, -1))/3)
  247. #assert str(P(X1 + X2 > 3)) == """Sum(Piecewise((2**(X2 - n - 2)*(2/3)**(X2 - 1)/6, """ +\
  248. # """(-X2 + n + 3 >= 1) & (-X2 + n + 3 < oo)), (0, True)), (X2, 1, oo), (n, 1, oo))"""
  249. assert P(X1 + X2 > 3).dummy_eq(Sum(Piecewise((2**(X2 - n - 2)*(Rational(2, 3))**(X2 - 1)/6,
  250. -X2 + n + 3 >= 1), (0, True)),
  251. (X2, 1, oo), (n, 1, oo)))
  252. # assert str(P(Eq(X1 + X2, 3))) == """Sum(Piecewise((2**(X2 - 2)*(2/3)**(X2 - 1)/6, """ +\
  253. # """X2 <= 2), (0, True)), (X2, 1, oo))"""
  254. assert P(Eq(X1 + X2, 3)) == Rational(1, 12)