rheon.polars.ModelBank.check

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

Return findings about the data each model was fed, one row per problem, the worst first.

Run it after a first pass over the data, before trusting a fit: it names features null on many rows, constant or collinear features, leakage, a clock that steps back, models that never warmed up, and more. To find what no model keeps, such as sentinel values or a frozen feed, put an rheon.spec.audit() in the bank, and read its counts with audit().

Parameters

spec

The spec to check, by name or by position. The default, None, checks every spec.

group

The group to check: one group key, as groups() reports it, or a list of keys, with None in the list for the null group. The default, None, checks every group. A key the bank has never seen gives no rows.

Returns

A DataFrame with one row per finding, in severity order, then spec order, then groups()’ order. A bank that has seen no rows gives an empty frame. The columns:

severity

error (the model cannot learn), warning (the fit is unreliable) or info: an Enum in that order, worst first.

code

Which check found it: a short name that stays the same across releases, one of the codes listed under Returns. It is a String, not an Enum as severity is. A minor release may add a check, and an Enum’s categories are part of its dtype, so a new code would change the column’s type under code that pins it.

spec, group

The spec and group, as summary() names them. group is null for not_checked, which is about the spec as a whole.

column

The input column the finding is about, where there is one.

value, threshold

The measured number, and the line it crossed. Null for withheld and not_checked.

message

What is wrong and, for most codes, what to do. It ends with a link to the code’s section of docs/DATA-QUALITY.md, which shows what the problem does to each model and how to fix it with Polars, before the bank, in a program that runs.

The codes every spec can find:

code

severity

fires when

nothing_learned

error

a model was fed rows and learned from none; the column null most often is named

missing

warning

a feature or the weight column is null, NaN, infinite or past 1e100 on 10% of the rows or more, each of them skipped; an error at 100%, and information for a target missing on half the rows or more, since sparse targets are a design, not a fault

few_learned

warning

more than half the rows were skipped for a missing feature or weight, whichever columns caused it

constant

error, warning

a target (error) or a feature (warning) took one value on every row

level_over_spread

warning, info

a feature’s |mean| / std passes the limit of a model that does not center it (Notes); information past 1e6 for the models that do

scales_apart

warning

the widest feature’s spread over the narrowest’s passes the limit of a model that takes one step for every column (Notes)

ridge_shrinks

warning

ewridge, huber or quantile without standardize, at ridge_scale="mean" (the default): the smallest ridge is a quarter of a feature’s variance or more, so it takes a fifth of that coefficient

collinear

warning

the largest variance inflation factor, or the condition index of the design the model solves, over the features passes 30, from the Gram of an ewridge or lasso (information for an ew_cov). A model with an intercept centers, so its index is the features’ correlation matrix’s, sqrt(d_max / d_min); one fitted through the origin reads Belsley’s uncentered index (rheon.gram.condition())

leakage

warning

a feature correlates with a target at 0.9999 or more, from the Gram or a marginal: it contains the target, both are one random walk, or the relation is nearly exact (a correlation of 0.9999 is an R² of 0.9998 for that feature alone)

step_back

warning

the clock stepped back, a reset or a late row under restart_after_step_back

resets

info

a model reset at a session_gap = "reset" or a step back

few_rows

warning

a regression learned from fewer rows than it has coefficients

group_sizes

info

the largest group learned from 100 times the rows of the smallest or more

never_settled

warning

a model has seen less than one half-life of clock (settled_frac below 0.5)

below_min_weight

warning

the weight a model’s weight_sum tops out at (weight_sum_settled) is below the spec’s min_weight, so its predictions stay null

withheld

warning

a model’s last row had its predictions withheld, with the reason

low_support

warning

the smallest coefficient’s data share is below 0.5, the command line’s readiness line; not repeated for a column already found constant, collinear or shrunk by the ridge

solve_failures

warning, info

a model’s solve_failures() count is above 0 (warning); for lasso, more than 10 (information)

not_checked

info

numpy is not installed, so collinear and leakage were not checked on a spec with a Gram and a group to read it from; said only of a spec with a group to check

An audit knows no roles, so its column’s missing share is information (an error at every row), and its constant a warning. step_back and resets are read from its summary() as for any spec. Its own codes, read from audit():

