arco 0.6.0

Automated Research into Computational Ontologies — a platform for discovering the conditions under which computation emerges
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
//! Nemenman–Shafee–Bialek entropy and mutual-information estimation.
//!
//! The NSB estimator is a Bayesian mixture of symmetric Dirichlet priors.
//!
//! `K` is represented as `u128` throughout the statistical/model layer.
//! Observed sample counts remain `usize`, since they cannot exceed the
//! number of samples actually present in memory.
//!
//! Numerical evaluation converts cardinalities to `f64` only at the
//! floating-point boundary.
//!
//! References:
//!
//! - Nemenman, Shafee & Bialek (2002),
//!   "Entropy and inference, revisited."
//! - Nemenman, Bialek & de Ruyter van Steveninck (2004),
//!   "Entropy and information in neural spike trains."
//! - Wolpert & Wolf (1995),
//!   posterior moments for Dirichlet distributions.
//!
//! https://arxiv.org/abs/physics/0108025

use std::collections::HashMap;
use std::hash::Hash;

use rand::{RngExt, SeedableRng, rngs::StdRng};

// ============================================================================
// Cardinality policy
// ============================================================================

/// Policy used to determine the finite alphabet cardinalities supplied to
/// the NSB estimator.
///
/// The important distinction is between:
///
/// - `Observed`: infer the cardinality from the actual sequence passed to
///   the estimator. This is resolved every time an NSB estimate is made.
/// - `Explicit`: use a known/modelled alphabet cardinality independently of
///   the observed support.
///
/// In particular, when `Observed` is used during shuffle correction, the
/// cardinality is re-resolved for every shuffled `(x, y)` pair. This means
/// that a shuffle that changes the observed support gets the corresponding
/// NSB cardinalities.
///
/// This is intentional: cardinality is a property of the dataset being
/// estimated, not a value that should necessarily be frozen before the
/// shuffle procedure.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum NsbCardinality {
    /// Use the observed support as the finite alphabet.
    ///
    /// For a pair `(x, y)`:
    ///
    /// - `k_x`  = number of distinct values observed in `x`
    /// - `k_y`  = number of distinct values observed in `y`
    /// - `k_xy` = number of distinct `(x, y)` pairs observed
    ///
    /// These values are recomputed every time `resolve()` is called.
    #[default]
    Observed,

    /// Use known/modelled alphabet cardinalities.
    ///
    /// This is appropriate when the observer's codomain is known
    /// independently of the sampled trajectories.
    Explicit { k_x: u128, k_y: u128, k_xy: u128 },
}

impl NsbCardinality {
    /// Cardinality name
    pub fn name(&self) -> String {
        match self {
            Self::Observed => "Observed".to_string(),
            Self::Explicit { k_x, k_y, k_xy } => {
                format!("Explicit (k_x={}, k_y={}, k_xy={})", k_x, k_y, k_xy)
            }
        }
    }
    /// Resolve the cardinalities for the supplied observations.
    ///
    /// This function is deliberately called from `nmi_nsb()` rather than
    /// once by the caller before shuffle correction.
    ///
    /// Consequently:
    ///
    /// ```text
    /// nmi_nsb(x, y)
    /// nmi_nsb(x, shuffled_y_1)
    /// nmi_nsb(x, shuffled_y_2)
    /// ...
    /// ```
    ///
    /// each independently resolves `Observed` cardinalities.
    pub fn resolve<T: Eq + Hash + Clone>(
        &self,
        x_seq: &[T],
        y_seq: &[T],
    ) -> Option<(u128, u128, u128)> {
        if x_seq.len() != y_seq.len() || x_seq.is_empty() {
            return None;
        }

        match self {
            Self::Observed => {
                let x_hist = histogram(x_seq);
                let y_hist = histogram(y_seq);

                let mut xy_hist: HashMap<(T, T), usize> = HashMap::new();

                for (x, y) in x_seq.iter().zip(y_seq.iter()) {
                    *xy_hist.entry((x.clone(), y.clone())).or_insert(0) += 1;
                }

                let k_x = x_hist.len() as u128;
                let k_y = y_hist.len() as u128;
                let k_xy = xy_hist.len() as u128;

                if k_x == 0 || k_y == 0 || k_xy == 0 {
                    return None;
                }

                Some((k_x, k_y, k_xy))
            }

            Self::Explicit { k_x, k_y, k_xy } => {
                if *k_x == 0 || *k_y == 0 || *k_xy == 0 {
                    None
                } else {
                    Some((*k_x, *k_y, *k_xy))
                }
            }
        }
    }
}

// ============================================================================
// Configuration
// ============================================================================

/// Numerical configuration for one NSB entropy estimate.
#[derive(Clone, Debug)]
pub struct NsbConfig {
    /// Finite alphabet cardinality.
    ///
    /// This is a model parameter, not the number of observed categories.
    ///
    /// `u128` is intentional because observer cardinalities can be much
    /// larger than `usize::MAX`.
    pub k: u128,

    /// Relative numerical tolerance for the adaptive quadrature.
    pub integration_tolerance: f64,

    /// Maximum adaptive subdivision depth.
    pub max_depth: usize,

    /// Maximum number of integrand evaluations.
    pub max_evaluations: usize,
}

impl Default for NsbConfig {
    fn default() -> Self {
        Self {
            k: 2,
            integration_tolerance: 1e-8,
            max_depth: 24,
            max_evaluations: 10_000,
        }
    }
}

/// Configuration for shuffle-corrected NSB normalized mutual information.
///
/// Cardinalities are represented by a policy rather than by already-resolved
/// `k_x`, `k_y`, and `k_xy` values.
///
/// This is important for `NsbCardinality::Observed`: the policy is resolved
/// independently for the observed data and for every shuffled dataset.
#[derive(Clone, Debug)]
pub struct NsbShuffleConfig {
    /// Policy used to determine `(k_x, k_y, k_xy)`.
    pub cardinality: NsbCardinality,

