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
marginalspec 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 anew_cov’s moments, and for what anauditcounted,audit().Parameters
specThe
marginalspec to read, by name or by position.groupThe group to read: one group key, as
groups()reports it, or a list of keys, withNonein 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
DataFramewith 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,instanceThe spec’s name, and the group and instance as
gram()reports them:""for a spec without agroupcolumn, and""for a single instance, else the field suffix, such as"@h500".half_life,lamThe instance’s decay, as
coef()gives it.feature,targetThe pair’s feature column, and the target’s name: its column, unless
rheon.target()gave it another name.weight_sumThe target’s accumulated weight
W_t: the rows where the target was present, weighted and decayed. It differs from the output’sweight_sumwhen targets have different null patterns.n_kishW_t^2 / Q_t, withQ_tthe 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 ofQ_t(at most64 epstimes the history’s).mean_x,var_x,mean_y,var_y,covThe pair’s EW moments, in population form over the decayed weights, so a variance is never negative.
corr,beta,t_statcov / sqrt(var_x var_y);cov / var_x, the slope of the target on that feature alone; andcorr * sqrt((n_kish - 2) / (1 - corr^2)), the t-statistic of that correlation at Kish’s size. Readt_statas a scale to compare pairs by, not as a p-value: the rows are neither independent nor Gaussian. Each is null untilweight_sumreaches the target’smin_weight, null while the instance’ssettled_fracis belowmin_settled_frac, and null where it is undefined (a constant column,n_kish <= 2). Both gates withholdt_stat_serial, thelag_corr_*columns,phi_x,phi_yand the split columns too.n_serial, a count asn_kishis, stays.
With
lags, four more list columns and four numbers:lag_corr_xx,lag_corr_yyEach series’ own autocorrelation at the spec’s lags (see Notes on the first rows).
lag_corr_xy,lag_corr_yxThe feature now against the target
lrows back, and the target now against the featurelrows back, at each ofcross_lags(every lag by default). Absent undercross_lags=[]. Notes say how to read them for a lead or a lag.n_serial,t_stat_serialn_kishdivided by Bartlett’s serial-dependence factor, and the statistic against it. Null withoutserial_rule.phi_x,phi_yThe 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
binsorbin_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_edgesThe 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_yThe 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 hasbin_n = 0and null for the two moments. Underbin_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_atThe 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, sosplit_gain - corr ** 2is the nonlinear surplus.split_gain_tThe
t_statacorrwould need to match that gain, againstn_serialwhere there is one. It is optimistic, since the cut was chosen as the best of the candidates: a ranking, not a p-value.
Raises
KeyErrorspecis a name the bank does not have.IndexErrorspecis a position the bank does not have.TypeErrorgroupis not a str, a list of them withNoneallowed, orNone.ValueErrorspecis not amarginalspec.
Examples
This code reads
dffrom 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
rheon.spec.marginal(), the spec, its lags and its bins.closed_groups(), the same columns for each group that has closed, aspair_<column>.gram(), the moments of anewridge,lassoorew_cov.
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 onelrows back. So each of the four lag columns reads 0.0, not null, at a laglwhile a group has learned no more thanlrows andweight_sumhas reachedmin_weight. Belowmin_weighteach is null, ascorris.For two series that describe the same moment, a feature whose
lag_corr_yx[0]exceeds itscorrleads the target, and one whoselag_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 targetlrows back is built partly from the feature’s newestlrows, so a feature built from the same rows’ information showslag_corr_xyabovecorrhowever it is sampled.