polars_online.ModelBank.coef

ModelBank.coef(spec: str | int | None = None, group: str | Iterable[str | None] | None = None) → DataFrame[source]

Return the coefficients of every fit the bank holds, one row per coefficient.

Use it to read the coefficients from a bank, or from one loaded from a state file, without a row of data. For the coefficients on each row of a stream, read the coef field of fit_predict()’s output, and for the sums behind a fit, gram().

Parameters

spec

The spec to read, by name or by position. The default, None, reads every spec and skips each one that has no coefficients (see Raises). A bank whose specs have no coefficients at all gives an empty frame, with the same columns.

group

The group to read: one group key, as groups() reports it, or a list of keys, with None in the list for the null group. The default, None, reads every group. A key the bank has never seen gives no rows.

Returns

A DataFrame with one row per (spec, group, instance, position), in spec order, then in groups()’ order. It leads with spec, as last_row(), summary() and describe() do. Every frame it returns has the same columns, so the frames of several banks stack with a plain pl.concat. The values are the fit after the last row each model learned, which the next row’s pred_<t> is computed from. That is the fit the output’s coef field shows on the rows where it is written. The columns:

spec, group, instance

The spec’s name, the group key as groups() reports it, and the instance’s field suffix ("@h500", or "" for a spec with one instance). An instance is the part of a model that fits one half-life of a grid.

half_life, lam

The instance’s decay, as polars_online.spec.output_index() gives it: the half-life in clock units (inf for no decay), or the lam. The other one is null.

weight_sum

The accumulated weight behind the fit, which the next row’s weight_sum field reports.

position, target, ridge, feature_set, penalty

polars_online.spec.coef_index()’s columns: position indexes the flat coef list, and the others name the slot it belongs to.

cluster, state, class

coef_index’s columns too: the kmeans center, the hmm state or the ew_class class a position belongs to. Null for every other model.

term

"intercept", a feature’s name, "level" or "trend" for holt, or "rho" for deco.

coef

The coefficient, in the features’ original units. Null until the model’s first solve, and null wherever the model reports no number, as the coef field is on those rows: a fit withheld as too ill-conditioned to keep a digit, or an ew_class class no row has carried yet.

Raises

KeyError

spec is a name the bank does not have.

IndexError

spec is a position the bank does not have.

TypeError

group is not a str, a list of them with None allowed, or None.

ValueError

spec names a spec whose model has no coefficients to index: ew_cov, which gives statistics; seqtest, which gives evidence; micro, whose number of clusters varies; and marginal, rcov, corrchange, bocpd and audit.

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(df)
coefs = bank.coef()                                 # one row per coefficient
for term, value in coefs.select("term", "coef").iter_rows():
    print(f"{term}: {value:.2f}")                   # the truth: 1, 2 and -1
intercept: 0.99
x0: 1.97
x1: -1.00

To read one slot of a grid, filter on its columns, such as coefs.filter(pl.col("ridge") == 0.1). To lay the terms out as columns, pivot the frame on every column that tells two fits apart: pivot("term", index=["spec", "group", "instance", "target", "ridge", "feature_set", "penalty", "cluster", "state", "class"], values="coef").

See also

Notes

coef can be filled before pred_<t> is. pred_<t> waits for min_weight, while coef reads each solve the model has run, and a model’s solve schedule can run one sooner. For example, an ewridge with three coefficients, min_weight=10, a half-life of 100 and the default schedule carries coefficients on its third row, while its weight_sum reads near 2. polars_online.spec.ewridge() gives its schedule (solve_every). A coefficient read while weight_sum is below min_weight comes from fewer rows than the spec asks of a prediction.

Sweeping every spec skips the ones with no coefficients, since the question was about the coefficients in the bank. Naming one of them still raises, since then the question was about that spec.