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))
# - # ## [Input parameters](https://www.arpm.co/lab/redirect.php?permalink=s_reg_lfm_bayes_prior_niw-parameters) beta_pri = 0.5 # prior location parameter of the loadings sigma2_pri = 3 # prior location parameter of the variance sigma2_zpri = 2 # prior dispersion parameter of the loadings t_pri = 3 # confidence on the prior loadings v_pri = 10 # confidence on the prior variance 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 2](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_posterior_niw-implementation-step02): Compute the parameters of the posterior distribution mu_pos = (t_pri / (t_pri + t_)) * mu_pri + (t_ / (t_pri + t_)) * mu_hat sigma2_pos = (v_pri / (v_pri + t_)) * sigma2_pri + \ (t_ / (v_pri + t_)) * sigma2_hat + \ (mu_pri - mu_hat) ** 2 / ((v_pri + t_) * (1 / t_ + 1 / t_pri)) t_pos = t_pri + t_ v_pos = v_pri + t_ # ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_bayes_posterior_niw-implementation-step03): Compute the mean and standard deviations of the sample, prior and posterior distributions of Sigma2 exp_sigma2_hat = wishart.mean(t_ - 1, sigma2_hat / t_) 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 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