[Scipy-svn] r5144 - trunk/scipy/stats/tests

scipy-svn at scipy.org scipy-svn at scipy.org
Tue Nov 18 01:23:09 EST 2008


Author: josef
Date: 2008-11-18 00:23:05 -0600 (Tue, 18 Nov 2008)
New Revision: 5144

Modified:
   trunk/scipy/stats/tests/test_continuous_basic.py
   trunk/scipy/stats/tests/test_continuous_extra.py
   trunk/scipy/stats/tests/test_discrete_basic.py
Log:
some test cleanup, add some test, make sure, some extra tests are renamed, so that nose doesn't run them

Modified: trunk/scipy/stats/tests/test_continuous_basic.py
===================================================================
--- trunk/scipy/stats/tests/test_continuous_basic.py	2008-11-18 06:19:37 UTC (rev 5143)
+++ trunk/scipy/stats/tests/test_continuous_basic.py	2008-11-18 06:23:05 UTC (rev 5144)
@@ -113,6 +113,12 @@
 ##    ['fatiguelife', (29,)],   #correction numargs = 1
 ##    ['loggamma', (0.41411931826052117,)]]
 
+# for testing ticket:767
+##distcont = [
+##    ['genextreme', (3.3184017469423535,)],
+##    ['genextreme', (0.01,)],
+##    ['genextreme', (0.00001,)]
+##    ]
 
 def test_cont_basic():
     for distname, arg in distcont[:]:
@@ -122,10 +128,11 @@
         sv = rvs.var()
         skurt = stats.kurtosis(rvs)
         sskew = stats.skew(rvs)
-        yield check_sample_meanvar_, distfn, arg, sm, sv, distname + \
+        m,v = distfn.stats(*arg)
+        yield check_sample_meanvar_, distfn, arg, m, v, sm, sv, distname + \
               'sample mean test'
         yield check_sample_skew_kurt, distfn, arg, skurt, sskew, distname
-        yield check_moment, distfn, arg, distname
+        yield check_moment, distfn, arg, m, v, distname
         yield check_cdf_ppf, distfn, arg, distname
         yield check_sf_isf, distfn, arg, distname
         yield check_pdf, distfn, arg, distname
@@ -133,27 +140,25 @@
 
 
 
-def check_moment(distfn, arg, msg):
-    m,v = distfn.stats(*arg)
+def check_moment(distfn, arg, m, v, msg):
     m1  = distfn.moment(1,*arg)
     m2  = distfn.moment(2,*arg)
     if not np.isinf(m):
         npt.assert_almost_equal(m1, m, decimal=10, err_msg= msg + \
                             ' - 1st moment')
-    else:
-        assert np.isinf(m1) or np.isnan(m1), \
+    else:                     # or np.isnan(m1), 
+        assert np.isinf(m1), \
                msg + ' - 1st moment -infinite, m1=%s' % str(m1)
         #np.isnan(m1) temporary special treatment for loggamma
     if not np.isinf(v):
         npt.assert_almost_equal(m2-m1*m1, v, decimal=10, err_msg= msg + \
                             ' - 2ndt moment')
-    else:
-        assert np.isinf(m2) or np.isnan(m2), \
+    else:                     #or np.isnan(m2), 
+        assert np.isinf(m2), \
                msg + ' - 2nd moment -infinite, m2=%s' % str(m2)
         #np.isnan(m2) temporary special treatment for loggamma
 
-def check_sample_meanvar_(distfn, arg, sm, sv, msg):
-    m,v = distfn.stats(*arg)
+def check_sample_meanvar_(distfn, arg, m, v, sm, sv, msg):
     check_sample_meanvar, sm, m, msg + 'sample mean test'
     check_sample_meanvar, sv, v, msg + 'sample var test'
 
@@ -167,7 +172,7 @@
         npt.assert_almost_equal(sm, m, decimal=DECIMAL, err_msg= msg + \
                                 ' - finite moment')
     else:
-        assert sm > 10000, 'infinite moment, sm = ' + str(sm)
+        assert abs(sm) > 10000, 'infinite moment, sm = ' + str(sm)
 
 def check_cdf_ppf(distfn,arg,msg):
     npt.assert_almost_equal(distfn.cdf(distfn.ppf([0.001,0.5,0.990], *arg), *arg),
@@ -184,6 +189,7 @@
                             ' - cdf-sf relationship')
 
 def check_pdf(distfn, arg, msg):
+    # compares pdf at median with numerical derivative of cdf
     median = distfn.ppf(0.5, *arg)
     eps = 1e-6
     pdfv = distfn.pdf(median, *arg)
@@ -193,7 +199,8 @@
         pdfv = distfn.pdf(median, *arg)
     cdfdiff = (distfn.cdf(median + eps, *arg) -
                distfn.cdf(median - eps, *arg))/eps/2.0
-    #replace with better diff and better test (more points)
+    #replace with better diff and better test (more points),
+    #actually, this works pretty well
     npt.assert_almost_equal(pdfv, cdfdiff,
                 decimal=DECIMAL, err_msg= msg + ' - cdf-pdf relationship')
 
