def test_frozen_matrix_normal(self): for i in range(1, 5): for j in range(1, 5): M = 0.3 * np.ones((i, j)) U = 0.5 * np.identity(i) + 0.5 * np.ones((i, i)) V = 0.7 * np.identity(j) + 0.3 * np.ones((j, j)) frozen = matrix_normal(mean=M, rowcov=U, colcov=V) rvs1 = frozen.rvs(random_state=1234) rvs2 = matrix_normal.rvs(mean=M, rowcov=U, colcov=V, random_state=1234) assert_equal(rvs1, rvs2) X = frozen.rvs(random_state=1234) pdf1 = frozen.pdf(X) pdf2 = matrix_normal.pdf(X, mean=M, rowcov=U, colcov=V) assert_equal(pdf1, pdf2) logpdf1 = frozen.logpdf(X) logpdf2 = matrix_normal.logpdf(X, mean=M, rowcov=U, colcov=V) assert_equal(logpdf1, logpdf2)
def pdf(x, cov_scale=0.25): # added trace mu01 = np.array([[-0.5, 0.5]]) mu02 = np.array([[0.5, -0.5]]) mu11 = np.array([[0.5, 0.5]]) mu12 = np.array([[-0.5, -0.5]]) cov = cov_scale * np.eye(2) inv_cov = np.linalg.inv(cov) p01 = matrix_normal.pdf(x, mean=mu01, colcov=cov) p02 = matrix_normal.pdf(x, mean=mu02, colcov=cov) p11 = matrix_normal.pdf(x, mean=mu11, colcov=cov) p12 = matrix_normal.pdf(x, mean=mu12, colcov=cov) # return np.max([p01,p02])/(p01+p02+p11+p12) return (p01 + p02) / (p01 + p02 + p11 + p12)
def matrix_normal_testcase(): from scipy.stats import matrix_normal M = np.arange(6).reshape(3, 2) U = np.diag([1, 2, 3]) V = 0.3 * np.identity(2) X = M + 0.1 logging.debug(matrix_normal.pdf(X, mean=M, rowcov=U, colcov=V)) vectorised_X = X.T.flatten() equiv_mean = M.T.flatten() equiv_cov = np.kron(V, U) mn_pdf = multivariate_normal.pdf(vectorised_X, mean=equiv_mean, cov=equiv_cov) logging.debug(mn_pdf)
def test_frozen_matrix_normal(self): for i in range(1,5): for j in range(1,5): M = 0.3 * np.ones((i,j)) U = 0.5 * np.identity(i) + 0.5 * np.ones((i,i)) V = 0.7 * np.identity(j) + 0.3 * np.ones((j,j)) frozen = matrix_normal(mean=M, rowcov=U, colcov=V) rvs1 = frozen.rvs(random_state=1234) rvs2 = matrix_normal.rvs(mean=M, rowcov=U, colcov=V, random_state=1234) assert_equal(rvs1, rvs2) X = frozen.rvs(random_state=1234) pdf1 = frozen.pdf(X) pdf2 = matrix_normal.pdf(X, mean=M, rowcov=U, colcov=V) assert_equal(pdf1, pdf2) logpdf1 = frozen.logpdf(X) logpdf2 = matrix_normal.logpdf(X, mean=M, rowcov=U, colcov=V) assert_equal(logpdf1, logpdf2)