Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1103 from rlamy/stats

Stats
  • Loading branch information...
commit 859c3a48e6bcd40fd009e894d730c9b15ad9bc45 2 parents 14f12a4 + 9bb59e0
Ronan Lamy authored March 05, 2012
52  sympy/stats/crv.py
@@ -8,11 +8,11 @@
8 8
 sympy.stats.frv
9 9
 """
10 10
 
11  
-from rv import (RandomDomain, SingleDomain, ConditionalDomain, ProductDomain,
12  
-        PSpace, SinglePSpace, random_symbols, ProductPSpace)
  11
+from sympy.stats.rv import (RandomDomain, SingleDomain, ConditionalDomain,
  12
+        ProductDomain, PSpace, SinglePSpace, random_symbols, ProductPSpace)
13 13
 from sympy.functions.special.delta_functions import DiracDelta
14  
-from sympy import (S, Interval, Dummy, FiniteSet, Mul, Integral, And, Or,
15  
-        Piecewise, solve, cacheit, integrate, oo)
  14
+from sympy import (S, Interval, symbols, Dummy, FiniteSet, Mul, Tuple,
  15
+        Integral, And, Or, Piecewise, solve, cacheit, integrate, oo, Lambda)
16 16
 from sympy.solvers.inequalities import reduce_poly_inequalities
17 17
 from sympy.polys.polyerrors import PolynomialError
18 18
 import random
@@ -102,12 +102,12 @@ def integrate(self, expr, variables=None, **kwargs):
102 102
                     # Add the appropriate Delta to the integrand
103 103
                     integrand *= DiracDelta(cond.lhs-cond.rhs)
104 104
                 else:
105  
-                    symbols = FiniteSet(cond.free_symbols) & self.symbols
  105
+                    symbols = cond.free_symbols & set(self.symbols)
106 106
                     if len(symbols)!=1: # Can't handle x > y
107 107
                         raise NotImplementedError(
108 108
                             "Multivariate Inequalities not yet implemented")
109 109
                     # Can handle x > 0
110  
-                    symbol = tuple(symbols)[0]
  110
+                    symbol = symbols.pop()
111 111
                     # Find the limit with x, such as (x, -oo, oo)
112 112
                     for i, limit in enumerate(limits):
113 113
                         if limit[0]==symbol:
@@ -170,45 +170,47 @@ def compute_density(self, expr, **kwargs):
170 170
             # Marginalize all other random symbols out of the density
171 171
             density = self.domain.integrate(self.density, set(rs.symbol
172 172
                 for rs in self.values - frozenset((expr,))),  **kwargs)
173  
-            return expr.symbol, density
  173
+            return Lambda(expr.symbol, density)
174 174
 
175  
-        z = Dummy('z', real=True, finite=True)
176  
-        return z, self.integrate(DiracDelta(expr - z), **kwargs)
  175
+        z = Dummy('z', real=True, bounded=True)
  176
+        return Lambda(z, self.integrate(DiracDelta(expr - z), **kwargs))
177 177
 
178 178
     def compute_cdf(self, expr, **kwargs):
179 179
         if not self.domain.set.is_Interval:
180 180
             raise ValueError("CDF not well defined on multivariate expressions")
181 181
 
182  
-        x,d = self.compute_density(expr, **kwargs)
183  
-        z = Dummy('z', real=True, finite=True)
  182
+        d = self.compute_density(expr, **kwargs)
  183
+        x, z = symbols('x, z', real=True, bounded=True, cls=Dummy)
184 184
         left_bound = self.domain.set.start
185 185
 
186 186
         # CDF is integral of PDF from left bound to z
187  
-        cdf = integrate(d, (x, left_bound, z), **kwargs)
  187
+        cdf = integrate(d(x), (x, left_bound, z), **kwargs)
188 188
         # CDF Ensure that CDF left of left_bound is zero
189 189
         cdf = Piecewise((0, z<left_bound), (cdf, True))
190  
-        return z, cdf
  190
+        return Lambda(z, cdf)
191 191
 
192 192
     def P(self, condition, **kwargs):
193 193
         evaluate = kwargs.get("evaluate", True)
  194
+        z = Dummy('z', real=True, bounded=True)
194 195
         # Univariate case can be handled by where
195 196
         try:
196 197
             domain = self.where(condition)
197 198
             rv = [rv for rv in self.values if rv.symbol == domain.symbol][0]
198 199
             # Integrate out all other random variables
199  
-            z, pdf = self.compute_density(rv, **kwargs)
200  
-            # Integrate out this last variable over the special domain
  200
+            pdf = self.compute_density(rv, **kwargs)
  201
+            # Integrate out the last variable over the special domain
201 202
             if evaluate:
202  
-                return integrate(pdf, (z, domain.set), **kwargs)
  203
+                return integrate(pdf(z), (z, domain.set), **kwargs)
203 204
             else:
204  
-                return Integral(pdf, (z, domain.set), **kwargs)
  205
+                return Integral(pdf(z), (z, domain.set), **kwargs)
  206
+
205 207
         # Other cases can be turned into univariate case
206 208
         # by computing a density handled by density computation
207 209
         except NotImplementedError:
208 210
             expr = condition.lhs - condition.rhs
209  
-            val, density = self.compute_density(expr, **kwargs)
  211
+            density = self.compute_density(expr, **kwargs)
210 212
             # Turn problem into univariate case
211  
-            space = SingleContinuousPSpace(val, density)
  213
+            space = SingleContinuousPSpace(z, density(z))
212 214
             return space.P(condition.__class__(space.value, 0))
213 215
 
214 216
 
@@ -259,17 +261,17 @@ def _inverse_cdf_expression(self):
259 261
         compute_cdf
260 262
         sample
261 263
         """
