rheon.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

merge() pools shards into their union, or within each part; subset() keeps some of the columns; from_row() reads a closed group’s row

fit it

solve() for a ridge; lasso_path() for the elastic net over a grid of penalties; lars_path() and lars_paths() for the lasso path from its start, knot by knot

judge a fit

coef_stats(): residual variance, standard errors and t

many fits at once

solve_subsets() merges each subset of the Grams and fits it

diagnose the design

correlation(), vif() and condition()

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 rheon[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.

rheon.gram.INTERCEPT = 'intercept'

The name gram() gives the constant column a spec’s fit_intercept puts in front of the features, matching the term column of rheon.spec.coef_index().

Pooling Grams

The Grams a bank exports, and how to pool, narrow and read them.

rheon.gram.Gram

The mapping that gram() returns and the functions here take.

rheon.gram.merge

Pool the Grams of disjoint sets of rows into the Gram of their union, or within each part.

rheon.gram.from_row

Read a closed group’s row as the Gram gram() would return.

rheon.gram.subset

Keep some of a Gram’s columns, in the order given.

rheon.gram.correlation

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.

rheon.gram.solve

Compute ridge coefficients from a Gram, in the features’ original units.

rheon.gram.lasso_path

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

rheon.gram.lars_path

Compute the lasso path from its start, knot by knot, by least angle regression.

rheon.gram.lars_paths

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

rheon.gram.solve_subsets

Merge each subset of the Grams and fit it, for many subsets at once.

rheon.gram.coef_stats

Compute the residual variance, standard errors and t-statistics of a fit from a Gram.

rheon.gram.vif

Compute each column’s variance inflation factor from a Gram.

rheon.gram.condition

Compute Belsley’s collinearity diagnostics for the design a Gram holds.