def lnprob(theta, Deltam, nsne, xi, redshiftterm): A, M, sigma, pv = theta if (A <= 0 or sigma < 0 or pv < 0): return -numpy.inf C = A * numpy.array(xi) numpy.fill_diagonal( C, C.diagonal() + sigma**2 / nsne + (pv * redshiftterm)**2) mterm = Deltam - M C = matrix(C) W = matrix(mterm) try: lapack.posv(C, W, uplo='U') except ArithmeticError: return -np.inf logdetC = 2 * numpy.log(numpy.array(C).diagonal()).sum() lp = -0.5 * (logdetC + blas.dot(matrix(mterm), W)) + cauchy.logpdf( sigma, loc=0.08, scale=0.5) + cauchy.logpdf( pv, loc=0, scale=600 / 3e5) if not numpy.isfinite(lp): return -np.inf return lp
def pdf(cls, x: Numeric, alpha: float, beta: float, gamma=1., delta=0., pm=0): cls._check_parameters(alpha, beta, gamma, pm) delta, gamma = cls._form_parameters(alpha, delta, gamma, beta, pm) x = (x - delta) / gamma if alpha == 2: ans = norm.logpdf(x, 0, np.sqrt(2)) if np.array(ans).size == 1: ans = np.ravel([ans]) elif alpha == 1 and beta == 0: ans = cauchy.logpdf(x) if np.array(ans).size == 1: ans = np.ravel([ans]) elif alpha == 1: # beta not 0 if isinstance(x, (float, int)): ans = np.array([_pdf_f2(x, beta)]) else: ans = np.array([_pdf_f2(e, beta) for e in x]) else: # alpha != 1 bt = beta * tanpi2(alpha) zeta = -bt theta0 = min(max(-PI2, np.arctan(bt) / alpha), PI2) if bt == 0: zeta_tol = 4e-10 elif 1 - abs(beta) < 0.01 or alpha < 0.01: zeta_tol = 2e-9 else: zeta_tol = 5e-5 if isinstance(x, (float, int)): ans = np.array([_pdf_f1(x, alpha, beta, zeta, theta0, zeta_tol)]) else: ans = np.array([_pdf_f1(e, alpha, beta, zeta, theta0, zeta_tol) for e in x]) infs = ans == 0 if np.any(ans): d = cls._pareto_pdf(x, alpha, beta) ans[infs] = d[infs] / gamma if np.any(~infs): ans[~infs] /= gamma return float(ans) if ans.size == 1 else ans
def compute_noise_loglike(physics, det): """ Compute the log-likelihood that the detection was generated by noise. """ assert (det.time > 0 and det.time < physics.T and det.amp > 0) loglike = 0 loglike += -log(physics.T) # detection time loglike += -log(360) # detection azimuth loglike += -log(compute_slowness(0) - compute_slowness(180)) # slowness # detection amplitude loglike += cauchy.logpdf(log(det.amp), physics.mu_f[det.stanum], physics.theta_f[det.stanum]) return loglike
def calc_pred_p(self, pred_labels): neg_logl = cauchy.logpdf(pred_labels, *self.wt_distr) pos_logl = cauchy.logpdf(pred_labels, *self.mut_distr) return 1 / (1 + np.exp(np.clip(neg_logl - pos_logl, -100, 100)))
def __call__(self, u: np.ndarray, y: float) -> np.ndarray: return cauchy.logpdf(x=u, loc=y, scale=self.scale)
def sample_and_plot(sess, kld, kl_from_pq, kl_from_cob, p_samples, q_samples, m_samples, log_ratio_p_q, log_ratio_p_m, mu_1, mu_2, scale_p, scale_q, mu_3, scale_m): kl_ratio_store = [] log_ratio_store = [] log_r_p_from_m_direct_store = [] feed_dict = {} kl_ratio, kl_true, kl_cob, p_s, q_s, m_s, lpq, lpq_from_cob_dre_direct = sess.run( [ kld, kl_from_pq, kl_from_cob, p_samples, q_samples, m_samples, log_ratio_p_q, log_ratio_p_m ], feed_dict=feed_dict) '''Save ratio estimates''' data_dir = "../data/sym/" + str(scale_p) + "-" + str(scale_q) + str( scale_m) + "/" if not os.path.exists(data_dir): os.makedirs(data_dir) f = open(data_dir + "KLD" + ".txt", "a") f.write("GT for mu_3 = " + str(mu_3) + ": " + str(kl_ratio) + "\nGT-est: " + str(kl_true) + "\nCoB: " + str(kl_cob) + "\n----------\n") f.close() log_ratio_store.append(lpq) log_r_p_from_m_direct_store.append(lpq_from_cob_dre_direct) pickle.dump( log_r_p_from_m_direct_store, open(data_dir + "log_r_p_from_m_direct_store" + str(mu_3) + ".p", "wb")) pickle.dump(m_s, open(data_dir + "xs" + str(mu_3) + ".p", "wb")) pickle.dump(log_ratio_store, open(data_dir + "log_ratio_store" + str(mu_3) + ".p", "wb")) xs = m_s fig, [ax1, ax2, ax3, ax4] = plt.subplots(1, 4, figsize=(13, 4)) ax1.hist(p_s, density=True, histtype='stepfilled', alpha=0.8, label='P') ax1.hist(q_s, density=True, histtype='stepfilled', alpha=0.8, label='Q') ax1.hist(m_s, density=True, histtype='stepfilled', alpha=0.8, label='M') ax1.legend(loc='best', frameon=False) ax1.set_xlim([-5, 5]) ax2.scatter(xs, log_ratio_store[0], label='True p/q', alpha=0.9, s=10., c='b') ax2.scatter(xs, log_r_p_from_m_direct_store[-1][:, 0] - log_r_p_from_m_direct_store[-1][:, 1], label='CoB p/q', alpha=0.9, s=10., c='r') ax2.scatter(xs, -log_ratio_store[0], label='True q/p', alpha=0.9, s=10., c='b') ax2.scatter(xs, log_r_p_from_m_direct_store[-1][:, 1] - log_r_p_from_m_direct_store[-1][:, 0], label='CoB q/p', alpha=0.9, s=10., c='r') ax2.set_xlabel("Samples") ax2.set_ylabel("Log Ratio") ax2.legend(loc='best') ax2.set_xlim([-6, 10]) ax2.set_ylim([-1000, 1000]) pm = [ np.squeeze( norm.logpdf(x, mu_1, scale_p) - cauchy.logpdf(x, mu_3, scale_m)) for x in xs ] qm = [ np.squeeze( norm.logpdf(x, mu_2, scale_q) - cauchy.logpdf(x, mu_3, scale_m)) for x in xs ] ax4.scatter(xs, pm, label='True p/m', alpha=0.9, s=10., c='b') ax4.scatter(xs, log_r_p_from_m_direct_store[-1][:, 0] - log_r_p_from_m_direct_store[-1][:, 2], label='CoB p/m', alpha=0.9, s=10., c='r') ax4.scatter(xs, qm, label='True q/m', alpha=0.9, s=10., c='y') ax4.scatter(xs, log_r_p_from_m_direct_store[-1][:, 1] - log_r_p_from_m_direct_store[-1][:, 2], label='CoB q/m', alpha=0.9, s=10., c='g') ax4.set_xlabel("Samples") ax4.set_ylabel("Log Ratio") ax4.legend(loc='best') ax4.set_xlim([-6, 10]) ax4.set_ylim([-1000, 1000]) rat = log_r_p_from_m_direct_store[-1][:, 0] - log_r_p_from_m_direct_store[ -1][:, 1] d = [np.squeeze(norm.logpdf(x, mu_2, scale_q)) for x in xs] b = [np.squeeze(norm.logpdf(x, mu_1, scale_p)) for x in xs] ax3.scatter(xs, b, label='True P', alpha=0.9, s=5.) ax3.scatter(xs, rat + d, label='P', alpha=0.9, s=5.) ax3.set_xlabel("Samples") ax3.set_ylabel("Log P(x)") ax3.legend(loc='best') ax3.set_xlim([-6, 10]) ax3.set_ylim([-600, 400]) plt.savefig(data_dir + str(mu_3) + ".jpg")
def cauchy_adj_log_pdf(x, loc, scale): if isinstance(x, float): correction = np.log(np.pi) else: correction = len(x) * np.log(np.pi) return np.sum(cauchy.logpdf(x, loc, scale)) + correction
def fitDist(ys, func, xlabel, varNames, params, plot, x=np.arange(-2, 2, 0.01)): vals, bins = np.histogram(ys, bins=x, normed=True) bins = bins[:-1] popt, _ = curve_fit(func, bins, vals) outputString = ", ".join( ["params[\"%s\"]"] * len(popt)) + " = " + ", ".join(["%f"] * len(popt)) for varName, val in zip(varNames, popt): params[varName] = val if func == funcHypsec: fitLabel = "hypsec fit" elif func == funcNorm: fitLabel = "normal fit" elif func == funcGamma: fitLabel = "gamma fit" else: fitLabel = "distribution fit" print(" " + outputString % tuple(varNames + list(popt))) if plot: import matplotlib.pyplot as plt plt.figure() plt.title("Curve fit for " + " ".join(varNames), fontsize=14) plt.bar(bins, vals, width=bins[1] - bins[0], label='observed distribution') plt.plot(bins, func(bins, *popt), 'g', label=fitLabel, linewidth=2.0) if func == funcHypsec: poptNormal, _ = curve_fit(funcNorm, bins, vals) plt.plot(bins, funcNorm(bins, *poptNormal), 'r', label='normal fit (for comparison)', linewidth=2.0, alpha=0.5) if False: funcStudentT = lambda x, df, mu, sigma: t.pdf( x, df=df, loc=mu, scale=sigma) poptStudentT, _ = curve_fit(funcStudentT, bins, vals) print(poptStudentT) funcCauchy = lambda x, mu, sigma: cauchy.pdf( x, loc=mu, scale=sigma) poptCauchy, _ = curve_fit(funcCauchy, bins, vals) print(poptCauchy) plt.plot(bins, funcStudentT(bins, *poptStudentT), 'm', label='student-t fit', linewidth=2.0) plt.plot(bins, funcCauchy(bins, *poptCauchy), 'c', label='cauchy fit', linewidth=2.0) funcLogStudentT = lambda x, df, mu, sigma: t.logpdf( x, df=df, loc=mu, scale=sigma) funcLogNorm = lambda x, mu, sigma: norm.logpdf( x, loc=mu, scale=sigma) funcLogCauchy = lambda x, mu, sigma: cauchy.logpdf( x, loc=mu, scale=sigma) plt.ylabel("relative frequency", fontsize=14) plt.xlabel(xlabel, fontsize=14) plt.legend() plt.figure() plt.plot(bins, funcLogHypsec(bins, *popt), 'g', label='hypsec log fit', linewidth=2.0) plt.plot(bins, funcLogNorm(bins, *poptNormal), 'r', label='normal log fit', linewidth=2.0) plt.plot(bins, funcLogStudentT(bins, *poptStudentT), 'm', label='student-t log fit', linewidth=2.0) plt.plot(bins, funcLogCauchy(bins, *poptCauchy), 'c', label='cauchy log fit', linewidth=2.0) plt.ylabel("relative frequency", fontsize=14) plt.xlabel(xlabel, fontsize=14) plt.legend() plt.tight_layout()
def pdf(cls, x, alpha, beta, gamma=1., delta=0., pm=0, log=False): """ Returns density for stable DF Parameters ---------- x: {array_like, scalar} Numeric vector of quantiles. alpha: float Value of the index parameter alpha in the interval = (0, 2] beta: float Skewness parameter in the range [-1, 1] gamma: float Scale parameter delta: float Location (or ‘shift’) parameter delta. pm: {0, 1, 2} Type of parameterization, an integer in {0, 1, 2}. Defaults to 0, 'S0' parameterization. log: bool If True, returns the log of the density Returns ------- {array_like, scalar} Numeric vectors of density """ cls._check_parameters(alpha, beta, gamma, pm) delta, gamma = cls._parameterize(alpha, delta, gamma, beta, pm) if isinstance(x, abc.Iterable): x = np.asarray(x) x = (x - delta) / gamma if alpha == 2: ans = norm.logpdf(x, 0, np.sqrt(2)) if log else norm.pdf( x, 0, np.sqrt(2)) elif alpha == 1 and beta == 0: ans = cauchy.logpdf(x) if log else cauchy.pdf(x) elif alpha == 1: # beta not 0 if isinstance(x, (complex, float, int)): ans = np.array([pdf.aux_f2(x, beta, log)]) else: ans = np.array([pdf.aux_f2(e, beta, log) for e in x.ravel()]).reshape(x.shape) else: # alpha != 1 if isinstance(x, (complex, float, int)): ans = np.array([pdf.aux_f1(x, alpha, beta, log)]) else: ans = np.array([ pdf.aux_f1(e, alpha, beta, log) for e in x.ravel() ]).reshape(x.shape) if ans.size == 1: ans = ans.ravel() infs = ans == 0 if np.any(ans): d = pdf.pareto(x, alpha, beta, log)[infs] ans[infs] = (d - np.log(gamma)) if log else (d / gamma) if np.any(~infs): d = ans[~infs] ans[~infs] = (d - np.log(gamma)) if log else (d / gamma) return ans.item(0) if ans.size == 1 else ans
def sample_and_plot(sess, kld, p_samples, q_samples, m_samples, log_ratio_p_q, log_ratio_p_m, mu_1, mu_2, scale_p, scale_q, mu_3, scale_m): kl_ratio_store=[] log_ratio_store=[] log_r_p_from_m_direct_store=[] feed_dict = {} kl_ratio, p_s, q_s, m_s, lpq, lpq_from_cob_dre_direct = sess.run([kld, p_samples, q_samples, m_samples, log_ratio_p_q, log_ratio_p_m], feed_dict=feed_dict) kl_ratio_store.append(kl_ratio) log_ratio_store.append(lpq) log_r_p_from_m_direct_store.append(lpq_from_cob_dre_direct) xs = m_s fig, [ax1,ax2,ax3, ax4] = plt.subplots(1, 4,figsize=(13,4)) ax1.hist(p_s, density=True, histtype='stepfilled', alpha=0.8, label='P') ax1.hist(q_s, density=True, histtype='stepfilled', alpha=0.8, label='Q') ax1.hist(m_s, density=True, histtype='stepfilled', alpha=0.8, label='M') ax1.legend(loc='best', frameon=False) ax1.set_xlim([-5,5]) ax2.scatter(xs,log_ratio_store[0],label='True p/q',alpha=0.9,s=10.,c='b') ax2.scatter(xs,log_r_p_from_m_direct_store[-1][:,0]-log_r_p_from_m_direct_store[-1][:,1],label='CoB p/q',alpha=0.9,s=10.,c='r') ax2.scatter(xs,-log_ratio_store[0],label='True q/p',alpha=0.9,s=10.,c='b') ax2.scatter(xs,log_r_p_from_m_direct_store[-1][:,1]-log_r_p_from_m_direct_store[-1][:,0],label='CoB q/p',alpha=0.9,s=10.,c='r') ax2.set_xlabel("Samples") ax2.set_ylabel("Log Ratio") ax2.legend(loc='best') ax2.set_xlim([-4,6]) ax2.set_ylim([-1000,1000]) pm = [np.squeeze(norm.logpdf(x,mu_1,scale_p)-cauchy.logpdf(x,mu_3,scale_m)) for x in xs] qm = [np.squeeze(norm.logpdf(x,mu_2,scale_q)-cauchy.logpdf(x,mu_3,scale_m)) for x in xs] ax4.scatter(xs,pm,label='True p/m',alpha=0.9,s=10.,c='b') ax4.scatter(xs,log_r_p_from_m_direct_store[-1][:,0]-log_r_p_from_m_direct_store[-1][:,2],label='CoB p/m',alpha=0.9,s=10.,c='r') ax4.scatter(xs,qm,label='True q/m',alpha=0.9,s=10.,c='y') ax4.scatter(xs,log_r_p_from_m_direct_store[-1][:,1]-log_r_p_from_m_direct_store[-1][:,2],label='CoB q/m',alpha=0.9,s=10.,c='g') ax4.set_xlabel("Samples") ax4.set_ylabel("Log Ratio") ax4.legend(loc='best') ax4.set_xlim([-4,6]) ax4.set_ylim([-1000,1000]) rat = log_r_p_from_m_direct_store[-1][:,0]-log_r_p_from_m_direct_store[-1][:,1] d = [np.squeeze(norm.logpdf(x,mu_2,scale_q)) for x in xs] b = [np.squeeze(norm.logpdf(x,mu_1,scale_p)) for x in xs] ax3.scatter(xs,b,label='True P',alpha=0.9,s=5.) ax3.scatter(xs,rat+d,label='P',alpha=0.9,s=5.) ax3.set_xlabel("Samples") ax3.set_ylabel("Log P(x)") ax3.legend(loc='best') ax3.set_xlim([-4.,4]) ax3.set_ylim([-600,400])
def sample_and_plot(sess, kl_p_q, kl_cob, kld, p_samples, q_samples, m_samples, log_ratio_p_q, log_ratio_p_m, mu_1, mu_2, scale_p, scale_q, mu_3, scale_m, training=True): kl_ratio_store=[] log_ratio_store=[] log_r_p_from_m_direct_store=[] feed_dict = {} encoder_m = ratios_critic(m_samples) encoder_p = ratios_critic(p_samples) kl_ratio, kl_cob, kl_true, p_s, q_s, xs, enc_m, enc_p, lpq, lpq_from_cob_dre_direct= sess.run([kl_p_q, kl_cob, kld, p_samples, q_samples, m_samples, encoder_m, encoder_p, log_ratio_p_q, log_ratio_p_m], feed_dict=feed_dict) log_ratio_store.append(lpq) log_r_p_from_m_direct_store.append(lpq_from_cob_dre_direct) fig, [ax1,ax2,ax3, ax4] = plt.subplots(1, 4,figsize=(13,4)) ax1.hist(p_s, density=True, histtype='stepfilled', alpha=0.8, label='P') ax1.hist(q_s, density=True, histtype='stepfilled', alpha=0.8, label='Q') ax1.hist(xs, density=True, histtype='stepfilled', alpha=0.8, label='M') ax1.legend(loc='best', frameon=False) ax1.set_xlim([-5,5]) ax2.scatter(xs,log_ratio_store[0],label='True p/q',alpha=0.9,s=10.,c='b') ax2.scatter(xs,log_r_p_from_m_direct_store[-1][:,0]-log_r_p_from_m_direct_store[-1][:,1],label='CoB p/q',alpha=0.9,s=10.,c='r') ax2.scatter(xs,-log_ratio_store[0],label='True q/p',alpha=0.9,s=10.,c='b') ax2.scatter(xs,log_r_p_from_m_direct_store[-1][:,1]-log_r_p_from_m_direct_store[-1][:,0],label='CoB q/p',alpha=0.9,s=10.,c='r') ax2.set_xlabel("Samples") ax2.set_ylabel("Log Ratio") ax2.legend(loc='best') ax2.set_xlim([-4,6]) ax2.set_ylim([-1000,1000]) pm = [np.squeeze(norm.logpdf(x,mu_1,scale_p)-cauchy.logpdf(x,mu_3,scale_m)) for x in xs] qm = [np.squeeze(norm.logpdf(x,mu_2,scale_q)-cauchy.logpdf(x,mu_3,scale_m)) for x in xs] ax4.scatter(xs,pm,label='True p/m',alpha=0.9,s=10.,c='b') ax4.scatter(xs,log_r_p_from_m_direct_store[-1][:,0]-log_r_p_from_m_direct_store[-1][:,2],label='CoB p/m',alpha=0.9,s=10.,c='r') ax4.scatter(xs,qm,label='True q/m',alpha=0.9,s=10.,c='y') ax4.scatter(xs,log_r_p_from_m_direct_store[-1][:,1]-log_r_p_from_m_direct_store[-1][:,2],label='CoB q/m',alpha=0.9,s=10.,c='g') ax4.set_xlabel("Samples") ax4.set_ylabel("Log Ratio") ax4.legend(loc='best') ax4.set_xlim([-4,6]) ax4.set_ylim([-1000,1000]) rat = log_r_p_from_m_direct_store[-1][:,0]-log_r_p_from_m_direct_store[-1][:,1] d = [np.squeeze(norm.logpdf(x,mu_2,scale_q)) for x in xs] b = [np.squeeze(norm.logpdf(x,mu_1,scale_p)) for x in xs] ax3.scatter(xs,b,label='True P',alpha=0.9,s=5.) ax3.scatter(xs,rat+d,label='P',alpha=0.9,s=5.) ax3.set_xlabel("Samples") ax3.set_ylabel("Log P(x)") ax3.legend(loc='best') ax3.set_xlim([-4.,4]) ax3.set_ylim([-600,400]) plt.show() print('KL : ',kl_true) print('KL from samples : ',kl_ratio) print('KL from CoB: ', kl_cob) n_posterior_samples candidate_ws = [] candidate_bs = [] if training: print(f"Taking {n_posterior_samples} samples from posterior distributions on weights\n") w_draw = layer.kernel_posterior.sample() else: print(f"Taking {n_posterior_samples} samples from prior distributions on weights\n") w_draw = layer.kernel_prior.sample() b_draw = layer.bias_posterior.sample() for mc in range(n_posterior_samples): w_, b_ = sess.run([w_draw, b_draw]) candidate_ws.append(w_) candidate_bs.append(b_) candidate_ws = np.array(candidate_ws).astype(np.float32) candidate_bs = np.array(candidate_bs).astype(np.float32) post, post_pred,post_std = log_ratio_predictive(enc_m, candidate_ws.T, candidate_bs.T) kl_post,_,_ = log_ratio_predictive(enc_p, candidate_ws.T, candidate_bs.T) x_sorted = [] m_sorted = [] s_sorted = [] p_sorted = [] [(x_sorted.append(a),m_sorted.append(b), s_sorted.append(c), p_sorted.append(d)) for a,b,c,d in sorted(zip(xs,post_pred, post_std, post))] fig1, ax = plt.subplots() plt.plot(x_sorted, norm.logpdf(x_sorted,mu_1,scale_p)-norm.logpdf(x_sorted,mu_2,scale_q),label='True log_prob',c='r') # plt.plot(x_sorted, m_sorted,label='BC_Bayes_e',alpha=0.7) quan = [np.abs(np.quantile(p_, .05)-np.quantile(p_, .95)) for p_ in p_sorted] plt.errorbar(x_sorted, m_sorted, yerr = quan,label='BC_Bayes_e') # plt.scatter(x_sorted,norm.logpdf(x_sorted,mu_1,scale_p)-norm.logpdf(x_sorted,mu_2,scale_q),label='True log_prob',alpha=0.99,s=5.) plt.xlabel("Samples") plt.ylabel("Log Ratio") plt.legend(loc='upper right') plt.xlim(-4,4) plt.ylim(-400,1000) plt.show() fig1, ax = plt.subplots(figsize=(30,4)) plt.plot(norm.logpdf(x_sorted[100:400],mu_1,scale_p)-norm.logpdf(x_sorted[100:400],mu_2,scale_q),label='True log_prob',alpha=0.99) plt.plot(m_sorted[100:400],label='BC_Bayes_e',alpha=0.7) plt.boxplot(p_sorted[100:400],widths=0.05,notch=True,labels=x_sorted[100:400], showfliers=False, showbox=False, showcaps=False) plt.xlabel("Samples") plt.ylabel("Log Ratio") plt.legend(loc='upper right') plt.xticks(rotation = -65) plt.locator_params(axis='x', nbins=100) plt.ylim(-400,1000) plt.show() fig1, ax = plt.subplots() print(kl_post.shape) plt.boxplot(kl_post.mean(0),widths=0.5,notch=True, showfliers=False) # plt.boxplot(kl_post.mean(0),widths=0.5,notch=True, showfliers=False) plt.ylabel("KLD")
def nll(params): return -sum( cauchy.logpdf(data['delta_food'], loc=params[0], scale=params[2]) + cauchy.logpdf(data['delta_money'], loc=params[1], scale=params[2]) + cauchy.logpdf(data['delta_music'], loc=params[1], scale=params[2]))