262  
-        x,d = self.compute_cdf(self.value)
263  
-        z = Dummy('z', real=True, positive=True)
  264
+        d = self.compute_cdf(self.value)
  265
+        x, z = symbols('x, z', real=True, positive=True, cls=Dummy)
264 266
         # Invert CDF
265 267
         try:
266  
-            inverse_cdf = solve(d-z, x)
  268
+            inverse_cdf = solve(d(x)-z, x)
267 269
         except NotImplementedError:
268 270
             raise NotImplementedError("Could not invert CDF")
269 271
         if len(inverse_cdf) != 1:
270 272
             raise NotImplementedError("Could not invert CDF")
271 273
 
272  
-        return z, inverse_cdf[0]
  274
+        return Lambda(z, inverse_cdf[0])
273 275
 
274 276
     def sample(self):
275 277
         """
@@ -277,8 +279,8 @@ def sample(self):
277 279
 
278 280
         Returns dictionary mapping RandomSymbol to realization value.
279 281
         """
280  
-        z, icdf = self._inverse_cdf_expression()
281  
-        return {self.value: icdf.subs(z, random.uniform(0,1))}
  282
+        icdf = self._inverse_cdf_expression()
  283
+        return {self.value: icdf(random.uniform(0,1))}
282 284
 
283 285
 class ProductContinuousPSpace(ProductPSpace, ContinuousPSpace):
284 286
     """
16  sympy/stats/crv_types.py
@@ -113,7 +113,7 @@ def Beta(alpha, beta, symbol=None):
113 113
 
114 114
     >>> X = Beta(a, b, symbol=x)
115 115
     >>> Density(X)
116  
-    (x, x**(a - 1)*(-x + 1)**(b - 1)*gamma(a + b)/(gamma(a)*gamma(b)))
  116
+    Lambda(_x, _x**(a - 1)*(-_x + 1)**(b - 1)*gamma(a + b)/(gamma(a)*gamma(b)))
117 117
     """
118 118
 
119 119
     return BetaPSpace(alpha, beta, symbol).value
@@ -151,7 +151,7 @@ def Exponential(rate, symbol=None):
151 151
 
152 152
     >>> X = Exponential(rate=10, symbol=Symbol('x')) # Decay rate equals 10
153 153
     >>> Density(X)
154  
-    (x, 10*exp(-10*x))
  154
+    Lambda(_x, 10*exp(-10*_x))
155 155
 
156 156
     >>> E(X)
157 157
     1/10
