Пример #1
0
def test_scaling_SGL():
    
    Sigma, Theta = group_power_network(p, K = 1, M = 2)
    S, samples = sample_covariance_matrix(Sigma, N); S = S[0,:,:]  
    
    S2 = 10*S
    reg_params = {'lambda1': 0.01}
    reg_params2 = {'lambda1': 0.1}
    
    P = glasso_problem(S = S2, N = N, reg = None, latent = False, do_scaling = True)
    P.set_reg_params(reg_params)
    P.solve(tol = 1e-10, rtol = 1e-15)
    
    solver_params = {'rho': 10}
        
    P2 = glasso_problem(S = S2, N = N, reg = None, latent = False, do_scaling = False)
    P2.set_reg_params(reg_params2)
    P2.solve(tol = 1e-10, rtol = 1e-15, solver_params = solver_params)    
    
    
    Theta = P.solution.precision_
    Theta2 = P2.solution.precision_
    
    assert np.linalg.norm(Theta - Theta2)/np.linalg.norm(Theta) <= 0.02
    
    return
    
Пример #2
0
def test_GGL_ext_latent():
    Sigma, Theta = group_power_network(p, K, M)
    S, samples = sample_covariance_matrix(Sigma, N)
    
    Sdict = dict()
    for k in np.arange(K):
        Sdict[k] = S[k,:,:].copy()
        
    G = construct_trivial_G(p, K)
    template_problem_MGL(S, N, reg = 'GGL', latent = True, G = G)
    return
Пример #3
0
def template_admm_vs_ppdna(p=50, K=3, N=1000, reg="GGL"):
    M = 5  # M should be divisor of p

    if reg == 'GGL':
        Sigma, Theta = group_power_network(p, K, M)
    elif reg == 'FGL':
        Sigma, Theta = time_varying_power_network(p, K, M)

    S, samples = sample_covariance_matrix(Sigma, N)

    lambda1 = 0.05
    lambda2 = 0.01

    Omega_0 = get_K_identity(K, p)

    sol, info = ADMM_MGL(S,
                         lambda1,
                         lambda2,
                         reg,
                         Omega_0,
                         stopping_criterion='kkt',
                         tol=1e-6,
                         rtol=1e-5,
                         verbose=True,
                         latent=False)

    sol2, info2 = warmPPDNA(S,
                            lambda1,
                            lambda2,
                            reg,
                            Omega_0,
                            eps=1e-6,
                            verbose=False,
                            measure=True)

    sol3, info3 = PPDNA(S,
                        lambda1,
                        lambda2,
                        reg,
                        Omega_0,
                        eps_ppdna=1e-6,
                        verbose=True,
                        measure=True)

    assert_array_almost_equal(sol['Theta'], sol2['Theta'], 2)
    assert_array_almost_equal(sol2['Theta'], sol3['Theta'], 2)

    return
Пример #4
0
def template_ADMM_MGL(p=100, K=5, N=1000, reg='GGL', latent=False):
    """
    template for test for ADMM MGL solver with conforming variables
    """
    M = 10  # M should be divisor of p

    if reg == 'GGL':
        Sigma, Theta = group_power_network(p, K, M)
    elif reg == 'FGL':
        Sigma, Theta = time_varying_power_network(p, K, M)

    S, samples = sample_covariance_matrix(Sigma, N)

    lambda1 = 0.05
    lambda2 = 0.01

    Omega_0 = get_K_identity(K, p)

    sol, info = ADMM_MGL(S,
                         lambda1,
                         lambda2,
                         reg,
                         Omega_0,
                         tol=1e-5,
                         rtol=1e-5,
                         verbose=True,
                         measure=True,
                         latent=latent,
                         mu1=0.01)
    assert info['status'] == 'optimal'

    _, info2 = ADMM_MGL(S,
                        lambda1,
                        lambda2,
                        reg,
                        Omega_0,
                        tol=1e-20,
                        rtol=1e-20,
                        max_iter=2,
                        latent=latent,
                        mu1=0.01)
    assert info2['status'] == 'max iterations reached'

    return
