Repository navigation
fix knn chebyshev exactness - #91
Merged
Merged
Conversation
htymjun
commented
Nov 1, 2026
Owner
- fix: Make the k-NN estimators exact for the Chebyshev metric
- chore: Bump version to 1.2.0
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]>
23 of 32 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.