def test__cov_2_corr():
    p = X.shape[0]
    C = covl._cov_2_corr(X)
    assert_array_almost_equal(C.flat[::p + 1], np.ones((p,)))
    C_ = C.copy()
    C_.flat[::p + 1] = 0.
    assert_array_less(np.abs(C_), np.ones(p, p))
    assert_greater(np.min(scipy.linalg.eigvals(C)), 0.)
    # test inverse transform
    var = X.flat[::p + 1]
    stddev = np.atleast_2d(np.sqrt(var))
    assert_array_almost_equal(C * stddev * stddev.T, X)
Beispiel #2
0
def alpha_max(X, cov_estimator=EmpiricalCovariance(assume_centered=True)):
    """ get the maximum penalisation parameter alpha_max for given data

    X is a mutable list passed by reference, so no need to return X!
    """
    for session in [1, 2]:
        alpha_max = list()
        Xs = [x for x in X if x["session"] == session]
        for train_ix in Xs[0]["train_ix"]:
            Y = np.concatenate([Xs[0]["data"][train_ix, :], Xs[1]["data"][train_ix, :]], axis=0)
            C = cov_estimator.fit(Y).covariance_
            C = covl._cov_2_corr(C)
            alpha_max.append(np.max(np.abs(C - np.diag(np.diag(C)))))
        for j in range(len(X)):
            if X[j]["session"] == session:
                X[j]["alpha_max"] = alpha_max
Beispiel #3
0
def fista_graph_lasso(X, alpha, L=.01, eta=1.1, Theta_init=None,
                      precision=1e-2, Hessian=False, max_iter=1e2,
                      base_estimator=EmpiricalCovariance(
                          assume_centered=True), fista=False):
    # incorporate the Hessian ...
    t = 1.
    covX = base_estimator.fit(X).covariance_
    p = covX.shape[0]
    penalty_mask = np.ones((p, p)) - np.identity(p)
    alpha = alpha * penalty_mask
    if Theta_init is None:
        Theta = np.identity(p)
    else:
        Theta = Theta_init
    S = covl._cov_2_corr(covX)
    print 'alpha_max = {}'.format(alpha_max(S))
    alpha *= alpha_max(S)
    G = grad_f_graphlasso(Theta, S)
    Z = pL_graphlasso(Theta, alpha, G, L)

#    if Hessian:
#        sqrtS = sqrtm(S)
#        max_eval = np.max(scipy.linalg.schur(
#            sqrtS.dot(Theta.dot(sqrtS)))[0])
#        L = max(L, max_eval)

    f_vals_ = [f_graphlasso(Theta, S) + g_graphlasso(Theta, alpha)]
    print 'F = {} (t = {})'.format(f_vals_[-1], t)

    n_iter = 0
    while True:
        try:
            n_iter += 1
            if not Hessian:
                Theta_ = scipy.linalg.eigh(Theta)
            else:
                Theta_ = Theta
            Z_old = Z.copy()
            G = grad_f_graphlasso(Theta_, S, Hessian=Hessian)
            Z = pL_graphlasso(Theta, alpha, G, L)
            fTheta = f_graphlasso(Theta_, S)
            fZ = f_graphlasso(Z, S)
            while fZ > Q_graphlasso(Z, Theta, G, L, alpha, fTheta,
                                    penalty_mask):
                L *= eta
#                if Hessian:
#                    max_eval = np.max(scipy.linalg.schur(
#                        sqrtS.dot(Theta.dot(sqrtS)))[0])
#                    L = max(L, max_eval)
                Z = pL_graphlasso(Theta, alpha, G, L)
                fZ = f_graphlasso(Z, S)
            if fista:
                t_old = t
                t = 0.5 + np.sqrt(1 + 4 * t ** 2) / 2
                Theta = Z + (t_old - 1.) / t * (Z - Z_old)
            else:
                Theta = Z
            f_vals_.append(f_graphlasso(Theta, S) +
                           g_graphlasso(Theta, alpha))
            print 'F = {} (t = {}, L = {})'.format(f_vals_[-1], t, L)
            if 1. / t < precision or n_iter >= max_iter:
                raise StopIteration
            if len(f_vals_) > 1 and f_vals_[-2] - f_vals_[-1] < precision:
                raise StopIteration
        except StopIteration:
            return Theta, f_vals_