Пример #1
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=0.3,
                                       type='chain',
                                       slash=1)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    print(truth)
    dist, sample, _, S = population.generate(n, numpy_like=True)
    #print(S)
    #print(np.array(sample))
    print(sample_mean(np.array(sample)))
    print(sample_cov(np.array(sample)))

    R = np.linalg.inv(S)
    #print(R)
    #print(sample)
    np.random.seed(0)
    model = GraphicalLassoCV()
    model.fit(np.array(sample))
    cov_ = model.covariance_
    prec_ = model.precision_

    heatmap(prec_)

    plt.figure(figsize=(4, 3))
    plt.axes([.2, .15, .75, .7])
    plt.plot(model.cv_alphas_, np.mean(model.grid_scores_, axis=1), 'o-')
    plt.axvline(model.alpha_, color='.5')
    plt.title('Model selection')
    plt.ylabel('Cross-validation score')
    plt.xlabel('alpha')

    plt.show()
    print(model.cv_alphas_, model.grid_scores_)

    model = GraphicalLasso()
    model.fit(sample)
    heatmap(model.precision_, 0.055)

    score = dict()
    score['log_lik'] = []
    score['AIC'] = []
    alpha_list = np.hstack((np.arange(0, 0.1,
                                      0.001), np.arange(0.11, 0.3, 0.01)))
    data = np.array(sample)
    for alpha in alpha_list:
        out_dict = cross_val_score_GLasso(data, alpha=alpha)
        score['log_lik'].append(out_dict['log_lik'])
        score['AIC'].append(out_dict['AIC'])
    plt.plot(alpha_list, score['log_lik'], 'o-')
    plt.show()
    plt.plot(alpha_list, score['AIC'])
    plt.show()
Пример #2
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = 2 * torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=0.3,
                                       type='chain',
                                       slash=1)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    print(truth)
    dist, sample, _, S = population.generate(n, numpy_like=True)
    # print(S)
    # print(np.array(sample))

    R = np.linalg.inv(S)
    # print(R)
    # print(sample)
    np.random.seed(0)

    for alpha in np.arange(0.001, 0.010, 0.001):
        model = LOCOV(alpha=alpha)
        model.fit(S)
        print(model.alpha)
        heatmap(model.prec)

    score = dict()
    score['log_lik'] = []
    score['AIC'] = []
    score['non_zero'] = []
    alpha_list = np.hstack((np.arange(0, 0.1,
                                      0.001), np.arange(0.11, 0.3, 0.01)))
    data = np.array(sample)
    for alpha in alpha_list:
        out_dict = cross_val_score_LOCOV(data, alpha=alpha)
        score['log_lik'].append(out_dict['log_lik'])
        score['AIC'].append(out_dict['AIC'])
        score['non_zero'].append(out_dict['non_zero'])
    plt.plot(alpha_list, score['log_lik'])
    plt.show()
    plt.plot(alpha_list, score['AIC'])
    plt.show()
    plt.plot(alpha_list, score['non_zero'])
    plt.show()
Пример #3
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = 2 * torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=0.3,
                                       type='chain',
                                       slash=1,
                                       cross=7)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    print(truth)
    dist, sample, _, emp_cov = population.generate(n, numpy_like=True)
    print(emp_cov)
    for alpha in np.arange(1e-3, 6e-3, 5e-4):
        model = MarjanovicCoordinateDescent(alpha=alpha)
        model.fit(emp_cov)
        print(alpha)
        heatmap(model.prec)
Пример #4
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean, diag=diag, sub=-0.3, type='chain', slash=1)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    print(truth)
    dist, sample, _, emp_cov = population.generate(n, numpy_like=True)

    model = Glasso(alpha=0.06)
    model.fit(emp_cov)

    heatmap(model.prec)
    #heatmap(np.linalg.inv(model.prec))
    #return

    score = dict()
    score['log_lik'] = []
    score['AIC'] = []
    score['non_zero'] = []
    alpha_list = np.hstack((np.arange(1e-5, 0.1, 0.005), np.arange(0.1, 0.3, 0.01)))
    data = np.array(sample)
    for alpha in alpha_list:
        out_dict = cross_val_score_My_Glasso(data, alpha=alpha)
        score['log_lik'].append(out_dict['log_lik'])
        score['AIC'].append(out_dict['AIC'])
        score['non_zero'].append(out_dict['non_zero'])
    plt.plot(alpha_list, score['log_lik'])
    plt.show()
    plt.plot(alpha_list, score['AIC'])
    plt.show()
    plt.plot(alpha_list, score['non_zero'])
    plt.show()
Пример #5
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = 2 * torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean, diag=diag, sub=0.3, type='chain', slash=1)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    data = pd.read_csv("samples.csv")
    sample = data.iloc[1:, 1:].values

    emp_cov = sample_cov(sample)

    #dist, sample, _, emp_cov = population.generate(n, numpy_like=True)
    model = GD()
    model.fit(emp_cov, alpha=0.05, lr=0.01)
    heatmap(emp_cov)
    heatmap(model.prec)