@@ -204,9 +211,11 @@
     'johnsonsb', 'truncexpon', 'rice', 'invnorm', 'invgamma',
     'powerlognorm']
 
+distmiss = [[dist,args] for dist,args in distcont if dist in distmissing]
+
 def test_missing_distributions():
-    #test from scipy.stats.tests
-    for dist, args in distcont:
+    # K-S test of distributions missing in test_distributions.py
+    for dist, args in distmiss:
         distfunc = getattr(stats, dist)
         alpha = 0.01
         yield check_distribution, dist, args, alpha
@@ -221,5 +230,6 @@
                "; alpha = " + str(alpha) + "\nargs = " + str(args)
 
 if __name__ == "__main__":
-    nose.run(argv=['', __file__])
+    #nose.run(argv=['', __file__])
+    nose.runmodule(argv=[__file__,'-s'], exit=False)
 

Modified: trunk/scipy/stats/tests/test_continuous_extra.py
===================================================================
--- trunk/scipy/stats/tests/test_continuous_extra.py	2008-11-18 06:19:37 UTC (rev 5143)
+++ trunk/scipy/stats/tests/test_continuous_extra.py	2008-11-18 06:23:05 UTC (rev 5144)
@@ -1,3 +1,11 @@
+# contains additional tests for continuous distributions
+#
+# NOTE: one test, _est_cont_skip, that is renamed so that nose doesn't
+#       run it,
+#       6 distributions return nan for entropy
+#       truncnorm fails by design for private method _ppf test
+
+
 import numpy.testing as npt
 import numpy as np
 import nose
@@ -11,23 +19,35 @@
 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'
         yield check_isf_limits, distfn, arg, distname + \
               ' isf limit test'
         yield check_loc_scale, distfn, arg, distname + \
               ' loc, scale test'
-        #entropy test checks only for isnan, currently 6 isnan
-##        yield check_entropy, distfn, arg, distname + \
-##              ' entropy nan test'
 
+def _est_cont_skip():
+    for distname, arg in distcont:
+        distfn = getattr(stats, distname)        
+        #entropy test checks only for isnan, currently 6 isnan left
+        yield check_entropy, distfn, arg, distname + \
+              ' entropy nan test'
+        # _ppf test has 1 failure be design
+        yield check_ppf_private, distfn, arg, distname + \
+              ' _ppf private test'
 
+def test_540_567():
+    # test for nan returned in tickets 540, 567
+    npt.assert_almost_equal(stats.norm.cdf(-1.7624320982),0.03899815971089126,
+                            decimal=10, err_msg = 'test_540_567')
+    npt.assert_almost_equal(stats.norm.cdf(-1.7624320983),0.038998159702449846,
+                            decimal=10, err_msg = 'test_540_567')
+    npt.assert_almost_equal(stats.norm.cdf(1.38629436112, loc=0.950273420309,
+                            scale=0.204423758009),0.98353464004309321,
+                            decimal=10, err_msg = 'test_540_567')
+
+
 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
@@ -37,6 +57,12 @@
     assert np.isnan(below), msg + 'ppf out of bounds - below'
     assert np.isnan(above), msg + 'ppf out of bounds - above'
 
+def check_ppf_private(distfn,arg,msg):
+    #fails by design for trunk norm self.nb not defined
+    ppfs = distfn._ppf(np.array([0.1,0.5,0.9]), *arg)
+    assert not np.any(np.isnan(ppfs)), msg + 'ppf private is nan'
+
+
 def check_isf_limits(distfn,arg,msg):
     below,low,upp,above = distfn.isf([-1,0,1,2], *arg)
     #print distfn.name, distfn.a, low, distfn.b, upp
@@ -62,7 +88,7 @@
 def assert_equal_inf_nan(v1,v2,msg):
     assert not np.isnan(v1)
     if not np.isinf(v1):
-        npt.assert_almost_equal(v1, v2, decimal=DECIMAL, err_msg= msg + \
+        npt.assert_almost_equal(v1, v2, decimal=DECIMAL, err_msg = msg + \
                                    ' - finite')
     else:
         assert np.isinf(v2) or np.isnan(v2), \
@@ -71,7 +97,5 @@
 if __name__ == "__main__":
     import nose
     #nose.run(argv=['', __file__])
-    #print distcont[:5]
-    #test_cont_extra()
     nose.runmodule(argv=[__file__,'-s'], exit=False)
 

Modified: trunk/scipy/stats/tests/test_discrete_basic.py
===================================================================
--- trunk/scipy/stats/tests/test_discrete_basic.py	2008-11-18 06:19:37 UTC (rev 5143)
+++ trunk/scipy/stats/tests/test_discrete_basic.py	2008-11-18 06:23:05 UTC (rev 5144)
@@ -44,7 +44,7 @@
         yield check_entropy, distfn, arg, distname + \
               ' entropy nan test'
 
-def test_discrete_private():
+def _est_discrete_private():
     #testing private methods mostly for debugging
     #   some tests might fail by design,
     #   e.g. incorrect definition of distfn.a and distfn.b




More information about the Scipy-svn mailing list