Exemple #1
0
def main():

    import random

    from niw import NIW

    logging.basicConfig(level=logging.INFO)

    random.seed(1)
    np.random.seed(1)

    # Data parameters
    D = 2           # dimensions
    N = 20         # number of points to generate
    K_true = 4      # the true number of components

    # Model parameters
    alpha = 1.
    K = 3           # initial number of components
    n_iter = 20

    # Generate data
    mu_scale = 4.0
    covar_scale = 0.7
    z_true = np.random.randint(0, K_true, N)
    mu = np.random.randn(D, K_true)*mu_scale
    X = mu[:, z_true] + np.random.randn(D, N)*covar_scale
    X = X.T

    # Intialize prior
    m_0 = np.zeros(D)
    k_0 = covar_scale**2/mu_scale**2
    v_0 = D + 3
    S_0 = covar_scale**2*v_0*np.eye(D)
    prior = NIW(m_0, k_0, v_0, S_0)

    # Setup IGMM
    igmm = IGMM(X, prior, alpha, assignments="rand", K=K)
    # igmm = IGMM(X, prior, alpha, assignments="each-in-own")
    # igmm = IGMM(X, prior, alpha, assignments="one-by-one", K=K)

    # Perform Gibbs sampling
    logger.info("Initial log marginal prob: " + str(igmm.log_marg()))
    record = igmm.gibbs_sample(n_iter)
    logger.info("Assignments: " + str(igmm.components.assignments))
Exemple #2
0
def main():

    from niw import NIW


    # LOG POSTERIOR EXAMPLE

    prior = NIW(m_0=np.array([0.5, -0.1, 0.1]), k_0=2.0, v_0=5.0, S_0=5.0*np.eye(3))
    gmm = GaussianComponents(np.array([
        [1.2, 0.9, 0.2],
        [-0.1, 0.8, -0.2],
        [0.5, 0.4, 0.3]
        ]), prior)
    gmm.add_item(0, 0)
    gmm.add_item(1, 0)
    print "Log prior of [0.5, 0.4, 0.3]:", gmm.log_prior(2)
    print "Log posterior of [0.5, 0.4, 0.3]:", gmm.log_post_pred_k(2, 0)
    print


    # ADDING AND REMOVING DATA VECTORS EXAMPLE

    prior = NIW(m_0=np.array([0.0, 0.0]), k_0=2.0, v_0=5.0, S_0=5.0*np.eye(2))
    gmm = GaussianComponents(np.array([
        [1.2, 0.9],
        [-0.1, 0.8],
        [0.5, 0.4]
        ]), prior)
    print "Log prior of [1.2, 0.9]:", gmm.log_prior(0)
    gmm.add_item(0, 0)
    gmm.add_item(1, 0)
    print "Log posterior of [0.5, 0.4]:", gmm.log_post_pred_k(2, 0)
    gmm.add_item(2, 0)
    print "Log posterior of [0.5, 0.4] after adding:", gmm.log_post_pred_k(2, 0)
    gmm.del_item(2)
    print "Log posterior of [0.5, 0.4] after removing:", gmm.log_post_pred_k(2, 0)
    print


    # LOG MARGINAL EXAMPLE

    # Data
    X = np.array([
        [-0.3406, -0.3593, -0.0686],
        [-0.3381, 0.2993, 0.925],
        [-0.5, -0.101, 0.75]
        ])
    N, D = X.shape

    # Setup densities
    m_0 = np.zeros(D)
    k_0 = 0.05
    v_0 = D + 3
    S_0 = 0.5*np.eye(D)
    prior = NIW(m_0, k_0, v_0, S_0)
    gmm = GaussianComponents(X, prior, [0, 0, 0])

    # Calculate log marginal of data
    log_marg = gmm.log_marg_k(0)
    print "Log marginal:", gmm.log_marg_k(0)
    print


    # HIGHER DIMENSIONAL EXAMPLE

    # Data generated with np.random.seed(2); np.random.rand(11, 4)
    X = np.array([
        [ 0.4359949 ,  0.02592623,  0.54966248,  0.43532239],
        [ 0.4203678 ,  0.33033482,  0.20464863,  0.61927097],
        [ 0.29965467,  0.26682728,  0.62113383,  0.52914209],
        [ 0.13457995,  0.51357812,  0.18443987,  0.78533515],
        [ 0.85397529,  0.49423684,  0.84656149,  0.07964548],
        [ 0.50524609,  0.0652865 ,  0.42812233,  0.09653092],
        [ 0.12715997,  0.59674531,  0.226012  ,  0.10694568],
        [ 0.22030621,  0.34982629,  0.46778748,  0.20174323],
        [ 0.64040673,  0.48306984,  0.50523672,  0.38689265],
        [ 0.79363745,  0.58000418,  0.1622986 ,  0.70075235],
        [ 0.96455108,  0.50000836,  0.88952006,  0.34161365]
        ])
    N, D = X.shape

    # Setup densities
    m_0 = X.mean(axis=0)
    k_0 = 0.05
    v_0 = D + 10
    S_0 = 0.5*np.eye(D)
    prior = NIW(m_0, k_0, v_0, S_0)
    gmm = GaussianComponents(X, prior, [0, 0, 0, 1, 0, 1, 3, 4, 3, 2, -1])

    print "Consider vector:", X[10]
    print "Log post predictive:", log_post_pred_unvectorized(gmm, 10)
    print "Log post predictive:", gmm.log_post_pred(10)
    print "Log marginal for component 0:", gmm.log_marg_k(0)