Пример #5
0
def test_GGL_latent():
    Sigma, Theta = group_power_network(p, K, M)    
    S, samples = sample_covariance_matrix(Sigma, N)
    template_problem_MGL(S, N, reg = 'GGL', latent = True)
    return
Пример #6
0
def test_SGL_latent():
    Sigma, Theta = group_power_network(p, K = 1, M = 2)
    S, samples = sample_covariance_matrix(Sigma, N); S = S[0,:,:]  
    template_problem_SGL(S, N, latent = True)
    return
Пример #7
0
from gglasso.solver.ppdna_solver import PPDNA, warmPPDNA
from gglasso.helper.data_generation import group_power_network, sample_covariance_matrix
from gglasso.helper.experiment_helper import get_K_identity, lambda_parametrizer, lambda_grid, discovery_rate, error, hamming_distance, mean_sparsity
from gglasso.helper.experiment_helper import draw_group_heatmap, plot_fpr_tpr, plot_diff_fpr_tpr, plot_error_accuracy, surface_plot, plot_gamma_influence
from gglasso.helper.model_selection import aic, ebic

p = 100
K = 5
N = 5000
N_train = 5000
M = 10

reg = 'GGL'
save = False

Sigma, Theta = group_power_network(p, K, M)

draw_group_heatmap(Theta, save=save)

S, sample = sample_covariance_matrix(Sigma, N)
S_train, sample_train = sample_covariance_matrix(Sigma, N_train)
Sinv = np.linalg.pinv(S, hermitian=True)

#%%
# grid search for best lambda values with warm starts
L1, L2, W2 = lambda_grid(num1=3, num2=9, reg=reg)
grid1 = L1.shape[0]
grid2 = L2.shape[1]

ERR = np.zeros((grid1, grid2))
FPR = np.zeros((grid1, grid2))
Пример #8
0
def template_extADMM_consistent(latent=False):
    """
    tests whether the extended ADMM solver results in the same as MGL-solver for the redundant case of conforming variables
    """
    p = 50
    N = 1000
    K = 5
    M = 10

    lambda1 = 0.05
    lambda2 = 0.01

    Sigma, Theta = group_power_network(p, K, M)
    S, samples = sample_covariance_matrix(Sigma, N)

    Sdict = dict()
    Omega_0 = dict()

    for k in np.arange(K):
        Sdict[k] = S[k, :, :].copy()
        Omega_0[k] = np.eye(p)

    # constructs the "trivial" groups, i.e. all variables present in all instances
    G = construct_trivial_G(p, K)

    solext, _ = ext_ADMM_MGL(Sdict,
                             lambda1,
                             lambda2 / np.sqrt(K),
                             'GGL',
                             Omega_0,
                             G,
                             tol=1e-9,
                             rtol=1e-9,
                             verbose=True,
                             latent=latent,
                             mu1=0.01)
    solext2, _ = ext_ADMM_MGL(Sdict,
                              lambda1,
                              lambda2 / np.sqrt(K),
                              'GGL',
                              Omega_0,
                              G,
                              stopping_criterion='kkt',
                              tol=1e-8,
                              verbose=True,
                              latent=latent,
                              mu1=0.01)

    Omega_0_arr = get_K_identity(K, p)
    solADMM, info = ADMM_MGL(S,
                             lambda1,
                             lambda2,
                             'GGL',
                             Omega_0_arr,
                             tol=1e-9,
                             rtol=1e-9,
                             verbose=False,
                             latent=latent,
                             mu1=0.01)

    for k in np.arange(K):
        assert_array_almost_equal(solext['Theta'][k],
                                  solADMM['Theta'][k, :, :], 2)
        assert_array_almost_equal(solext2['Theta'][k],
                                  solADMM['Theta'][k, :, :], 2)

    if latent:
        for k in np.arange(K):
            assert_array_almost_equal(solext['L'][k], solADMM['L'][k, :, :], 2)
            assert_array_almost_equal(solext2['L'][k], solADMM['L'][k, :, :],
                                      2)

    return
