Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
0
solP, infoP = warmPPDNA(S,
                        l1opt,
                        l2opt,
                        reg,
                        Omega_0,
                        Theta_0=Theta_0,
                        eps=5e-5,
                        verbose=True,
                        measure=True)

solA, infoA = ADMM_MGL(S,
                       l1opt,
                       l2opt,
                       reg,
                       Omega_0,
                       Theta_0=Theta_0,
                       eps_admm=5e-5,
                       verbose=True,
                       measure=True)

Theta_sol = solP['Theta']
Omega_sol = solP['Omega']

with sns.axes_style("white"):
    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 3))
    draw_group_heatmap(Theta, method='truth', ax=axs[0])
    draw_group_heatmap(Theta_sol, method='PPDNA', ax=axs[1])

#%%
# plotting (set save = False if plot should not be saved)
Ejemplo n.º 4
0
RT_PPA = np.zeros(len(vecN))
TPR = np.zeros(len(vecN))
FPR = np.zeros(len(vecN))

iA = {}
iP = {}

for j in np.arange(len(vecN)):

    S, sample = sample_covariance_matrix(Sigma, vecN[j])

    #start = time()
    solA, infoA = ADMM_MGL(S,
                           l1[j],
                           l2[j],
                           reg,
                           Omega_0,
                           eps_admm=5e-5,
                           verbose=False,
                           measure=True)
    #end = time()
    #RT_ADMM[j] = end-start
    iA[j] = infoA

    TPR[j] = discovery_rate(solA['Theta'], Theta)['TPR']
    FPR[j] = discovery_rate(solA['Theta'], Theta)['FPR']

    #start = time()
    solP, infoP = warmPPDNA(S,
                            l1[j],
                            l2[j],
                            reg,
Ejemplo n.º 5
0
l2 = 2e-2 * np.ones(len(f))

Omega_0 = get_K_identity(K,p)


TPR = np.zeros(len(vecN))
FPR = np.zeros(len(vecN))

iA = {}
iP = {}

for j in np.arange(len(vecN)):  
    
    S, sample = sample_covariance_matrix(Sigma, vecN[j])
    
    solA, infoA = ADMM_MGL(S, l1[j], l2[j], reg , Omega_0 , tol = 5e-5, stopping_criterion = 'kkt', verbose = False, measure = True)
    iA[j] = infoA
    
    TPR[j] = discovery_rate(solA['Theta'], Theta)['TPR']
    FPR[j] = discovery_rate(solA['Theta'], Theta)['FPR']
    
    
    solP, infoP = warmPPDNA(S, l1[j], l2[j], reg, Omega_0, eps = 5e-5, eps_admm = 1e-2, verbose = True, measure = True)
    iP[j] = infoP

#%%
# plotting
save = False

plot_runtime(iA, iP, vecN, save = save)
        
Ejemplo n.º 6
0
solPPDNA, info = warmPPDNA(S,
                           lambda1,
                           lambda2,
                           reg,
                           Omega_0,
                           eps=1e-5,
                           verbose=True,
                           measure=True)

#solADMM, info = ADMM_MGL(S, lambda1, lambda2, reg, Omega_0, n_samples = None, eps_admm = 1e-4 , verbose = True)

solADMM, info = ADMM_MGL(S,
                         lambda1,
                         lambda2,
                         reg,
                         Omega_0,
                         n_samples=None,
                         eps_admm=1e-4,
                         verbose=True,
                         latent=True,
                         mu1=.2)

#%%
# tests for the extended ADMM version

from gglasso.solver.ext_admm_solver import ext_ADMM_MGL
from gglasso.helper.ext_admm_helper import construct_trivial_G

Sdict = dict()
Omega_0 = dict()

for k in np.arange(K):
Ejemplo n.º 7
0
AIC = np.zeros((grid1, grid2))
BIC = np.zeros((grid1, grid2))

Omega_0 = np.zeros((K, p, p))
Theta_0 = np.zeros((K, p, p))

for g1 in np.arange(grid1):
    for g2 in np.arange(grid2):
        lambda1 = L1[g1, g2]
        lambda2 = L2[g1, g2]

        sol, info = ADMM_MGL(S,
                             lambda1,
                             lambda2,
                             reg,
                             Omega_0,
                             eps_admm=1e-3,
                             verbose=False)
        Theta_sol = sol['Theta']
        Omega_sol = sol['Omega']

        # warm start
        Omega_0 = Omega_sol.copy()
        Theta_0 = Theta_sol.copy()

        AIC[g1, g2] = aic(S, Theta_sol, n.mean())
        BIC[g1, g2] = ebic(S, Theta_sol, n.mean(), gamma=0.1)

ix = np.unravel_index(np.nanargmin(BIC), BIC.shape)
ix2 = np.unravel_index(np.nanargmin(AIC), AIC.shape)
Ejemplo n.º 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
Ejemplo n.º 9
0
# solve with PPDNA
Omega_0 = results.get('GLASSO').get('Theta')
Theta_0 = Omega_0.copy()
X_0 = np.zeros((K,p,p))

start = time()
sol, info = warmPPDNA(S, lambda1, lambda2, reg, Omega_0, Theta_0 = Theta_0, X_0 = X_0, eps = 5e-4 , verbose = True, measure = True)
end = time()

print(f"Running time for PPDNA was {end-start} seconds")
results['PPDNA'] = {'Theta' : sol['Theta']}

#%%
# solve with general ADMM
start = time()
sol, info = ADMM_MGL(S, lambda1, lambda2, reg, Omega_0, Theta_0 = Theta_0, X_0 = X_0, rho = 1, max_iter = 100, \
                                                        eps_admm = 5e-4, verbose = True, measure = True)
end = time()

print(f"Running time for ADMM was {end-start} seconds")

results['ADMM'] = {'Theta' : sol['Theta']}

#%%
# solve with ragin (LTGL)

start = time()
alpha = N*lambda1
beta = N*lambda2 
ltgl = TimeGraphicalLasso(alpha = alpha, beta = beta , psi = 'l1', \
                          rho = 1., tol = 5e-4, rtol = 5e-4,  max_iter = 2000, verbose = True)
ltgl = ltgl.fit(sample.transpose(0,2,1))
Ejemplo n.º 10
0
DFPR = np.zeros((grid1, grid2))
DTPR = np.zeros((grid1, grid2))
AIC = np.zeros((grid1, grid2))
BIC = np.zeros((grid1, grid2))


Omega_0 = get_K_identity(K,p)
Theta_0 = get_K_identity(K,p)
X_0 = np.zeros((K,p,p))

for g2 in np.arange(grid2):
    for g1 in np.arange(grid1):
        lambda1 = L1[g1,g2]
        lambda2 = L2[g1,g2]
              
        sol, info =  ADMM_MGL(S, lambda1, lambda2, reg , Omega_0, Theta_0 = Theta_0, X_0 = X_0, tol = 1e-8, rtol = 1e-8, verbose = False, measure = False)

        Theta_sol = sol['Theta']
        Omega_sol = sol['Omega']
        X_sol = sol['X']
        
        # warm start
        Omega_0 = Omega_sol.copy()
        Theta_0 = Theta_sol.copy()
        X_0 = X_sol.copy()
        
        dr = discovery_rate(Theta_sol, Theta)
        TPR[g1,g2] = dr['TPR']
        FPR[g1,g2] = dr['FPR']
        DTPR[g1,g2] = dr['TPR_DIFF']
        DFPR[g1,g2] = dr['FPR_DIFF']
Ejemplo n.º 11
0
Omega_0 = get_K_identity(K, p)
Theta_0 = get_K_identity(K, p)
X_0 = np.zeros((K, p, p))

for g2 in np.arange(grid2):
    for g1 in np.arange(grid1):

        lambda1 = L1[g1, g2]
        lambda2 = L2[g1, g2]

        sol, info = ADMM_MGL(S,
                             lambda1,
                             lambda2,
                             reg,
                             Omega_0,
                             Theta_0=Theta_0,
                             X_0=X_0,
                             tol=1e-8,
                             rtol=1e-8,
                             verbose=False,
                             measure=False)

        Theta_sol = sol['Theta']
        Omega_sol = sol['Omega']
        X_sol = sol['X']

        # warm start
        Omega_0 = Omega_sol.copy()
        Theta_0 = Theta_sol.copy()
        X_0 = X_sol.copy()
Ejemplo n.º 12
0
Omega_0 = get_K_identity(K, p)
Theta_0 = get_K_identity(K, p)

for g1 in np.arange(grid1):
    for g2 in np.arange(grid2):
        lambda1 = L1[g1, g2]
        lambda2 = L2[g1, g2]

        #sol, info = warmPPDNA(S_train, lambda1, lambda2, reg, Omega_0, Theta_0 = Theta_0, eps = 1e-3, verbose = False, measure = False)

        sol, info = ADMM_MGL(S_train,
                             lambda1,
                             lambda2,
                             reg,
                             Omega_0,
                             Theta_0=Theta_0,
                             tol=1e-10,
                             rtol=1e-10,
                             verbose=False,
                             measure=False)

        Theta_sol = sol['Theta']
        Omega_sol = sol['Omega']

        # warm start
        Omega_0 = Omega_sol.copy()
        Theta_0 = Theta_sol.copy()

        TPR[g1, g2] = discovery_rate(Theta_sol, Theta)['TPR']
        FPR[g1, g2] = discovery_rate(Theta_sol, Theta)['FPR']
        DTPR[g1, g2] = discovery_rate(Theta_sol, Theta)['TPR_DIFF']
Ejemplo n.º 13
0
                                       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],
                max_iter=10,
                measure=False,
                latent=False)
_, _ = ADMM_MGL(allS[0],
                lambda1,
                lambda2,
                "FGL",
                allS[0],
                max_iter=10,
                measure=False,
                latent=False)

_ = ADMM_SGL(allS[0][0],
             lambda1,
             allS[0][0],