[Scipy-svn] r5112 - trunk/scipy/stats

scipy-svn@scip... scipy-svn@scip...
Fri Nov 14 21:48:50 CST 2008


Author: josef
Date: 2008-11-14 21:48:48 -0600 (Fri, 14 Nov 2008)
New Revision: 5112

Modified:
   trunk/scipy/stats/distributions.py
Log:
improve generic entropy integration for continuous rv, when integration returns nan.
6 continuous distributions still return nan for entropy because of integration problems.

Modified: trunk/scipy/stats/distributions.py
===================================================================
--- trunk/scipy/stats/distributions.py	2008-11-15 03:45:41 UTC (rev 5111)
+++ trunk/scipy/stats/distributions.py	2008-11-15 03:48:48 UTC (rev 5112)
@@ -843,7 +843,22 @@
         def integ(x):
             val = self._pdf(x, *args)
             return val*log(val)
-        return -scipy.integrate.quad(integ,self.a,self.b)[0]
+        
+        entr = -scipy.integrate.quad(integ,self.a,self.b)[0]
+        if not np.isnan(entr):
+            return entr
+        else:  # try with different limits if integration problems
+            low,upp = self.ppf([0.001,0.999],*args)
+            if np.isinf(self.b):
+                upper = upp
+            else:
+                upper = self.b
+            if np.isinf(self.a):
+                lower = low
+            else:
+                lower = self.a
+            return -scipy.integrate.quad(integ,lower,upper)[0]
+            
 
     def entropy(self, *args, **kwds):
         loc,scale=map(kwds.get,['loc','scale'])



More information about the Scipy-svn mailing list