polars_online.gram.vif

polars_online.gram.vif(g: Gram, *, features: Sequence[str | int] | None = None) → Any[source]

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

Use it to find the columns whose coefficients a fit cannot pin down, because the other columns explain them. To see which columns share a near-dependency, use condition(). To read the correlations themselves, use correlation().

Parameters

g

The Gram, as gram() returns it.

features

The columns to diagnose, by name or position. Default None, the Gram’s columns without the intercept: a constant is explained perfectly by any other constant, so its factor is undefined. The intercept, or a column named twice, is refused.

Returns

A float array over features: 1 / (1 - R2_j) for each column j, with R2_j the share of its variance the other columns explain. A column the model found constant reports inf. Above about 10, a common rule of thumb, the column’s coefficient has more than ten times the variance it would have on uncorrelated columns. Fit such a column with a ridge, on a subset, or as a feature set the spec fits beside the full one (ewridge’s feature_sets). Read anything past 1e10 as an exact dependency (Notes).

Raises

ValueError

The intercept or a column named twice among features.

KeyError

A name the Gram does not have.

IndexError

A position outside the Gram’s columns.

Examples

This code reads df from 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
print([f"{v:.0f}" for v in po.gram.vif(bank.gram("ridge")[0])])   # x0, x1, x2
['10432', '1', '10432']

See also

Notes

The factors are the diagonal of the inverse correlation matrix, read from its eigendecomposition R = V diag(d) V' as VIF_j = sum_i V_ji^2 / d_i. An eigenvalue at or below numpy’s rank tolerance, d_max * k * eps, with k the number of columns and eps the float64 rounding unit, is a dependency: a column with a weight past sqrt(eps) in its direction reports inf. A pseudo-inverse would drop that direction instead, and it read an exact duplicate at 0.25, below the floor of 1 a factor cannot go under.

A column that is an exact linear combination of the others reads inf or a number past 1e10, not always inf. Its R2_j is 1, but rounding in the accumulated moments can leave the dependency’s eigenvalue just above the tolerance, and the column then reads about 1e14. On 5,000 rows, a copy times 100 plus 3 read 2.2e14 at lam = 0.999 and inf at lam = 1. Three columns where the third is the sum of the first two read 1.2e14 to 2.4e14 at lam = 1 and inf at 0.999.

A factor near 1 does not say a column is in no dependency. A column whose part in a dependency is tiny reads near 1, as if it stood apart. Take c = a + b with a spread a million times as wide as b: b’s weight in the dependency’s direction, in correlation units, is 5e-13, far below sqrt(eps). It read 1.0002 where a and c read inf.