    /// Numerical configuration shared by all entropy calculations.
    pub entropy: NsbConfig,

    /// Number of random Y permutations used for shuffle correction.
    pub n_shuffles: usize,

    /// Seed for deterministic shuffling.
    pub seed: u64,
}

impl Default for NsbShuffleConfig {
    fn default() -> Self {
        Self {
            cardinality: NsbCardinality::Observed,
            entropy: NsbConfig::default(),
            n_shuffles: 10,
            seed: 0,
        }
    }
}

// ============================================================================
// Diagnostics
// ============================================================================

/// Numerical diagnostics for an NSB entropy estimate.
#[derive(Clone, Debug)]
pub struct NsbDiagnostics {
    /// Estimated entropy in bits.
    pub entropy_bits: f64,

    /// Posterior mean of beta.
    ///
    /// For the standard NSB hyperposterior this moment is not generally
    /// finite because the large-beta tail is heavy. Therefore this field is
    /// reported as +∞ rather than as a misleading finite cutoff-dependent
    /// number.
    pub posterior_mean_beta: f64,

    /// Conservative numerical integration error estimate in bits.
    pub integration_error: f64,

    /// Number of integrand evaluations.
    pub evaluations: usize,

    /// True when the requested quadrature tolerance was reached.
    pub converged: bool,

    /// Number of observed categories.
    pub k_observed: u128,

    /// Number of repeated observations, N - K_observed.
    pub coincidences: u128,

    /// True if K is smaller than the observed support.
    pub k_mismatch: bool,
}

// ============================================================================
// Special functions
// ============================================================================

#[allow(clippy::excessive_precision)]
pub fn lgamma(x: f64) -> f64 {
    const G: f64 = 7.0;

    const COEF: [f64; 9] = [
        0.999_999_999_999_809_93,
        676.520_368_121_885_1,
        -1_259.139_216_722_402_8,
        771.323_428_777_653_13,
        -176.615_029_162_140_59,
        12.507_343_278_686_905,
        -0.138_571_095_265_720_12,
        9.984_369_578_019_572e-6,
        1.505_632_735_149_311_6e-7,
    ];

    if x.is_nan() || x <= 0.0 {
        return f64::NAN;
    }

    if x < 0.5 {
        let pi = std::f64::consts::PI;
        return (pi / (pi * x).sin()).ln() - lgamma(1.0 - x);
    }

    let z = x - 1.0;

    let mut sum = COEF[0];

    for (i, coefficient) in COEF.iter().enumerate().skip(1) {
        sum += coefficient / (z + i as f64);
    }

    let t = z + G + 0.5;

    0.5 * (2.0 * std::f64::consts::PI).ln() + (z + 0.5) * t.ln() - t + sum.ln()
}

pub fn digamma(mut x: f64) -> f64 {
    if x.is_nan() || x <= 0.0 {
        return f64::NAN;
    }

    let mut result = 0.0;

    while x < 12.0 {
        result -= 1.0 / x;
        x += 1.0;
    }

    let inv = 1.0 / x;
    let inv2 = inv * inv;

    result += x.ln() - 0.5 * inv;

    result -= inv2
        * (1.0 / 12.0
            - inv2
                * (1.0 / 120.0
                    - inv2
                        * (1.0 / 252.0
                            - inv2
                                * (1.0 / 240.0
                                    - inv2 * (1.0 / 132.0 - inv2 * (691.0 / 32760.0))))));

    result
}

pub fn trigamma(mut x: f64) -> f64 {
    if x.is_nan() || x <= 0.0 {
        return f64::NAN;
    }

    let mut result = 0.0;

    while x < 12.0 {
        result += 1.0 / (x * x);
        x += 1.0;
    }

    let inv = 1.0 / x;
    let inv2 = inv * inv;

    result += inv + 0.5 * inv2;

    result += inv2
        * inv
        * (1.0 / 6.0
            - inv2
                * (1.0 / 30.0
                    - inv2
                        * (1.0 / 42.0
                            - inv2
                                * (1.0 / 30.0 - inv2 * (5.0 / 66.0 - inv2 * (691.0 / 2730.0))))));

    result
}

// ============================================================================
// Stable scalar helpers
// ============================================================================

fn log_gamma_ratio(z: f64, n: usize) -> f64 {
    if n == 0 {
        return 0.0;
    }

    if !z.is_finite() || z <= 0.0 {
        return f64::NAN;
    }

    let nf = n as f64;

    if z > 100.0 * nf.max(1.0) {
        let n1 = nf - 1.0;

        let s1 = nf * n1 / 2.0;
        let s2 = nf * n1 * (2.0 * nf - 1.0) / 6.0;
        let s3 = nf * nf * n1 * n1 / 4.0;

        return nf * z.ln() + s1 / z - s2 / (2.0 * z * z) + s3 / (3.0 * z * z * z);
    }

    lgamma(z + nf) - lgamma(z)
}

fn dxi_dbeta(beta: f64, k: f64) -> f64 {
    if beta <= 0.0 || !beta.is_finite() || k <= 1.0 {
        return 0.0;
    }

    if beta < 1.0e6 {
        let value = k * trigamma(k * beta + 1.0) - trigamma(beta + 1.0);

        return if value.is_finite() && value > 0.0 {
            value
        } else {
            0.0
        };
    }

    let inv = 1.0 / beta;
    let inv2 = inv * inv;
    let inv3 = inv2 * inv;
    let inv5 = inv3 * inv2;
    let inv7 = inv5 * inv2;

    let k_inv = 1.0 / k;

    let value = (k - 1.0) * k_inv * 0.5 * inv2
        + (k_inv * k_inv - 1.0) / 6.0 * inv3
        + (1.0 - k_inv.powi(4)) / 30.0 * inv5
        + (1.0 - k_inv.powi(6)) / 42.0 * inv7;

    value.max(0.0)
}

