rheon.polars.eval.clark_west

rheon.polars.eval.clark_west(df: pl.DataFrame, *, big: str, small: str, targets: Sequence[str] | None = None, big_suffix: str = '', small_suffix: str = '', lags: int = 0, group: str | Iterable[str] | None = None, half_life: float | None = None, min_samples: int = 30, name: str = 'clark_west') → pl.DataFrame[source]
rheon.polars.eval.clark_west(df: pl.LazyFrame, *, big: str, small: str, targets: Sequence[str] | None = None, big_suffix: str = '', small_suffix: str = '', lags: int = 0, group: str | Iterable[str] | None = None, half_life: None = None, min_samples: int = 30, name: str = 'clark_west') → pl.DataFrame
rheon.polars.eval.clark_west(df: pl.LazyFrame, *, big: str, small: str, targets: Sequence[str] | None = None, big_suffix: str = '', small_suffix: str = '', lags: int = 0, group: str | Iterable[str] | None = None, half_life: float, min_samples: int = 30, name: str = 'clark_west') → pl.LazyFrame

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

Use it when one spec’s features are a subset of the other’s, such as a spec with and without one more feature. It is Clark and West’s (2007) test, with Newey and West’s variance. For two specs that are not nested, use diebold_mariano(). For evidence you may read at every row as the rows come, use seqtest(), which asks which spec is closer on more rows, not which has the smaller mean squared error.

Parameters

df

A frame a bank returned, holding both specs’ output columns, or a query of one, a LazyFrame, which this reads one chunk at a time as diebold_mariano() reads one.

big, small

The names of the larger and the smaller spec’s output columns, as diebold_mariano()’s a and b.

targets

The targets whose residuals are compared; every target the two share by default.

big_suffix, small_suffix

The suffix of each side’s residual field, as diebold_mariano()’s a_suffix and b_suffix; "" by default.

lags, group, half_life, min_samples

diebold_mariano()’s own.

name

With half_life, the name of the record column added, "clark_west" by default.

Returns

Without half_life, a DataFrame with one row per group key and target: n, the rows read; mean, the mean of f (see Update equation); cw, the statistic; and p_value, its one-sided p-value. With half_life, df with a record column name added, holding cw_<t> per target t, the exponentially weighted statistic, null on the rows diebold_mariano()’s dm_<t> is and read as its Returns say, one test per row; given a LazyFrame, a LazyFrame of the same.

Raises

Whatever diebold_mariano() raises, with big and small in place of a and b.

Update equation

Per target, over the n rows where both have a residual:

f  = resid_small² - (resid_big² - (pred_small - pred_big)²)
   = resid_small² - resid_big² + (resid_big - resid_small)²
cw = f̄ · n / sqrt(S)          S as in diebold_mariano, of f

f̄ is the mean of f. cw is about N(0, 1) under the null that the smaller spec is the truth, and the test is one-sided: cw > 1.645 rejects the null at 5%. pred_small - pred_big equals resid_big - resid_small, so the two residual fields are all the test reads.

Examples

This code reads df from Example data:

small = rp.spec.ewridge("small", targets=["y"], features=["x0"], half_life=500.0)
big = rp.spec.ewridge("big", targets=["y"], features=["x0", "x1"], half_life=500.0)
out = rp.ModelBank([small, big]).fit_predict(df)
test = rp.eval.clark_west(out, big="big", small="small")
n, cw, p = test.select("n", "cw", "p_value").row(0)
print(f"{n} rows, cw {cw:.2f}, p {p:.2g}")   # cw above 1.645: x1 helps, at 5%
396 rows, cw 12.24, p 9.3e-35

See also

Notes

Diebold and Mariano’s differential leans toward the smaller of two nested specs. Under the null, the larger spec’s extra coefficients are zero but estimated, and their noise adds (pred_small - pred_big)² to its squared error on average. Clark and West add that term back.

cw is null where diebold_mariano()’s dm is, of f: where every f of a key is the same value, on a key with at most 4 (lags + 1) rows with an f, and, with half_life, before a key’s min_samples-th row and where S is zero but for rounding. Its lags are chosen as diebold_mariano()’s are: 0 only where f is uncorrelated from row to row, and about twice the horizon for a target that looks ahead.

Given a query, this holds and computes what diebold_mariano() does for one, of f in place of d, with the same agreement with the frame, beside what Polars reads of the query ahead of it, which Cut what Polars reads ahead bounds. Scoring a scan of two specs, a file in row groups of 100,000 rows, by a key of four values with lags=5 held at most 97 MiB at a million rows, 100 MiB at four million and 100 MiB at sixteen million with four threads, the read-ahead cut to one row group (POLARS_ROW_GROUP_PREFETCH_SIZE=1) and the allocator told to return freed pages at once, and 241, 425 and 787 MiB under Polars’ defaults, measured as diebold_mariano()’s Notes say.