polars_online.gram¶
Pool, narrow, solve and diagnose the Grams a bank exports.
A Gram is the set of sums behind a least-squares fit: the weight of the
rows, the columns’ means and co-moments, and each target’s moments.
gram() returns one for each group and each
instance of a spec (the part of a model that fits one half-life), from one
pass over data that is never held in memory.
This module works on them after the pass, in five kinds of call:
what you want |
the calls |
|---|---|
shape a Gram |
|
fit it |
|
judge a fit |
|
many fits at once |
|
diagnose the design |
Every function but from_row() takes the mappings gram() returns,
one or a list, described by Gram. Between them, the functions
read its columns, targets, means, comoments,
lag_comoments, cross_moments, means_by_target,
cross_centered, target_weights, target_means, target_vars,
weight_sum, n_kish and target_n_kish. merge(), subset() and
from_row() return a mapping of the same shape, so a closed group’s
row (closed_groups()) is read the same way.
The compact forms gram() returns under dtype="float32" or
layout="packed" are taken too, and read in float64. merge() with
within=True returns two more keys, within_parts and
target_within_parts, which every fit reads.
The arithmetic is the models’ own, so solve() on a spec’s Gram
reproduces that spec’s coefficients where it takes the spec’s penalty, and
lasso_path() reproduces the lasso model’s path. It does not agree
to the last bit. The models factorize with faer’s Cholesky. Here numpy
runs LAPACK’s symmetric eigendecomposition (numpy.linalg.eigh) in
solve() and vif(), and an inverse (numpy.linalg.inv) in
coef_stats(). Where the columns’ scales differ, solve() takes
an LU solve for each ridge above 0 (its Notes say when). Each of these
rounds differently in the last place or two, so the tests hold the two to
a relative tolerance, not to equality.
The paths and solve_subsets() run in Rust. lars_paths() and
solve_subsets() run on the process’s threads, or on as many as their
threads gives.
The module needs numpy, an optional extra of this package (pip install
polars-online[numpy]) and not a dependency, since it is not one of
Polars’ either. Without it, every function raises ModuleNotFoundError.
Nothing here needs scipy or scikit-learn.
docs/GRAM.md is the guide: the theory behind each function, and a table from each task on weekly blocks from one pass to the how-to whose recipe runs it.
- polars_online.gram.INTERCEPT = 'intercept'¶
The name
gram()gives the constant column a spec’sfit_interceptputs in front of the features, matching thetermcolumn ofpolars_online.spec.coef_index().
Pooling Grams¶
The Grams a bank exports, and how to pool, narrow and read them.
The mapping that |
|
Pool the Grams of disjoint sets of rows into the Gram of their union, or within each part. |
|
Read a closed group’s row as the Gram |
|
Keep some of a Gram’s columns, in the order given. |
|
Compute the correlation matrix of a Gram’s columns from their co-moments. |
Solving Grams¶
Fits, paths, standard errors and collinearity diagnostics computed from a Gram.
Compute ridge coefficients from a Gram, in the features’ original units. |
|
Compute the elastic-net path from a Gram, one row of coefficients per penalty. |
|
Compute the lasso path from its start, knot by knot, by least angle regression. |
|
Compute |
|
Merge each subset of the Grams and fit it, for many subsets at once. |
|
Compute the residual variance, standard errors and t-statistics of a fit from a Gram. |
|
Compute each column’s variance inflation factor from a Gram. |
|
Compute Belsley’s collinearity diagnostics for the design a Gram holds. |