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, useseqtest(), which asks which spec is closer on more rows, not which has the smaller mean squared error.Parameters
dfA frame a bank returned, holding both specs’ output columns, or a query of one, a
LazyFrame, which this reads one chunk at a time asdiebold_mariano()reads one.big,smallThe names of the larger and the smaller spec’s output columns, as
diebold_mariano()’saandb.targetsThe targets whose residuals are compared; every target the two share by default.
big_suffix,small_suffixThe suffix of each side’s residual field, as
diebold_mariano()’sa_suffixandb_suffix;""by default.lags,group,half_life,min_samplesdiebold_mariano()’s own.nameWith
half_life, the name of the record column added,"clark_west"by default.
Returns
Without
half_life, aDataFramewith one row pergroupkey and target:n, the rows read;mean, the mean off(see Update equation);cw, the statistic; andp_value, its one-sided p-value. Withhalf_life,dfwith a record columnnameadded, holdingcw_<t>per targett, the exponentially weighted statistic, null on the rowsdiebold_mariano()’sdm_<t>is and read as its Returns say, one test per row; given aLazyFrame, aLazyFrameof the same.Raises
Whatever
diebold_mariano()raises, withbigandsmallin place ofaandb.Update equation
Per target, over the
nrows 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 off.cwis aboutN(0, 1)under the null that the smaller spec is the truth, and the test is one-sided:cw > 1.645rejects the null at 5%.pred_small - pred_bigequalsresid_big - resid_small, so the two residual fields are all the test reads.Examples
This code reads
dffrom 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
diebold_mariano(), the test for two specs that are not nested.compare_specs(), whose Notes say when to use each test.
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.cwis null wherediebold_mariano()’sdmis, off: where everyfof a key is the same value, on a key with at most4 (lags + 1)rows with anf, and, withhalf_life, before a key’smin_samples-th row and whereSis zero but for rounding. Itslagsare chosen asdiebold_mariano()’s are: 0 only wherefis 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, offin place ofd, 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 withlags=5held 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 asdiebold_mariano()’s Notes say.