Skip to content

measure: distance to criticality (d2) for univariate time series - #443

Merged
HarperZ9 merged 2 commits into
mainfrom
feat/measure-criticality
Nov 5, 2026
Merged

HarperZ9 merged 2 commits into
mainfrom
feat/measure-criticality

Conversation

@HarperZ9

@HarperZ9 HarperZ9 commented Nov 5, 2026

Copy link
Copy Markdown
Owner

What it does

harness.measure.criticality measures how close one time series runs to criticality: d2, the KL divergence rate in bits per step from the series' best-fit AR model to the nearest critical AR model of the same order. Method: Sooter, Fontenele, Barreiro, Ly, Hengen and Shew 2026, PNAS 123(44) e2526984123 (CC BY 4.0).

  • d2(x, order, beta, fit, dt, err): Yule-Walker or least-squares fit, the paper's delta-method error bar, segmented series, a scope on every result.
  • d_beta: an exact closed form derived here (half the log of the order-(n - beta/2) prediction error variance of the differenced fitted process). It replaces the paper's numerical search.
  • sliding, detrend, kendall_tau: time-resolved d2 and the early-warning trend. sliding detrends with the past only by default, because a two-sided smoother let a leap fake an early warning in a learning-curve study.
  • sims: AR, Hawkes and Langevin models with exact truths, for planted checks.

Standard library only, under harness/measure/ beside harness/studyrun (#436, merged).

Clean room

The authors' MATLAB toolkit is CC BY-NC 4.0. It was read to check conventions and was neither copied nor run. The code follows the paper's equations; the closed form is this package's own derivation.

Checks

  • tests/test_measure_criticality.py, 18 tests. The closed form is compared with a direct numerical minimisation of the paper's integral over the paper's own parameterisation (scipy, test-only; skipped where absent), and a formula without the differencing must fail that comparison. The causal-detrend test changes only future values and requires the past to stay identical.
  • A numpy port of the same package, used for the benchmark study, agrees with this one within 1.3e-14 nats over 1,200 random kernels and on Yule-Walker, unbiased and OLS fits.
  • Benchmarks (preregistered study, forecasts hashed before power, 13 of 13 verdicts pass): Hawkes log r^2 0.992; Langevin linear r^2 0.990 against the paper's 0.95; AR(1) worst miss 0.010 in log10; white noise 0.1317 against 0.1315; the paper's bursty limit reproduces (10.7 x overestimate). Numbers in docs/features/criticality.md.
  • Local: file gate, path length, verifier stdlib, claim language and public instructions gates clean; changelog, shard, docs-site, feature-doc and public-instructions tests pass.

🤖 Generated with Claude Code

HarperZ9 and others added 2 commits November 5, 2026 05:16
harness.measure.criticality measures the KL rate from a series' best-fit
AR model to the nearest critical AR model of the same order (Sooter et
al. 2026, PNAS, CC BY 4.0). d_beta uses an exact closed form, checked
against numerical minimisation of the paper's integral; a formula
without the differencing fails that check. Standard library only.

Clean room: the authors' MATLAB toolkit is CC BY-NC 4.0 and was read for
conventions only, never copied or run.

sliding() detrends with the past only by default: a two-sided smoother
let a leap fake an early warning in a learning-curve study.

Docs: docs/features/criticality.md with the reproduced benchmarks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
My first commit rewrote every line ending in docs/features/README.md. This
restores them and keeps only the one new entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HarperZ9
HarperZ9 merged commit 681810f into main Nov 5, 2026
79 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant