def find_model1_map(raw_data): mat = model1_design_matrix(raw_data) t = mat[:, -1] x_1s = np.cos(psi_sf) * mat[:, 0] + np.sin(psi_sf) * mat[:, 1] return scp_bayes.find_map(lp1_theta, lp1_n, [t_n, x_1], [u, sigma], [t, x_1s], [0.0, 0.1], (u_bounds, sigma_bounds))
def find_model3c_map(raw_data): mat = model3c_design_matrix(raw_data) t = mat[:, 2] phi = mat[:, 0] psi = mat[:, 1] theta0 = (0.0, 0.1) theta_bounds = (u_bounds, sigma_bounds) return scp_bayes.find_map(lp3c_theta, lp3c_n, [t_n, phi_n, psi_n], [u, sigma], [t, phi, psi], theta0, theta_bounds)
def find_model3a_map(raw_data): mat = model3_design_matrix(raw_data) t = mat[:, 2] x_1 = mat[:, 0] x_2 = mat[:, 1] theta0 = (0.0, 0.0, 0.1) theta_bounds = (A_bounds, B_bounds, sigma_bounds) return scp_bayes.find_map(lp3a_theta, lp3a_n, [t_n, x_1n, x_2n], [A, B, sigma], [t, x_1, x_2], theta0, theta_bounds)
def find_model2_map(raw_data): feat_mat = model2_feature_matrix(raw_data) t = feat_mat[:, 2] phi = feat_mat[:, 0] psi = feat_mat[:, 1] theta0 = (0.0, 0.0, 0.1) theta_bounds = (u_bounds, v_bounds, sigma_bounds) return scp_bayes.find_map(lp2_theta, lp2_n, [t_n, phi_n, psi_n], [u, v, sigma], [t, phi, psi], theta0, theta_bounds)
def plot_posterior(prior_logp_expr, datum_logp_expr, data_syms, theta_syms, data_values, theta0, theta_bounds, plotted_i): """. """ map_optres = scp_bayes.find_map(prior_logp_expr, datum_logp_expr, data_syms, theta_syms, data_values, theta0, theta_bounds) cov = linalg.inv(map_optres.hess) std_dev = np.sqrt(cov[plotted_i, plotted_i]) mode = map_optres.x[plotted_i] radius = 5 * std_dev xs = np.linspace(mode - radius, mode + radius, 100) ## Laplace Approximation ys_lpl = np.array([np.exp(-0.5 * ((x - mode) / std_dev)**2) for x in xs]) ys_lpl = ys_lpl / np.sum(ys_lpl) plt.plot(xs, ys_lpl, '', label="Laplace Approximation") all_syms = [] + list(data_syms) + list(theta_syms) lam_f_n = lambdify(all_syms, datum_logp_expr) lam_f_prior = lambdify(all_syms, prior_logp_expr) pvalues = [] + list(data_values) def f(theta): args = pvalues + list(theta) ## NOTE we're normalizing such that the density is 1 at the mode of the distribution. return np.exp( np.sum(lam_f_n(*args)) + lam_f_prior(*args) + map_optres.fun) D = len(theta_bounds) def fx(x): def h(*theta1): theta = theta1[0:plotted_i] + (x, ) + theta1[plotted_i:D - 1] return f(theta) return h theta1_bounds = theta_bounds[0:plotted_i] + theta_bounds[plotted_i + 1:D] def var_intgr_opts(i): points = [(map_optres.x[i] + k * np.sqrt(cov[i, i])) for k in [-10, 0, 10]] return {'points': points} intgr_opts = [var_intgr_opts(i) for i in range(D) if i != plotted_i] def g(x): r, err = intgr.nquad(fx(x), theta1_bounds, opts=intgr_opts) return r ys_intr = np.array([g(x) for x in xs]) ys_intr = ys_intr / np.sum(ys_intr) plt.plot(xs, ys_intr, '', label="Numerical Integration") ax = plt.gca() ax.set_title('Posterior probability of {}'.format( str(theta_syms[plotted_i]))) ax.set_ylabel('Probability density') ax.set_xlabel(str(theta_syms[plotted_i])) plt.legend() plt.show()