Skip to content

Add transfer entropy (KSG + binned) — first bivariate information-flow measure - #1

Merged
Par-python merged 13 commits into
masterfrom
feat/transfer-entropy
Jun 3, 2026
Merged

Add transfer entropy (KSG + binned) — first bivariate information-flow measure#1
Par-python merged 13 commits into
masterfrom
feat/transfer-entropy

Conversation

@Par-python

Copy link
Copy Markdown
Owner

Adds entroscope.transfer, the library's first bivariate / information-flow
measure: directional transfer entropy TE(X→Y) (Schreiber 2000) with two
estimators — Kraskov KSG k-NN (default, no binning) and a binned histogram
cross-check. Mirrors the house compute / rolling / delta / plot contract,
takes two series, preserves the pandas index.

Born from an r/dataisbeautiful thread asking for transfer entropy + KSG.

Correctness — three independent authorities

The KSG estimator is validated against ground truth that shares no code with it:

Gate Authority Truth Result
A Gaussian closed form 0.5·log₂(1+b²/σ²) = 0.7828 bits KSG 0.7814, err 0.0014
B Kraskov 2004 analytic MI −0.5·ln(1−r²) = 0.2231 nats KSG 0.2341, err 0.011
C Hand-computed embedding exact vectors exact match

The binned-vs-KSG cross-check is treated as a consistency check only (a shared
embedding bug would fool both), so correctness rests on A/B/C.

Notes

  • v1 uses history length 1 (keeps embedding dim low — avoids the KSG variance
    blowup flagged in the thread).
  • Sample-size guard warns when n is small for the embedding dimension.
  • Binned estimator has inherent ~7% histogram bias; KSG is the default.
  • KSG rolling is O(windows × kNN) — slower than single-series measures.

Tests

132 passed, ruff clean. New tests/test_transfer.py (22 tests): the three
gates + independence + directionality + cross-check + API/contract + guard.

Also includes the correlation-stability example (this branch is stacked on it).

Par-python and others added 13 commits June 3, 2026 11:31
Demonstrates entropy of a rolling correlation series as a correlation-regime
stability gauge (idea from an r/dataisbeautiful thread). Ships alongside a
rolling-std baseline so the example shows that an entropy signal must be checked
against a trivial baseline before being trusted — on this synthetic data, std
separates the regimes and entropy of the smooth correlation swing does not.

- _synthetic.correlated_assets(): two return streams, stable shared-factor
  regime then a correlation breakdown
- examples/correlation_stability.py: rolling corr -> permutation entropy vs
  rolling-std baseline, prints regime separation for both
- tests: smoke test for the new example + a behavioral test asserting the
  generator's stable half is more correlated than its broken half

Uses only the existing public API; no library changes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Implements embed() in entroscope/_transfer_estimators.py — the single
source of (y_future, y_past, x_past) aligned columns consumed by both
the binned and KSG estimators. Verified in isolation against hand-computed
values (Gate C).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Remove unused imports and split semicolon statements flagged by ruff in the
transfer tests; drop a pre-existing unused pandas import in the
correlation-stability example. Full suite: 132 passed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…g perf

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
CI runs 'ruff format --check'; the new files weren't formatter-clean. Cosmetic
only (import wrapping, comment spacing, line reflow), no logic change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Par-python
Par-python merged commit 461d5fa into master Jun 3, 2026
8 checks passed
@Par-python
Par-python deleted the feat/transfer-entropy branch June 3, 2026 06:28
@Par-python Par-python mentioned this pull request Jun 3, 2026
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