def test_frozen(self): # Test that the frozen and non-frozen inverse Wishart gives the same # answers # Construct an arbitrary positive definite scale matrix dim = 4 scale = np.diag(np.arange(dim) + 1) scale[np.tril_indices(dim, k=-1)] = np.arange(dim * (dim - 1) / 2) scale = np.dot(scale.T, scale) # Construct a collection of positive definite matrices to test the PDF X = [] for i in range(5): x = np.diag(np.arange(dim) + (i + 1)**2) x[np.tril_indices(dim, k=-1)] = np.arange(dim * (dim - 1) / 2) x = np.dot(x.T, x) X.append(x) X = np.array(X).T # Construct a 1D and 2D set of parameters parameters = [ (10, 1, np.linspace(0.1, 10, 5)), # 1D case (10, scale, X) ] for (df, scale, x) in parameters: iw = invwishart(df, scale) assert_equal(iw.var(), invwishart.var(df, scale)) assert_equal(iw.mean(), invwishart.mean(df, scale)) assert_equal(iw.mode(), invwishart.mode(df, scale)) assert_allclose(iw.pdf(x), invwishart.pdf(x, df, scale))
def test_frozen(self): # Test that the frozen and non-frozen inverse Wishart gives the same # answers # Construct an arbitrary positive definite scale matrix dim = 4 scale = np.diag(np.arange(dim)+1) scale[np.tril_indices(dim, k=-1)] = np.arange(dim*(dim-1)/2) scale = np.dot(scale.T, scale) # Construct a collection of positive definite matrices to test the PDF X = [] for i in range(5): x = np.diag(np.arange(dim)+(i+1)**2) x[np.tril_indices(dim, k=-1)] = np.arange(dim*(dim-1)/2) x = np.dot(x.T, x) X.append(x) X = np.array(X).T # Construct a 1D and 2D set of parameters parameters = [ (10, 1, np.linspace(0.1, 10, 5)), # 1D case (10, scale, X) ] for (df, scale, x) in parameters: iw = invwishart(df, scale) assert_equal(iw.var(), invwishart.var(df, scale)) assert_equal(iw.mean(), invwishart.mean(df, scale)) assert_equal(iw.mode(), invwishart.mode(df, scale)) assert_allclose(iw.pdf(x), invwishart.pdf(x, df, scale))
def test1(): np.random.seed(0) nside = 8 ell = np.arange(3. * nside) Cltrue = np.zeros_like(ell) Cltrue[2:] = 1 / ell[2:]**2 m = hp.synfast(Cltrue, nside) Clhat = hp.anafast(m) sigma_l = (2 * ell + 1) * Clhat sigma_l = Clhat Cl = np.linspace(5e-3, 5, 1000) sl = sigma_l[2] l = ell[2] y = np.exp(-(2 * l + 1) * sl / (2 * Cl)) / np.sqrt(Cl**(2 * l + 1)) plt.semilogx(Cl, y / y.max()) alpha = (2 * l - 1) / 2 beta = (2 * l + 1) * sl / 2 pdf = invgamma.pdf(Cl, alpha, scale=beta) plt.semilogx(Cl, pdf / pdf.max()) pdf = invwishart.pdf(Cl, df=alpha * 2, scale=beta * 2) plt.semilogx(Cl, pdf / pdf.max()) plt.axvline(Clhat[2]) plt.axvline(Cltrue[2], color='k', linestyle='--') plt.savefig('test1.pdf') #plt.show() plt.close() return
def pdf(self, x): """ PDF for Inverse Wishart prior Parameters ---------- x : float Covariance matrix for which the prior is being formed over Returns ---------- - p(x) """ return invwishart.pdf(X, df=self.v, scale=self.Psi)
def pdf(self, X): """ PDF for Inverse Wishart prior Parameters ---------- x : float Covariance matrix for which the prior is being formed over Returns ---------- - p(x) """ return invwishart.pdf(X, df=self.v, scale=self.Psi)
cov_est = s / (j1 + N) print(cov_est) print("Independent Jeffrey's prior") cov_est = s / (j2 + N) print(cov_est) # Q4 Monte carlo estimation M = [1000, 10000, 100000] print("Monte Carlo Bayesian Estimation") for m in M: print("m =", m) sigmas = invwishart.rvs(v0, d0, size=m) lx = invwishart.pdf(sigmas.T, df=N - 3, scale=s) A = np.dot(sigmas.T, lx) / np.sum(lx) print(A) d0 = np.array([[2, 0], [0, 4]]) print(" part b") for m in M: print("m =", m) sigmas = invwishart.rvs(v0, d0, size=m) lx = invwishart.pdf(sigmas.T, df=N - 3, scale=s) A = np.dot(sigmas.T, lx) / np.sum(lx) print(A) # Q5 Gibbs Sampling print("Gibbs sampler")
k_ = 500 # number of grid points # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_prior_niw-implementation-step01): Compute the expectation and standard deviations of Sigma2 and B # + exp_sigma2 = invwishart.mean(v_pri, v_pri * sigma2_pri) std_sigma2 = np.sqrt(invwishart.var(v_pri, v_pri * sigma2_pri)) exp_beta = beta_pri std_beta = np.sqrt(sigma2_pri / (sigma2_zpri * t_pri) * v_pri / (v_pri - 2.)) # - # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_prior_niw-implementation-step02): Compute the marginal pdf of Sigma2 s = np.linspace(0.1, exp_sigma2 + 3 * std_sigma2, k_) f_sigma2 = invwishart.pdf(s, v_pri, v_pri * sigma2_pri) # ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_prior_niw-implementation-step03): Compute the marginal pdf of B b = np.linspace(exp_beta - 3 * std_beta, exp_beta + 3 * std_beta, k_) f_beta = t.pdf((b - beta_pri) / np.sqrt(sigma2_pri / (sigma2_zpri * t_pri)), v_pri) / np.sqrt(sigma2_pri / (sigma2_zpri * t_pri)) # ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_prior_niw-implementation-step04): Compute the joint pdf of B and Sigma2 f_joint = np.zeros((k_, k_)) for k in range(k_): f_joint[:, k] = norm.pdf(b, beta_pri, np.sqrt( s[k] / (sigma2_zpri * t_pri))) * f_sigma2[k] # ## Plots
def pdf(self,X): return invwishart.pdf(X, df=self.v, scale=self.Psi)
import matplotlib.pyplot as plt from scipy.stats import invwishart, invgamma x = np.linspace(0.01, 1, 100) iw = invwishart.pdf(x, df=6, scale=1) iw[:3] # array([ 1.20546865e-15, 5.42497807e-06, 4.45813929e-03]) ig = invgamma.pdf(x, 6/2., scale=1./2) ig[:3] # array([ 1.20546865e-15, 5.42497807e-06, 4.45813929e-03]) plt.plot(x, iw) # The input quantiles can be any shape of array, as long as the last # axis labels the components.
exp_sigma2_pri = invwishart.mean(v_pri, v_pri * sigma2_pri) std_sigma2_pri = np.sqrt(invwishart.var(v_pri, v_pri * sigma2_pri)) exp_sigma2_pos = invwishart.mean(v_pos, v_pos * sigma2_pos) std_sigma2_pos = np.sqrt(invwishart.var(v_pos, v_pos * sigma2_pos)) # ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_posterior_niw-implementation-step04): Compute marginal pdfs of the sample, prior and posterior distributions of Sigma2 # + s_max = np.max([ exp_sigma2_hat + 3. * std_sigma2_hat, exp_sigma2_pri + 3. * std_sigma2_pri, exp_sigma2_pos + 3. * std_sigma2_pos ]) s = np.linspace(0.01, s_max, k_) # grid f_sigma2_hat = wishart.pdf(s, t_ - 1, sigma2_hat / t_) # sample pdf f_sigma2_pri = invwishart.pdf(s, v_pri, v_pri * sigma2_pri) # prior pdf f_sigma2_pos = invwishart.pdf(s, v_pos, v_pos * sigma2_pos) # posterior pdf # - # ## [Step 5](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_posterior_niw-implementation-step05): Compute the pdf of the sample, prior and posterior distributions of M # + m_min = np.min([ mu_hat - 3. * np.sqrt(sigma2_hat / t_), mu_pri - 3. * np.sqrt(sigma2_pri / t_pri), mu_pos - 3. * np.sqrt(sigma2_pos / t_pos) ]) m_max = np.max([ mu_hat + 3. * np.sqrt(sigma2_hat / t_), mu_pri + 3. * np.sqrt(sigma2_pri / t_pri), mu_pos + 3. * np.sqrt(sigma2_pos / t_pos)
k_ = 500 # number of grid points # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_prior_niw-implementation-step01): Compute the expectation and standard deviations of Sigma2 and M # + exp_sigma2 = invwishart.mean(v_pri, v_pri * sigma2_pri) # expectation std_sigma2 = np.sqrt(invwishart.var(v_pri, v_pri * sigma2_pri)) # std exp_m = mu_pri # expectation std_m = np.sqrt((sigma2_pri / t_pri) * (v_pri / (v_pri - 2.))) # std # - # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_prior_niw-implementation-step02): Compute the marginal pdf of Sigma2 y = np.linspace(0.1, exp_sigma2 + 3 * std_sigma2, k_) # grid f_sigma2 = invwishart.pdf(y, v_pri, v_pri * sigma2_pri) # pdf # ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_prior_niw-implementation-step03): Compute the marginal pdf of M x = np.linspace(exp_m - 3 * std_m, exp_m + 3 * std_m, k_) # grid f_m = t.pdf((x - mu_pri) / np.sqrt(sigma2_pri / t_pri), v_pri) / \ np.sqrt(sigma2_pri / t_pri) # pdf # ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_prior_niw-implementation-step04): Compute the joint pdf of M and Sigma2 f_joint = np.zeros((k_, k_)) for k in range(k_): f_joint[k, :] = norm.pdf(x, mu_pri, np.sqrt(y[k] / t_pri)) * f_sigma2[k] # ## Plots
std_sigma2_hat = np.sqrt(wishart.var(t_ - 1, sigma2_hat / t_)) exp_sigma2_pri = invwishart.mean(v_pri, v_pri * sigma2_pri) std_sigma2_pri = np.sqrt(invwishart.var(v_pri, v_pri * sigma2_pri)) exp_sigma2_pos = invwishart.mean(v_pos, v_pos * sigma2_pos) std_sigma2_pos = np.sqrt(invwishart.var(v_pos, v_pos * sigma2_pos)) # ## [Step 6](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_posterior_niw-implementation-step06): Compute marginal pdfs of the sample, prior and posterior distributions of Sigma2 # + s_max = np.max([exp_sigma2_hat + 2. * std_sigma2_hat, exp_sigma2_pri + 2. * std_sigma2_pri, exp_sigma2_pos + 2. * std_sigma2_pos]) s = np.linspace(0.01, s_max, k_) f_sigma2_hat = wishart.pdf(s, t_ - 1, sigma2_hat / t_) f_sigma2_pri = invwishart.pdf(s, v_pri, v_pri * sigma2_pri) f_sigma2_pos = invwishart.pdf(s, v_pos, v_pos * sigma2_pos) # - # ## [Step 7](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_posterior_niw-implementation-step07): Compute the mean and standard deviations of the sample, prior and posterior distributions of B exp_beta_hat = beta_hat std_beta_hat = np.sqrt(sigma2_hat / (sigma2_zhat * t_)) exp_beta_pri = beta_pri std_beta_pri = np.sqrt(sigma2_pri / (sigma2_zpri * t_pri) * v_pri / (v_pri - 2.)) exp_beta_pos = beta_pos std_beta_pos = np.sqrt(sigma2_pos / (sigma2_zpos * t_pos) * v_pos / (v_pos - 2.)) # ## [Step 8](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_posterior_niw-implementation-step08): Compute marginal pdfs of the sample, prior and posterior distributions of B