rheon.spec.coef_fields

rheon.spec.coef_fields(spec: dict[str, Any]) → _Frame[source]

List every coefficient a spec reports, with its place in coef and its unnested column.

Use it to reach one coefficient in the nested output without writing its field’s name or its position by hand, or to read the column names unnest() gives. For one coef list’s layout alone, use coef_index().

Parameters

spec

A spec, as a builder of rheon.spec returns it.

Returns

A frame with one row per instance, target, grid point and term. It is empty, with the same columns, for the kinds that report no coefficients (ew_cov, seqtest, marginal, rcov, corrchange, bocpd and audit), and for micro, whose coef has no fixed layout. Its columns:

field

The coef list the coefficient sits in: coef, or coef@h500 for each half-life’s instance.

position

Its index in that list.

name

The column unnest() gives it: coef_{target}_{term}{combo}{instance}, so coef_y_x1__r0.5@h500 sits beside pred_y__r0.5@h500. The coef_ prefix and the target are there because a bare x1 would collide with the feature column of that name in the same frame. deco()’s are coef_{entry}{instance}, its entry rho or a block or pair of blocks (coef_tech, coef_tech_banks).

target, instance, half_life, lam, ridge, feature_set, penalty

As output_index() reports them. target is ew_class()’s label column, a deco() entry, and null for a kmeans() center and an hmm() state, which belong to no target.

cluster, state, class

The kmeans() center, the hmm() state or the ew_class() class whose coordinates the entry holds, and null for every other kind.

term

"intercept", a feature name, "level" or "trend" for holt(), or "rho" for deco().

Raises

ValueError

spec is not a valid spec, as for output_fields().

Examples

This code reads df from Example data:

grid = rp.spec.ewridge(
    "m", targets=["y"], features=["x0", "x1"], half_life=[100.0, 500.0], ridge=[1e-6, 0.5]
)
out = rp.ModelBank([grid]).fit_predict(df)    # a half-life grid by a ridge grid
cf = rp.spec.coef_fields(grid)
row = cf.filter(
    (pl.col("target") == "y")
    & (pl.col("term") == "x1")
    & (pl.col("ridge") == 0.5)
    & (pl.col("half_life") == 500.0)
).row(0, named=True)
slope = out["m"].struct.field(row["field"]).list.get(row["position"])
print(row["field"], row["position"], row["name"])
coef@h500 5 coef_y_x1__r0.5@h500

See also

Notes

The table is made by the Rust code that names the fields, from the order the models lay the list out in. That order is the intercept first when the spec has one, then every feature, each target and grid point in turn, in the order the pred fields are declared. A feature outside a feature set reads zero.