Exemple #1
0
def init_params(y, N_data, K_clusters, D_data, tol,
                max_iter):  #initialize parameters for FI-NPL by picking MLE
    R_restarts = 10

    pi_bb = np.zeros((R_restarts, K_clusters))  #mixing weights
    mu_bb = np.zeros((R_restarts, K_clusters, D_data))  #means
    sigma_bb = np.zeros((R_restarts, K_clusters, D_data))  #covariances
    ll_bb = np.zeros(R_restarts)

    #Initialize parameters randomly
    pi_init = np.random.dirichlet(np.ones(K_clusters), R_restarts)
    mu_init = 8 * np.random.rand(R_restarts, K_clusters, D_data) - 2
    sigma_init = invgamma.rvs(1, size=(R_restarts, K_clusters, D_data))

    for i in range(R_restarts):
        model = skgm.GaussianMixture(K_clusters,
                                     covariance_type='diag',
                                     means_init=mu_init[i],
                                     weights_init=pi_init[i],
                                     precisions_init=1 / sigma_init[i],
                                     tol=tol,
                                     max_iter=max_iter)
        model.fit(y, np.ones(N_data))
        pi_bb[i] = model.weights_
        mu_bb[i] = model.means_
        sigma_bb[i] = np.sqrt(model.covariances_)
        ll_bb[i] = model.score(y, np.ones(N_data)) * N_data
    ind = np.argmax(ll_bb)
    return pi_bb[ind], mu_bb[ind], sigma_bb[ind]
Exemple #2
0
def maximise_mle(y, weights, pi_init, mu_init, sigma_init, K_clusters, tol,
                 max_iter, N_data):  #maximization for FI-NPL
    model = skgm.GaussianMixture(K_clusters,
                                 covariance_type='diag',
                                 means_init=mu_init,
                                 weights_init=pi_init,
                                 precisions_init=1 / sigma_init,
                                 tol=tol,
                                 max_iter=max_iter)
    model.fit(y, N_data * weights)
    pi_bb = model.weights_
    mu_bb = model.means_
    sigma_bb = np.sqrt(model.covariances_)
    return pi_bb, mu_bb, sigma_bb
Exemple #3
0
def lppd(y, pi, mu, sigma, K):  #calculate posterior predictive of test
    model = skgm.GaussianMixture(K, covariance_type='diag')
    B = np.shape(mu)[0]
    N_test = np.shape(y)[0]
    ll_test = np.zeros((B, N_test))
    model.fit(y, np.ones(N_test))
    for i in range(B):
        model.means_ = mu[i, :]
        model.covariances_ = sigma[i, :]**2
        model.precisions_ = 1 / (sigma[i, :]**2)
        model.weights_ = pi[i, :]
        model.precisions_cholesky_ = _compute_precision_cholesky(
            model.covariances_, model.covariance_type)
        ll_test[i] = model.score_lppd(y)
    lppd_test = np.sum(sp.special.logsumexp(ll_test, axis=0) - np.log(B))
    return lppd_test
def IS_lppd(y, pi, mu, sigma, K,
            logweights_is):  #calculate posterior predictive of test
    model = skgm.GaussianMixture(K, covariance_type='diag')
    B = np.shape(mu)[0]
    N_test = np.shape(y)[0]
    ll_test = np.zeros(N_test)
    model.fit(y, np.ones(N_test))

    for i in tqdm(range(B)):
        model.means_ = mu[i, :, :]
        model.covariances_ = sigma[i, :]**2
        model.precisions_ = 1 / (sigma[i, :]**2)
        model.weights_ = pi[i, :]
        model.precisions_cholesky_ = _compute_precision_cholesky(
            model.covariances_, model.covariance_type)
        if i == 0:
            ll_test = model.score_lppd(y) + logweights_is[i]
        else:
            ll_test = np.logaddexp(ll_test,
                                   model.score_lppd(y) + logweights_is[i])
    lppd_test = np.sum(ll_test)
    return lppd_test
Exemple #5
0
def maximise(y,
             y_prior,
             weights,
             pi_init,
             mu_init,
             sigma_init,
             alph_conc,
             T_trunc,
             K_clusters,
             tol,
             max_iter,
             R_restarts,
             N_data,
             D_data,
             postsamples=None):  #maximization for RR-NPL
    if alph_conc != 0:
        y_tot = np.concatenate((y, y_prior))
    else:
        y_tot = y
    pi_bb = np.zeros((R_restarts, K_clusters))  #mixing weights
    mu_bb = np.zeros((R_restarts, K_clusters, D_data))  #means
    sigma_bb = np.zeros((R_restarts, K_clusters, D_data))  #covariances
    ll_bb = np.zeros(R_restarts)
    n_tot = np.shape(y_tot)[0]
    for i in range(R_restarts):
        model = skgm.GaussianMixture(K_clusters,
                                     covariance_type='diag',
                                     means_init=mu_init[i],
                                     weights_init=pi_init[i],
                                     precisions_init=1 / sigma_init[i],
                                     tol=tol,
                                     max_iter=max_iter)
        model.fit(y_tot, n_tot * weights)
        pi_bb[i] = model.weights_
        mu_bb[i] = model.means_
        sigma_bb[i] = np.sqrt(model.covariances_)
        ll_bb[i] = model.score(y_tot, weights) * n_tot
    ind = np.argmax(ll_bb)
    return pi_bb[ind], mu_bb[ind], sigma_bb[ind]