Skip to content

fix knn chebyshev exactness - #91

Merged
htymjun merged 2 commits into
mainfrom
fix-knn-chebyshev-exactness
Nov 1, 2026
Merged

htymjun merged 2 commits into
mainfrom
fix-knn-chebyshev-exactness

Conversation

@htymjun

@htymjun htymjun commented Nov 1, 2026

Copy link
Copy Markdown
Owner
  • fix: Make the k-NN estimators exact for the Chebyshev metric
  • chore: Bump version to 1.2.0

htymjun and others added 2 commits November 1, 2026 16:59
Verification against brute-force references and Kraskov, Stoegbauer &
Grassberger, PRE 69, 066138 (2004) revealed five defects:

1. nanoflann's searchLevel() combines per-dimension cell offsets by
   summing them, which is a lower bound only for additive metrics. For
   the Chebyshev metric the cell distance is the maximum, so cells with
   true neighbours were pruned and k-NN radii were too large (~9% of the
   points in 2-D, 32% in 4-D). All estimators were affected. The new
   ChebyshevKDTree hides findNeighbors() with an exact L-infinity search;
   nanoflann itself is unchanged.
2. Points with zero marginal counts were dropped from the average,
   biasing I(1) for k = 1. Only degenerate points (eps == 0) are skipped.
3. 1-D marginal counts compared x_j < v + eps, so rounding could count
   the boundary point; the edges are now re-tested in difference form.
4. shannon_entropy used the Euclidean unit-ball constant with Chebyshev
   distances (bias of -0.24 nats in 2-D, -1.13 in 4-D); c_d = 1 now.
5. information_flow_causal_map accepted per-variable tau although several
   code paths read only tau[0]; a non-uniform tau now raises ValueError.

After the fix, mutual_info, conditional_mutual_info, shannon_entropy,
KL_div, Theiler-window MI, information_flow, transfer_entropy and all
information_flow_causal_map paths agree with exhaustive-distance
references to 1e-12. Before the fix, the mean I(1) of independent
variables was down to -0.11 nats (|z| up to 297), Fig. 4 of Kraskov et
al. was missed by 0.10-0.18 nats, and I(X1; X2..X8) for equicorrelated
Gaussians (r = 0.9) gave 0.549 instead of 1.086. After the fix the
independence conjecture holds within 2 sem in 45 cases, Fig. 4 and
Fig. 5 match the digitized paper values within 0.0006 and 0.004, and
Fig. 14 matches the exact values within 0.007.

The exact search is slower: x1.2-1.3 for the 2-D joint space of 1-D
variables, x5 in 4-D.

Tests: test_knn_exact.py (brute-force agreement, multi-dimensional
entropy and MI, tau validation) and test_ksg_kraskov.py (independence,
Figs. 4 and 5, exact MI; draws Figs. 4, 5 and 14 into
tests/results/kraskov.png). test_MI.py now covers rho = 0.1-0.9 and
k = 1-30 with statistical tolerances. The pre-fix build fails 10 of the
11 Kraskov tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The k-NN fixes change the numerical output of every estimator.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@htymjun
htymjun merged commit af8f03c into main Nov 1, 2026
7 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