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

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Jan 14 22:28:40 EST 2011


Author: josef
Date: 2011-01-14 21:28:40 -0600 (Fri, 14 Jan 2011)
New Revision: 7034

Modified:
   trunk/scipy/stats/distributions.py
   trunk/scipy/stats/tests/test_distributions.py
Log:
stats distribution chi2.pdf work around 0*log(0)==nan, ticket:1326

Modified: trunk/scipy/stats/distributions.py
===================================================================
--- trunk/scipy/stats/distributions.py	2011-01-15 02:06:35 UTC (rev 7033)
+++ trunk/scipy/stats/distributions.py	2011-01-15 03:28:40 UTC (rev 7034)
@@ -2267,7 +2267,10 @@
     def _pdf(self, x, df):
         return exp(self._logpdf(x, df))
     def _logpdf(self, x, df):
-        return (df/2.-1)*log(x)-x/2.-gamln(df/2.)-(log(2)*df)/2.
+        #term1 = (df/2.-1)*log(x)
+        #term1[(df==2)*(x==0)] = 0
+        #avoid 0*log(0)==nan
+        return (df/2.-1)*log(x+1e-300) - x/2. - gamln(df/2.) - (log(2)*df)/2.
 ##        Px = x**(df/2.0-1)*exp(-x/2.0)
 ##        Px /= special.gamma(df/2.0)* 2**(df/2.0)
 ##        return log(Px)

Modified: trunk/scipy/stats/tests/test_distributions.py
===================================================================
--- trunk/scipy/stats/tests/test_distributions.py	2011-01-15 02:06:35 UTC (rev 7033)
+++ trunk/scipy/stats/tests/test_distributions.py	2011-01-15 03:28:40 UTC (rev 7034)
@@ -661,5 +661,11 @@
     g = stats.distributions.gamma_gen(name='gamma')
 
 
+def test_regression_ticket_1326():
+    """Regression test for ticket #1326."""
+    #adjust to avoid nan with 0*log(0)
+    assert_almost_equal(stats.chi2.pdf(0.0, 2), 0.5, 14)
+
+
 if __name__ == "__main__":
     run_module_suite()




More information about the Scipy-svn mailing list