Пример #6
0
def main():
    mean = torch.tensor(np.ones(16), dtype=torch.float32)
    diag = torch.tensor(np.ones(16), dtype=torch.float32)

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=0.25,
                                       type='chain',
                                       slash=1)
    truth = population.invcov.numpy()
    n = 1000
    d = population.dim

    print(truth)
    dist, sample, _, S = population.generate(n, numpy_like=True)

    # #############################################################################
    # Generate the data
    n_samples = 60
    n_features = 20

    prng = np.random.RandomState(1)
    prec = make_sparse_spd_matrix(n_features,
                                  alpha=.98,
                                  smallest_coef=.4,
                                  largest_coef=.7,
                                  random_state=prng)
    cov = linalg.inv(prec)
    d = np.sqrt(np.diag(cov))
    cov /= d
    cov /= d[:, np.newaxis]
    prec *= d
    prec *= d[:, np.newaxis]
    X = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)
    X -= X.mean(axis=0)
    X /= X.std(axis=0)

    #prec = population.invcov
    # #############################################################################
    # Estimate the covariance
    emp_cov = np.dot(X.T, X) / n_samples

    model = GraphicalLassoCV()
    model.fit(sample)
    cov_ = model.covariance_
    prec_ = model.precision_

    lw_cov_, _ = ledoit_wolf(X)
    lw_prec_ = linalg.inv(lw_cov_)

    # #############################################################################
    # Plot the results
    plt.figure(figsize=(10, 6))
    plt.subplots_adjust(left=0.02, right=0.98)

    # plot the covariances
    covs = [('Empirical', emp_cov), ('Ledoit-Wolf', lw_cov_),
            ('GraphicalLassoCV', cov_), ('True', cov)]
    vmax = cov_.max()
    for i, (name, this_cov) in enumerate(covs):
        plt.subplot(2, 4, i + 1)
        plt.imshow(this_cov,
                   interpolation='nearest',
                   vmin=-vmax,
                   vmax=vmax,
                   cmap=plt.cm.RdBu_r)
        plt.xticks(())
        plt.yticks(())
        plt.title('%s covariance' % name)

    # plot the precisions
    precs = [('Empirical', linalg.inv(emp_cov)), ('Ledoit-Wolf', lw_prec_),
             ('GraphicalLasso', prec_), ('True', prec)]
    vmax = .9 * prec_.max()
    for i, (name, this_prec) in enumerate(precs):
        ax = plt.subplot(2, 4, i + 5)
        plt.imshow(np.ma.masked_equal(this_prec, 0),
                   interpolation='nearest',
                   vmin=-vmax,
                   vmax=vmax,
                   cmap=plt.cm.RdBu_r)
        plt.xticks(())
        plt.yticks(())
        plt.title('%s precision' % name)
        if hasattr(ax, 'set_facecolor'):
            ax.set_facecolor('.7')
        else:
            ax.set_axis_bgcolor('.7')

    # plot the model selection metric
    plt.figure(figsize=(4, 3))
    plt.axes([.2, .15, .75, .7])
    plt.plot(model.cv_alphas_, np.mean(model.grid_scores_, axis=1), 'o-')
    plt.axvline(model.alpha_, color='.5')
    plt.title('Model selection')
    plt.ylabel('Cross-validation score')
    plt.xlabel('alpha')

    plt.show()
Пример #7
0
def main():
    mean = torch.tensor(np.zeros(32), dtype=torch.float32)
    diag = torch.tensor(np.ones(32), dtype=torch.float32)
    X = torch.eye(32, dtype=torch.float32)
    X[5, 10] = X[10, 5] = X[19, 20] = X[20, 19] = X[1, 31] = X[31, 1] = -0.5

    population = Gaussian_Distribution(mean=mean,
                                       diag=diag,
                                       sub=-0.2,
                                       type='DIY',
                                       slash=1,
                                       prec=X)
    truth = population.invcov.numpy()
    n = 400
    p = population.dim

    print(truth)
    heatmap(truth)

    data = pd.read_csv("chain.csv")
    sample = data.values[1:, 1:]
    sample = z_score(sample)
    emp_cov = sample_cov(sample)

    score = dict()
    score['log_lik'] = []
    score['AIC'] = []
    score['non_zero'] = []
    alpha_list = np.hstack((np.arange(1e-5, 0.1,
                                      0.002), np.arange(0.1, 0.3, 0.01)))
    data = np.array(sample)
    for alpha in alpha_list:
        out_dict = cross_val_score_ProxGrad(data, alpha=alpha, type='FISTA')
        score['log_lik'].append(out_dict['log_lik'])
        score['AIC'].append(out_dict['AIC'])
        score['non_zero'].append(out_dict['non_zero'])
    plt.plot(alpha_list, score['log_lik'])
    plt.show()
    plt.plot(alpha_list, score['AIC'])
    plt.show()
    plt.plot(alpha_list, score['non_zero'])
    plt.show()

    model = ProxGrad()
    l = len(alpha_list)
    alpha = 0
    log_lik = -1e12
    for i in range(0, l):
        if score['log_lik'][i] > log_lik:
            alpha = alpha_list[i]
            log_lik = score['log_lik'][i]
    print(alpha)
    prec = model.fit_FISTA(emp_cov, alpha)
    heatmap(prec)
    print('nonzero:', L0_penal(prec))

    alpha = 0
    aic = 1e12
    for i in range(0, l):
        if score['AIC'][i] < aic:
            alpha = alpha_list[i]
            aic = score['AIC'][i]
    print(alpha)
    prec = model.fit_FISTA(emp_cov, alpha)
    heatmap(prec)
    print('nonzero:', L0_penal(prec))

    model = GraphicalLassoCV(tol=1e-8)
    model.fit(sample)
    heatmap(model.precision_)
    print('nonzero:', L0_penal(model.precision_))