rheon.polars.ModelBank.fit_predict

ModelBank.fit_predict(df: DataFrame) → DataFrame[source]

Learn a frame’s rows, and return them with each spec’s output.

Use it for rows held in memory as a frame, when you want the output back as one frame. For a query or a stream too large to hold, use lf.rheon.fit_predict or fit_predict_batches(). To learn without keeping the output, use fit(), and to predict without learning, predict().

Parameters

df

The next rows of the stream, as one chunk: a DataFrame holding every column the bank’s specs read. Those are the specs’ targets and features, the clock, session, weight, group and label_clock columns they name, and the columns a formula target’s expression reads. In each group of a spec with a clock, the rows continue the group’s clock within each session. A step back is refused, unless restart_after_step_back is set below it, where the model resets.

Returns

df with one output column added for each spec, named after the spec, whose value in each row is a record of named fields. For a regression they are pred_<t>, resid_<t>, weight_sum, settled_frac, withheld_reason and coef, with support_coef where the model reports it, lasso’s penalty_selected_<t>, and the fields of the diagnostics switched on. Under a grid of half-lives every field read from one instance carries its suffix, such as weight_sum@h100 (an instance is the part of the model that fits one half-life). The fields that choose across instances (emit_selected, emit_averaged) and those emit_clocks writes carry none. Under a grid of ridge values, feature sets or penalties, each slot’s fields, its pred, its resid and its diagnostics, carry the slot’s suffix, such as pred_y__r0.1. output_fields lists a bank’s fields, rheon.spec describes them under What a spec writes, and docs/OUTPUTS.md gives every model’s. Each prediction is out-of-sample: the model makes it from what it learned before the row, and only then learns the row. coef, support_coef and the standard errors are the fit after the row.

Raises

TypeError

df is not a DataFrame, such as a LazyFrame.

ColumnNotFoundError

A column a spec reads is not in df. It is both a ValueError and Polars’ ColumnNotFoundError.

WrongTypeError

A column of a type its role cannot take. It is both a TypeError and a ValueError. When:

  • a column of df is UInt128, which rheon cannot read;

  • a target column (other than ew_class’s), a feature or a weight column is neither numeric nor Boolean;

  • a clock is not a number, a Boolean, a Datetime, a Date or a Duration (a Time starts again at midnight), or is an Int128;

  • a temporal column is read as a feature, target or weight, or by a formula target, other than as the clock;

  • a label_clock column’s type does not match the clock’s, a temporal one for a temporal clock and a numeric one for a numeric clock;

  • a group or session column, or an ew_class target, has a type with no text form, such as a nested one;

  • under group_close="monotone", the group column is not an integer, String or Categorical type.

ValueError

When:

  • a clock is a UInt64 past the largest Int64;

  • between chunks, an integer clock column changes its width or becomes a float, a float clock becomes an integer, or a group or session column changes between integer, float, text and Boolean keys;

  • a spec gives its clock the other kind of clock parameter: a plain number to a temporal clock, or a duration to a numeric one (Clock units in rheon.spec), or a gap_cap or restart_after_step_back finer than the clock’s smallest step;

  • the clock has a null or non-finite value, or a step between two of its values that is not a finite number;

  • a weight is negative (a null weight skips the row);

  • a spec is named like an input column, which its output column would replace;

  • a group’s clock steps back within a session, with restart_after_step_back not set, the default, or by no more than it. A chunk that overlaps what a loaded state has learned is refused the same way, and skip_learned() drops the overlap;

  • under group_close="monotone", a group key is null or below the key before it;

  • a label_clock time is before its row’s clock, or a group’s rows waiting for their label_clock times would pass max_held_rows under held_overflow="refuse", the default;

  • a formula target’s embargo is shorter than its longest look-ahead window_size;

  • a chunk would take a hard window’s snapshots past a window_budget that refuses;

  • a model refuses a value of its own, such as a strict_binary target that is not 0 or 1, as its builder’s docstring says;

  • the bank refused an earlier chunk after learning part of it (see Notes);

  • RHEON_MAX_THREADS is not a whole number, or is above 1024 (or the machine’s core count, where that is larger), on a bank with threads=None, at the first call that runs on the process’s threads.

RuntimeError

The bank is in use on another thread (ModelBank).

ReadinessWarning

A warning, not an error: issued when a finding that casts doubt on what a model reports first holds, such as a gate still unmet once the model has learned long enough to judge. That class’s docstring lists each finding and how often it is issued.

Examples

This code reads df from Example data:

spec = rp.spec.ewridge(
    "ridge", targets=["y"], features=["x0", "x1"], half_life=100.0, ridge=[1e-6, 0.1]
)
bank = rp.ModelBank([spec])
out = bank.fit_predict(df)                         # df, with the column "ridge" added
pred = out["ridge"].struct.field("pred_y__r0.1")   # the slot with ridge 0.1
first = pred.is_not_null().arg_true()[0]           # the first row after its warm-up
print(f"{pred.count()} of {out.height} rows predicted, the first at row {first}")
396 of 400 rows predicted, the first at row 4

See also

Notes

Chunk boundaries never change the numbers: a stream fed as one chunk or a thousand gives the same predictions to the bit. By default they move the rows coef is reported on, each group’s last row in the chunk; under coef_every or max_rows_between_coefs not even those.

A refused chunk leaves the bank as it was, so the corrected chunk can be fed. The bank checks every group’s clock steps in every spec over the whole chunk, and plays the chunk’s clock through each hard window’s snapshot schedule, before it learns a row. Two overruns of a window_budget are found only as the rows are learned: one under drift_action="reset", whose resets the check cannot foresee, and one where a snapshot grows within the chunk. Then the chunk is refused after some of it has been learned, and the bank refuses every later call that learns, predicts or saves: load its last saved state with load() and feed it the rows since that save.