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

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Nov 14 10:28:40 EST 2008


Author: josef
Date: 2008-11-14 09:28:34 -0600 (Fri, 14 Nov 2008)
New Revision: 5108

Added:
   trunk/scipy/stats/tests/test_continuous_extra.py
Modified:
   trunk/scipy/stats/distributions.py
Log:
add test for bounds of ppf and isf of continuous rv, fix nan for out-of-bounds isf arguments

Modified: trunk/scipy/stats/distributions.py
===================================================================
--- trunk/scipy/stats/distributions.py	2008-11-14 06:24:24 UTC (rev 5107)
+++ trunk/scipy/stats/distributions.py	2008-11-14 15:28:34 UTC (rev 5108)
@@ -640,11 +640,13 @@
         cond2 = (q==1) & cond0
         cond = cond0 & cond1
         output = valarray(shape(cond),value=self.b)
-        place(output,(1-cond0)*(cond1==cond1), self.badvalue)
+        #place(output,(1-cond0)*(cond1==cond1), self.badvalue)
+        place(output,(1-cond0)*(cond1==cond1)+(1-cond1)*(q!=0.0), self.badvalue)
         place(output,cond2,self.a)
-        goodargs = argsreduce(cond, *((q,)+args+(scale,loc)))  #PB replace 1-q by q
-        scale, loc, goodargs = goodargs[-2], goodargs[-1], goodargs[:-2]
-        place(output,cond,self._isf(*goodargs)*scale + loc) #PB use _isf instead of _ppf
+        if any(cond):  #call only if at least 1 entry
+            goodargs = argsreduce(cond, *((q,)+args+(scale,loc)))  #PB replace 1-q by q
+            scale, loc, goodargs = goodargs[-2], goodargs[-1], goodargs[:-2]
+            place(output,cond,self._isf(*goodargs)*scale + loc) #PB use _isf instead of _ppf
         if output.ndim == 0:
             return output[()]
         return output
@@ -766,10 +768,10 @@
         if (n > 0) and (n < 5):
             signature = inspect.getargspec(self._stats.im_func)
             if (signature[2] is not None) or ('moments' in signature[0]):
-                dict = {'moments':{1:'m',2:'v',3:'vs',4:'vk'}[n]}
+                mdict = {'moments':{1:'m',2:'v',3:'vs',4:'vk'}[n]}
             else:
-                dict = {}
-            mu, mu2, g1, g2 = self._stats(*args,**dict)
+                mdict = {}
+            mu, mu2, g1, g2 = self._stats(*args,**mdict)
             if (n==1):
                 if mu is None: return self._munp(1,*args)
                 else: return mu

Added: trunk/scipy/stats/tests/test_continuous_extra.py
===================================================================
--- trunk/scipy/stats/tests/test_continuous_extra.py	2008-11-14 06:24:24 UTC (rev 5107)
+++ trunk/scipy/stats/tests/test_continuous_extra.py	2008-11-14 15:28:34 UTC (rev 5108)
@@ -0,0 +1,55 @@
+import numpy.testing as npt
+import numpy as np
+import nose
+
+from scipy import stats
+
+from test_continuous_basic import distcont
+
+DECIMAL = 5
+
+def test_cont_extra():
+    for distname, arg in distcont[:]:
+        distfn = getattr(stats, distname)
+##        rvs = distfn.rvs(size=1000,*arg)
+##        sm = rvs.mean()
+##        sv = rvs.var()
+##        skurt = stats.kurtosis(rvs)
+##        sskew = stats.skew(rvs)
+        yield check_ppf_limits, distfn, arg, distname + \
+              ' ppf limit test'
+
+def check_ppf_limits(distfn,arg,msg):
+    below,low,upp,above = distfn.ppf([-1,0,1,2], *arg)
+    #print distfn.name, distfn.a, low, distfn.b, upp
+    print distfn.name,below,low,upp,above
+    assert_equal_inf_nan(distfn.a,low, msg + 'ppf lower bound')
+    assert_equal_inf_nan(distfn.b,upp, msg + 'ppf upper bound')
+    assert np.isnan(below), msg + 'ppf out of bounds - below'
+    assert np.isnan(above), msg + 'ppf out of bounds - above'
+
+def check_ppf_limits(distfn,arg,msg):
+    below,low,upp,above = distfn.isf([-1,0,1,2], *arg)
+    #print distfn.name, distfn.a, low, distfn.b, upp
+    print distfn.name,below,low,upp,above
+    assert_equal_inf_nan(distfn.a,upp, msg + 'ppf lower bound')
+    assert_equal_inf_nan(distfn.b,low, msg + 'ppf upper bound')
+    assert np.isnan(below), msg + 'ppf out of bounds - below'
+    assert np.isnan(above), msg + 'ppf out of bounds - above'
+
+
+def assert_equal_inf_nan(v1,v2,msg):
+    if not np.isinf(v1):
+        npt.assert_almost_equal(v1, v2, decimal=DECIMAL, err_msg= msg + \
+                                   ' - finite')
+    else:
+        assert np.isinf(v2) or np.isnan(v2), \
+               msg + ' - infinite, v2=%s' % str(v2)
+
+if __name__ == "__main__":
+    import nose
+    #nose.run(argv=['', __file__])
+    print distcont[:5]
+    test_cont_extra()
+    nose.runmodule(argv=[__file__,'-s'], exit=False)
+




More information about the Scipy-svn mailing list