Skip to content

chauvenet_criterion

← Robust filters

chauvenet_criterion is one of the other three classic rules, with the same (clean, idx) return as hampel_filter. Chauvenet rejects when the expected number of samples that far out, \(N \cdot P\), is below 0.5.

The number of days flagged out of 500, for 8 window radii and 8 costs of the bad day.

The surface shows how many days the function flags out of 500 on the stream of the example, for 8 window radii and 8 costs of the bad day. The height is the number of days flagged.

A first example: daily order execution cost

The stream is the one of the first example of the section: 500 days of the cost of executing an order, and one bad day, day 250, at 25.0.

import numpy as np
from dense_armor.core.online.anomaly.robust_filters import chauvenet_criterion

rng = np.random.default_rng(0)
cost = 12.0 + 1.5 * rng.standard_normal(500)
cost[250] = 25.0
clean, idx = chauvenet_criterion(cost, radius=15)
print(250 in idx, round(float(clean[250]), 2), len(idx))
True 11.48 10

The function flags day 250 and replaces it with the local median, 11.48. It also flags 9 ordinary days, 10 positions in all.

The daily cost in grey; every flagged day is a pink line from the usual level to its cost.

The pink lines are the 10 days that the function flags.

The same on a robot

import numpy as np
from dense_armor.core.online.anomaly.robust_filters import chauvenet_criterion

rng = np.random.default_rng(42)
v = 0.5 + 0.1 * rng.standard_normal(3000)
v[1500] = 2.5
clean, idx = chauvenet_criterion(v, radius=15)
print(1500 in idx, len(idx))
True 48

It catches the collision and also flags 47 ordinary samples.

How it works

It returns (clean, idx), as explained on the page of Robust filters.

For point \(i\), with the window as the sample (mean \(\mu\), standard deviation \(\sigma\), size \(N\)), \(d = |x_i - \mu| / \sigma\) and \(P = \mathrm{erfc}(d / \sqrt 2)\). If \(N \cdot P < 0.5\) the point is rejected. It uses the mean and the standard deviation, not robust ones, as in the original formulation: on a window with more than one outlier they are already distorted before point \(i\) is judged. For such windows hampel_filter or tukey_fences, which use robust statistics, are the better choice.

The parameters

chauvenet_criterion(x, radius=10)

Parameter Default What it does
x required the whole series, a one-dimensional array
radius 10 half-width of the window, which is centred on each sample: radius = 15 is a window of 31 samples

API reference

chauvenet_criterion

chauvenet_criterion(
    x: ndarray, radius: int = 10
) -> tuple[np.ndarray, list[int]]

Chauvenet's criterion (1863) on a local window of radius radius.

For point i, with the window as the sample (mean mu, std sigma, size N): d = |x[i] - mu| / sigma; P = erfc(d / sqrt(2)) (two-tailed probability of such a deviation under a Gaussian hypothesis). If N*P < 0.5 -- the expected number of deviations equal to this one, repeating the experiment, is less than half a sample -- the point is rejected as an outlier.

Note: it uses mean/std (not robust), as in the original formulation -- on a window with MORE than one outlier, mean/std are already distorted by the other outliers before even judging point i (known and documented limit of the original criterion, not specific to this implementation; for windows with multiple outliers prefer hampel_filter or tukey_fences, which use robust statistics).

Examples:

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> x = rng.normal(0, 1, 60)
>>> x[30] = 50.0
>>> clean, idx = chauvenet_criterion(x, radius=5)
>>> 30 in idx
True
>>> abs(float(clean[30])) < 5.0
True
Source code in dense_armor/core/online/anomaly/robust_filters.py
def chauvenet_criterion(x: np.ndarray, radius: int = 10) -> tuple[np.ndarray, list[int]]:
    """Chauvenet's criterion (1863) on a local window of radius `radius`.

    For point i, with the window as the sample (mean mu, std sigma, size
    N): d = |x[i] - mu| / sigma; P = erfc(d / sqrt(2)) (two-tailed probability
    of such a deviation under a Gaussian hypothesis). If N*P < 0.5 -- the expected
    number of deviations equal to this one, repeating the experiment, is less
    than half a sample -- the point is rejected as an outlier.

    Note: it uses mean/std (not robust), as in the original formulation --
    on a window with MORE than one outlier, mean/std are already distorted by the
    other outliers before even judging point i (known and
    documented limit of the original criterion, not specific to this implementation;
    for windows with multiple outliers prefer hampel_filter or tukey_fences,
    which use robust statistics).

    Examples:
        >>> import numpy as np
        >>> rng = np.random.default_rng(0)
        >>> x = rng.normal(0, 1, 60)
        >>> x[30] = 50.0
        >>> clean, idx = chauvenet_criterion(x, radius=5)
        >>> 30 in idx
        True
        >>> abs(float(clean[30])) < 5.0
        True
"""
    x = as_default_array(x)
    n = len(x)
    out = np.copy(x)
    anomalie = []
    for i in range(n):
        w = _window(x, i, radius)
        if w.size < 3:
            continue
        mu, sigma = float(np.mean(w)), float(np.std(w))
        if sigma < 1e-12:
            continue
        d = abs(x[i] - mu) / sigma
        p = math.erfc(d / math.sqrt(2))
        if w.size * p < 0.5:
            anomalie.append(i)
            out[i] = float(np.median(w))
    return out, anomalie

Details

  • Sources. Chauvenet, W. (1863), A Manual of Spherical and Practical Astronomy, vol. 2.