code

severity

fires when

sentinel

warning, info

a column holds +inf, -inf or a value past 1e100 (warning); holds NaN, not null (information); or one value is 1% of its usable rows or more, at least 5 times as often as any other can be (warning), unless the column has 10 values or fewer or is frozen

frozen

warning

the rows equal to the row before, less those the column’s value frequencies give by chance, outnumber the changes at its smallest step by five standard errors, (repeats - chance - at_step) / sqrt(repeats + at_step), where that step is less than half the column’s moves: an atom of changes at zero, which a stale feed adds and a column on a coarse grid (an hour, a regime, a price in ticks) does not show

few_values

info

10 distinct values or fewer over 100 usable rows or more

random_walk

warning

the Dickey-Fuller statistic is above -3.5 (the 1% critical value is -3.43) over 100 pairs of rows or more

heavy_tails

info

an excess kurtosis of 5 or more, or a value 10 robust standard deviations or more from the median; value is the robust z where it fired, the kurtosis otherwise, each beside its own threshold

duplicate

warning

with pairs=True, two columns correlate at 0.999 or more

duplicate_stamps

info

a row shares the clock of the row before

gaps

info

a step reaches gap_cap

irregular_clock

info

the regular steps’ coefficient of variation is 0.5 or more

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.

Examples

This code reads df from Example data:

spec = rp.spec.ewridge(
    "ridge", targets=["y"], features=["x0", "x1", "x2"], half_life=100.0
)
bank = rp.ModelBank([spec])
bank.fit(df.with_columns(x2=pl.lit(1.0)))           # x2 made constant
problems = bank.check()                             # the worst first
print(problems.select("severity", "code", "column").rows())
[('warning', 'constant', 'x2')]

To keep only what needs action, drop the information: problems.filter(pl.col("severity") != "info").

See also

Notes

check reads only what the bank keeps anyway: summary(), describe(), solve_failures(), last_row(), and gram(), marginal() or audit() where the spec has one. So it adds no work to the run, a bank loaded from its file gives the same findings as the bank that saved it, and the input’s chunking moves none of them.

Every threshold was measured before it shipped, for how often it finds a planted problem and how often it fires on clean data. How the checks work gives the clean streams, the planted problems and the numbers each line was set from.

level_over_spread and scales_apart read a limit for each model: the largest value at which its out-of-sample R² fell by no more than 0.05, at half-lives of 20, 200 and infinite (for rls’s level limit, at half-lives of 200, 2,000 and infinite, over 2,000 and 20,000 rows).

model

level limit

spread-ratio limit

ftrl

0.5

1.5

kalman, standardize=False

2

2

sgd, standardize=False

3

3

pa, standardize=False

3

2

rls

0.9 sqrt(W / delta_left)

30

rls’s level limit is read from the summary. A level lowers its accuracy only through the part of its prior delta I it still holds, delta_left = delta (1 - settled_frac) (all of delta without decay), set against the weight its fit holds, W (settled_frac * weight_sum_settled, or weight_sum without decay). So a decaying rls that has forgotten its prior is told nothing, at any level. The models that center their features (ewridge, lasso, huber, quantile, and kalman, sgd and pa at their default standardize=True) lost nothing at a level of 1e14 times the spread. Nor did they at spreads 1e8 apart, except where a narrow feature met an unstandardized ridge, which ridge_shrinks finds.

A level or a spread ratio counts only when it is five standard errors from what a centered column, or two of equal spread, would show by chance in that many rows. The Gram’s checks wait for ten Kish rows a coefficient. Both guards keep a small group quiet.

What it cannot see. describe() keeps every row’s moments undecayed, so a feature that drifted slowly can sit far above its recent spread without showing as a level. The Gram’s checks cover the three models whose Gram the bank exports (gram()), and leakage also reads a marginal. An audit measures what no model keeps: sentinel values counted apart from nulls, a frozen feed, a column of few distinct values, a level fed as a feature, heavy tails, duplicate columns, and the clock’s duplicate stamps, gaps and irregular spacing. Without one in the bank, nothing is said about them. A group a spec closes (group_close) leaves the bank with its model, so nothing is said about it either: the findings cover the groups still open. An audit refuses group_close for that reason.