// ============================================================================
// Count grouping
// ============================================================================

fn group_counts(counts: &[usize]) -> (Vec<(usize, usize)>, usize, usize) {
    let mut frequencies: HashMap<usize, usize> = HashMap::new();

    let mut n = 0usize;

    for &count in counts {
        if count == 0 {
            continue;
        }

        *frequencies.entry(count).or_insert(0) += 1;
        n = n.saturating_add(count);
    }

    let k_observed = frequencies.values().sum();

    let mut grouped: Vec<(usize, usize)> = frequencies.into_iter().collect();

    grouped.sort_unstable_by_key(|&(count, _)| count);

    (grouped, k_observed, n)
}

// ============================================================================
// NSB equations
// ============================================================================

fn log_evidence(grouped: &[(usize, usize)], beta: f64, k: f64, n: usize) -> f64 {
    let k_beta = k * beta;

    let mut result = -log_gamma_ratio(k_beta, n);

    for &(count, multiplicity) in grouped {
        result += multiplicity as f64 * log_gamma_ratio(beta, count);
    }

    result
}

/// Posterior mean entropy in nats at fixed beta.
fn posterior_mean_entropy_nats(
    grouped: &[(usize, usize)],
    k_observed: u128,
    beta: f64,
    k: f64,
    n: usize,
) -> f64 {
    let total = n as f64 + k * beta;

    if !total.is_finite() || total <= 0.0 {
        return f64::NAN;
    }

    let psi_total = digamma(total + 1.0);

    let mut weighted = 0.0;

    for &(count, multiplicity) in grouped {
        let alpha = count as f64 + beta;

        weighted += multiplicity as f64 * alpha * (psi_total - digamma(alpha + 1.0));
    }

    let k_observed_f64 = k_observed as f64;

    if k_observed_f64 > k {
        return f64::NAN;
    }

    let empty = k - k_observed_f64;

    if empty > 0.0 {
        weighted += empty * beta * (psi_total - digamma(beta + 1.0));
    }

    weighted / total
}

// ============================================================================
// Complete beta-domain transformation
// ============================================================================

fn beta_from_w(w: f64) -> f64 {
    let log_beta = 2.0 * (w.ln() - (-w).ln_1p());

    if log_beta >= 709.0 {
        f64::MAX
    } else {
        log_beta.exp()
    }
}

fn log_beta_jacobian(w: f64) -> f64 {
    2.0_f64.ln() + w.ln() - 3.0 * (-w).ln_1p()
}

// ============================================================================
// Log-scaled NSB integrand
// ============================================================================

fn log_weight(grouped: &[(usize, usize)], k: f64, n: usize, w: f64) -> Option<(f64, f64)> {
    if !(w > 0.0 && w < 1.0) {
        return None;
    }

    let beta = beta_from_w(w);

    if !beta.is_finite() || beta <= 0.0 {
        return None;
    }

    let dxi = dxi_dbeta(beta, k);

    if !dxi.is_finite() || dxi <= 0.0 {
        return None;
    }

    let evidence = log_evidence(grouped, beta, k, n);

    if !evidence.is_finite() {
        return None;
    }

    let log_weight = evidence + dxi.ln() + log_beta_jacobian(w);

    if !log_weight.is_finite() {
        return None;
    }

    Some((log_weight, beta))
}

fn integration_scale(grouped: &[(usize, usize)], k: f64, n: usize) -> f64 {
    let mut maximum = f64::NEG_INFINITY;

    for i in 1..256 {
        let w = i as f64 / 256.0;

        if let Some((log_weight, _)) = log_weight(grouped, k, n, w) {
            maximum = maximum.max(log_weight);
        }
    }

    maximum
}

fn integrand(
    grouped: &[(usize, usize)],
    k_observed: u128,
    k: f64,
    n: usize,
    log_scale: f64,
    w: f64,
) -> [f64; 2] {
    let Some((log_weight, beta)) = log_weight(grouped, k, n, w) else {
        return [0.0, 0.0];
    };

    let scaled_weight = (log_weight - log_scale).exp();

    if !scaled_weight.is_finite() {
        return [0.0, 0.0];
    }

    let entropy = posterior_mean_entropy_nats(grouped, k_observed, beta, k, n);

    if !entropy.is_finite() {
        return [0.0, 0.0];
    }

    [scaled_weight, scaled_weight * entropy]
}

// ============================================================================
// Gauss–Legendre quadrature
// ============================================================================

#[derive(Clone, Copy, Debug, Default)]
struct VectorIntegral {
    denominator: f64,
    numerator: f64,
}

impl VectorIntegral {
    fn add(self, other: Self) -> Self {
        Self {
            denominator: self.denominator + other.denominator,
            numerator: self.numerator + other.numerator,
        }
    }

    fn sub(self, other: Self) -> Self {
        Self {
            denominator: self.denominator - other.denominator,
            numerator: self.numerator - other.numerator,
        }
    }

    fn abs_max(self) -> f64 {
        self.denominator.abs().max(self.numerator.abs())
    }
}

#[derive(Clone, Copy, Debug)]
struct Interval {
    a: f64,
    b: f64,
    estimate: VectorIntegral,
    tolerance: f64,
    depth: usize,
}