@@ -198,7 +198,7 @@ def Gamma(k, theta, symbol=None):
198 198
 
199 199
     >>> X = Gamma(k, theta, symbol=x)
200 200
     >>> Density(X)
201  
-    (x, theta**(-k)*x**(k - 1)*exp(-x/theta)/gamma(k))
  201
+    Lambda(_x, _x**(k - 1)*theta**(-k)*exp(-_x/theta)/gamma(k))
202 202
 
203 203
     >>> E(X)
204 204
     theta*gamma(k + 1)/gamma(k)
@@ -240,7 +240,7 @@ def LogNormal(mean, std, symbol=None):
240 240
 
241 241
     >>> X = LogNormal(0, 1, symbol=Symbol('x')) # Mean 0, standard deviation 1
242 242
     >>> Density(X)
243  
-    (x, sqrt(2)*exp(-log(x)**2/2)/(2*sqrt(pi)*x))
  243
+    Lambda(_x, sqrt(2)*exp(-log(_x)**2/2)/(2*_x*sqrt(pi)))
244 244
     """
245 245
 
246 246
     return LogNormalPSpace(mean, std, symbol).value
@@ -280,7 +280,7 @@ def Normal(mean, std, symbol=None):
280 280
 
281 281
     >>> X = Normal(0, 1, symbol=Symbol('x')) # Mean 0, standard deviation 1
282 282
     >>> Density(X)
283  
-    (x, sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)))
  283
+    Lambda(_x, sqrt(2)*exp(-_x**2/2)/(2*sqrt(pi)))
284 284
 
285 285
     >>> E(2*X + 1)
286 286
     1
@@ -327,7 +327,7 @@ def Pareto(xm, alpha, symbol=None):
327 327
     >>> x, xm, beta = symbols('x xm beta', positive=True)
328 328
     >>> X = Pareto(xm, beta, symbol=x)
329 329
     >>> Density(X)
330  
-    (x, beta*x**(-beta - 1)*xm**beta)
  330
+    Lambda(_x, _x**(-beta - 1)*beta*xm**beta)
331 331
     """
332 332
 
333 333
     return ParetoPSpace(xm, alpha, symbol).value
@@ -369,7 +369,7 @@ def Uniform(left, right, symbol=None):
369 369
     >>> X = Uniform(l, r, symbol=x)
370 370
 
371 371
     >>> Density(X)
372  
-    (x, Piecewise((0, x < l), (0, x > r), (1/(-l + r), True)))
  372
+    Lambda(_x, Piecewise((0, _x < l), (0, _x > r), (1/(-l + r), True)))
373 373
 
374 374
     >>> simplify(E(X))
375 375
     l/2 + r/2
@@ -416,7 +416,7 @@ def Weibull(alpha, beta, symbol=None):
416 416
 
417 417
     >>> X = Weibull(a, b, symbol=x)
418 418
     >>> Density(X)
419  
-    (x, b*(x/a)**(b - 1)*exp(-(x/a)**b)/a)
  419
+    Lambda(_x, b*(_x/a)**(b - 1)*exp(-(_x/a)**b)/a)
420 420
     >>> simplify(E(X))
421 421
     a*gamma(1 + 1/b)
422 422
     >>> simplify(Var(X))
15  sympy/stats/frv.py
@@ -244,13 +244,14 @@ class SingleFinitePSpace(FinitePSpace, SinglePSpace):
244 244
     _count = 0
245 245
     _name = 'fx'
246 246
 
247  
-def create_SingleFinitePSpace(density, symbol=None, cls = SingleFinitePSpace):
248  
-    symbol = symbol or cls.create_symbol()
249  
-    domain = SingleFiniteDomain(symbol, frozenset(density.keys()))
250  
-    density = dict((frozenset(((symbol, val),)) , prob)
251  
-            for val, prob in density.items())
252  
-    density = Dict(density)
253  
-    return FinitePSpace.__new__(cls, domain, density)
  247
+    @classmethod
  248
+    def fromdict(cls, density, symbol=None):
  249
+        symbol = symbol or cls.create_symbol()
  250
+        domain = SingleFiniteDomain(symbol, frozenset(density.keys()))
  251
