예제 #1
0
def test_log_marg_k():

    np.random.seed(1)

    # Generate data
    D = 10
    N_1 = 10
    X_1 = 5 * np.random.rand(N_1, D) - 1

    # Prior
    var = 10 * np.random.rand(D)
    mu_0 = 5 * np.random.rand(D) - 2
    var_0 = 2 * np.random.rand(D)
    prior = FixedVarPrior(var, mu_0, var_0)
    precision = 1. / var
    precision_0 = 1. / var_0

    # Setup GMM
    assignments = np.concatenate([np.zeros(N_1)])
    gmm = GaussianComponentsFixedVar(X_1, prior, assignments=assignments)

    # Calculate marginal for component by hand
    expected_log_marg = np.sum(
        np.log([
            np.sqrt(var[i]) / (np.sqrt(2 * np.pi * var[i])**N_1 *
                               np.sqrt(N_1 * var_0[i] + var[i])) *
            np.exp(-0.5 * np.square(X_1).sum(axis=0)[i] / var[i] - mu_0[i]**2 /
                   (2 * var_0[i])) * np.exp(
                       (var_0[i] * N_1**2 * X_1.mean(axis=0)[i]**2 / var[i] +
                        var[i] * mu_0[i]**2 / var_0[i] +
                        2 * N_1 * X_1.mean(axis=0)[i] * mu_0[i]) /
                       (2. * (N_1 * var_0[i] + var[i]))) for i in range(D)
        ]))

    npt.assert_almost_equal(gmm.log_marg_k(0), expected_log_marg)
예제 #2
0
def test_log_prod_norm():

    np.random.seed(1)

    # Prior
    D = 10
    var = 1 * np.random.rand(D)
    mu_0 = 5 * np.random.rand(D) - 2
    var_0 = 2 * np.random.rand(D)
    prior = FixedVarPrior(var, mu_0, var_0)

    # GMM will be used to access `_log_prod_norm`
    x = 3 * np.random.rand(D) + 4
    gmm = GaussianComponentsFixedVar(np.array([x]), prior)

    expected_prior = np.sum(
        [log_norm_pdf(x[i], mu_0[i], var_0[i]) for i in range(len(x))])

    npt.assert_almost_equal(gmm.log_prior(0), expected_prior)