fn gauss8<F>(f: &mut F, a: f64, b: f64) -> VectorIntegral
where
    F: FnMut(f64) -> [f64; 2],
{
    const NODES: [f64; 4] = [
        0.183_434_642_495_649_8,
        0.525_532_409_916_329,
        0.796_666_477_413_626_7,
        0.960_289_856_497_536_3,
    ];

    const WEIGHTS: [f64; 4] = [
        0.362_683_783_378_362,
        0.313_706_645_877_887_3,
        0.222_381_034_453_374_5,
        0.101_228_536_290_376_3,
    ];

    let midpoint = 0.5 * (a + b);
    let half_width = 0.5 * (b - a);

    let mut denominator = 0.0;
    let mut numerator = 0.0;

    for i in 0..4 {
        let delta = half_width * NODES[i];

        let left = f(midpoint - delta);
        let right = f(midpoint + delta);

        let weight = WEIGHTS[i];

        denominator += weight * (left[0] + right[0]);
        numerator += weight * (left[1] + right[1]);
    }

    VectorIntegral {
        denominator: denominator * half_width,
        numerator: numerator * half_width,
    }
}

fn gauss16<F>(f: &mut F, a: f64, b: f64) -> VectorIntegral
where
    F: FnMut(f64) -> [f64; 2],
{
    const NODES: [f64; 8] = [
        0.095_012_509_837_637_44,
        0.281_603_550_779_258_9,
        0.458_016_777_657_227_4,
        0.617_876_244_402_643_8,
        0.755_404_408_355_003,
        0.865_631_202_387_831_8,
        0.944_575_023_073_232_6,
        0.989_400_934_991_649_9,
    ];

    const WEIGHTS: [f64; 8] = [
        0.189_450_610_455_068_5,
        0.182_603_415_044_923_6,
        0.169_156_519_395_002_5,
        0.149_595_988_816_576_7,
        0.124_628_971_255_533_9,
        0.095_158_511_682_492_8,
        0.062_253_523_938_647_9,
        0.027_152_459_411_754_1,
    ];

    let midpoint = 0.5 * (a + b);
    let half_width = 0.5 * (b - a);

    let mut denominator = 0.0;
    let mut numerator = 0.0;

    for i in 0..8 {
        let delta = half_width * NODES[i];

        let left = f(midpoint - delta);
        let right = f(midpoint + delta);

        let weight = WEIGHTS[i];

        denominator += weight * (left[0] + right[0]);
        numerator += weight * (left[1] + right[1]);
    }

    VectorIntegral {
        denominator: denominator * half_width,
        numerator: numerator * half_width,
    }
}

fn adaptive_gauss<F>(
    f: &mut F,
    tolerance: f64,
    max_depth: usize,
    max_evaluations: usize,
) -> (VectorIntegral, f64, usize, bool)
where
    F: FnMut(f64) -> [f64; 2],
{
    let mut evaluations = 24usize;

    let whole8 = gauss8(f, 0.0, 1.0);
    let whole16 = gauss16(f, 0.0, 1.0);

    let initial_error = whole16.sub(whole8).abs_max();

    let mut stack = vec![Interval {
        a: 0.0,
        b: 1.0,
        estimate: whole16,
        tolerance,
        depth: 0,
    }];

    let mut result = VectorIntegral::default();
    let mut error = 0.0;
    let mut converged = true;

    while let Some(interval) = stack.pop() {
        if evaluations + 48 > max_evaluations {
            result = result.add(interval.estimate);
            converged = false;
            continue;
        }

        let midpoint = 0.5 * (interval.a + interval.b);

        let left8 = gauss8(f, interval.a, midpoint);
        let left16 = gauss16(f, interval.a, midpoint);

        let right8 = gauss8(f, midpoint, interval.b);
        let right16 = gauss16(f, midpoint, interval.b);

        evaluations += 48;

        let refined = left16.add(right16);

        let local_error = refined.sub(left8.add(right8)).abs_max();

        let scale = refined.abs_max().max(1.0);

        if local_error <= interval.tolerance * scale {
            result = result.add(refined);
            error += local_error;
            continue;
        }

        if interval.depth >= max_depth {
            result = result.add(refined);
            error += local_error;
            converged = false;
            continue;
        }

        let child_tolerance = interval.tolerance * 0.5;

        stack.push(Interval {
            a: midpoint,
            b: interval.b,
            estimate: right16,
            tolerance: child_tolerance,
            depth: interval.depth + 1,
        });

        stack.push(Interval {
            a: interval.a,
            b: midpoint,
            estimate: left16,
            tolerance: child_tolerance,
            depth: interval.depth + 1,
        });
    }

    error = error.max(initial_error);

    (result, error, evaluations, converged)
}

// ============================================================================
// Entropy estimation
// ============================================================================

pub fn nsb_entropy(counts: &[usize], config: &NsbConfig) -> f64 {
    nsb_entropy_diagnostics(counts, config).entropy_bits
}

