[Scipy-svn] r3246 - in trunk/Lib/stats: . tests

scipy-svn@scip... scipy-svn@scip...
Sat Aug 18 06:21:19 CDT 2007


Author: stefan
Date: 2007-08-18 06:20:58 -0500 (Sat, 18 Aug 2007)
New Revision: 3246

Modified:
   trunk/Lib/stats/distributions.py
   trunk/Lib/stats/tests/test_distributions.py
Log:
Fix rv_discrete.rvs for values with 0 probability.


Modified: trunk/Lib/stats/distributions.py
===================================================================
--- trunk/Lib/stats/distributions.py	2007-08-15 13:38:19 UTC (rev 3245)
+++ trunk/Lib/stats/distributions.py	2007-08-18 11:20:58 UTC (rev 3246)
@@ -20,6 +20,7 @@
 import numpy.random as mtrand
 from numpy import flatnonzero as nonzero
 from scipy.special import gammaln as gamln
+from copy import copy
 
 __all__ = [
            'rv_continuous',
@@ -3289,7 +3290,9 @@
 
 def reverse_dict(dict):
     newdict = {}
-    for key in dict.keys():
+    sorted_keys = copy(dict.keys())
+    sorted_keys.sort()
+    for key in sorted_keys[::-1]:
         newdict[dict[key]] = key
     return newdict
 

Modified: trunk/Lib/stats/tests/test_distributions.py
===================================================================
--- trunk/Lib/stats/tests/test_distributions.py	2007-08-15 13:38:19 UTC (rev 3245)
+++ trunk/Lib/stats/tests/test_distributions.py	2007-08-18 11:20:58 UTC (rev 3246)
@@ -200,5 +200,16 @@
         assert(isinstance(val, numpy.ndarray))
         assert(val.dtype.char in typecodes['AllInteger'])
 
+class test_rv_discrete(NumpyTestCase):
+    def check_rvs(self):
+        states = [-1,0,1,2,3,4]
+        probability = [0.0,0.3,0.4,0.0,0.3,0.0]
+        samples = 1000
+        r = stats.rv_discrete(name='sample',values=(states,probability))
+        x = r.rvs(size=samples)
+
+        for s,p in zip(states,probability):
+            assert abs(sum(x == s)/float(samples) - p) < 0.05
+
 if __name__ == "__main__":
     NumpyTest('stats.distributions').run()



More information about the Scipy-svn mailing list