def main():

    # Load data
    X = pickle.load(open(data_fn, "rb"))
    N, D = X.shape

    # Model parameters
    alpha = 1.
    K = 2  # initial number of components
    mu_scale = 3.0
    covar_scale = 1.0

    # Sampling parameters
    n_runs = 1
    n_iter = 100

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

    #  Initialize component assignment: this is not random for testing purposes
    z = np.array([i * np.ones(N / K) for i in range(K)],
                 dtype=np.int).flatten()

    # Setup IGMM
    igmm = IGMM(X, prior, alpha, assignments=z)
    print "Initial log marginal prob:", igmm.log_marg()

    # Perform several Gibbs sampling runs and average the log marginals
    log_margs = np.zeros(n_iter)
    for j in range(n_runs):
        # Perform Gibbs sampling
        record = igmm.gibbs_sample(n_iter)
        log_margs += record["log_marg"]
    log_margs /= n_runs

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, igmm)
    for k in xrange(igmm.components.K):
        mu, sigma = igmm.components.rand_k(k)
        plot_ellipse(ax, mu, sigma)

    # Plot log probability
    plt.figure()
    plt.plot(range(n_iter), log_margs)
    plt.xlabel("Iterations")
    plt.ylabel("Log prob")

    plt.show()
Beispiel #2
0
def main():

    # Load data
    X = pickle.load(open(data_fn, "rb"))
    N, D = X.shape

    # Model parameters
    alpha = 1.
    K = 2  # initial number of components
    mu_scale = 3.0
    covar_scale = 1.0

    # Sampling parameters
    n_runs = 1
    n_iter = 100

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

    #  Initialize component assignment: this is not random for testing purposes
    z = np.array([i*np.ones(N/K) for i in range(K)], dtype=np.int).flatten()
    
    # Setup IGMM
    igmm = IGMM(X, prior, alpha, assignments=z)
    print("Initial log marginal prob:", igmm.log_marg())

    # Perform several Gibbs sampling runs and average the log marginals
    log_margs = np.zeros(n_iter)
    for j in range(n_runs):
        # Perform Gibbs sampling
        record = igmm.gibbs_sample(n_iter)
        log_margs += record["log_marg"]
    log_margs /= n_runs

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, igmm)
    for k in range(igmm.components.K):
        mu, sigma = igmm.components.rand_k(k)
        plot_ellipse(ax, mu, sigma)

    # Plot log probability
    plt.figure()
    plt.plot(list(range(n_iter)), log_margs)
    plt.xlabel("Iterations")
    plt.ylabel("Log prob")

    plt.show()
Beispiel #3
0
def main():

    # Data parameters
    D = 2  # dimensions
    N = 100  # 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
    var_scale = 2.
    mu_0 = np.zeros(D)
    k_0 = covar_scale**2 / mu_scale**2
    var = covar_scale**2 * np.ones(D) * var_scale
    var_0 = var / k_0
    prior = FixedVarPrior(var, mu_0, var_0)

    # Setup IGMM
    igmm = IGMM(X,
                prior,
                alpha,
                assignments="rand",
                K=K,
                covariance_type="fixed")

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, igmm)
    for k in range(igmm.components.K):
        mu = igmm.components.rand_k(k)
        sigma = np.diag(var)
        plot_ellipse(ax, mu, sigma)
    plt.show()
Beispiel #4
0
def test_sampling_2d_assignments():

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

    # Data parameters
    D = 2           # dimensions
    N = 100         # 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 = 10

    # 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 = 5
    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, K=K)

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    assignments_expected = np.array([
        1, 2, 0, 0, 2, 1, 2, 1, 2, 0, 0, 1, 0, 2, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0,
        2, 0, 1, 0, 1, 1, 1, 0, 2, 2, 1, 1, 2, 1, 0, 1, 1, 1, 1, 2, 2, 1, 1, 1,
        1, 0, 0, 1, 0, 0, 1, 2, 2, 1, 1, 0, 1, 2, 2, 1, 1, 1, 1, 2, 0, 0, 1, 2,
        0, 1, 0, 0, 1, 2, 1, 1, 2, 0, 0, 1, 2, 1, 2, 2, 1, 1, 0, 1, 1, 2, 2, 1,
        2, 1, 0, 2
        ])
    assignments = igmm.components.assignments

    npt.assert_array_equal(assignments, assignments_expected)
