rheon.gram.vif¶
- rheon.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, usecorrelation().Parameters
gThe Gram, as
gram()returns it.featuresThe 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 columnj, withR2_jthe share of its variance the other columns explain. A column the model found constant reportsinf. 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’sfeature_sets). Read anything past 1e10 as an exact dependency (Notes).Raises
ValueErrorThe intercept or a column named twice among
features; an array not sized to the Gram’s columns and targets, such as ameansof one entry.KeyErrorA name the Gram does not have.
IndexErrorA position outside the Gram’s columns.
Examples
This code reads
dffrom 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 print([f"{v:.0f}" for v in rp.gram.vif(bank.gram("ridge")[0])]) # x0, x1, x2
['10432', '1', '10432']
See also
condition(), which says which columns share a near-dependency.Is the fit determined? in docs/GRAM.md, the theory, and Check that a fit is determined, a recipe that runs.
Notes
The factors are the diagonal of the inverse correlation matrix, read from its eigendecomposition
R = V diag(d) V'asVIF_j = sum_i V_ji^2 / d_i. An eigenvalue at or below numpy’s rank tolerance,d_max * k * eps, withkthe number of columns andepsthe float64 rounding unit, is a dependency: a column with a weight pastsqrt(eps)in its direction reportsinf. 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
infor a number past 1e10, not alwaysinf. ItsR2_jis 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 atlam = 0.999andinfatlam = 1. Three columns where the third is the sum of the first two read 1.2e14 to 2.4e14 atlam = 1andinfat 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 + bwithaspread a million times as wide asb:b’s weight in the dependency’s direction, in correlation units, is 5e-13, far belowsqrt(eps). It read 1.0002 whereaandcreadinf.