def compute_quantile_difference(maxpowers, theo_sample, interval=0.99): all_intervals = [0.5-interval/2.0, 0.5+interval/2.0] cl_diff = [] for m,t in zip(maxpowers, theo_sample): qmaxp = quantiles(m, all_intervals) qtheo = quantiles(t, all_intervals) cl_diff.append(qmaxp[1]-qtheo[1]) return cl_diff
def stretchlim(im, bottom=0.01, top=None, mask=None): """Stretch the image so new image range corresponds to given quantiles. Parameters ---------- im : array, shape (M, N, [...,] P) The input image. bottom : float, optional The lower quantile. top : float, optional The upper quantile. If not provided, it is set to 1 - `bottom`. mask : array of bool, shape (M, N, [...,] P), optional Only consider intensity values where `mask` is ``True``. Returns ------- out : np.ndarray of float The stretched image. """ if mask is None: mask = np.ones(im.shape, dtype=bool) if top is None: top = 1. - bottom im = im.astype(float) q0, q1 = quantiles(im[mask], [bottom, top]) out = (im - q0) / (q1 - q0) out[out < 0] = 0 out[out > 1] = 1 return out
def five_summary(data): minimum = np.min(data) maximum = np.max(data) quants = quantiles(data, [0.25, 0.5, 0.75]) ## make ordered dictionary and store values d = OrderedDict() d["minimum"] = minimum d["25% quantile"] = quants[0] d["median"] = quants[1] d["75% quantile"] = quants[2] d["maximum"] = maximum return d
def _quantiles(self, mcall): ### empty lists for quantiles ci0, ci1 = [], [] ### loop over the parameters ### for i,k in enumerate(self.topt): print("I am on parameter: " + str(i)) ### pick parameter out of array tpar = np.array([t[i] for t in mcall]) ### reshape back into array of niter*nchain dimensions tpar = np.reshape(tpar, (self.nchain, len(tpar)/self.nchain)) ### compute mean of variance of each chain intv = map(lambda y: quantiles(y, prob=[0.1, 0.9]), tpar) ### quantiles will return a list with two elements for each ### chain: the 0.1 and 0.9 quantiles ### need to pick out these for each chain c0 = np.array([x[0] for x in intv]) c1 = np.array([x[1] for x in intv]) ### now compute the scale scale = np.mean(c1-c0)/2.0 ### compute means of each chain mt = map(lambda y: np.mean(y), tpar) ### mean of means of all chains offset = np.mean(mt) ### rescale quantiles (WHY??) ci0.append((c0 - offset)/scale) ci1.append((c1 - offset)/scale) return ci0, ci1
def stretchlim(im, bottom=0.001, top=None, mask=None, in_place=False): """Stretch the image so new image range corresponds to given quantiles. Parameters ---------- im : array, shape (M, N, [...,] P) The input image. bottom : float, optional The lower quantile. top : float, optional The upper quantile. If not provided, it is set to 1 - `bottom`. mask : array of bool, shape (M, N, [...,] P), optional Only consider intensity values where `mask` is ``True``. in_place : bool, optional If True, modify the input image in-place (only possible if it is a float image). Returns ------- out : np.ndarray of float The stretched image. """ if in_place and np.issubdtype(im.dtype, np.float): out = im else: out = np.empty(im.shape, np.float32) out[:] = im if mask is None: mask = np.ones(im.shape, dtype=bool) if top is None: top = 1 - bottom q0, q1 = quantiles(im[mask], [bottom, top]) out -= q0 out /= q1 - q0 out = np.clip(out, 0, 1, out=out) return out
def mcmc_infer(self, namestr='test', printobj = None): #if printobj: # print = printobj #else: # from __builtin__ import print as print ### covariance of the parameters from simulations covsim = np.cov(self.mcall) print("Covariance matrix (after simulations): \n") print(str(covsim)) ### calculate for each parameter its (posterior) mean and equal tail ### 90% (credible) interval from the MCMC self.mean = map(lambda y: np.mean(y), self.mcall) self.std = map(lambda y: np.std(y), self.mcall) self.ci = map(lambda y: quantiles(y, prob=[0.05, 0.95]), self.mcall) ### print to screen print("-- Posterior Summary of Parameters: \n") print("parameter \t mean \t\t sd \t\t 5% \t\t 95% \n") print("---------------------------------------------\n") for i in range(len(self.topt)): print("theta[" + str(i) + "] \t " + str(self.mean[i]) + "\t" + str(self.std[i]) + "\t" + str(self.ci[i][0]) + "\t" + str(self.ci[i][1]) + "\n" ) ### produce matrix scatter plots N = len(self.topt) ### number of parameters print("N: " + str(N)) n, bins, patches = [], [], [] if self.plot: fig = plt.figure(figsize=(15,15)) plt.subplots_adjust(top=0.925, bottom=0.025, left=0.025, right=0.975, wspace=0.2, hspace=0.2) for i in range(N): for j in range(N): xmin, xmax = self.mcall[j][:1000].min(), self.mcall[j][:1000].max() ymin, ymax = self.mcall[i][:1000].min(), self.mcall[i][:1000].max() ax = fig.add_subplot(N,N,i*N+j+1) ax.xaxis.set_major_locator(MaxNLocator(5)) ax.ticklabel_format(style="sci", scilimits=(-2,2)) if i == j: #pass ntemp, binstemp, patchestemp = ax.hist(self.mcall[i][:1000], 30, normed=True, histtype='stepfilled') n.append(ntemp) bins.append(binstemp) patches.append(patchestemp) ax.axis([ymin, ymax, 0, max(ntemp)*1.2]) else: ax.axis([xmin, xmax, ymin, ymax]) ### make a scatter plot first ax.scatter(self.mcall[j][:1000], self.mcall[i][:1000], s=7) ### then add contours xmin, xmax = self.mcall[j][:1000].min(), self.mcall[j][:1000].max() ymin, ymax = self.mcall[i][:1000].min(), self.mcall[i][:1000].max() ### Perform Kernel density estimate on data try: X,Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j] positions = np.vstack([X.ravel(), Y.ravel()]) values = np.vstack([self.mcall[j][:1000], self.mcall[i][:1000]]) kernel = scipy.stats.gaussian_kde(values) Z = np.reshape(kernel(positions).T, X.shape) ax.contour(X,Y,Z,7) except ValueError: print("Not making contours.") plt.savefig(namestr + "_scatter.png", format='png') plt.close() return