Example #1
0
def main():

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

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

    # Sampling parameters
    n_runs = 2
    n_iter = 12

    # 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 FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, assignments=z)
    print("Initial log marginal prob:", fbgmm.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 = fbgmm.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, fbgmm)
    for k in range(fbgmm.components.K):
        mu, sigma = fbgmm.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()
Example #2
0
def main():

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

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

    # Sampling parameters
    n_runs = 2
    n_iter = 12

    # 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 FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, assignments=z)
    print("Initial log marginal prob:", fbgmm.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 = fbgmm.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, fbgmm)
    for k in range(fbgmm.components.K):
        mu, sigma = fbgmm.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()
Example #3
0
def gmm(X,
        K=4,
        n_iter=100,
        alpha=1.0,
        mu_scale=4.0,
        var_scale=0.5,
        covar_scale=0.7,
        posterior_predictive_check=False):
    N, D = X.shape

    # Initialize 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.ones(D)
    prior = NIW(m_0, k_0, v_0, S_0)

    # Setup FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand")

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

    K = fbgmm.components.K

    mus = np.zeros(shape=(K, D))
    covars = [np.zeros((D, D)) for i in range(0, K)]
    for k in range(fbgmm.components.K):
        mu, var = fbgmm.components.rand_k(k)
        mus[k, :] = mu
        covars[k] = np.diag(var)

    # Generate new points for posterior predictive check
    # Generate the same number of points as N
    if posterior_predictive_check:
        np.random.seed(1)
        rstate = 1
        alphas = (alpha / K) + fbgmm.components.counts
        pis = dirichlet.rvs(alphas, random_state=rstate)[0]
        Z = np.zeros(N, dtype=np.uint32)
        X = np.zeros((N, D))
        for n in range(N):
            Z[n] = np.floor(np.argmax(multinomial(1, pis)))
            X[n] = multivariate_normal.rvs(mean=mus[Z[n]], cov=covars[Z[n]])

        return fbgmm.components.assignments, mus, (X, Z)

    return fbgmm.components.assignments, mus
Example #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           # 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 = D + 3
    S_0 = covar_scale**2*v_0*np.eye(D)
    prior = NIW(m_0, k_0, v_0, S_0)

    # Setup FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand")

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

    assignments_expected = np.array([
        0, 2, 0, 0, 2, 0, 2, 2, 2, 0, 0, 0, 0, 2, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0,
        2, 0, 1, 0, 2, 1, 1, 0, 2, 2, 0, 0, 2, 1, 0, 1, 0, 0, 0, 2, 2, 0, 1, 0,
        0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 0, 2, 0, 0, 0, 2,
        0, 1, 0, 0, 0, 2, 2, 1, 2, 0, 0, 0, 2, 1, 2, 2, 1, 0, 0, 1, 0, 2, 2, 1,
        2, 0, 0, 2
        ])
    assignments = fbgmm.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 = 4           # 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 = 0.5  # if you make this really small, you basically get k-means
    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 FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand", covariance_type="fixed")

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

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, fbgmm)
    for k in range(fbgmm.components.K):
        mu = fbgmm.components.rand_k(k)
        sigma = np.diag(var)
        plot_ellipse(ax, mu, sigma)
    plt.show()
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 = 4  # 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 = 0.5  # if you make this really small, you basically get k-means
    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 FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand", covariance_type="fixed")

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

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

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

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

    # Model parameters
    alpha = 1.
    K = 6           # 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

    # 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 FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand")

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

    expected_log_marg = -60.1448630929
    log_marg = fbgmm.log_marg()

    print(fbgmm.components.assignments)

    npt.assert_almost_equal(log_marg, expected_log_marg)
Example #8
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 = 4  # 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 FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand")

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

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, fbgmm)
    for k in range(fbgmm.components.K):
        # mu, sigma = fbgmm.components.map(k)
        mu, sigma = fbgmm.components.rand_k(k)
        plot_ellipse(ax, mu, sigma)
    plt.show()
Example #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.0
    K = 4  # 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.ones(D)
    prior = NIW(m_0, k_0, v_0, S_0)

    # Setup FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand", covariance_type="diag")

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

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