polars_online.gram.coef_stats¶
- polars_online.gram.coef_stats(g: Gram, coef: Sequence[float], *, target: str | int = 0, features: Sequence[str | int] | None = None) dict[str, Any][source]¶
Compute the residual variance, standard errors and t-statistics of a fit from a Gram.
Use it to judge a fit read off a Gram, such as one
solve()returns: how good it is, and which coefficients are real, without the rows. For many merged subsets of Grams at once,solve_subsets()returns the same statistics beside each fit.Parameters
gThe Gram, as
gram()returns it, with its target moments (target_varsandtarget_n_kish).coefThe fit, one entry per Gram column, as
solve()returns it.targetThe target the fit predicts, by name or position. Default 0, the first.
featuresThe columns fitted, as for
solve(). The other columns’ entries incoefare not read, and theirseandtarenan. DefaultNone, every column.
Returns
A dict:
resid_var,sigma2,r2The residual variance of the fit, its estimate corrected for the degrees of freedom, and
1 - resid_var / Var[y].nKish’s effective sample size, which the correction and the errors use (
target_n_kish).se,tArrays over the same columns as
coef, the intercept’s included (nanon a Gram pooled within parts), andnanfor a column not fitted. A fitted column of no variance, such as a constant thatsolve()’sstandardizedrops at 0, hasnantoo, and the others’ are read from the inverse of the columns with variance, as a fit without it gives them. Acoefthat isnan, such assolve()’s on a constant column withoutstandardize, givesnanthroughout.
Raises
ValueErrorA
coefwithout one entry per Gram column; a Gram without target moments, such as a mapping built by hand without them (gram()reports both); the intercept or a column named twice amongfeatures, as forsolve().KeyError,IndexErrorAs for
solve().
Examples
This code reads
dffrom Example data:spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) bank = po.ModelBank([spec]) bank.fit_predict(df) g = bank.gram("ridge")[0] coef = po.gram.solve(g, target="y") # least squares stats = po.gram.coef_stats(g, coef, target="y") print(stats["se"].round(3), stats["t"].round(1), round(stats["r2"], 3))
[0.031 0.033 0.032] [ 31.5 60.6 -31.7] 0.943
See also
solve(), the fit.solve_subsets(), the fit and these statistics for many merged subsets.A ridge and its t in docs/GRAM.md, the theory, and Fit a ridge from a Gram, a recipe that runs.
Notes
The statistics read the target’s variance
Var[y]from the Gram:resid_var = Var[y] - 2 b' Cov[X, y] + b' C b sigma2 = resid_var * n / (n - k) # n = target_n_kish se = sqrt(diag(inv(C)) * sigma2 / n) se_0 = sqrt((1 + m' inv(C) m) * sigma2 / n) # the intercept's t = b / se r2 = 1 - resid_var / Var[y]
bis the slopes,Cthe features’ centered co-moments,Cov[X, y]the target’s centered cross-moments (cross_centered) andmthe column means over the target’s rows.kcounts the features with variance and the intercept: a column of none, such as a constant thatstandardizedrops at 0, has no coefficient to estimate and spends no degree of freedom.se_0is least squares’ error of the intercept,ybar - m . b: the mean and the slopes are uncorrelated, so its variance isVar[ybar] + m' Var[b] m. On a Gram pooled within parts (merge(within=True)),kcounts the features with variance and one intercept per part the target has rows in (target_within_parts). The intercept then has no error, since each part had its own. The fit’sr2is the share of the variance inside the parts that it explains.For a ridge fit, the standard errors are those of least squares, at the ridge fit’s residual variance.
inv(C)is the inverse of the unpenalized matrix, so the errors take no account of the shrinkage, which makes a ridge’s coefficients less variable than least squares’, nor of the bias it adds. At a ridge near 0 they are least squares’ own.Without an intercept, the statistics read the raw moments, as least squares through the origin does. On a Gram without an intercept column, as a
subset()without"intercept"gives,CisE[z z'], the co-moments plus the outer product of the means,Cov[X, y]isE[z y](cross_moments), andVar[y]isE[y**2]. Soresid_varis the residuals’ mean square,seandtare least squares’ through the origin, andr2is uncentered,1 - E[e**2] / E[y**2], as statsmodels reports a fit with no constant.kcounts the features alone, those not identically 0. On a Gram pooled within parts the means are 0, the target’s too, so the raw moments are the centered ones and nothing moves.The sample size is Kish’s effective sample size, not the weight.
nistarget_n_kish, notweight_sum: a weighted stream’s weight sum is not a count, and dividing by it would report standard errors too small by the factor the weights are unequal by. The rows behind an exponentially weighted fit are also neither independent nor identically distributed, so read athere as a scale for comparing coefficients, not as a p-value.