Beispiel #5
0
def test_sampling_2d_assignments():

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

    # Data parameters
    D = 2  # dimensions
    N = 100  # 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 = 10

    # 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 = 5
    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, K=K)

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    assignments_expected = np.array([
        1, 2, 0, 0, 2, 1, 2, 1, 2, 0, 0, 1, 0, 2, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0,
        2, 0, 1, 0, 1, 1, 1, 0, 2, 2, 1, 1, 2, 1, 0, 1, 1, 1, 1, 2, 2, 1, 1, 1,
        1, 0, 0, 1, 0, 0, 1, 2, 2, 1, 1, 0, 1, 2, 2, 1, 1, 1, 1, 2, 0, 0, 1, 2,
        0, 1, 0, 0, 1, 2, 1, 1, 2, 0, 0, 1, 2, 1, 2, 2, 1, 1, 0, 1, 1, 2, 2, 1,
        2, 1, 0, 2
    ])
    assignments = igmm.components.assignments

    npt.assert_array_equal(assignments, assignments_expected)
def main():

    # Data parameters
    D = 2           # dimensions
    N = 100         # 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
    var_scale = 2.
    mu_0 = np.zeros(D)
    k_0 = covar_scale**2/mu_scale**2
    var = covar_scale**2*np.ones(D)*var_scale
    var_0 = var/k_0
    prior = FixedVarPrior(var, mu_0, var_0)

    # Setup IGMM
    igmm = IGMM(X, prior, alpha, assignments="rand", K=K, covariance_type="fixed")

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, igmm)
    for k in xrange(igmm.components.K):
        mu = igmm.components.rand_k(k)
        sigma = np.diag(var)
        plot_ellipse(ax, mu, sigma)
    plt.show()
Beispiel #7
0
def test_sampling_2d_log_marg_deleted_components():

    random.seed(2)
    np.random.seed(2)

    # Data parameters
    D = 2           # dimensions
    N = 5           # 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 = 1

    # 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
    print X

    # 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="each-in-own")

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)
    print igmm.components.assignments

    expected_log_marg = -30.771535771
    log_marg = igmm.log_marg()

    npt.assert_almost_equal(log_marg, expected_log_marg)
Beispiel #8
0
def test_sampling_2d_log_marg_deleted_components():

    random.seed(2)
    np.random.seed(2)

    # Data parameters
    D = 2  # dimensions
    N = 5  # 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 = 1

    # 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
    print(X)

    # 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="each-in-own")

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)
    print(igmm.components.assignments)

    expected_log_marg = -30.771535771
    log_marg = igmm.log_marg()

    npt.assert_almost_equal(log_marg, expected_log_marg)
Beispiel #9
0
def main():

    # Data parameters
    D = 2           # dimensions
    N = 100         # 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="one-by-one", K=K)

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, igmm)
    for k in range(igmm.components.K):
        mu, sigma = igmm.components.rand_k(k)
        plot_ellipse(ax, mu, sigma)
    plt.show()
Beispiel #10
0
def main():

    # Data parameters
    D = 2           # dimensions
    N = 100         # 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="one-by-one", K=K)

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, igmm)
    for k in xrange(igmm.components.K):
        mu, sigma = igmm.components.rand_k(k)
        plot_ellipse(ax, mu, sigma)
    plt.show()
Beispiel #11
0
def test_sampling_2d_log_marg():

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

    # Data parameters
    D = 2           # dimensions
    N = 100         # 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 = 10

    # 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 = 5
    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, K=K)

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    expected_log_marg = -411.811711231
    log_marg = igmm.log_marg()

    npt.assert_almost_equal(log_marg, expected_log_marg)
Beispiel #12
0
def test_sampling_2d_log_marg():

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

    # Data parameters
    D = 2  # dimensions
    N = 100  # 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 = 10

    # 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 = 5
    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, K=K)

    # Perform Gibbs sampling
    record = igmm.gibbs_sample(n_iter)

    expected_log_marg = -411.811711231
    log_marg = igmm.log_marg()

    npt.assert_almost_equal(log_marg, expected_log_marg)