polars_online.eval

Scores of a bank’s output, and tests of one spec’s residuals against another’s.

The scores are out-of-sample R², IC (the correlation of the prediction with the target) and hit rate per spec, group and target, over a whole frame or in windows of the clock. sums() gives the same scores one chunk at a time, and grid_search() chooses a grid point from a stream’s own residuals.

Most calls read the frame a ModelBank returns: the input’s columns plus one output column per spec, whose value in each row is a record of the model’s fields, or a query of one. merge_sums() and from_sums() read the frames sums() makes. seqtest() can also test the sign of plain numeric columns, and grid_search() runs a bank over a query itself. The calls fall into five kinds:

kind

the calls

what they hold in memory, given a query

scores over the rows, in windows of the clock, or of several specs

metrics(), window_metrics(), compare_specs()

one chunk, and ten numbers per slot and key (and window)

scores one chunk at a time

sums(), merge_sums(), from_sums()

one chunk, and ten numbers per slot and key

one spec against another

diebold_mariano(), clark_west(), seqtest()

one chunk, and per key and target the last lags values and a few sums

the long form the scores read

unpack()

nothing until the query it returns runs, and then one chunk

a grid point chosen from a stream’s own residuals

grid_search()

three numbers per slot for each run of rows between the places a test span starts or its choice stops, and the bank’s state and one chunk

Give a call the query to score an output too long to hold. A query (a LazyFrame) runs only when its result is asked for, and every call that reads a bank’s output reads a query’s result one chunk at a time. The scores keep the ten sums per key that sums() keeps, added chunk to chunk as merge_sums() adds them. The tests of two specs keep sums of their own, and the last rows of each key, which the next chunk’s first rows pair with. So scoring the file a bank’s output was written to, pl.scan_parquet("out.parquet"), holds one chunk and the sums, not the file, and Polars what it reads of the file ahead of them, which Cut what Polars reads ahead bounds. A call whose result has a row for each row of the output returns a query of that result, as Polars’ own calls on a query return a query: unpack(), seqtest(), and diebold_mariano() and clark_west() with half_life. The results are the frame’s, as each function’s Notes say. Given a frame, a call reads it in memory. To keep scores beside the model as it runs, set a spec’s emit_metrics, which keeps exponentially weighted R², IC and hit rate, in memory the size of the model’s state.

Pass the spec itself as ``spec`` where a target is not a plain column. spec is the spec’s name, which names its output column, or the spec dict, which names the column too. Only the dict can say where a target’s values are when they are not in a column of the target’s name: a column renamed with polars_online.target(), or a formula target (see unpack()). The dict also says whether a fit is a classifier, whose scores take other names (metrics()’ binary).

Every call here that reads a bank’s output takes a LazyFrame as well as a DataFrame, and refuses a query as it refuses the frame, with the same message, before the query runs. merge_sums() and from_sums() read the sums a call returned, and given a LazyFrame raise TypeError: pass lf.collect(), which runs the query and returns its result. The scores start from unpack() and raise what it raises. It raises a OutputColumnNotFoundError (a KeyError too) for a spec whose output column the frame does not have, TypeError for a column that is not a spec’s output or is the output of a model that predicts no target. A slot whose target column cannot be found raises a ColumnNotFoundError when spec is the spec dict, and ValueError when it is the spec’s name alone. A group, clock or weight column the frame does not have raises a ColumnNotFoundError too. A group column of the scores named like a column they make (RESERVED, SUM_FIELDS, a score’s name or window_start), and a clock or weight column named like one of RESERVED, raise ValueError, since the column made would take its place: rename it. An Object group column raises WrongTypeError, since a query’s chunks cannot be matched by it: cast it to String or Categorical.

group names one key column, as a spec’s group does, or a list of them, and None, the default, is no key. min_samples is the fewest rows a key is scored on, as Polars’ min_samples is: a key with fewer rows keeps its row, with its count n filled and its scores null. The keywords by and min_obs and the function rolling_metrics are refused, and the error names the keyword or function that replaced each: group, min_samples and window_metrics(). window_metrics() refuses window_size the same way, naming every. spec_name (spec_names in compare_specs()) is read as spec, with a PolarsOnlineDeprecationWarning. Given beside a spec passed by position, it raises TypeError, and a spec name given beside a spec dict whose name differs raises ValueError.

docs/DIAGNOSTICS.md says which question each call and each diagnostic answers, with its threshold, its false-alarm rate as measured, and a recipe that runs.

polars_online.eval.SUM_FIELDS = ('n', 'w', 'mean_y', 'mean_pred', 'm2_y', 'm2_pred', 'cov', 'sse', 'hits', 'signed')

The columns sums() produces beside the keys, in order: ten numbers per (key, slot) are all the memory evaluating a stream takes.

polars_online.eval.RESERVED = ('slot', 'target', 'pred', 'y')

Column names unpack() produces. Input columns with these names are dropped rather than duplicated (the target’s values come back as y).

Scores

The fields of a bank’s output pulled into columns, and scores of its predictions over a frame or one chunk at a time.

polars_online.eval.unpack

Turn a spec’s output into long form, one row per input row and slot.

polars_online.eval.metrics

Score a spec’s out-of-sample predictions over a whole frame.

polars_online.eval.window_metrics

Score a spec’s out-of-sample predictions in windows of the clock.

polars_online.eval.sums

Reduce a bank’s output, or one chunk of it, to the sums its scores are read from.

polars_online.eval.merge_sums

Add the sums of disjoint sets of rows, as if their rows were one chunk.

polars_online.eval.from_sums

Read metrics()’ scores from the sums sums() made.

Comparing specs

Tests of one spec’s errors against another’s, and a search over a grid of settings.

polars_online.eval.compare_specs

Score several specs’ out-of-sample predictions in one table.

polars_online.eval.grid_search

Score every grid point in one pass over a stream, and choose one from the rows before each test span.

polars_online.eval.diebold_mariano

Test whether two specs’ out-of-sample squared errors differ on average, by Diebold and Mariano’s test.

polars_online.eval.clark_west

Test whether a larger spec predicts better than a smaller one nested in it, by Clark and West’s test.

polars_online.eval.seqtest

Test a sign sequentially by betting, over a frame or a query.