rheon.spec.seqtest¶
- rheon.spec.seqtest(name: str, *, targets: Sequence[str | Target | FormulaTarget | _Expr], a: str | None = None, b: str | None = None, a_suffix: str | None = None, b_suffix: str | None = None, **common: Unpack[CommonKwargs]) dict[str, Any][source]¶
A sequential test of a target’s sign by betting, whose evidence can be read at any row.
Use it to ask whether a target is positive more often than negative, or, with
aandb, whether one spec of the bank predicts closer than another. You can read the answer at every row and act the first time it is enough, which a p-value checked over and over does not allow. Userheon.polars.eval.seqtest()for the same test over a frame in memory, andrheon.polars.eval.compare_specs()for several specs’ scores over the whole stream in one table.Parameters
The stream parameters every builder takes, such as
clock,group,sessionandrestart_after_step_back, are inrheon.spec. This model is not a regression and makes no prediction. It reads only itstargets(or the residuals of the specsaandbname) and takes nofeatures: a spec dict or a TOML table saysfeatures = []. A trial is a row, soweightis refused. A test that forgot its losses would not keep its guarantee, sohalf_lifeandlamare refused.min_weightdefaults to 0, andfit_intercepthas nothing to act on. The test resets at a session change undersession_gap = "reset"and at a step back pastrestart_after_step_back, and starts over at each session span undergroup_close = "session". This model’s own:a,b,a_suffix,b_suffixCompare two specs of the same bank. Each target
tnames a residual field both carry:resid_<t>on each side, plus the side’s grid suffix when it is a grid (a_suffix="@h50"picksresid_<t>@h50ofa, and"__r0.1"a ridge value). The sign tested is that of|resid_b| - |resid_a|, positive whenawas closer on the row. Any loss that grows with|resid|(squared, absolute, Huber) gives the same sign, so this tests “abeatsb” under any of them. The bank runsaandbfirst, so the comparison reads the same out-of-sample residuals their output columns report. A row where either side is null (warm-up, a skipped row) is a row the test sits out. Default none: the test is of each target’s own sign.aandbare given together, and a suffix only beside them. Two instances of one grid (an instance is the part of a model that fits one half-life), such asa_suffix="@h20"againstb_suffix="@h400", are a comparison like any other.
Output
One output column, named after the spec, whose value in each row is a record of these fields, per target
tand read before the row is learned. docs/OUTPUTS.md#seqtest gives each field’s type and when it is null:log_e_pos_<t>,log_e_neg_<t>The log wealth (
log E) of the two gamblers of The update, one betting that the target is positive and one that it is negative. 0 is no evidence, andlog(20), about 3.0, is level 0.05.n_pos_<t>,n_neg_<t>The signs counted so far (
Int64). A zero or null target bets nothing and counts nothing.weight_sumThe weight the model has accumulated before the row, decayed by nothing: the count of rows before it.
settled_fracAlways null, since nothing decays.
withheld_reasonWhy the row’s fields were withheld: null on every row at the default
min_weightof 0, andbelow_min_weightwhere amin_weightgiven is not yet reached.
With
aandb, these fields take the place oflog_e_pos_<t>,log_e_neg_<t>,n_pos_<t>andn_neg_<t>:log_e_a_<t>,log_e_b_<t>The log wealth of the gambler betting that
ais closer on the next row, and of the one betting thatbis:log_e_a_<t>pastlog(1/alpha)is evidence at levelalphathatabeatsb.wins_a_<t>,wins_b_<t>The rows so far on which
awas closer, and on whichbwas (Int64). A tie, or a row where either side is null, counts for neither.
There is no
coef, and no diagnostic that reads residuals applies, since the model makes no prediction.Raises
As every builder does (
rheon.spec),TypeErrorforfeatures, which this model does not take, andValueErrornaming the problem for:weight,half_lifeorlam;awithoutbor the reverse, and a suffix without them;aandbnaming the same spec with no suffix or the same one, or either naming this spec.
The bank refuses, by name, when it is built: a side that is not in the bank or is itself a
seqtest, and a target either side has no residual for.The update
The model keeps, per target, the wealth of two gamblers:
E_posbets that the next sign is positive, andE_negthat it is negative, each 1 at the first row. The row’s value is reduced to its signsin{-1, 0, +1}.n_posandn_negare the counts of positive and negative rows before this one, andn = n_pos + n_neg:stake_pos = max(0, (n_pos - n_neg) / (n + 1)) E_pos *= 1 + stake_pos * s stake_neg = max(0, (n_neg - n_pos) / (n + 1)) E_neg *= 1 - stake_neg * s
The stakes are computed from the rows before.
(E_pos + E_neg) / 2is the two-sided e-value (The guarantee in Notes).Example
This code tests two targets, one positive half the time and one 58% of the time, and prints the most evidence each reached and the first row where it passed level 0.05:
import numpy as np rng = np.random.default_rng(3) fair = rng.standard_normal(2_000) # positive half the time up = 0.2 + rng.standard_normal(2_000) # positive 58% of the time pl.DataFrame({"fair": fair, "up": up}).write_parquet("signs.parquet") sign = rp.spec.seqtest("sign", targets=["fair", "up"]) out = pl.scan_parquet("signs.parquet").rheon.fit_predict([sign]).collect() out = out.unnest("sign") for t in ["fair", "up"]: e = out[f"log_e_pos_{t}"] crossed = (e >= np.log(20)).arg_true() # level 0.05 first = crossed[0] if len(crossed) else None print(f"{t}: largest log_e_pos {e.max():.2f}, first row past log(20): {first}")
fair: largest log_e_pos 0.51, first row past log(20): None up: largest log_e_pos 19.81, first row past log(20): 46
See also
rheon.polars.eval.seqtest(), the same computation in Polars expressions over a frame in memory.rheon.polars.eval.compare_specs(), several specs’ scores in one table.
Notes
The stake.
(n_pos - n_neg) / (n + 1)is2p - 1for the Krichevsky-Trofimov estimatep = (n_pos + 1/2) / (n + 1)ofP(s = +1). That is the stake a gambler with aBeta(1/2, 1/2)prior puts on the next sign, clipped so that each side bets only on the direction it tests.The guarantee. Under the null (given everything before it, a row is at least as likely to be negative as positive),
E_posis a non-negative supermartingale withE_pos[0] = 1. Ville’s inequality then givesP(max_t E_pos[t] >= 1/alpha) <= alpha. That is the whole guarantee.log_e_pos >= log(1/alpha)on any row rejects “no more positives than negatives” at levelalpha, and the stream can be read at every row and stopped the moment it crosses. A p-value cannot be: checking it repeatedly inflates its error rate. Nothing about the target but its sign is assumed: no independence of the sizes, no bound, no variance. What it does not test is the size: a stream up by a hair 60% of the time and down by a mile the rest rejects.