Skip to main content

anomaly()

Check that a measure of the table is in the normal range of its values in earlier runs.

Usage

Source

anomaly(
    window=30,
    method="mad",
    k=None,
    min_runs=5,
)

A step that measures something about the table (its row count, its column count, the fraction of Null values in a column, or the sum, average, or standard deviation of a column) usually compares the measure with a fixed value. With anomaly() in place of that value, the step checks that the measure isn’t unusual for the earlier runs of the validation instead: the range of normal values is found from the measure’s values in the last window= runs, and the step fails if the measure is outside of it. So row_count_match(count=pb.anomaly()) flags a day whose row count is far from those of the last 30 runs, without saying in advance what a normal row count is.

The earlier runs are given to the validation with Validate(history=), as with baseline(). Where baseline() gives a single value that’s then compared with a tolerance, anomaly() gives a range that’s found from the spread of the earlier values, so it takes no tol=. It can be used in these places:

Parameters

window: int | None = 30

The number of earlier runs to find the normal range from: the last window= runs that have the measure. The default is 30, and None uses all of them.

method: Literal["mad", "iqr", "zscore"] = "mad"

How the normal range is found from the earlier values (see The Normal Range below): "mad" (the default) is the median plus or minus k= scaled median absolute deviations, "iqr" is the quartiles widened by k= interquartile ranges, and "zscore" is the mean plus or minus k= standard deviations.

k: float | None = None

How wide the normal range is, in units of the method’s spread. The default, None, is 3 for "mad" and "zscore" and 1.5 for "iqr" (the usual choices for each). A larger k= flags fewer values.

min_runs: int = 5
The number of earlier runs needed for a normal range. With fewer runs than this (as in the first days of a new validation), the step passes and the report has a note saying so. The default is 5, since a spread can’t be found from only one or two values.

Returns

Anomaly
A marker for the expected range, which is resolved when the validation is interrogated.

The Normal Range

The three methods are simple estimators, so that it’s always possible to say why a value was flagged:

  • "mad": the median of the earlier values, plus or minus k= times their median absolute deviation (MAD, the median of the distances from the median), scaled by 1.4826 so that it estimates the standard deviation of normally distributed values. The median and the MAD aren’t moved much by a few unusual values among the earlier runs, so this is a good default.
  • "iqr": from the first quartile minus k= interquartile ranges (IQR, the third quartile minus the first) to the third quartile plus k= IQRs. These are the fences of a box plot, and they are robust to unusual values as well.
  • "zscore": the mean of the earlier values, plus or minus k= standard deviations. The mean and the standard deviation are moved by unusual values, so an earlier drop widens the range.

The range is limited to the values the measure can take: counts and standard deviations aren’t below 0, a fraction of Null values is between 0 and 1, and a range of counts is narrowed to whole numbers. When most of the earlier values are the same, the spread is 0 with "mad" and "iqr", and the range is that value.

Examples

Let’s say that an orders table is validated every day. Here are ten days of runs, each recording the table’s row count:

import pointblank as pb
import polars as pl


def orders_on(n_rows):
    return pl.DataFrame({"amount": [20.0] * n_rows})


runs = [
    pb.Validate(data=orders_on(n), tbl_name="orders").row_count_match(count=100).interrogate()
    for n in [98, 104, 101, 99, 103, 100, 97, 102, 105, 100]
]

history = pb.MultiValidate(*runs)

Today’s table has 82 rows. The step checks that the row count is in the normal range of the earlier runs:

validation = (
    pb.Validate(data=orders_on(82), tbl_name="orders", history=history)
    .row_count_match(count=pb.anomaly())
    .interrogate()
)

validation
STEP COLUMNS VALUES TBL EVAL UNITS PASS FAIL W E C EXT
#4CA64C66 1
row_count_match
row_count_match()
— 92 – 109 (normal range) ✓ 1 0
0.00
1
1.00
— — — —

Notes

Step 1 (anomaly) Normal range: the median ± 3 × the scaled MAD of the runs from 2026-12-04 20:52:41 UTC to 2026-12-04 20:52:41 UTC (n = 10).

The VALUES column shows the normal range, found from the median and the MAD of the ten earlier row counts, and 82 rows is well below it, so the step fails. The note below the table says how the range was found. With method="iqr" or method="zscore", and with a different k=, the range is found in another way or made wider or narrower.

See Also

baseline()

Compare with a single value from earlier runs, with a tolerance.

Validate

The history= argument, which gives the earlier runs.

write_results()

Keep the results of a run for later runs to compare with.