Exemple #3
0
def main():

    from niw import NIW

    # logging.basicConfig(level=logging.DEBUG)

    # LOG POSTERIOR EXAMPLE

    # Prior
    D = 3
    m_0 = np.array([0.5, -0.1, 0.1])
    k_0 = 2.0
    S_0 = 5.0 * np.ones(D)
    v_0 = 5
    prior = NIW(m_0=m_0, k_0=k_0, v_0=v_0, S_0=S_0)

    # Data
    X = np.array([[0.5, 0.4, 0.3], [1.2, 0.9, 0.2], [-0.1, 0.8, -0.2],
                  [0.0, 0.5, -1.0]])
    x = X[0]

    # Setup single-component model
    gmm = GaussianComponentsDiag(X, prior)
    N, _ = X.shape
    for i in range(N):
        gmm.add_item(i, 0)

    # Calculate posterior by hand
    k_N = k_0 + N
    v_N = v_0 + N
    m_N = (k_0 * m_0 + N * X[:N].mean(axis=0)) / k_N
    S_N = S_0 + np.square(
        X[:N]).sum(axis=0) + k_0 * np.square(m_0) - k_N * np.square(m_N)
    var = S_N * (k_N + 1) / (k_N * v_N)

    print("Log posterior of " + str(x) + ":", gmm.log_post_pred_k(0, 0))
    print("Log posterior of " + str(x) + ": " + str(
        np.sum([
            students_t(x[i], m_N[i], S_N[i] * (k_N + 1) / (k_N * v_N), v_N)
            for i in range(len(x))
        ])))
    print()

    # MULTIPLE COMPONENT EXAMPLE

    # Data generated with np.random.seed(2); np.random.rand(11, 4)
    X = np.array([[0.4359949, 0.02592623, 0.54966248, 0.43532239],
                  [0.4203678, 0.33033482, 0.20464863, 0.61927097],
                  [0.29965467, 0.26682728, 0.62113383, 0.52914209],
                  [0.13457995, 0.51357812, 0.18443987, 0.78533515],
                  [0.85397529, 0.49423684, 0.84656149, 0.07964548],
                  [0.50524609, 0.0652865, 0.42812233, 0.09653092],
                  [0.12715997, 0.59674531, 0.226012, 0.10694568],
                  [0.22030621, 0.34982629, 0.46778748, 0.20174323],
                  [0.64040673, 0.48306984, 0.50523672, 0.38689265],
                  [0.79363745, 0.58000418, 0.1622986, 0.70075235],
                  [0.96455108, 0.50000836, 0.88952006, 0.34161365]])
    N, D = X.shape

    # Setup densities
    m_0 = X.mean(axis=0)
    k_0 = 0.05
    v_0 = D + 10
    S_0 = 0.5 * np.ones(D)
    prior = NIW(m_0, k_0, v_0, S_0)
    gmm = GaussianComponentsDiag(X, prior, [0, 0, 0, 1, 0, 1, 3, 4, 3, 2, -1])

    print("Consider vector:", X[10])
    print("Log post predictive:", log_post_pred_unvectorized(gmm, 10))
    print("Log post predictive:", gmm.log_post_pred(10))
    print("Log marginal for component 0:", gmm.log_marg_k(0))