rheon.gram.condition

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

g

The Gram, as gram() returns it.

features

The columns to diagnose, by name or position. Default None, every column of the Gram, the intercept included.

Returns

A dict:

columns

The columns diagnosed, in order.

singular_values

The singular values of the column-scaled design, largest first.

condition_indexes

s_max / s_j for each singular value s_j.

kappa

The largest condition index.

proportions

The 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

KeyError

A name the Gram does not have.

IndexError

A position outside the Gram’s columns.

ValueError

A column named twice, by name or by position: two copies of one column are an exact dependency, which needs no diagnosis. Or an array not sized to the Gram’s columns and targets, such as a means of one entry.

Examples

This code reads df from Example data:

spec = rp.spec.ewridge(
    "ridge", targets=["y"], features=["x0", "x1", "x2"], half_life=100.0
)
bank = rp.ModelBank([spec])
bank.fit_predict(df.with_columns(x2=pl.col("x0") + 0.01 * pl.col("x2")))  # x2 nearly x0
diag = rp.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

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_values are of that scaled raw matrix, so they are the square roots of the eigenvalues of the scaled second-moment matrix.