pub fn nsb_entropy_diagnostics(counts: &[usize], config: &NsbConfig) -> NsbDiagnostics {
    let invalid = NsbDiagnostics {
        entropy_bits: f64::NAN,
        posterior_mean_beta: f64::INFINITY,
        integration_error: f64::NAN,
        evaluations: 0,
        converged: false,
        k_observed: 0,
        coincidences: 0,
        k_mismatch: false,
    };

    if counts.is_empty()
        || config.k == 0
        || config.integration_tolerance <= 0.0
        || !config.integration_tolerance.is_finite()
        || config.max_depth == 0
        || config.max_evaluations < 24
    {
        return invalid;
    }

    if config.k == 1 {
        let k_observed = counts.iter().filter(|&&c| c > 0).count();

        let n = counts.iter().sum::<usize>();

        return NsbDiagnostics {
            entropy_bits: 0.0,
            posterior_mean_beta: f64::INFINITY,
            integration_error: 0.0,
            evaluations: 0,
            converged: true,
            k_observed: k_observed as u128,
            coincidences: n.saturating_sub(k_observed) as u128,
            k_mismatch: k_observed > 1,
        };
    }

    let (grouped, k_observed_usize, n) = group_counts(counts);

    if k_observed_usize == 0 || n == 0 {
        return invalid;
    }

    let coincidences_usize = n.saturating_sub(k_observed_usize);

    let k_observed = k_observed_usize as u128;
    let coincidences = coincidences_usize as u128;

    if k_observed > config.k {
        return NsbDiagnostics {
            k_observed,
            coincidences,
            k_mismatch: true,
            ..invalid
        };
    }

    let k = config.k as f64;

    if !k.is_finite() || k <= 0.0 {
        return NsbDiagnostics {
            k_observed,
            coincidences,
            ..invalid
        };
    }

    let log_scale = integration_scale(&grouped, k, n);

    if !log_scale.is_finite() {
        return NsbDiagnostics {
            k_observed,
            coincidences,
            ..invalid
        };
    }

    let mut function = |w: f64| integrand(&grouped, k_observed, k, n, log_scale, w);

    let (integral, integration_error_scaled, evaluations, converged) = adaptive_gauss(
        &mut function,
        config.integration_tolerance,
        config.max_depth,
        config.max_evaluations,
    );

    let denominator = integral.denominator;

    if !denominator.is_finite() || denominator <= 0.0 {
        return NsbDiagnostics {
            k_observed,
            coincidences,
            evaluations,
            converged,
            integration_error: f64::NAN,
            ..invalid
        };
    }

    let entropy_nats = integral.numerator / denominator;

    if !entropy_nats.is_finite() {
        return NsbDiagnostics {
            k_observed,
            coincidences,
            evaluations,
            converged,
            integration_error: f64::NAN,
            ..invalid
        };
    }

    let log_k = k.ln();

    let entropy_nats = entropy_nats.clamp(0.0, log_k);

    let denominator_error = integration_error_scaled * denominator.abs();

    let numerator_error = integration_error_scaled * integral.numerator.abs();

    let ratio_error = if denominator > 0.0 {
        (numerator_error + entropy_nats.abs() * denominator_error) / denominator
    } else {
        f64::INFINITY
    };

    let entropy_bits = entropy_nats / std::f64::consts::LN_2;

    let integration_error = ratio_error / std::f64::consts::LN_2;

    NsbDiagnostics {
        entropy_bits,
        posterior_mean_beta: f64::INFINITY,
        integration_error,
        evaluations,
        converged,
        k_observed,
        coincidences,
        k_mismatch: false,
    }
}

// ============================================================================
// Histograms
// ============================================================================

fn histogram<T: Eq + Hash + Clone>(values: &[T]) -> HashMap<T, usize> {
    let mut result = HashMap::new();

    for value in values {
        *result.entry(value.clone()).or_insert(0) += 1;
    }

    result
}

// ============================================================================
// Mutual information
// ============================================================================

/// Compute the three NSB entropies needed for mutual information.
///
/// Cardinality resolution is deliberately performed here, immediately before
/// the entropy calculations.
///
/// This is the key architectural point:
///
/// ```text
/// dmi_nsb(x, y)
///     └── resolve cardinality for (x, y)
///
/// shuffle_corrected_nsb(x, y)
///     ├── nmi_nsb(x, y)
///     │      └── resolve cardinality for observed (x, y)
//////     ├── nmi_nsb(x, shuffle_1(y))
///     │      └── resolve cardinality for shuffle 1
//////     ├── nmi_nsb(x, shuffle_2(y))
///     │      └── resolve cardinality for shuffle 2
//////     └── ...
/// ```
///
/// Therefore `NsbCardinality::Observed` is never accidentally frozen to
/// the cardinality of the original dataset.
fn dmi_nsb_full<T: Eq + Hash + Clone>(
    x_seq: &[T],
    y_seq: &[T],
    config: &NsbShuffleConfig,
) -> (f64, f64, f64) {
    if x_seq.len() != y_seq.len() || x_seq.len() < 2 {
        return (f64::NAN, f64::NAN, f64::NAN);
    }

    // ---------------------------------------------------------------
    // Resolve cardinality HERE.
    //
    // For NsbCardinality::Observed this is a fresh resolution for the
    // exact `(x_seq, y_seq)` pair passed to this invocation.
    // ---------------------------------------------------------------

    let Some((k_x, k_y, k_xy)) = config.cardinality.resolve(x_seq, y_seq) else {
        return (f64::NAN, f64::NAN, f64::NAN);
    };

    let x_hist = histogram(x_seq);
    let y_hist = histogram(y_seq);

    let mut xy_hist: HashMap<(T, T), usize> = HashMap::new();

    for (x, y) in x_seq.iter().zip(y_seq.iter()) {
        *xy_hist.entry((x.clone(), y.clone())).or_insert(0) += 1;
    }

    let x_counts: Vec<usize> = x_hist.into_values().collect();

    let y_counts: Vec<usize> = y_hist.into_values().collect();

    let xy_counts: Vec<usize> = xy_hist.into_values().collect();

    let x_config = NsbConfig {
        k: k_x,
        ..config.entropy.clone()
    };

    let y_config = NsbConfig {
        k: k_y,
        ..config.entropy.clone()
    };

    let xy_config = NsbConfig {
        k: k_xy,
        ..config.entropy.clone()
    };

    let h_x = nsb_entropy(&x_counts, &x_config);

    let h_y = nsb_entropy(&y_counts, &y_config);

    let h_xy = nsb_entropy(&xy_counts, &xy_config);

    (h_x, h_y, h_xy)
}

