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

g

The Gram, as gram() returns it, with its target moments (target_vars and target_n_kish).

coef

The fit, one entry per Gram column, as solve() returns it.

target

The target the fit predicts, by name or position. Default 0, the first.

features

The columns fitted, as for solve(). The other columns’ entries in coef are not read, and their se and t are nan. Default None, every column.

Returns

A dict:

resid_var, sigma2, r2

The residual variance of the fit, its estimate corrected for the degrees of freedom, and 1 - resid_var / Var[y].

n

Kish’s effective sample size, which the correction and the errors use (target_n_kish).

se, t

Arrays over the same columns as coef, the intercept’s included (nan on a Gram pooled within parts), and nan for a column not fitted. A fitted column of no variance, such as a constant that solve()’s standardize drops at 0, has nan too, and the others’ are read from the inverse of the columns with variance, as a fit without it gives them. A coef that is nan, such as solve()’s on a constant column without standardize, gives nan throughout.

Raises

ValueError

A coef without 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 among features, as for solve().

KeyError, IndexError

As for solve().

Examples

This code reads df from 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

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]

b is the slopes, C the features’ centered co-moments, Cov[X, y] the target’s centered cross-moments (cross_centered) and m the column means over the target’s rows. k counts the features with variance and the intercept: a column of none, such as a constant that standardize drops at 0, has no coefficient to estimate and spends no degree of freedom. se_0 is least squares’ error of the intercept, ybar - m . b: the mean and the slopes are uncorrelated, so its variance is Var[ybar] + m' Var[b] m. On a Gram pooled within parts (merge(within=True)), k counts 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’s r2 is 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, C is E[z z'], the co-moments plus the outer product of the means, Cov[X, y] is E[z y] (cross_moments), and Var[y] is E[y**2]. So resid_var is the residuals’ mean square, se and t are least squares’ through the origin, and r2 is uncentered, 1 - E[e**2] / E[y**2], as statsmodels reports a fit with no constant. k counts 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. n is target_n_kish, not weight_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 a t here as a scale for comparing coefficients, not as a p-value.