rheon.gram.lasso_path

rheon.gram.lasso_path(g: Gram, penalties: Sequence[float], *, l1_ratio: float = 1.0, penalty_weights: Sequence[float] | None = None, target: str | int = 0, features: Sequence[str | int] | None = None, max_iter: int = 1000, tol: float = 1e-10) → Any[source]

Compute the elastic-net path from a Gram, one row of coefficients per penalty.

Use it to fit the lasso or the elastic net on a Gram over a grid of penalties you choose, as the lasso model does over its stream. For the start of the lasso path, knot by knot and exactly, use lars_path(). For a ridge, use solve().

Parameters

g

The Gram, as gram() returns it.

penalties

The penalties l, from large to small, as a path is meant to be walked: each penalty’s descent starts from the fit at the one before it, which makes the walk faster and better conditioned. They are a lasso spec’s penalties, and bank.coef() reports each as penalty. Each is refused unless finite and at least 0. The keyword’s older name, lambdas, is refused, and the error names this one.

l1_ratio

The share of the penalty that is L1. Default 1, the lasso; below 1, an elastic net. Refused outside [0, 1].

penalty_weights

A scale on the penalty per feature, in features order (or in columns order without the intercept): 0 leaves a column unpenalized. A column with no variance in the Gram is dropped whatever its weight. Default None, every weight 1. The lasso model takes no such parameter: here the path is walked again on every call, so each call may weigh the columns its own way. Refused at the wrong length, or with a weight that is not finite and at least 0.

target, features

As for solve().

max_iter

The most sweeps of the coordinate descent at each penalty. Default 1,000, where the model’s is 100, since this path starts its first penalty from zero and the model starts each solve from its last fit. Refused below 1.

tol

The stopping rule: a sweep ends the descent when it moves no standardized coefficient by tol * s or more, or moves none at all, with s the target’s spread (Notes). Default 1e-10, the model’s. Refused unless finite and above 0.

Returns

A float array of shape (len(penalties), k) over the Gram’s k columns, the intercept first when there is one, in the features’ original units with the intercept recovered from the means, so a row is directly comparable to bank.coef(). On a Gram pooled within parts (merge(within=True)) the intercept is nan: such a Gram has an intercept per part and none of its own.

Raises

ValueError

A number outside its range, by the rule a spec applies to the same parameter (each listed under Parameters); penalty_weights of the wrong length; the intercept or a column named twice among features, as for solve().

KeyError, IndexError

As for solve().

TypeError

penalties’ older name, lambdas, is given.

ReadinessWarning

A warning, not an error: issued for each penalty whose descent runs out of sweeps before it meets tol. That row keeps the coefficients where the descent stopped. Increase max_iter, or take the knots exactly from lars_path().

Examples

This code reads df from Example data:

spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
bank = rp.ModelBank([spec])
bank.fit_predict(df)
g = bank.gram("ridge")[0]
path = rp.gram.lasso_path(g, [1.0, 0.1, 0.001], target="y")   # one row per penalty
print(path.round(3) + 0.0)                                  # + 0.0 prints -0.0 as 0.0
[[ 0.966  0.784  0.   ]
 [ 0.991  1.848 -0.885]
 [ 0.992  1.968 -1.002]]

See also

Notes

The descent is the model’s own: the lasso model’s, run on the standardized (correlation-form) matrix, each penalty starting from the fit at the one before it:

b_i = soft(rho_i, l * l1_ratio * pw_i) / (C_ii + l * (1 - l1_ratio) * pw_i)
soft(v, t) = sign(v) * max(|v| - t, 0)

where b_i is column i’s standardized coefficient, rho_i its standardized cross-correlation with the target less the other columns’ contributions, C_ii its standardized variance and pw_i its penalty weight. With an intercept the system is centered at the target’s own means, as the model’s descent reads it. Through the origin nothing is centered and the raw moments are used, as the model does.

The stopping rule has no units. s is the target’s spread, read from the Gram’s target moments: its standard deviation with an intercept, its root mean square through the origin. The standardized coefficients are in the target’s units, so the target scaled by a power of two, with the penalties, gives the same path scaled, to the bit, stopped on the same sweep. s is never below the largest standardized cross-moment |d_i|, which cannot exceed the target’s spread, so a Gram without target moments has a scale too. On 15 features at seven penalties, tol=1e-10 cost the same as 1e-7 (0.07 ms) and landed 3e-10 from the converged path, where 1e-7 landed 4e-7.

A descent on columns that nearly repeat each other takes many sweeps. A cyclic descent shrinks the error along two columns of correlation rho by rho**2 a sweep. Their correlation matrix’s condition number is kappa = (1 + rho) / (1 - rho), so rho**2 = ((kappa - 1) / (kappa + 1))**2, about 1 - 4 / kappa, and each e-fold of the error takes about kappa / 4 sweeps.

The descent runs in Rust. On 40 penalties at 15 strongly correlated columns it took 1.8 ms, where the same descent as a Python loop took 129 ms. Each sweep moves the coordinates in the loop’s order, so a row matches the loop’s to rounding (2e-15 there), not to the bit: the sum over the other columns runs in a different order.