polars_online.ModelBank.audit

ModelBank.audit(spec: str | int | None = None, group: str | Iterable[str | None] | None = None, *, table: Literal['columns', 'pairs', 'clock'] = 'columns', pooled: bool = False) → DataFrame[source]

Return what an audit spec counted, one row per column, pair of columns or clock.

Use it to read the counts an audit keeps about the data itself: sentinel values, frozen feeds, few distinct values, heavy tails, duplicate columns and the clock’s spacing. For the findings drawn from them use check(), and for the statistics every spec keeps, describe().

Parameters

spec

The audit spec to read, by name or by position (polars_online.spec.audit()). The default, None, reads the bank’s one audit spec.

group

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

table

Which table to return: "columns", the default, one row per (group, column); "pairs", one row per (group, pair of columns), for a spec built with pairs=True; or "clock", one row per group.

pooled

True merges the groups the frame covers into one audit, as if they were one stream, and drops the group column. The counts merge exactly and the moments to rounding, and the counters past the cap and the digests approximately, as polars_online.spec.audit() says. The default, False, gives a row per group.

Returns

A DataFrame led by spec, as every reader of the bank’s is, with every count an Int64. table="columns" has spec, group and column, then:

rows_fed, null_count, nan_count

The rows read, then two kinds of value a model cannot learn from: the nulls, as Polars’ null_count counts them, and the NaNs.

pos_inf_count, neg_inf_count, beyond_bound_count, count

The other kinds, each counted apart (beyond_bound_count is finite but past 1e100 in magnitude), and count, the usable values. The six counts add up to rows_fed.

mean, std, skew, kurtosis, min, max

Over the usable values: std with ddof = 1, and skew and the excess kurtosis as scipy’s biased estimators. Null where undefined: no usable value, one, or no spread.

median, mad, robust_z

The median and the median absolute deviation, exact while n_unique is not null and from a t-digest past the cap (about 1% of the rows in rank), and the largest robust z, max(max - median, median - min) / (1.4826 mad).

n_unique

The distinct usable values, as Polars’ n_unique counts them, with -0 read as 0. Null past the spec’s distinct_cap.

top_value, top_count, second_count, count_error

The most repeated value, its count and the next value’s count. They are exact while n_unique is, and past the cap each is at most count_error short of the truth, where a value not held has a count of at most count_error.

longest_run, equal_prev, adjacent, equal_by_chance

longest_run is the longest run of one value on consecutive rows. equal_prev counts the rows equal to the row before, out of adjacent, the rows whose row before was usable too. equal_by_chance is sum p_v^2 over the values’ shares p_v: the share of adjacent rows that independent rows would make equal, a lower bound past the cap.

min_change, min_change_count

The smallest nonzero change |x_t - x_(t-1)| between two consecutive usable rows (null before there is one), and how many changes sit at it. Two changes count as one size when they differ by no more than the rounding of one step of a grid, 8 eps of the largest magnitude seen. check()’s frozen weighs min_change_count against equal_prev, the changes at 0.

autocorr, unit_root_t

autocorr is the correlation of each usable row with the usable row before it. unit_root_t is the Dickey-Fuller statistic of that regression with a constant. It reads:

  • above about -3 for a random walk;

  • about -sqrt(n) for rows that do not persist;

  • +inf for a fit with no residual and a slope of 1, such as a row counter;

  • -inf for a fit with no residual and a slope below 1.

table="pairs" has spec, group, column_a, column_b, and, over the rows where both are usable, count, corr and equal, the rows on which the two are equal.

table="clock" has no rows for a spec without a clock. Its columns:

spec, group

As groups() reports them.

steps

The number of steps between consecutive rows.

duplicates

How many steps are 0.

gaps

How many steps are at or past gap_cap (a clock requires gap_cap).

regular

How many steps are neither.

step_mean, step_std, step_cv

The regular steps’ mean, population standard deviation and coefficient of variation.

step_max

The largest step of all.

On a temporal clock step_mean, step_std and step_max are a Duration in the clock column’s unit (milliseconds for a Date), on a number clock numbers in its units, and a Float64 before a chunk has said which.

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.

ValueError

When:

  • spec is None and the bank holds no audit spec, or several;

  • spec is not an audit spec;

  • table="pairs" reads a spec built without pairs=True;

  • table is not "columns", "pairs" or "clock".

Examples

This code reads df from Example data:

audit = po.spec.audit(
    "audit", columns=["x0", "x1"], clock="t", gap_cap=300.0, pairs=True
)
bank = po.ModelBank([audit])
holes = pl.when(pl.col("t") < 10).then(None).otherwise(pl.col("x1"))
bank.fit(df.with_columns(x1=holes))                 # x1 null on the first 10 rows
columns = bank.audit()                              # one row per (group, column)
pairs = bank.audit(table="pairs")                   # one row per (group, pair)
clock = bank.audit(table="clock")                   # one row per group
print(columns.select("column", "rows_fed", "null_count", "count").rows())
print(pairs.select("column_a", "column_b", "count").rows())
print(clock.select("steps", "regular", "gaps").rows())
[('x0', 400, 0, 400), ('x1', 400, 10, 390)]
[('x0', 'x1', 390)]
[(399, 399, 0)]

bank.audit(pooled=True) gives every group as one stream, with no group column.

See also

Notes

The counts are read from the state, so a bank loaded from its file reports what the bank that saved it would, and the stream’s chunking moves none of them.

A clock step is the time that passed between the two rows, uncapped: a model ages by at most gap_cap of it, and the audit counts all of it (across a session change under a numeric session_gap, that gap). A step back is refused, as for every spec. With restart_after_step_back each one resets the audit’s steps instead (0 resets at every one), and summary()’s clock_backwards and check()’s step_back count them.