Пример #9
0
from gglasso.solver.admm_solver import ADMM_MGL
from gglasso.helper.data_generation import group_power_network, sample_covariance_matrix
from gglasso.helper.experiment_helper import lambda_grid, discovery_rate, error
from gglasso.helper.utils import get_K_identity
from gglasso.helper.experiment_helper import draw_group_heatmap, plot_fpr_tpr, plot_diff_fpr_tpr, surface_plot
from gglasso.helper.model_selection import aic, ebic

p = 100
K = 5
N = 80
M = 10

reg = 'GGL'

Sigma, Theta = group_power_network(p, K, M, scale = False, nxseed = 2340)

S, sample = sample_covariance_matrix(Sigma, N)

# %%
#  Parameter selection (GGL)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# We do a grid search over :math:`\lambda_1` and :math:`\lambda_2` values.
# On each grid point we evaluate True/False Discovery Rate (TPR/FPR), True/False Discovery of Differential edges and AIC and eBIC.
#
# Note: the package contains functions for doing this grid search, but here we also want to evaluate True and False positive rates on each grid points.
#
#

L1, L2, W2 = lambda_grid(num1 = 3, num2 = 10, reg = reg)
grid1 = L1.shape[0]; grid2 = L2.shape[1]
Пример #10
0
from gglasso.helper.experiment_helper import lambda_parametrizer, lambda_grid, discovery_rate, error
from gglasso.helper.utils import get_K_identity, hamming_distance
from gglasso.helper.experiment_helper import draw_group_heatmap, plot_fpr_tpr, plot_diff_fpr_tpr, plot_error_accuracy, surface_plot, plot_gamma_influence
from gglasso.helper.model_selection import aic, ebic

p = 100
K = 5
N = 5000
N_train = 5000
M = 10

reg = 'GGL'
# whether to save the plots as pdf-files
save = False

Sigma, Theta = group_power_network(p, K, M, nxseed=2340)

draw_group_heatmap(Theta, save=save)

S, sample = sample_covariance_matrix(Sigma, N)
S_train, sample_train = sample_covariance_matrix(Sigma, N_train)
Sinv = np.linalg.pinv(S, hermitian=True)

#%% grid search for best lambda values with warm starts

L1, L2, W2 = lambda_grid(num1=3, num2=9, reg=reg)
grid1 = L1.shape[0]
grid2 = L2.shape[1]

ERR = np.zeros((grid1, grid2))
FPR = np.zeros((grid1, grid2))
Пример #11
0
lambda2 = 0.05

allK = [2, 5, 10, 15, 25]
allS = list()

J = len(allK)

rt_ggl = np.zeros(J)
rt_fgl = np.zeros(J)
rt_sgl = np.zeros(J)

for j in range(J):

    Sigma, Theta = group_power_network(p,
                                       K=allK[j],
                                       M=5,
                                       scale=False,
                                       nxseed=1235)
    S, sample = sample_covariance_matrix(Sigma, N)

    print("Shape of empirical covariance matrix: ", S.shape)
    print("Shape of the sample array: ", sample.shape)

    allS.append(S)

#%% initialize solvers (for numba jitting)
_, _ = ADMM_MGL(allS[0],
                lambda1,
                lambda2,
                "GGL",
                allS[0],
Пример #12
0
"""
# sphinx_gallery_thumbnail_number = 2

from gglasso.helper.data_generation import generate_precision_matrix, group_power_network, sample_covariance_matrix
from gglasso.problem import glasso_problem
from gglasso.helper.basic_linalg import adjacency_matrix

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

p = 20
N = 5000

Sigma, Theta = group_power_network(p, K = 1, M = 1, nxseed = 1235)
Sigma, Theta = generate_precision_matrix(p=p, M=1, style = 'powerlaw', gamma = 2.8, nxseed = 12345)

S, sample = sample_covariance_matrix(Sigma, N)

print("Shape of empirical covariance matrix: ", S.shape)
print("Shape of the sample array: ", sample.shape)


#%%
# Draw the graph of the true precision matrix.
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A = adjacency_matrix(Theta)
np.fill_diagonal(A,1)