Permalink
Browse files

ENH: improve accuracy of distributions.ncx2, add logpdf method. Closes

  • Loading branch information...
1 parent ff96ef0 commit 98c9f7c8c7a286e2debdf0bc04a6709ae7754fa3 @rgommers rgommers committed Aug 22, 2011
Showing with 5 additions and 4 deletions.
  1. +5 −4 scipy/stats/distributions.py
@@ -4351,11 +4351,12 @@ class ncx2_gen(rv_continuous):
"""
def _rvs(self, df, nc):
return mtrand.noncentral_chisquare(df,nc,self._size)
- def _pdf(self, x, df, nc):
+ def _logpdf(self, x, df, nc):
a = arr(df/2.0)
- Px = exp(-nc/2.0)*special.hyp0f1(a,nc*x/4.0)
- Px *= exp(-x/2.0)*x**(a-1) / arr(2**a * special.gamma(a))
- return Px
+ fac = -nc/2.0 - x/2.0 + (a-1)*np.log(x) - a*np.log(2) - special.gammaln(a)
+ return fac + np.nan_to_num(np.log(special.hyp0f1(a, nc * x/4.0)))
+ def _pdf(self, x, df, nc):
+ return np.exp(self._logpdf(x, df, nc))
def _cdf(self, x, df, nc):
return special.chndtr(x,df,nc)
def _ppf(self, q, df, nc):

0 comments on commit 98c9f7c

Please sign in to comment.