[Scipy-svn] r7028 - in trunk/scipy/stats: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Jan 14 14:21:01 EST 2011


Author: josef
Date: 2011-01-14 13:21:00 -0600 (Fri, 14 Jan 2011)
New Revision: 7028

Modified:
   trunk/scipy/stats/distributions.py
   trunk/scipy/stats/tests/test_distributions.py
Log:
stats.distributions expect:  fix bound handling and add tests, continuous distribution only

Modified: trunk/scipy/stats/distributions.py
===================================================================
--- trunk/scipy/stats/distributions.py	2011-01-11 12:49:17 UTC (rev 7027)
+++ trunk/scipy/stats/distributions.py	2011-01-14 19:21:00 UTC (rev 7028)
@@ -1824,11 +1824,12 @@
             def fun(x, *args):
                 return func(x)*self.pdf(x, *args, **{'loc':loc, 'scale':scale})
         if lb is None:
-            lb = (self.a - loc)/(1.0*scale)
+            lb = loc + self.a * scale
         if ub is None:
-            ub = (self.b - loc)/(1.0*scale)
+            ub = loc + self.b * scale
         if conditional:
-            invfac = self.sf(lb,*args) - self.sf(ub,*args)
+            invfac = (self.sf(lb, *args, **{'loc':loc, 'scale':scale})
+                      - self.sf(ub, *args, **{'loc':loc, 'scale':scale}))
         else:
             invfac = 1.0
         kwds['args'] = args

Modified: trunk/scipy/stats/tests/test_distributions.py
===================================================================
--- trunk/scipy/stats/tests/test_distributions.py	2011-01-11 12:49:17 UTC (rev 7027)
+++ trunk/scipy/stats/tests/test_distributions.py	2011-01-14 19:21:00 UTC (rev 7028)
@@ -559,7 +559,51 @@
         # the focus of this test.
         assert_equal(m1, m2)
 
+class TestExpect(TestCase):
+    """Test for expect method, continuous distributions only.
 
+    Uses normal distribution and beta distribution for finite bounds.
+    """
+    def test_norm(self):
+        v = stats.norm.expect(lambda x: (x-5)*(x-5), loc=5, scale=2)
+        assert_almost_equal(v, 4, decimal=14)
+
+        m = stats.norm.expect(lambda x: (x), loc=5, scale=2)
+        assert_almost_equal(m, 5, decimal=14)
+
+        lb = stats.norm.ppf(0.05, loc=5, scale=2)
+        ub = stats.norm.ppf(0.95, loc=5, scale=2)
+        prob90 = stats.norm.expect(lambda x: 1, loc=5, scale=2, lb=lb, ub=ub)
+        assert_almost_equal(prob90, 0.9, decimal=14)
+        
+        prob90c = stats.norm.expect(lambda x: 1, loc=5, scale=2, lb=lb, ub=ub,
+                                    conditional=True)
+        assert_almost_equal(prob90c, 1., decimal=14)
+
+    def test_beta(self):
+        #case with finite support interval
+##        >>> mtrue, vtrue = stats.beta.stats(10,5, loc=5., scale=2.)
+##        >>> mtrue, vtrue
+##        (array(6.333333333333333), array(0.055555555555555552))
+        v = stats.beta.expect(lambda x: (x-19/3.)*(x-19/3.), args=(10,5),
+                              loc=5, scale=2)
+        assert_almost_equal(v, 1./18., decimal=14)
+
+        m = stats.beta.expect(lambda x: x, args=(10,5), loc=5., scale=2.)
+        assert_almost_equal(m, 19/3., decimal=14)
+
+        ub = stats.beta.ppf(0.95, 10, 10, loc=5, scale=2)
+        lb = stats.beta.ppf(0.05, 10, 10, loc=5, scale=2)
+        prob90 = stats.beta.expect(lambda x: 1., args=(10,10), loc=5.,
+                                   scale=2.,lb=lb, ub=ub, conditional=False)
+        assert_almost_equal(prob90, 0.9, decimal=14)
+        
+        prob90c = stats.beta.expect(lambda x: 1, args=(10,10), loc=5,
+                                    scale=2, lb=lb, ub=ub, conditional=True)
+        assert_almost_equal(prob90c, 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