rheon.gram.lars_paths

rheon.gram.lars_paths(grams: Sequence[Gram], *, targets: Sequence[str | int] | None = None, features: Sequence[str | int] | None = None, max_iter: int | None = None, max_active: int | None = None, penalty_weights: Sequence[float] | None = None, threads: int | None = None) → list[list[dict[str, Any]]][source]

Compute lars_path() for many Grams and targets at once, on the process’s threads.

Use it to walk the lasso path of every group’s Gram, or of every target, in one call. To pool subsets of the Grams first and walk each pooled Gram’s path, use solve_subsets(). For one path, lars_path() is the same arithmetic.

Parameters

grams

The Grams, each as gram() returns it, all with the same columns. Each is fitted as it is: nothing is merged, and a Gram pooled within parts has its intercept nan.

targets

The targets to walk, by name or position, in each Gram. Default None, every target of each Gram.

features, max_iter, max_active, penalty_weights

As for lars_path(), applied to every path, max_iter’s older name max_steps refused, as for lars_path().

threads

How many threads the Grams run on, beside each other: None, the default, is the process’s threads, which RHEON_MAX_THREADS sizes, and an integer that many threads, as ModelBank takes it. Refused unless an integer of at least 1, or None.

Returns

One list per Gram, with one path per target, each the dict lars_path() returns. An empty grams gives an empty list.

Raises

ValueError

Grams of different columns, as for merge().

Otherwise, it raises what lars_path() raises.

Examples

This code reads df from Example data:

spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"],
                       half_life=100.0, group="stock_id")
bank = rp.ModelBank([spec])
bank.fit_predict(df)
paths = rp.gram.lars_paths(bank.gram("ridge"), max_active=1)   # one list per stock
# the first column to enter each stock's path
print([per_gram[0]["active"][-1] for per_gram in paths])
[['x0'], ['x0'], ['x0'], ['x0']]

See also

Notes

Each Gram’s column scaling is formed once, and its targets’ paths run beside each other on it, each reading only its active columns’ rows of the correlation matrix from the co-moments. A path is the same arithmetic on one thread whatever the number of threads, so its numbers are those of lars_path() to the bit. solve_subsets() with path and one Gram a subset, [[0], [1], ...], gives each subset the path lars_path() gives on that Gram, to the bit.