Example #1
0
    def test_frozen(self):
        # Test that the frozen and non-frozen 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:
            w = wishart(df, scale)
            assert_equal(w.var(), wishart.var(df, scale))
            assert_equal(w.mean(), wishart.mean(df, scale))
            assert_equal(w.mode(), wishart.mode(df, scale))
            assert_equal(w.entropy(), wishart.entropy(df, scale))
            assert_equal(w.pdf(x), wishart.pdf(x, df, scale))
Example #2
0
    def test_frozen(self):
        # Test that the frozen and non-frozen 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:
            w = wishart(df, scale)
            assert_equal(w.var(), wishart.var(df, scale))
            assert_equal(w.mean(), wishart.mean(df, scale))
            assert_equal(w.mode(), wishart.mode(df, scale))
            assert_equal(w.entropy(), wishart.entropy(df, scale))
            assert_equal(w.pdf(x), wishart.pdf(x, df, scale))
Example #3
0
epsi = norm.rvs(mu, np.sqrt(sigma2), t_)
mu_hat = np.mean(epsi)
sigma2_hat = np.var(epsi)

# ## [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