Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENH: stats.distributions: propagate nans, changes thanks to Per Brodt…

…korb.

Closes #835.
  • Loading branch information...
commit 96e39ecc6a2b671ed7f99a9c0375adc9238c6056 1 parent 1fb3710
@josef-pkt josef-pkt authored rgommers committed
Showing with 31 additions and 12 deletions.
  1. +12 −12 scipy/stats/distributions.py
  2. +19 −0 scipy/stats/tests/test_distributions.py
View
24 scipy/stats/distributions.py
@@ -1255,7 +1255,7 @@ def pdf(self,x,*args,**kwds):
cond1 = (scale > 0) & (x >= self.a) & (x <= self.b)
cond = cond0 & cond1
output = zeros(shape(cond),'d')
- putmask(output,(1-cond0)*array(cond1,bool),self.badvalue)
+ putmask(output,(1-cond0)+np.isnan(x),self.badvalue)
if any(cond):
goodargs = argsreduce(cond, *((x,)+args+(scale,)))
scale, goodargs = goodargs[-1], goodargs[:-1]
@@ -1298,7 +1298,7 @@ def logpdf(self, x, *args, **kwds):
cond = cond0 & cond1
output = empty(shape(cond),'d')
output.fill(NINF)
- putmask(output,(1-cond0)*array(cond1,bool),self.badvalue)
+ putmask(output,(1-cond0)+np.isnan(x),self.badvalue)
if any(cond):
goodargs = argsreduce(cond, *((x,)+args+(scale,)))
scale, goodargs = goodargs[-1], goodargs[:-1]
@@ -1340,7 +1340,7 @@ def cdf(self,x,*args,**kwds):
cond2 = (x >= self.b) & cond0
cond = cond0 & cond1
output = zeros(shape(cond),'d')
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0)+np.isnan(x),self.badvalue)
place(output,cond2,1.0)
if any(cond): #call only if at least 1 entry
goodargs = argsreduce(cond, *((x,)+args))
@@ -1382,7 +1382,7 @@ def logcdf(self,x,*args,**kwds):
cond = cond0 & cond1
output = empty(shape(cond),'d')
output.fill(NINF)
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0)*(cond1==cond1)+np.isnan(x),self.badvalue)
place(output,cond2,0.0)
if any(cond): #call only if at least 1 entry
goodargs = argsreduce(cond, *((x,)+args))
@@ -1423,7 +1423,7 @@ def sf(self,x,*args,**kwds):
cond2 = cond0 & (x <= self.a)
cond = cond0 & cond1
output = zeros(shape(cond),'d')
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0)+np.isnan(x),self.badvalue)
place(output,cond2,1.0)
if any(cond):
goodargs = argsreduce(cond, *((x,)+args))
@@ -1468,7 +1468,7 @@ def logsf(self,x,*args,**kwds):
cond = cond0 & cond1
output = empty(shape(cond),'d')
output.fill(NINF)
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0)+np.isnan(x),self.badvalue)
place(output,cond2,0.0)
if any(cond):
goodargs = argsreduce(cond, *((x,)+args))
@@ -5820,7 +5820,7 @@ def pmf(self, k,*args, **kwds):
cond1 = (k >= self.a) & (k <= self.b) & self._nonzero(k,*args)
cond = cond0 & cond1
output = zeros(shape(cond),'d')
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0) + np.isnan(k),self.badvalue)
if any(cond):
goodargs = argsreduce(cond, *((k,)+args))
place(output,cond,self._pmf(*goodargs))
@@ -5858,7 +5858,7 @@ def logpmf(self, k,*args, **kwds):
cond = cond0 & cond1
output = empty(shape(cond),'d')
output.fill(NINF)
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0) + np.isnan(k),self.badvalue)
if any(cond):
goodargs = argsreduce(cond, *((k,)+args))
place(output,cond,self._logpmf(*goodargs))
@@ -5896,7 +5896,7 @@ def cdf(self, k, *args, **kwds):
cond2 = (k >= self.b)
cond = cond0 & cond1
output = zeros(shape(cond),'d')
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0) + np.isnan(k),self.badvalue)
place(output,cond2*(cond0==cond0), 1.0)
if any(cond):
@@ -5937,7 +5937,7 @@ def logcdf(self, k, *args, **kwds):
cond = cond0 & cond1
output = empty(shape(cond),'d')
output.fill(NINF)
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0) + np.isnan(k),self.badvalue)
place(output,cond2*(cond0==cond0), 0.0)
if any(cond):
@@ -5977,7 +5977,7 @@ def sf(self,k,*args,**kwds):
cond2 = (k < self.a) & cond0
cond = cond0 & cond1
output = zeros(shape(cond),'d')
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0) + np.isnan(k),self.badvalue)
place(output,cond2,1.0)
if any(cond):
goodargs = argsreduce(cond, *((k,)+args))
@@ -6017,7 +6017,7 @@ def logsf(self,k,*args,**kwds):
cond = cond0 & cond1
output = empty(shape(cond),'d')
output.fill(NINF)
- place(output,(1-cond0)*(cond1==cond1),self.badvalue)
+ place(output,(1-cond0) + np.isnan(k),self.badvalue)
place(output,cond2,0.0)
if any(cond):
goodargs = argsreduce(cond, *((k,)+args))
View
19 scipy/stats/tests/test_distributions.py
@@ -731,6 +731,25 @@ def test_regression_ticket_1421():
assert_('pdf(x, mu, loc=0, scale=1)' not in stats.poisson.__doc__)
assert_('pmf(x,' in stats.poisson.__doc__)
+def test_nan_arguments_ticket_835():
+ assert_(np.isnan(stats.t.logcdf(np.nan)))
+ assert_(np.isnan(stats.t.cdf(np.nan)))
+ assert_(np.isnan(stats.t.logsf(np.nan)))
+ assert_(np.isnan(stats.t.sf(np.nan)))
+ assert_(np.isnan(stats.t.pdf(np.nan)))
+ assert_(np.isnan(stats.t.logpdf(np.nan)))
+ assert_(np.isnan(stats.t.ppf(np.nan)))
+ assert_(np.isnan(stats.t.isf(np.nan)))
+
+ assert_(np.isnan(stats.bernoulli.logcdf(np.nan, 0.5)))
+ assert_(np.isnan(stats.bernoulli.cdf(np.nan, 0.5)))
+ assert_(np.isnan(stats.bernoulli.logsf(np.nan, 0.5)))
+ assert_(np.isnan(stats.bernoulli.sf(np.nan, 0.5)))
+ assert_(np.isnan(stats.bernoulli.pmf(np.nan, 0.5)))
+ assert_(np.isnan(stats.bernoulli.logpmf(np.nan, 0.5)))
+ assert_(np.isnan(stats.bernoulli.ppf(np.nan, 0.5)))
+ assert_(np.isnan(stats.bernoulli.isf(np.nan, 0.5)))
+
def test_frozen_fit_ticket_1536():
Please sign in to comment.
Something went wrong with that request. Please try again.