skglm icon indicating copy to clipboard operation
skglm copied to clipboard

TST add sparse group lasso testing against GSROPTIM

Open mathurinm opened this issue 7 months ago • 1 comments

for the record, if one want s to compare against gsrpotim (by Eugene Ndiaye), here's a script showing that we get the same results:

from skglm.utils.data import make_correlated_data
from skglm.datafits import QuadraticGroup
from skglm.penalties import WeightedL1GroupL2
from skglm import GeneralizedLinearEstimator
from skglm.utils.data import (make_correlated_data, grp_converter)
from skglm.solvers import GroupBCD

from gsroptim.sgl import sgl_path


import numpy as np

n_features = 30
n_samples= 40
X, y, _ = make_correlated_data(n_features=n_features, n_samples=n_samples, random_state=0)

grp_indices, grp_ptr = grp_converter(3, n_features)
n_groups = len(grp_ptr) - 1

alpha = 0.1
weights_g = np.ones(n_groups, dtype=np.float64)
# beware of tau in GSR optim implem: need to scale alpha by 2
res = sgl_path(X, y, size_groups=[3] * n_groups, lambdas=[alpha*2 * n_samples], omega=weights_g)[0]

weights_f = 1. * np.ones(n_features)
pen = WeightedL1GroupL2(
    alpha=alpha, weights_groups=weights_g,
    weights_features=weights_f, grp_indices=grp_indices, grp_ptr=grp_ptr)

solver = GroupBCD(ws_strategy="fixpoint", verbose=3,
                    fit_intercept=False, tol=1e-10)

model = GeneralizedLinearEstimator(
    QuadraticGroup(grp_ptr, grp_indices), pen, solver=solver)

model.fit(X, y)
print(model.coef_.reshape([-1, 3]))
print(res.reshape([-1, 3]))

gsroptim requires a bit of work to install on the CI but it's doable

mathurinm avatar Jul 14 '24 18:07 mathurinm