[Scipy-svn] r7029 - in trunk/scipy/stats: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Fri Jan 14 15:55:29 EST 2011
Author: josef
Date: 2011-01-14 14:55:28 -0600 (Fri, 14 Jan 2011)
New Revision: 7029
Modified:
trunk/scipy/stats/distributions.py
trunk/scipy/stats/tests/test_distributions.py
Log:
stats.distributions expect: fix bound handling and add tests for discrete distributions
Modified: trunk/scipy/stats/distributions.py
===================================================================
--- trunk/scipy/stats/distributions.py 2011-01-14 19:21:00 UTC (rev 7028)
+++ trunk/scipy/stats/distributions.py 2011-01-14 20:55:28 UTC (rev 7029)
@@ -5582,10 +5582,18 @@
self._argcheck(*args) # (re)generate scalar self.a and self.b
if lb is None:
lb = (self.a)
+ else:
+ lb = lb - loc #convert bound for standardized distribution
if ub is None:
ub = (self.b)
+ else:
+ ub = ub - loc #convert bound for standardized distribution
if conditional:
- invfac = self.sf(lb,*args) - self.sf(ub+1,*args)
+ if np.isposinf(ub)[()]:
+ #work around bug: stats.poisson.sf(stats.poisson.b, 2) is nan
+ invfac = 1 - self.cdf(lb-1,*args)
+ else:
+ invfac = 1 - self.cdf(lb-1,*args) - self.sf(ub,*args)
else:
invfac = 1.0
Modified: trunk/scipy/stats/tests/test_distributions.py
===================================================================
--- trunk/scipy/stats/tests/test_distributions.py 2011-01-14 19:21:00 UTC (rev 7028)
+++ trunk/scipy/stats/tests/test_distributions.py 2011-01-14 20:55:28 UTC (rev 7029)
@@ -560,9 +560,11 @@
assert_equal(m1, m2)
class TestExpect(TestCase):
- """Test for expect method, continuous distributions only.
+ """Test for expect method.
- Uses normal distribution and beta distribution for finite bounds.
+ Uses normal distribution and beta distribution for finite bounds, and
+ hypergeom for discrete distribution with finite support
+
"""
def test_norm(self):
v = stats.norm.expect(lambda x: (x-5)*(x-5), loc=5, scale=2)
@@ -603,7 +605,55 @@
assert_almost_equal(prob90c, 1., decimal=14)
+ def test_hypergeom(self):
+ #test case with finite bounds
+ #without specifying bounds
+ m_true, v_true = stats.hypergeom.stats(20, 10, 8, loc=5.)
+ m = stats.hypergeom.expect(lambda x: x, args=(20, 10, 8), loc=5.)
+ assert_almost_equal(m, m_true, decimal=14)
+
+ v = stats.hypergeom.expect(lambda x: (x-9.)**2, args=(20, 10, 8),
+ loc=5.)
+ assert_almost_equal(v, v_true, decimal=14)
+
+ #with bounds, bounds equal to shifted support
+ v_bounds = stats.hypergeom.expect(lambda x: (x-9.)**2, args=(20, 10, 8),
+ loc=5., lb=5, ub=13)
+ assert_almost_equal(v_bounds, v_true, decimal=14)
+
+ #drop boundary points
+ prob_true = 1-stats.hypergeom.pmf([5, 13], 20, 10, 8, loc=5).sum()
+ prob_bounds = stats.hypergeom.expect(lambda x: 1, args=(20, 10, 8),
+ loc=5., lb=6, ub=12)
+ assert_almost_equal(prob_bounds, prob_true, decimal=14)
+
+ #conditional
+ prob_bc = stats.hypergeom.expect(lambda x: 1, args=(20, 10, 8), loc=5.,
+ lb=6, ub=12, conditional=True)
+ assert_almost_equal(prob_bc, 1, decimal=14)
+
+ #check simple integral
+ prob_b = stats.hypergeom.expect(lambda x: 1, args=(20, 10, 8),
+ lb=0, ub=8)
+ assert_almost_equal(prob_b, 1, decimal=14)
+
+ def test_poisson(self):
+ #poisson, use lower bound only
+ prob_bounds = stats.poisson.expect(lambda x: 1, args=(2,), lb=3,
+ conditional=False)
+ prob_b_true = 1-stats.poisson.cdf(2,2)
+ assert_almost_equal(prob_bounds, prob_b_true, decimal=14)
+
+
+ prob_lb = stats.poisson.expect(lambda x: 1, args=(2,), lb=2,
+ conditional=True)
+ assert_almost_equal(prob_lb, 1, decimal=14)
+
+
+
+
+
def test_regression_ticket_1316():
"""Regression test for ticket #1316."""
# The following was raising an exception, because _construct_default_doc()
More information about the Scipy-svn
mailing list