polars_online.gram.condition¶
- polars_online.gram.condition(g: Gram, *, features: Sequence[str | int] | None = None) dict[str, Any][source]¶
Compute Belsley’s collinearity diagnostics for the design a Gram holds.
Use it to find which columns share a near-dependency, and how close to singular the design is.
vif()says only that a column is in a dependency, and a single condition number only that there is one.Parameters
gThe Gram, as
gram()returns it.featuresThe columns to diagnose, by name or position. Default
None, every column of the Gram, the intercept included.
Returns
A dict:
columnsThe columns diagnosed, in order.
singular_valuesThe singular values of the column-scaled design, largest first.
condition_indexess_max / s_jfor each singular values_j.kappaThe largest condition index.
proportionsThe variance-decomposition proportions: one row per component and one column per column diagnosed, each column summing to 1.
A component with a large condition index and a large share of two or more columns’ variance is a near-dependency between exactly those columns. Belsley’s rule of thumb is an index above 30 with two proportions above 0.5.
Raises
KeyErrorA name the Gram does not have.
IndexErrorA position outside the Gram’s columns.
ValueErrorA column named twice, by name or by position: two copies of one column are an exact dependency, which needs no diagnosis.
Examples
This code reads
dffrom Example data:spec = po.spec.ewridge( "ridge", targets=["y"], features=["x0", "x1", "x2"], half_life=100.0 ) bank = po.ModelBank([spec]) bank.fit_predict(df.with_columns(x2=pl.col("x0") + 0.01 * pl.col("x2"))) # x2 nearly x0 diag = po.gram.condition(bank.gram("ridge")[0]) print(round(diag["kappa"]), diag["proportions"][-1].round(2)) # the last component's
208 [0. 1. 0. 1.]
See also
vif(), each column’s variance inflation.Is the fit determined? in docs/GRAM.md, the theory, and Check that a fit is determined, a recipe that runs.
Notes
The design is scaled to unit column length first, Belsley’s prescription, but not centered: the intercept is part of the collinearity when a column is nearly constant, and centering hides that.
singular_valuesare of that scaled raw matrix, so they are the square roots of the eigenvalues of the scaled second-moment matrix.