def TestArgsreduce(): a = array([1, 3, 2, 1, 2, 3, 3]) b, c = argsreduce(a > 1, a, 2) assert_array_equal(b, [3, 2, 2, 3, 3]) assert_array_equal(c, [2, 2, 2, 2, 2]) b, c = argsreduce(2 > 1, a, 2) assert_array_equal(b, a[0]) assert_array_equal(c, [2]) b, c = argsreduce(a > 0, a, 2) assert_array_equal(b, a) assert_array_equal(c, [2] * numpy.size(a))
def TestArgsreduce(): a = array([1,3,2,1,2,3,3]) b,c = argsreduce(a > 1, a, 2) assert_array_equal(b, [3,2,2,3,3]) assert_array_equal(c, [2,2,2,2,2]) b,c = argsreduce(2 > 1, a, 2) assert_array_equal(b, a[0]) assert_array_equal(c, [2]) b,c = argsreduce(a > 0, a, 2) assert_array_equal(b, a) assert_array_equal(c, [2] * numpy.size(a))
def isf(self, q, *args, **kwds): """ Inverse survival function at q of the given RV. Parameters ---------- q : array-like upper tail probability arg1, arg2, arg3,... : array-like The shape parameter(s) for the distribution (see docstring of the instance object for more information) loc : array-like, optional location parameter (default=0) scale : array-like, optional scale parameter (default=1) Returns ------- x : array-like quantile corresponding to the upper tail probability q. """ (loc, scale) = map(kwds.get, ['loc', 'scale']) (args, loc, scale) = self._fix_loc_scale(args, loc, scale) (q, loc, scale, shape) = map(arr, (q, loc, scale, args[0])) q = np.where(scale > 0, q, 1. - q) # isgamma = (shape > 0) & (scale != 0) isnorm = (shape == 0) & (scale > 0) ispe3 = (isgamma | isnorm) indom = (q > 0) & (q < 1) islarge = (q == 1) & ispe3 valid = ispe3 & indom output = valarray(np.shape(valid), value=self.b) #place(output,(1-cond0)*(cond1==cond1), self.badvalue) np.place(output, (1 - ispe3) * (indom == indom) + (1 - indom) * (q != 0.0), self.badvalue) np.place(output, islarge, self.a) if np.any(valid): #call only if at least 1 entry goodargs = argsreduce(valid, *((q,) + args + (scale, loc))) #PB replace 1-q by q (q, shape, scale, loc) = argsreduce(valid, *((q, shape, scale, loc))) np.place(output, (valid & isgamma), self._isf(q, shape) * scale + loc) np.place(output, (valid & isnorm), -dist._norm_ppf(q)) if output.ndim == 0: return output[()] return output
def cdf(self, x, *args, **kwds): """ Cumulative distribution function at x of the given RV. Parameters ---------- x : array-like quantiles arg1, arg2, arg3,... : array-like The shape parameter(s) for the distribution (see docstring of the instance object for more information) loc : array-like, optional location parameter (default=0) scale : array-like, optional scale parameter (default=1) Returns ------- cdf : array-like Cumulative distribution function evaluated at x """ (loc, scale) = map(kwds.get, ['loc', 'scale']) (args, loc, scale) = self._fix_loc_scale(args, loc, scale) (x, loc, scale, shape) = map(arr, (x, loc, scale, args[0])) x = (x - loc) * 1.0 / scale # isgamma = (shape > 0) & (scale != 0) isnorm = (shape == 0) & (scale > 0) ispe3 = (isnorm | isgamma) indomain = (x > self.a) & (x < self.b) toolarge = (x >= self.b) valid = ispe3 & indomain output = np.zeros(np.shape(valid), 'd') np.place(output, (1 - ispe3) * (indomain == indomain), self.badvalue) np.place(output, toolarge, 1.0) if any(valid): #call only if at least 1 entry (x, shape) = argsreduce(valid, *((x,) + (shape,))) vals = self._cdf(x, shape) np.place(output, (valid & isgamma), np.where(scale > 0, vals, 1. - vals)) np.place(output, (valid & isnorm), dist._norm_cdf(x)) if output.ndim == 0: return output[()] return output
def pdf(self, x, *args, **kwds): """ Probability density function at x of the given RV. Parameters ---------- x : array-like quantiles arg1, arg2, arg3,... : array-like The shape parameter(s) for the distribution (see docstring of the instance object for more information) loc : array-like, optional location parameter (default=0) scale : array-like, optional scale parameter (default=1) Returns ------- pdf : array-like Probability density function evaluated at x """ (loc, scale) = map(kwds.get, ['loc', 'scale']) (args, loc, scale) = self._fix_loc_scale(args, loc, scale) (x, loc, scale, shape) = map(arr, (x, loc, scale, args[0])) x = (x - loc) * 1.0 / scale scale = np.abs(scale) # isgamma = (shape > 0) & (scale != 0) isnorm = (shape == 0) & (scale > 0) ispe3 = (isgamma | isnorm) indom = (x > self.a) & (x < self.b) valid = ispe3 & indom # output = np.zeros(np.shape(valid), 'd') np.putmask(output, (1 - ispe3) * np.array(indom, bool), self.badvalue) (x, shape, scale) = argsreduce(valid, *((x, shape, scale,))) np.place(output, (valid & isgamma), self._pdf(x, shape) / scale) np.place(output, (valid & isnorm), dist._norm_pdf(x) / scale) if output.ndim == 0: return output[()] return output
def ppf7(self, q, *args, **kwds): """ Percent point function (inverse of cdf) at q of the given RV Parameters ---------- q : array-like lower tail probability arg1, arg2, arg3,... : array-like The shape parameter(s) for the distribution (see docstring of the instance object for more information) loc : array-like, optional location parameter (default=0) Returns ------- k : array-like quantile corresponding to the lower tail probability, q. """ loc = kwds.get('loc') args, loc = self._rv_discrete__fix_loc(args, loc) q, loc = map(arr, (q, loc)) args = tuple(map(arr, args)) cond0 = self._argcheck(*args) & (loc == loc) cond1 = (q > 0) & (q < 1) cond2 = (q == 1) & cond0 cond = cond0 & cond1 output = valarray(shape(cond), value=self.badvalue, typecode='d') #output type 'd' to handle nin and inf place(output, (q == 0) * (cond == cond), self.a - 1) place(output, cond2, self.b) if any(cond): goodargs = argsreduce(cond, *((q, ) + args + (loc, ))) loc, goodargs = goodargs[-1], goodargs[:-1] place(output, cond, self._ppf(*goodargs) + loc) if output.ndim == 0: return output[()] return output
def ppf7(self,q,*args,**kwds): """ Percent point function (inverse of cdf) at q of the given RV Parameters ---------- q : array-like lower tail probability arg1, arg2, arg3,... : array-like The shape parameter(s) for the distribution (see docstring of the instance object for more information) loc : array-like, optional location parameter (default=0) Returns ------- k : array-like quantile corresponding to the lower tail probability, q. """ loc = kwds.get('loc') args, loc = self._rv_discrete__fix_loc(args, loc) q,loc = map(arr,(q,loc)) args = tuple(map(arr,args)) cond0 = self._argcheck(*args) & (loc == loc) cond1 = (q > 0) & (q < 1) cond2 = (q==1) & cond0 cond = cond0 & cond1 output = valarray(shape(cond),value=self.badvalue,typecode='d') #output type 'd' to handle nin and inf place(output,(q==0)*(cond==cond), self.a-1) place(output,cond2,self.b) if any(cond): goodargs = argsreduce(cond, *((q,)+args+(loc,))) loc, goodargs = goodargs[-1], goodargs[:-1] place(output,cond,self._ppf(*goodargs) + loc) if output.ndim == 0: return output[()] return output