def _hd_1D(data,prob,var): "Computes the HD quantiles for a 1D array." xsorted = numpy.squeeze(numpy.sort(data.compressed().view(ndarray))) n = len(xsorted) #......... hd = empty((2,len(prob)), float_) if n < 2: hd.flat = numpy.nan if var: return hd return hd[0] #......... v = arange(n+1) / float(n) betacdf = beta.cdf for (i,p) in enumerate(prob): _w = betacdf(v, (n+1)*p, (n+1)*(1-p)) w = _w[1:] - _w[:-1] hd_mean = dot(w, xsorted) hd[0,i] = hd_mean # hd[1,i] = dot(w, (xsorted-hd_mean)**2) # hd[0, prob == 0] = xsorted[0] hd[0, prob == 1] = xsorted[-1] if var: hd[1, prob == 0] = hd[1, prob == 1] = numpy.nan return hd return hd[0]
def cov(x, y=None, rowvar=True, bias=False, strict=False): """ Estimate the covariance matrix. If x is a vector, return the variance. For matrices, returns the covariance matrix. If y is given, it is treated as an additional (set of) variable(s). Normalization is by (N-1) where N is the number of observations (unbiased estimate). If bias is True then normalization is by N. If rowvar is non-zero (default), then each row is a variable with observations in the columns, otherwise each column is a variable and the observations are in the rows. If strict is True, masked values are propagated: if a masked value appears in a row or column, the whole row or column is considered masked. """ X = narray(x, ndmin=2, subok=True, dtype=float) if X.shape[0] == 1: rowvar = True if rowvar: axis = 0 tup = (slice(None),None) else: axis = 1 tup = (None, slice(None)) # if y is not None: y = narray(y, copy=False, ndmin=2, subok=True, dtype=float) X = concatenate((X,y),axis) # X -= X.mean(axis=1-axis)[tup] n = X.count(1-axis) # if bias: fact = n*1.0 else: fact = n-1.0 # if not rowvar: return (dot(X.T, X.conj(), strict=False) / fact).squeeze() else: return (dot(X, X.T.conj(), strict=False) / fact).squeeze()
def _hdsd_1D(data,prob): "Computes the std error for 1D arrays." xsorted = numpy.sort(data.compressed()) n = len(xsorted) #......... hdsd = empty(len(prob), float_) if n < 2: hdsd.flat = numpy.nan #......... vv = arange(n) / float(n-1) betacdf = beta.cdf # for (i,p) in enumerate(prob): _w = betacdf(vv, (n+1)*p, (n+1)*(1-p)) w = _w[1:] - _w[:-1] mx_ = numpy.fromiter([dot(w,xsorted[r_[range(0,k), range(k+1,n)].astype(int_)]) for k in range(n)], dtype=float_) mx_var = numpy.array(mx_.var(), copy=False, ndmin=1) * n / float(n-1) hdsd[i] = float(n-1) * sqrt(numpy.diag(mx_var).diagonal() / float(n)) return hdsd