+        density = dict((frozenset(((symbol, val),)) , prob)
  252
+                for val, prob in density.items())
  253
+        density = Dict(density)
  254
+        return FinitePSpace.__new__(cls, domain, density)
254 255
 
255 256
 class ProductFinitePSpace(ProductPSpace, FinitePSpace):
256 257
     """
12  sympy/stats/frv_types.py
@@ -12,7 +12,7 @@
12 12
 Hypergeometric
13 13
 """
14 14
 
15  
-from sympy.stats.frv import SingleFinitePSpace, create_SingleFinitePSpace
  15
+from sympy.stats.frv import SingleFinitePSpace
16 16
 from sympy import S, sympify, Rational, binomial
17 17
 
18 18
 __all__ = ['FiniteRV', 'DiscreteUniform', 'Die', 'Bernoulli', 'Coin',
@@ -34,7 +34,7 @@ def FiniteRV(density, symbol=None):
34 34
     >>> P(X>=2)
35 35
     0.700000000000000
36 36
     """
37  
-    return create_SingleFinitePSpace(density, symbol).value
  37
+    return SingleFinitePSpace.fromdict(density, symbol).value
38 38
 
39 39
 class DiscreteUniformPSpace(SingleFinitePSpace):
40 40
     """
@@ -62,7 +62,7 @@ class DiscreteUniformPSpace(SingleFinitePSpace):
62 62
     def __new__(cls, items, symbol=None):
63 63
         density = dict((sympify(item), Rational(1, len(items)))
64 64
                        for item in items)
65  
-        return create_SingleFinitePSpace(density, symbol, cls)
  65
+        return cls.fromdict(density, symbol)
66 66
 
67 67
 def DiscreteUniform(items, symbol=None):
68 68
     """
@@ -156,7 +156,7 @@ class BernoulliPSpace(SingleFinitePSpace):
156 156
     def __new__(cls, p, succ=1, fail=0, symbol=None):
157 157
         succ, fail, p = map(sympify, (succ, fail, p))
158 158
         density = {succ:p, fail:(1-p)}
159  
-        return create_SingleFinitePSpace(density, symbol, cls)
  159
+        return cls.fromdict(density, symbol)
160 160
 
161 161
 def Bernoulli(p, succ=1, fail=0, symbol=None):
162 162
     """
@@ -248,7 +248,7 @@ def __new__(cls, n, p, succ=1, fail=0, symbol=None):
248 248
         n, p, succ, fail = map(sympify, (n, p, succ, fail))
249 249
         density = dict((k*succ + (n-k)*fail,
250 250
                 binomial(n, k) * p**k * (1-p)**(n-k)) for k in range(0, n+1))
251  
-        return create_SingleFinitePSpace(density, symbol, cls)
  251
+        return cls.fromdict(density, symbol)
252 252
 
253 253
 def Binomial(n, p, succ=1, fail=0, symbol=None):
254 254
     """
@@ -292,7 +292,7 @@ def __new__(cls, N, m, n, symbol=None):
292 292
         N, m, n = map(sympify, (N, m, n))
293 293
         density = dict((k, binomial(m, k) * binomial(N-m, n-k) / binomial(N, n))
294 294
                 for k in range(max(0, n+m-N), min(m, n) + 1))
295  
-        return create_SingleFinitePSpace(density, symbol, cls)
  295
+        return cls.fromdict(density, symbol)
296 296
 
297 297
 def Hypergeometric(N, m, n, symbol=None):
298 298
     """
54  sympy/stats/rv.py
@@ -283,12 +283,9 @@ class ProductDomain(RandomDomain):
283 283
     sympy.stats.frv.ProductFiniteDomain
284 284
     """
285 285
     is_ProductDomain = True
286  
-    def __new__(cls, *domains):
287 286
 
288  
-        symbolslist = sumsets([domain.symbols for domain in domains])
289  
-        symbols = frozenset(symbolslist)
290  
-        if len(symbols) != len(symbolslist):
291  
-            raise ValueError("Overlapping Domains")
  287
+    def __new__(cls, *domains):
  288
+        symbols = sumsets([domain.symbols for domain in domains])
292 289
 
293 290
         # Flatten any product of products
294 291
         domains2 = []
@@ -338,16 +335,13 @@ def __contains__(self, other):
338 335
     def as_boolean(self):
339 336
         return And(*[domain.as_boolean() for domain in self.domains])
340 337
 
341  
-def is_random(x):
342  
-    return isinstance(x, RandomSymbol)
343  
-
344 338
 def random_symbols(expr):
345 339
     """
346 340
     Returns all RandomSymbols within a SymPy Expression.
347 341
     """
348 342
     try:
349  
-        return [s for s in expr.free_symbols if is_random(s)]
350  
-    except:
  343
+        return list(expr.atoms(RandomSymbol))
  344
+    except AttributeError:
351 345
         return []
352 346
 
353 347
 def pspace(expr):
@@ -523,11 +517,10 @@ def Density(expr, given=None, **kwargs):
523 517
 
524 518
     Optionally given a second condition
525 519
 
526  
-    This density will take on different forms for different types of probability
527  
-    spaces.
528  
-    Discrete RV's produce Dicts
529  
-    Continuous RV's produce a Tuple with expression representing the PDF and
530  
-    a symbol designating the active variable
  520
+    This density will take on different forms for different types of
  521
+    probability spaces.
  522
+    Discrete variables produce Dicts.
  523
+    Continuous variables produce Lambdas.
531 524
 
532 525
     Examples
533 526
     ========
@@ -543,7 +536,7 @@ def Density(expr, given=None, **kwargs):
543 536
     >>> Density(2*D)
544 537
     {2: 1/6, 4: 1/6, 6: 1/6, 8: 1/6, 10: 1/6, 12: 1/6}
545 538
     >>> Density(X)
546  
-    (x, sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)))
  539
+    Lambda(_x, sqrt(2)*exp(-_x**2/2)/(2*sqrt(pi)))
547 540
     """
548 541
     if given is not None: # If there is a condition
549 542
         # Recompute on new conditional expr
@@ -558,11 +551,10 @@ def CDF(expr, given=None, **kwargs):
558 551
 
559 552
     optionally given a second condition
560 553
 
561  
-    This density will take on different forms for different types of probability
562  
-    spaces.
563  
-    Discrete RV's produce list of tuples.
564  
-    Continuous RV's produce a Tuple with expression representing the PDF and
565  
-    a symbol designating the active variable.
  554
+    This density will take on different forms for different types of
  555
+    probability spaces.
  556
+    Discrete variables produce Dicts.
  557
+    Continuous variables produce Lambdas.
566 558
 
567 559
     Examples
568 560
     ========
@@ -581,7 +573,7 @@ def CDF(expr, given=None, **kwargs):
581 573
     {9: 1/4, 12: 1/2, 15: 3/4, 18: 1}
582 574
 
583 575
     >>> CDF(X)
584  
-    (_z, erf(sqrt(2)*_z/2)/2 + 1/2)
  576
+    Lambda(_z, erf(sqrt(2)*_z/2)/2 + 1/2)
585 577
     """
586 578
     if given is not None: # If there is a condition
587 579
         # Recompute on new conditional expr
@@ -660,9 +652,11 @@ def sample_iter(expr, given=None, numsamples=S.Infinity, **kwargs):
660 652
     sample_iter_subs
661 653
     """
662 654
     # lambdify is much faster but not as robust
663  
-    try:          return sample_iter_lambdify(expr, given, numsamples, **kwargs)
  655
+    try:
  656
+        return sample_iter_lambdify(expr, given, numsamples, **kwargs)
664 657
     # use subs when lambdify fails
665  
-    except TypeError: return sample_iter_subs(expr, given, numsamples, **kwargs)
  658
+    except TypeError:
  659
+        return sample_iter_subs(expr, given, numsamples, **kwargs)
666 660
 
667 661
 def sample_iter_lambdify(expr, given=None, numsamples=S.Infinity, **kwargs):
668 662
     """
@@ -760,8 +754,10 @@ def sampling_P(condition, given=None, numsamples=1, evalf=True, **kwargs):
760 754
             count_false += 1
761 755
 
762 756
     result = S(count_true) / numsamples
763  
-    if evalf:   return result.evalf()
764  
-    else:       return result
  757
+    if evalf:
  758
+        return result.evalf()
  759
+    else:
  760
+        return result
765 761
 
766 762
 def sampling_E(condition, given=None, numsamples=1, evalf=True, **kwargs):
767 763
     """
@@ -776,8 +772,10 @@ def sampling_E(condition, given=None, numsamples=1, evalf=True, **kwargs):
776 772
     samples = sample_iter(condition, given, numsamples=numsamples, **kwargs)
777 773
 
778 774
     result = Add(*list(samples)) / numsamples
779  
-    if evalf:   return result.evalf()
780  
-    else:       return result
  775
+    if evalf:
  776
+        return result.evalf()
  777
+    else:
  778
+        return result
781 779
 
782 780
 def dependent(a, b):
783 781
     """
3  sympy/stats/rv_interface.py
... ...
@@ -1,4 +1,5 @@
1  
-from rv import (P, E, Density, Where, Given, pspace, CDF, Sample, sample_iter, random_symbols, independent, dependent)
  1
+from rv import (P, E, Density, Where, Given, pspace, CDF, Sample, sample_iter,
  2
+        random_symbols, independent, dependent)
2 3
 from sympy import sqrt
3 4
 
4 5
 __all__ = ['P', 'E', 'Density', 'Where', 'Given', 'Sample', 'CDF', 'pspace',
54  sympy/stats/tests/test_continuous_rv.py
@@ -2,7 +2,7 @@
2 2
         Var, Covar, Skewness, Gamma, Pareto, Weibull, Beta, Uniform, Given, pspace, CDF, ContinuousRV, Sample)
3 3
 from sympy import (Symbol, exp, S, N, pi, simplify, Interval, erf, Eq, symbols,
4 4
         sqrt, And, gamma, beta, Piecewise, Integral, sin)
5  
-from sympy.utilities.pytest import raises
  5
+from sympy.utilities.pytest import raises, XFAIL
6 6
 
7 7
 oo = S.Infinity
8 8
 
@@ -14,18 +14,20 @@ def test_single_normal():
14 14
 
15 15
     assert simplify(E(Y)) == mu
16 16
     assert simplify(Var(Y)) == sigma**2
17  
-    x, pdf = Density(Y)
18  
-    assert pdf == 2**S.Half*exp(-(x - mu)**2/(2*sigma**2))/(2*pi**S.Half*sigma)
  17
+    pdf = Density(Y)
  18
+    x = Symbol('x')
  19
+    assert pdf(x) == 2**S.Half*exp(-(x - mu)**2/(2*sigma**2))/(2*pi**S.Half*sigma)
19 20
 
20  
-    assert P(X**2<1) == erf(2**S.Half/2)
  21
+    assert P(X**2 < 1) == erf(2**S.Half/2)
21 22
 
22 23
     assert E(X, Eq(X, mu)) == mu
23 24
 
  25
+@XFAIL
24 26
 def test_conditional_1d():
25 27
     X = Normal(0,1)
26 28
     Y = Given(X, X>=0)
27 29
 
28  
-    assert Density(Y)[1] == 2 * Density(X)[1]
  30
+    assert Density(Y) == 2 * Density(X)
29 31
 
30 32
     assert Y.pspace.domain.set == Interval(0, oo)
31 33
     assert E(Y) == sqrt(2) / sqrt(pi)
@@ -76,26 +78,25 @@ def test_symbolic():
76 78
 
77 79
 def test_CDF():
78 80
     X = Normal(0,1)
79  
-    x,d = CDF(X)
80  
-
81  
-    assert P(X<1) == d.subs(x,1)
82  
-    assert d.subs(x,0) == S.Half
83 81
 
84  
-    x,d = CDF(X, X>0) # given X>0
  82
+    d = CDF(X)
  83
+    assert P(X<1) == d(1)
  84
+    assert d(0) == S.Half
85 85
 
86  
-    assert d.subs(x,0) == 0
  86
+    d = CDF(X, X>0) # given X>0
  87
+    assert d(0) == 0
87 88
 
88 89
     Y = Exponential(10)
89  
-    y,d = CDF(Y)
90  
-
91  
-    assert d.subs(y, -5) == 0
92  
-    assert P(Y>3) == 1 - d.subs(y, 3)
  90
+    d = CDF(Y)
  91
+    assert d(-5) == 0
  92
+    assert P(Y > 3) == 1 - d(3)
93 93
 
94 94
     raises(ValueError, "CDF(X+Y)")
95 95
 
96 96
     Z = Exponential(1)
97  
-    z, cdf = CDF(Z)
98  
-    assert cdf == Piecewise((0, z < 0), (1 - exp(-z), True))
  97
+    cdf = CDF(Z)
  98
+    z = Symbol('z')
  99
+    assert cdf(z) == Piecewise((0, z < 0), (1 - exp(-z), True))
99 100
 
100 101
 def test_sample():
101 102
     z = Symbol('z')
@@ -157,8 +158,9 @@ def test_pareto():
157 158
     alpha = beta + 5
158 159
     X = Pareto(xm, alpha)
159 160
 
160  
-    x, density = Density(X)
161  
-    assert density == x**(-(alpha+1))*xm**(alpha)*(alpha)
  161
+    density = Density(X)
  162
+    x = Symbol('x')
  163
+    assert density(x) == x**(-(alpha+1))*xm**(alpha)*(alpha)
162 164
 
163 165
     # These fail because SymPy can not deduce that 1/xm != 0
164 166
     # assert simplify(E(X)) == alpha*xm/(alpha-1)
@@ -199,8 +201,9 @@ def test_beta():
199 201
 
200 202
     assert pspace(B).domain.set == Interval(0, 1)
201 203
 
202  
-    x, dens = Density(B)
203  
-    assert dens == x**(a-1)*(1-x)**(b-1) / beta(a,b)
  204
+    dens = Density(B)
  205
+    x = Symbol('x')
  206
+    assert dens(x) == x**(a-1)*(1-x)**(b-1) / beta(a,b)
204 207
 
205 208
     # This is too slow
206 209
     # assert E(B) == a / (a + b)
@@ -257,6 +260,7 @@ def test_input_value_assertions():
257 260
         raises(ValueError, "%s(p, a)" % fn_name)
258 261
         eval("%s(p, q)" % fn_name) # No error raised
259 262
 
  263
+@XFAIL
260 264
 def test_unevaluated():
261 265
     x = Symbol('x')
262 266
     X = Normal(0,1, symbol=x)
@@ -266,10 +270,10 @@ def test_unevaluated():
266 270
     assert E(X+1, evaluate=False) == \
267 271
             Integral(sqrt(2)*x*exp(-x**2/2)/(2*sqrt(pi)), (x, -oo, oo)) + 1
268 272
 
269  
-    assert P(X>0, evaluate=False).args == \
270  
-            Integral(sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)), (x, 0, oo)).args
  273
+    assert P(X>0, evaluate=False) == \
  274
+            Integral(sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)), (x, 0, oo))
271 275
 
272  
-    assert P(X>0, X**2<1, evaluate=False).args == \
  276
+    assert P(X>0, X**2<1, evaluate=False) == \
273 277
             Integral(sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)*
274 278
             Integral(sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)),
275  
-                (x, -1, 1))), (x, 0, 1)).args
  279
+                (x, -1, 1))), (x, 0, 1))
6  sympy/stats/tests/test_rv.py
@@ -132,7 +132,7 @@ def test_dependent_finite():
132 132
 
133 133
 def test_normality():
134 134
     X, Y = Normal(0,1), Normal(0,1)
135  
-    z = Symbol('z', real=True)
136  
-    x, density = Density(X-Y, Eq(X+Y, z))
  135
+    x, z = symbols('x, z', real=True)
  136
+    density = Density(X-Y, Eq(X+Y, z))
137 137
 
138  
-    assert integrate(density, (x, -oo, oo)) == 1
  138
+    assert integrate(density(x), (x, -oo, oo)) == 1

0 notes on commit 859c3a4

Please sign in to comment.
Something went wrong with that request. Please try again.