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
def wishart(covariance,mean,df):
    if isinstance(covariance, int) or isinstance(covariance, float):
        return chi2.pdf(covariance, scale=mean/df, df=df)
    return scip_wishart.pdf(covariance, scale=mean/df, df=df)
Example #4
0
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
# -

# ## [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),
Example #5
0
import matplotlib.pyplot as plt
from scipy.stats import wishart, chi2
x = np.linspace(1e-5, 8, 100)
w = wishart.pdf(x, df=3, scale=1)
w[:5]
# array([ 0.00126156,  0.10892176,  0.14793434,  0.17400548,  0.1929669 ])
c = chi2.pdf(x, 3)
c[:5]
# array([ 0.00126156,  0.10892176,  0.14793434,  0.17400548,  0.1929669 ])
plt.plot(x, w)

# The input quantiles can be any shape of array, as long as the last
# axis labels the components.
Example #6
0
def g0(theta, mu0, beta, nu, S):
    """(12.32) の式"""
    mu, Lambda = theta
    return multivariate_normal.pdf(mu, mu0, inv(beta * Lambda)) * wishart.pdf(
        Lambda, nu, S)