rheon.polars.ModelBank.marginal

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

Return the feature and target pairs a marginal spec keeps, one row per pair.

Use it to rank features by how each one alone relates to a target, from the bank or from one loaded from a state file. For a spec of another model use gram(), which gives an ew_cov’s moments, and for what an audit counted, audit().

Parameters

spec

The marginal spec to read, by name or by position.

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 an empty frame.

Returns

A DataFrame with one row per (group, instance, feature, target), an instance being the part of the model that fits one half-life: groups sorted, targets in the spec’s order, and features in the spec’s order within each. Its moments are exponentially weighted (EW). The columns:

spec, group, instance

The spec’s name, and the group and instance as gram() reports them: "" for a spec without a group column, and "" for a single instance, else the field suffix, such as "@h500".

half_life, lam

The instance’s decay, as coef() gives it.

feature, target

The pair’s feature column, and the target’s name: its column, unless rheon.target() gave it another name.

weight_sum

The target’s accumulated weight W_t: the rows where the target was present, weighted and decayed. It differs from the output’s weight_sum when targets have different null patterns.

n_kish

W_t^2 / Q_t, with Q_t the accumulated squared weight: Kish’s effective sample size, the number of equally weighted rows that carry the same information. Null before the target’s first row, and under a hard window where subtracting the rows that leave keeps no digit of Q_t (at most 64 eps times the history’s).

mean_x, var_x, mean_y, var_y, cov

The pair’s EW moments, in population form over the decayed weights, so a variance is never negative.

corr, beta, t_stat

cov / sqrt(var_x var_y); cov / var_x, the slope of the target on that feature alone; and corr * sqrt((n_kish - 2) / (1 - corr^2)), the t-statistic of that correlation at Kish’s size. Read t_stat as a scale to compare pairs by, not as a p-value: the rows are neither independent nor Gaussian. Each is null until weight_sum reaches the target’s min_weight, null while the instance’s settled_frac is below min_settled_frac, and null where it is undefined (a constant column, n_kish <= 2). Both gates withhold t_stat_serial, the lag_corr_* columns, phi_x, phi_y and the split columns too. n_serial, a count as n_kish is, stays.

With lags, four more list columns and four numbers:

lag_corr_xx, lag_corr_yy

Each series’ own autocorrelation at the spec’s lags (see Notes on the first rows).

lag_corr_xy, lag_corr_yx

The feature now against the target l rows back, and the target now against the feature l rows back, at each of cross_lags (every lag by default). Absent under cross_lags=[]. Notes say how to read them for a lead or a lag.

n_serial, t_stat_serial

n_kish divided by Bartlett’s serial-dependence factor, and the statistic against it. Null without serial_rule.

phi_x, phi_y

The per-row decays fitted under serial_rule = "geometric". Null otherwise, and null when fewer than two kept lags are positive. They are fitted from the lag correlations, so both gates withhold them where they withhold those.

With bins or bin_edges, the nonlinear view. These columns are present whenever the spec asked for bins, and hold empty lists and nulls until the edges are fixed:

bin_edges

The feature’s interior edges, fixed once and never moved. Ragged: a feature keeps only the bins it can support, so a binary feature has two bins and a constant one has a single bin.

bin_n, bin_mean_y, bin_var_y

The target’s weight, mean and variance inside each bin: the feature’s response curve. Each has one more entry than bin_edges, since the outer two bins are open. A bin no row has landed in has bin_n = 0 and null for the two moments. Under bin_stat="huber" the mean is Huber’s M-location of the bin’s rows, and the variance the mean square of their deviations from it, clipped at the cut. The weight is the rows’ either way (rheon.spec.marginal()).

split_gain, split_at

The fraction of the target’s variance removed by the best single cut of the feature, and where that cut falls. It compares directly with corr ** 2, so split_gain - corr ** 2 is the nonlinear surplus.

split_gain_t

The t_stat a corr would need to match that gain, against n_serial where there is one. It is optimistic, since the cut was chosen as the best of the candidates: a ranking, not a p-value.

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 is not a marginal spec.

Examples

This code reads df from Example data:

pairs = rp.spec.marginal(
    "pairs", targets=["y"], features=["x0", "x1", "x2"], half_life=500.0
)
bank = rp.ModelBank([pairs])
bank.fit(df)
table = bank.marginal("pairs")
table = table.sort(pl.col("t_stat").abs(), descending=True)   # strongest first
for feature, corr, beta in table.select("feature", "corr", "beta").iter_rows():
    print(f"{feature}: corr {corr:.2f}, beta {beta:.2f}")
x0: corr 0.86, beta 1.89
x1: corr -0.37, beta -0.81
x2: corr 0.04, beta 0.09

See also

Notes

The pairs are read from the state, so a bank loaded from a file reports them as the bank that saved it would. Feeding the rows in one chunk or a thousand gives the same numbers to the bit.

As in ew_cov, a lagged moment starts at 0 and takes its first product when a learned row has one l rows back. So each of the four lag columns reads 0.0, not null, at a lag l while a group has learned no more than l rows and weight_sum has reached min_weight. Below min_weight each is null, as corr is.

For two series that describe the same moment, a feature whose lag_corr_yx[0] exceeds its corr leads the target, and one whose lag_corr_xy[0] does follows it. That reading does not hold against a forward-looking target, one built from the rows after its own. There the target l rows back is built partly from the feature’s newest l rows, so a feature built from the same rows’ information shows lag_corr_xy above corr however it is sampled.