pub fn dmi_nsb<T: Eq + Hash + Clone>(x_seq: &[T], y_seq: &[T], config: &NsbShuffleConfig) -> f64 {
    let (h_x, h_y, h_xy) = dmi_nsb_full(x_seq, y_seq, config);

    if !h_x.is_finite() || !h_y.is_finite() || !h_xy.is_finite() {
        return f64::NAN;
    }

    (h_x + h_y - h_xy).max(0.0)
}

pub fn nmi_nsb<T: Eq + Hash + Clone>(x_seq: &[T], y_seq: &[T], config: &NsbShuffleConfig) -> f64 {
    let (h_x, h_y, h_xy) = dmi_nsb_full(x_seq, y_seq, config);

    if !h_x.is_finite() || !h_y.is_finite() || !h_xy.is_finite() {
        return f64::NAN;
    }

    if h_x <= 0.0 || h_y <= 0.0 {
        return 0.0;
    }

    let mi = (h_x + h_y - h_xy).max(0.0);

    (mi / (h_x * h_y).sqrt()).clamp(0.0, 1.0)
}

// ============================================================================
// Shuffle correction
// ============================================================================

/// Shuffle-corrected normalized mutual information using NSB.
///
/// The procedure is intentionally structurally identical to the other
/// estimators:
///
/// 1. Compute NMI on the observed `(X, Y)`.
/// 2. Shuffle `Y`.
/// 3. Compute NMI on `(X, shuffled_Y)`.
/// 4. Repeat `n_shuffles` times.
/// 5. Subtract the mean shuffle baseline.
/// 6. Clamp to `[0, 1]`.
///
/// Crucially, cardinality is NOT resolved here.
///
/// Instead every call to `nmi_nsb()` resolves cardinality through
/// `config.cardinality`.
///
/// Thus with:
///
/// ```text
/// NsbCardinality::Observed
/// ```
///
/// the sequence of operations is:
///
/// ```text
/// observed:
///     k_x  = support(X)
///     k_y  = support(Y)
///     k_xy = support(X,Y)
///
/// shuffle 1:
///     k_x  = support(X)
///     k_y  = support(shuffle_1(Y))
///     k_xy = support(X, shuffle_1(Y))
///
/// shuffle 2:
///     k_x  = support(X)
///     k_y  = support(shuffle_2(Y))
///     k_xy = support(X, shuffle_2(Y))
///
/// ...
/// ```
///
/// With:
///
/// ```text
/// NsbCardinality::Explicit { ... }
/// ```
///
/// all estimates use the same explicitly supplied cardinalities.
pub fn shuffle_corrected_nsb<T: Eq + Hash + Clone>(
    x_seq: &[T],
    y_seq: &[T],
    config: &NsbShuffleConfig,
) -> f64 {
    if x_seq.len() != y_seq.len() || x_seq.len() < 4 {
        return 0.0;
    }

    // ---------------------------------------------------------------
    // Observed NMI.
    //
    // nmi_nsb() resolves cardinality for THIS exact dataset.
    // ---------------------------------------------------------------

    let nmi_obs = nmi_nsb(x_seq, y_seq, config);

    if !nmi_obs.is_finite() {
        return 0.0;
    }

    // ---------------------------------------------------------------
    // No shuffle correction requested.
    // ---------------------------------------------------------------

    if config.n_shuffles == 0 {
        return nmi_obs;
    }

    // ---------------------------------------------------------------
    // Shuffle Y and recompute the complete NMI estimator.
    // ---------------------------------------------------------------

    let mut rng = StdRng::seed_from_u64(config.seed);

    let mut y_shuffled = y_seq.to_vec();

    let mut nmi_shuffles = Vec::with_capacity(config.n_shuffles);

    for _ in 0..config.n_shuffles {
        // Fisher-Yates shuffle.
        for i in (1..y_shuffled.len()).rev() {
            let j = rng.random_range(0..=i);

            y_shuffled.swap(i, j);
        }

        // IMPORTANT:
        //
        // Cardinality isn't resolved outside this call.
        //
        // nmi_nsb() -> dmi_nsb_full() -> resolve()
        //
        // Consequently `Observed` cardinality is recomputed for this
        // shuffled dataset.
        let nmi_shuffle = nmi_nsb(x_seq, &y_shuffled, config);

        if nmi_shuffle.is_finite() {
            nmi_shuffles.push(nmi_shuffle);
        }
    }

    if nmi_shuffles.is_empty() {
        return 0.0;
    }

    let mean_shuffle = nmi_shuffles.iter().sum::<f64>() / nmi_shuffles.len() as f64;

    (nmi_obs - mean_shuffle).clamp(0.0, 1.0)
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn entropy_config(k: u128) -> NsbConfig {
        NsbConfig {
            k,
            integration_tolerance: 1e-7,
            max_depth: 20,
            max_evaluations: 5_000,
        }
    }

    fn mi_config(k_x: u128, k_y: u128, k_xy: u128) -> NsbShuffleConfig {
        NsbShuffleConfig {
            cardinality: NsbCardinality::Explicit { k_x, k_y, k_xy },
            entropy: NsbConfig {
                k: 2,
                integration_tolerance: 1e-7,
                max_depth: 20,
                max_evaluations: 5_000,
            },
            n_shuffles: 10,
            seed: 42,
        }
    }

    fn observed_mi_config() -> NsbShuffleConfig {
        NsbShuffleConfig {
            cardinality: NsbCardinality::Observed,
            entropy: NsbConfig {
                k: 2,
                integration_tolerance: 1e-7,
                max_depth: 20,
                max_evaluations: 5_000,
            },
            n_shuffles: 10,
            seed: 42,
        }
    }

    #[test]
    fn digamma_known_values() {
        let gamma = 0.577_215_664_901_532_9;

        assert!((digamma(1.0) + gamma).abs() < 1e-10);

        assert!((digamma(2.0) - (1.0 - gamma)).abs() < 1e-10);
    }

    #[test]
    fn lgamma_known_values() {
        assert!((lgamma(5.0) - 24.0_f64.ln()).abs() < 1e-10);

        assert!((lgamma(0.5) - std::f64::consts::PI.sqrt().ln()).abs() < 1e-10);
    }

    #[test]
    fn uniform_distribution() {
        let diagnostics = nsb_entropy_diagnostics(&[100, 100, 100, 100], &entropy_config(4));

        assert!(diagnostics.converged, "{diagnostics:?}");

        assert!(diagnostics.entropy_bits.is_finite());

        assert!((diagnostics.entropy_bits - 2.0).abs() < 0.02);
    }

    #[test]
    fn deterministic_distribution_has_small_entropy() {
        let diagnostics = nsb_entropy_diagnostics(&[1000], &entropy_config(4));

        assert!(diagnostics.converged, "{diagnostics:?}");

        assert!(diagnostics.entropy_bits.is_finite());

        assert!(diagnostics.entropy_bits < 0.05);

        assert!(diagnostics.entropy_bits >= 0.0);
    }

    #[test]
    fn deterministic_entropy_decreases_with_more_data() {
        let h_100 = nsb_entropy(&[100], &entropy_config(4));

        let h_1000 = nsb_entropy(&[1000], &entropy_config(4));

        assert!(h_100.is_finite());
        assert!(h_1000.is_finite());

        assert!(h_1000 < h_100);
    }

    #[test]
    fn severe_undersampling_requires_coincidences() {
        let counts = vec![1usize; 40]
            .into_iter()
            .chain([10usize, 10, 10, 10, 10])
            .collect::<Vec<_>>();

        let diagnostics = nsb_entropy_diagnostics(&counts, &entropy_config(4096));

        assert!(diagnostics.entropy_bits.is_finite(), "{diagnostics:?}");

        assert!(diagnostics.coincidences > 0);

        let n = 90.0;

        let plugin = counts
            .iter()
            .map(|&c| c as f64)
            .filter(|&c| c > 0.0)
            .fold(0.0, |h, c| {
                let p = c / n;
                h - p * p.log2()
            });

        assert!(diagnostics.entropy_bits > plugin);
    }

    #[test]
    fn no_coincidences_are_valid() {
        let counts = vec![1usize; 50];

        let diagnostics = nsb_entropy_diagnostics(&counts, &entropy_config(4096));

        assert!(diagnostics.entropy_bits.is_finite());

        assert_eq!(diagnostics.coincidences, 0);
    }

    #[test]
    fn k_mismatch_is_detected() {
        let diagnostics = nsb_entropy_diagnostics(&[1, 1, 1, 1], &entropy_config(3));

        assert!(diagnostics.k_mismatch);

        assert!(!diagnostics.entropy_bits.is_finite());
    }

    #[test]
    fn integration_converges_without_grid_parameter() {
        let counts = [25usize, 25, 25, 25];

        let loose = nsb_entropy(
            &counts,
            &NsbConfig {
                k: 4,
                integration_tolerance: 1e-5,
                max_depth: 18,
                max_evaluations: 5_000,
            },
        );

        let tight = nsb_entropy(
            &counts,
            &NsbConfig {
                k: 4,
                integration_tolerance: 1e-9,
                max_depth: 24,
                max_evaluations: 10_000,
            },
        );

        assert!(loose.is_finite());
        assert!(tight.is_finite());

        assert!((loose - tight).abs() < 1e-5);
    }

    #[test]
    fn independent_mi_is_small() {
        use rand::{RngExt, SeedableRng, rngs::StdRng};

        let mut rng_x = StdRng::seed_from_u64(1);

        let mut rng_y = StdRng::seed_from_u64(2);

        let x: Vec<u8> = (0..600).map(|_| rng_x.random_range(0..4)).collect();

        let y: Vec<u8> = (0..600).map(|_| rng_y.random_range(0..4)).collect();

        let mi = dmi_nsb(&x, &y, &mi_config(4, 4, 16));

        assert!(mi.is_finite());
        assert!(mi < 0.3);
    }

    #[test]
    fn deterministic_mi_is_large() {
        let x: Vec<u8> = (0..400).map(|i| (i % 4) as u8).collect();

        let mi = dmi_nsb(&x, &x, &mi_config(4, 4, 4));

        assert!(mi.is_finite());
        assert!(mi > 1.5);
    }

    #[test]
    fn nmi_is_bounded() {
        let x: Vec<u8> = (0..400).map(|i| (i % 4) as u8).collect();

        let nmi = nmi_nsb(&x, &x, &mi_config(4, 4, 4));

        assert!(nmi.is_finite());
        assert!((0.0..=1.0).contains(&nmi));
        assert!(nmi > 0.9);
    }

    #[test]
    fn shuffle_correction_preserves_dependency() {
        use rand::{RngExt, SeedableRng, rngs::StdRng};

        let mut rng = StdRng::seed_from_u64(123);

        let x: Vec<u8> = (0..400).map(|_| rng.random_range(0..8)).collect();

        let dependent = x.clone();

        let independent: Vec<u8> = (0..400).map(|_| rng.random_range(0..8)).collect();

        let config = mi_config(8, 8, 64);

        let dependent_score = shuffle_corrected_nsb(&x, &dependent, &config);

        let independent_score = shuffle_corrected_nsb(&x, &independent, &config);

        assert!(dependent_score.is_finite());

        assert!(independent_score.is_finite());

        assert!(dependent_score > independent_score);
    }

    // ------------------------------------------------------------------------
    // Observed cardinality tests
    // ------------------------------------------------------------------------

    #[test]
    fn observed_cardinality_uses_support() {
        let x = [0u8, 1, 0, 1, 0, 1];
        let y = [1u8, 1, 0, 1, 0, 0];

        let cardinality = NsbCardinality::Observed;

        let resolved = cardinality.resolve(&x, &y).unwrap();

        assert_eq!(resolved, (2, 2, 4));
    }

    #[test]
    fn observed_cardinality_is_resolved_again_after_shuffle() {
        let x = [0u8, 0, 0, 0, 1, 1, 1, 1];

        let y = [0u8, 0, 0, 0, 1, 1, 1, 1];

        let cardinality = NsbCardinality::Observed;

        let original = cardinality.resolve(&x, &y).unwrap();

        let shuffled = [0u8, 1, 0, 1, 0, 1, 0, 1];

        let shuffled_cardinality = cardinality.resolve(&x, &shuffled).unwrap();

        assert_eq!(original.0, shuffled_cardinality.0);

        assert_eq!(original.1, 2);

        assert_eq!(shuffled_cardinality.1, 2);

        // The important part is that this is resolved from the actual
        // `(x, shuffled_y)` data rather than copied from the original pair.
        assert_eq!(shuffled_cardinality.2, 4);
    }

    #[test]
    fn explicit_cardinality_is_stable() {
        let cardinality = NsbCardinality::Explicit {
            k_x: 4096,
            k_y: 4096,
            k_xy: 16_777_216,
        };

        let x = [0u8, 1, 0, 1];
        let y = [0u8, 0, 1, 1];

        let shuffled_y = [1u8, 0, 1, 0];

        assert_eq!(
            cardinality.resolve(&x, &y).unwrap(),
            (4096, 4096, 16_777_216)
        );

        assert_eq!(
            cardinality.resolve(&x, &shuffled_y).unwrap(),
            (4096, 4096, 16_777_216)
        );
    }

    #[test]
    fn observed_nsb_mi_works_without_known_cardinality() {
        let x = [0u8, 1, 1, 0, 0, 0];

        let y = [1u8, 1, 1, 1, 0, 1];

        let config = observed_mi_config();

        let mi = dmi_nsb(&x, &y, &config);

        assert!(mi.is_finite(), "MI = {mi}");
    }

    #[test]
    fn observed_nmi_works_without_known_cardinality() {
        let x = [0u8, 1, 1, 0, 0, 0];

        let y = [1u8, 1, 1, 1, 0, 1];

        let config = observed_mi_config();

        let nmi = nmi_nsb(&x, &y, &config);

        assert!(nmi.is_finite(), "NMI = {nmi}");

        assert!((0.0..=1.0).contains(&nmi));
    }

    #[test]
    fn observed_shuffle_correction_is_finite() {
        let x: Vec<u8> = (0..100).map(|i| (i % 2) as u8).collect();

        let y = x.clone();

        let config = observed_mi_config();

        let score = shuffle_corrected_nsb(&x, &y, &config);

        assert!(score.is_finite(), "score = {score}");

        assert!((0.0..=1.0).contains(&score));
    }

    // ------------------------------------------------------------------------
    // u128 cardinality regression tests
    // ------------------------------------------------------------------------

    #[test]
    fn large_u128_cardinality_is_accepted() {
        let k = 1u128 << 100;

        let config = entropy_config(k);

        assert_eq!(config.k, k);

        let diagnostics = nsb_entropy_diagnostics(&[1usize], &config);

        assert!(diagnostics.entropy_bits.is_finite(), "{diagnostics:?}");

        assert_eq!(diagnostics.k_observed, 1);

        assert_eq!(diagnostics.coincidences, 0);

        assert!(!diagnostics.k_mismatch);
    }

    #[test]
    fn u128_cardinality_does_not_truncate_to_usize() {
        let k = 1u128 << 100;

        let diagnostics = nsb_entropy_diagnostics(&[1usize], &entropy_config(k));

        assert_eq!(diagnostics.k_observed, 1);

        assert!(!diagnostics.k_mismatch);
    }

    #[test]
    fn large_cardinality_mismatch_still_works() {
        let k = 1u128 << 100;

        let diagnostics = nsb_entropy_diagnostics(&[1usize, 1usize], &entropy_config(1));

        assert!(diagnostics.k_mismatch);

        assert_eq!(diagnostics.k_observed, 2);

        let diagnostics_large = nsb_entropy_diagnostics(&[1usize], &entropy_config(k));

        assert!(!diagnostics_large.k_mismatch);
    }

    // ------------------------------------------------------------------------
    // Shuffle cardinality regression
    // ------------------------------------------------------------------------

    #[test]
    fn observed_cardinality_is_not_frozen_before_shuffle() {
        let x = vec![0u8, 0, 0, 0, 1, 1, 1, 1];

        let y = vec![0u8, 0, 0, 0, 1, 1, 1, 1];

        let config = observed_mi_config();

        // Resolve the original pair.
        let original = config.cardinality.resolve(&x, &y).unwrap();

        // Construct a pair whose support differs from the original.
        let y_reduced = vec![0u8, 0, 0, 0, 0, 0, 0, 0];

        let reduced = config.cardinality.resolve(&x, &y_reduced).unwrap();

        assert_ne!(original, reduced);

        assert_eq!(original.1, 2);

        assert_eq!(reduced.1, 1);

        // This is exactly what shuffle_corrected_nsb() now does:
        // every nmi_nsb() call resolves cardinality against its actual
        // `(x, y)` arguments.
    }
}