fix(medications): widen the medication-effect intervals to a week of lag

The automatic Newey-West rule gives a lag of 3 on 30 to 270 daily
values, which leaves most of a physiological series' carry-over out of
the intervals. The lag is now at least 7. Reference values are
recomputed with statsmodels at the new default, and the lag-3 values
stay pinned with an explicit lag. A fit whose residual autocorrelation
exceeds 0.5 is flagged, and the card, the clinician view and the PDF
say the ranges may still be too narrow.
This commit is contained in:
Marc-André Bombeck committed 2026-10-21 23:20:28 +02:00
1 parent 99dfd68aad
commit cfd7c18d48
19 files changed
+208 -31

No files matched your search

@@ -2,16 +2,22 @@
* Exact statistics for the segmented (interrupted time series) regression.
*
* The reference values for the noisy fixtures were computed independently
* with statsmodels 0.14 (`OLS(...).fit(cov_type="HAC", cov_kwds={"maxlags":
* L, "use_correction": True}, use_t=True)`, L = floor(4·(n/100)^(2/9))) and
* scipy's `stats.t`. The fixture generator below is the same deterministic
* with statsmodels 0.14.4 (`OLS(...).fit(cov_type="HAC", cov_kwds={"maxlags":
* L, "use_correction": True}, use_t=True)`) and scipy's `stats.t`, at the
* default lag L = max(7, floor(4·(n/100)^(2/9))) = 7 and, to pin the HAC meat
* at a second lag, at the automatic L = 3 passed explicitly. To recompute:
* build the fixture below in Python (Python's floored `%`), fit the design
* [1, t, D, t·D] with the call above at each L, and read `params`, `bse` and
* `conf_int(0.05)`; the coefficients and R² do not depend on L. The fixture generator below is the same deterministic
* formula the reference script used, so a drift in the estimator, the HAC
* meat, the small-sample factor or the t quantile fails here.
*/
import { describe, expect, it } from "vitest";
import {
defaultHacLag,
fitSegmentedRegression,
MIN_HAC_LAG,
neweyWestLag,
studentTCdf,
studentTQuantile,
@@ -62,10 +68,27 @@ describe("fitSegmentedRegression", () => {
expect(fit!.rSquared).toBeCloseTo(1, 12);
});
it("matches statsmodels HAC on a contiguous 90-day series", () => {
it("matches statsmodels HAC on a contiguous 90-day series at the default lag", () => {
const fit = fitSegmentedRegression(fixture(40, 50))!;
expect(fit.n).toBe(90);
expect(fit.df).toBe(86);
expect(fit.hacLag).toBe(7);
expect(fit.levelChange.estimate).toBeCloseTo(-7.336815685253282, 9);
expect(fit.intercept.standardError).toBeCloseTo(0.3926378134611552, 9);
expect(fit.preSlope.standardError).toBeCloseTo(0.015434960504949637, 10);
expect(fit.levelChange.standardError).toBeCloseTo(0.5544100003043471, 9);
expect(fit.slopeChange.standardError).toBeCloseTo(0.02119259135971675, 10);
expect(fit.levelChange.ciLow).toBeCloseTo(-8.43894628913928, 8);
expect(fit.levelChange.ciHigh).toBeCloseTo(-6.234685081367285, 8);
expect(fit.slopeChange.ciLow).toBeCloseTo(-0.07897452738576954, 9);
expect(fit.slopeChange.ciHigh).toBeCloseTo(0.005284427179883318, 9);
expect(fit.rSquared).toBeCloseTo(0.8683372493940129, 10);
});
it("matches statsmodels HAC on a contiguous 90-day series at lag 3", () => {
const fit = fitSegmentedRegression(fixture(40, 50), { hacLag: 3 })!;
expect(fit.n).toBe(90);
expect(fit.df).toBe(86);
expect(fit.hacLag).toBe(3);
expect(fit.intercept.estimate).toBeCloseTo(139.46950523140714, 9);
expect(fit.preSlope.estimate).toBeCloseTo(-0.09725785860840647, 10);
@@ -82,9 +105,24 @@ describe("fitSegmentedRegression", () => {
expect(fit.rSquared).toBeCloseTo(0.8683372493940129, 10);
});
it("matches statsmodels HAC on a series with gaps", () => {
it("matches statsmodels HAC on a series with gaps at the default lag", () => {
const fit = fitSegmentedRegression(fixture(30, 30, { gaps: true }))!;
expect(fit.n).toBe(48);
expect(fit.hacLag).toBe(7);
expect(fit.levelChange.estimate).toBeCloseTo(-6.123258149435133, 9);
expect(fit.levelChange.standardError).toBeCloseTo(0.7919225818543464, 9);
expect(fit.levelChange.ciLow).toBeCloseTo(-7.71927324237417, 8);
expect(fit.levelChange.ciHigh).toBeCloseTo(-4.527243056496095, 8);
expect(fit.slopeChange.estimate).toBeCloseTo(-0.11857109294626766, 10);
expect(fit.slopeChange.ciLow).toBeCloseTo(-0.22143394263874167, 9);
expect(fit.slopeChange.ciHigh).toBeCloseTo(-0.01570824325379365, 9);
});
it("matches statsmodels HAC on a series with gaps at lag 3", () => {
const fit = fitSegmentedRegression(fixture(30, 30, { gaps: true }), {
hacLag: 3,
})!;
expect(fit.n).toBe(48);
expect(fit.hacLag).toBe(3);
expect(fit.levelChange.estimate).toBeCloseTo(-6.123258149435133, 9);
expect(fit.levelChange.standardError).toBeCloseTo(0.9608527406825199, 9);
@@ -106,9 +144,16 @@ describe("fitSegmentedRegression", () => {
expect(fitSegmentedRegression(fixture(2, 2, { noise: false }))).toBeNull();
});
it("uses the automatic Newey–West lag", () => {
it("uses the automatic Newey–West lag, never below a week", () => {
expect(neweyWestLag(90)).toBe(3);
expect(neweyWestLag(30)).toBe(3);
expect(neweyWestLag(10)).toBe(2);
expect(MIN_HAC_LAG).toBe(7);
expect(defaultHacLag(90)).toBe(7);
expect(defaultHacLag(270)).toBe(7);
expect(defaultHacLag(100_000)).toBe(neweyWestLag(100_000));
// A short series clamps to n − 1.
expect(fitSegmentedRegression(fixture(4, 4))!.hacLag).toBe(7);
expect(fitSegmentedRegression(fixture(3, 3))!.hacLag).toBe(5);
});
});
+17 -6
View File
@@ -13,11 +13,14 @@
* Daily health series are autocorrelated: today's blood pressure says a lot
* about tomorrow's. Plain OLS standard errors ignore that and come out too
* narrow, so the covariance here is the Newey–West estimator (Bartlett
* kernel) with the usual automatic lag `floor(4·(n/100)^(2/9))` and the
* small-sample factor n/(n−k). Intervals use Student's t with n−k degrees of
* freedom. This matches `statsmodels` OLS `cov_type="HAC"` with
* `use_correction=True` and `use_t=True`; the unit tests pin it to values
* computed there.
* kernel) and the small-sample factor n/(n−k). The lag is the usual automatic
* `floor(4·(n/100)^(2/9))`, but never below {@link MIN_HAC_LAG}: on 30 to 270
* daily values the automatic rule gives 3, while a physiological series
* carries over for about a week (weight, resting heart rate, blood pressure
* under a new medication), and three lags leave most of that out. Intervals
* use Student's t with n−k degrees of freedom. This matches `statsmodels` OLS
* `cov_type="HAC"` with `use_correction=True` and `use_t=True` at the same
* `maxlags`; the unit tests pin it to values computed there.
*
* Observations are taken in the order given, which for a daily series with
* gaps treats a gap as adjacent days. That is a known approximation of the
@@ -61,11 +64,19 @@ export interface SegmentedFit {
const K = 4;
/** Smallest Newey–West lag applied: a week of daily carry-over. */
export const MIN_HAC_LAG = 7;
/** Automatic Newey–West lag: floor(4·(n/100)^(2/9)). */
export function neweyWestLag(n: number): number {
return Math.floor(4 * Math.pow(n / 100, 2 / 9));
}
/** The lag applied by default: the automatic rule, at least {@link MIN_HAC_LAG}. */
export function defaultHacLag(n: number): number {
return Math.max(MIN_HAC_LAG, neweyWestLag(n));
}
/** Invert a small symmetric positive-definite matrix (Gauss–Jordan). */
function invert(matrix: number[][]): number[][] | null {
const size = matrix.length;
@@ -131,7 +142,7 @@ export function fitSegmentedRegression(
);
// Meat: Σ e_t² x_t x_tᵀ + Σ_l w_l Σ_t e_t e_{t−l} (x_t x_{t−l}ᵀ + x_{t−l} x_tᵀ)
const lag = Math.max(0, Math.min(options.hacLag ?? neweyWestLag(n), n - 1));
const lag = Math.max(0, Math.min(options.hacLag ?? defaultHacLag(n), n - 1));
const meat = Array.from({ length: K }, () => new Array<number>(K).fill(0));
for (let l = 0; l <= lag; l++) {
const w = l === 0 ? 1 : 1 - l / (lag + 1);