[Scipy-svn] r7049 - in branches/0.9.x/scipy/stats: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Sat Jan 15 09:05:17 EST 2011


Author: rgommers
Date: 2011-01-15 08:05:17 -0600 (Sat, 15 Jan 2011)
New Revision: 7049

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

Modified: branches/0.9.x/scipy/stats/distributions.py
===================================================================
--- branches/0.9.x/scipy/stats/distributions.py	2011-01-15 14:04:18 UTC (rev 7048)
+++ branches/0.9.x/scipy/stats/distributions.py	2011-01-15 14:05:17 UTC (rev 7049)
@@ -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: branches/0.9.x/scipy/stats/tests/test_distributions.py
===================================================================
--- branches/0.9.x/scipy/stats/tests/test_distributions.py	2011-01-15 14:04:18 UTC (rev 7048)
+++ branches/0.9.x/scipy/stats/tests/test_distributions.py	2011-01-15 14:05:17 UTC (rev 7049)
@@ -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