rustyml 0.12.0

A high-performance machine learning & deep learning library in pure Rust, offering ML algorithms and neural network support
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
//! Clustering evaluation metrics
//!
//! Provides extrinsic metrics that compare a clustering against ground-truth labels (NMI, AMI, ARI,
//! homogeneity, completeness, V-measure, Fowlkes-Mallows) and intrinsic metrics that score a
//! clustering from the feature geometry alone (silhouette, Davies-Bouldin, Calinski-Harabasz)

use ahash::AHashMap;
use ndarray::{Array2, ArrayBase, ArrayView1, ArrayViewMut1, Axis, Data, Ix1, Ix2, Zip};

use super::validate_pair;
use crate::math::squared_euclidean_distance_row;
pub use crate::types::DistanceCalculationMetric;

/// Denominator magnitude below which an AMI/ARI normaliser is treated as a degenerate (perfect)
/// clustering and the score is defined to be `1.0`
const DEGENERATE_DENOM: f64 = 1e-10;

/// Total scanned-element work (`n * n * d`) at or above which [`silhouette_score`] fills its
/// pairwise-distance matrix in parallel; below it the serial path is used
///
/// The fill is n tasks of an O(n * d) distance-row scan each, the same cost class as the crate's
/// calibrated f64 row-scan gate (crossover bracket 65K-262K scanned elements). The constant is
/// restated here rather than imported because `metrics` stays a lightweight leaf module
const SILHOUETTE_PARALLEL_MIN_ELEMS: usize = 262_144;

/// Maps each distinct label to a dense index in `0..k` in order of first appearance
fn label_index(labels: &[usize]) -> AHashMap<usize, usize> {
    let mut index = AHashMap::new();
    for &label in labels {
        let next = index.len();
        index.entry(label).or_insert(next);
    }
    index
}

/// Builds the contingency matrix of two label assignments together with its row and column sums
/// (the cluster sizes in `labels_true` and `labels_pred` respectively)
fn contingency_matrix(
    labels_true: &[usize],
    labels_pred: &[usize],
) -> (Array2<usize>, Vec<usize>, Vec<usize>) {
    let index_true = label_index(labels_true);
    let index_pred = label_index(labels_pred);

    let mut matrix = Array2::<usize>::zeros((index_true.len(), index_pred.len()));
    for (&lt, &lp) in labels_true.iter().zip(labels_pred.iter()) {
        matrix[[index_true[&lt], index_pred[&lp]]] += 1;
    }

    let row_sums = matrix.sum_axis(Axis(1)).to_vec();
    let col_sums = matrix.sum_axis(Axis(0)).to_vec();
    (matrix, row_sums, col_sums)
}

/// Computes the mutual information (in nats): `MI = sum_ij (n_ij/n) * ln(n*n_ij / (a_i*b_j))`
fn mutual_information(
    contingency: &Array2<usize>,
    n: usize,
    row_sums: &[usize],
    col_sums: &[usize],
) -> f64 {
    let n_f = n as f64;
    let mut mi = 0.0;
    for ((i, j), &n_ij) in contingency.indexed_iter() {
        if n_ij > 0 {
            let n_ij_f = n_ij as f64;
            let a = row_sums[i] as f64;
            let b = col_sums[j] as f64;
            mi += (n_ij_f / n_f) * ((n_f * n_ij_f) / (a * b)).ln();
        }
    }
    mi
}

/// Computes the entropy (in nats) of a partition from its cluster sizes: `H = -sum_i p_i * ln(p_i)`
fn entropy_nats(counts: &[usize], n: usize) -> f64 {
    let n_f = n as f64;
    let mut h = 0.0;
    for &count in counts {
        if count > 0 {
            let p = count as f64 / n_f;
            h -= p * p.ln();
        }
    }
    h
}

/// Builds a table of natural log-factorials, `table[i] = ln(i!)`, for `i` in `0..=n_max`
///
/// Computed by cumulative summation so that each log-binomial coefficient needed by the expected
/// mutual information becomes an `O(1)` lookup rather than an `O(k)` sum
fn ln_factorial_table(n_max: usize) -> Vec<f64> {
    let mut table = Vec::with_capacity(n_max + 1);
    table.push(0.0); // ln(0!) = ln(1) = 0
    let mut acc = 0.0;
    for i in 1..=n_max {
        acc += (i as f64).ln();
        table.push(acc);
    }
    table
}

/// Computes the expected mutual information (EMI) under the hypergeometric model of random
/// clusterings with the given cluster sizes
///
/// For each pair of clusters `(a_i, b_j)` the overlap `n_ij` follows a hypergeometric distribution;
/// EMI sums `P(n_ij = k) * (k/n) * ln(n*k / (a_i*b_j))` over its support. All binomial coefficients
/// are evaluated in log space from a shared log-factorial table, so the whole computation is
/// `O(R*C*range)` with `O(1)` inner work
fn expected_mutual_information(row_sums: &[usize], col_sums: &[usize], n: usize) -> f64 {
    let n_f = n as f64;
    let ln_fact = ln_factorial_table(n);
    // log C(a, b) = ln(a!) - ln(b!) - ln((a-b)!); valid for 0 <= b <= a <= n
    let log_binom = |a: usize, b: usize| ln_fact[a] - ln_fact[b] - ln_fact[a - b];

    let mut emi = 0.0;
    for &a_i in row_sums {
        for &b_j in col_sums {
            // Hypergeometric support max(0, a_i + b_j - n) <= k <= min(a_i, b_j); k = 0 adds nothing
            // to MI, so start from 1
            let lower = (a_i + b_j).saturating_sub(n).max(1);
            let upper = a_i.min(b_j);

            // log C(n, b_j) is constant across k for this pair; hoist it out of the loop
            let log_c_n_bj = log_binom(n, b_j);
            for k in lower..=upper {
                // ln P(k) = log C(a_i, k) + log C(n - a_i, b_j - k) - log C(n, b_j)
                let log_p = log_binom(a_i, k) + log_binom(n - a_i, b_j - k) - log_c_n_bj;
                let term = (k as f64 / n_f) * ((n_f * k as f64) / (a_i as f64 * b_j as f64)).ln();
                emi += log_p.exp() * term;
            }
        }
    }
    emi
}

/// Collects a `usize` label array into a contiguous `Vec`, tolerating non-contiguous views
fn to_label_vec<S>(labels: &ArrayBase<S, Ix1>) -> Vec<usize>
where
    S: Data<Elem = usize>,
{
    labels.iter().copied().collect()
}

/// Calculates the Normalized Mutual Information (NMI) between two cluster assignments
///
/// NMI normalizes the mutual information by the arithmetic mean of the two clusterings' entropies,
/// giving `0.0` for independent assignments and `1.0` for identical ones. If either clustering has
/// zero entropy (a single cluster), NMI is defined as `0.0`
///
/// # Parameters
///
/// - `labels_true` - Ground-truth cluster assignment for each sample
/// - `labels_pred` - Predicted cluster assignment for each sample
///
/// # Returns
///
/// - `f64` - Normalized mutual information in `[0.0, 1.0]`
///
/// # Panics
///
/// - Panics if `labels_true` and `labels_pred` have different lengths
/// - Panics if the inputs are empty
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::normalized_mutual_info;
///
/// let labels_true = array![0, 0, 1, 1, 2, 2];
/// let labels_pred = array![0, 0, 1, 2, 1, 2];
/// let nmi = normalized_mutual_info(&labels_true, &labels_pred);
/// println!("Normalized Mutual Information: {:.4}", nmi);
/// ```
pub fn normalized_mutual_info<S>(
    labels_true: &ArrayBase<S, Ix1>,
    labels_pred: &ArrayBase<S, Ix1>,
) -> f64
where
    S: Data<Elem = usize>,
{
    validate_pair(
        labels_true.len(),
        labels_pred.len(),
        "labels_true and labels_pred",
    );

    let n = labels_true.len();
    let labels_true = to_label_vec(labels_true);
    let labels_pred = to_label_vec(labels_pred);

    let (contingency, row_sums, col_sums) = contingency_matrix(&labels_true, &labels_pred);
    let mi = mutual_information(&contingency, n, &row_sums, &col_sums);
    let h_true = entropy_nats(&row_sums, n);
    let h_pred = entropy_nats(&col_sums, n);

    // Arithmetic mean of the entropies
    let denominator = (h_true + h_pred) / 2.0;
    if denominator == 0.0 {
        0.0
    } else {
        mi / denominator
    }
}

/// Calculates the Adjusted Mutual Information (AMI) between two cluster assignments
///
/// AMI corrects the mutual information for the agreement expected by chance, scoring `1.0` for
/// identical clusterings and about `0.0` for independent ones (it can be slightly negative). When
/// the normaliser is degenerate (e.g. both clusterings put every sample in one cluster) the score
/// is defined as `1.0`
///
/// # Parameters
///
/// - `labels_true` - Ground-truth cluster assignment for each sample
/// - `labels_pred` - Predicted cluster assignment for each sample
///
/// # Returns
///
/// - `f64` - Adjusted mutual information (typically in `[-1.0, 1.0]`)
///
/// # Panics
///
/// - Panics if `labels_true` and `labels_pred` have different lengths
/// - Panics if the inputs are empty
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::adjusted_mutual_info;
///
/// let labels_true = array![0, 0, 1, 1, 2, 2];
/// let labels_pred = array![0, 0, 1, 2, 1, 2];
/// let ami = adjusted_mutual_info(&labels_true, &labels_pred);
/// println!("Adjusted Mutual Information: {:.4}", ami);
/// ```
pub fn adjusted_mutual_info<S>(
    labels_true: &ArrayBase<S, Ix1>,
    labels_pred: &ArrayBase<S, Ix1>,
) -> f64
where
    S: Data<Elem = usize>,
{
    validate_pair(
        labels_true.len(),
        labels_pred.len(),
        "labels_true and labels_pred",
    );

    let n = labels_true.len();
    let labels_true = to_label_vec(labels_true);
    let labels_pred = to_label_vec(labels_pred);

    let (contingency, row_sums, col_sums) = contingency_matrix(&labels_true, &labels_pred);
    let mi = mutual_information(&contingency, n, &row_sums, &col_sums);
    let h_true = entropy_nats(&row_sums, n);
    let h_pred = entropy_nats(&col_sums, n);
    let emi = expected_mutual_information(&row_sums, &col_sums, n);

    let denominator = (h_true + h_pred) / 2.0 - emi;
    if denominator.abs() < DEGENERATE_DENOM {
        1.0
    } else {
        (mi - emi) / denominator
    }
}

/// Calculates the Adjusted Rand Index (ARI) between two cluster assignments
///
/// ARI is the Rand index (the fraction of sample pairs that two clusterings agree on, whether by
/// grouping them together or apart) corrected for chance: `1.0` for identical clusterings, about
/// `0.0` for independent ones, and possibly negative for worse-than-random agreement. When the
/// normaliser is degenerate (e.g. fewer than two samples, or both clusterings trivial) the score
/// is defined as `1.0`
///
/// # Parameters
///
/// - `labels_true` - Ground-truth cluster assignment for each sample
/// - `labels_pred` - Predicted cluster assignment for each sample
///
/// # Returns
///
/// - `f64` - Adjusted Rand Index (typically in `[-0.5, 1.0]`)
///
/// # Panics
///
/// - Panics if `labels_true` and `labels_pred` have different lengths
/// - Panics if the inputs are empty
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::adjusted_rand_index;
///
/// let labels_true = array![0, 0, 1, 1, 2, 2];
/// let labels_pred = array![0, 0, 1, 2, 1, 2];
/// let ari = adjusted_rand_index(&labels_true, &labels_pred);
/// println!("Adjusted Rand Index: {:.4}", ari);
/// ```
pub fn adjusted_rand_index<S>(
    labels_true: &ArrayBase<S, Ix1>,
    labels_pred: &ArrayBase<S, Ix1>,
) -> f64
where
    S: Data<Elem = usize>,
{
    validate_pair(
        labels_true.len(),
        labels_pred.len(),
        "labels_true and labels_pred",
    );

    let n = labels_true.len();
    let labels_true = to_label_vec(labels_true);
    let labels_pred = to_label_vec(labels_pred);

    let (contingency, row_sums, col_sums) = contingency_matrix(&labels_true, &labels_pred);

    // Number of unordered pairs within a group of `size` elements: C(size, 2)
    let comb2 = |size: usize| {
        let size = size as f64;
        size * (size - 1.0) / 2.0
    };

    let sum_comb_cells: f64 = contingency.iter().map(|&n_ij| comb2(n_ij)).sum();
    let sum_comb_true: f64 = row_sums.iter().map(|&a| comb2(a)).sum();
    let sum_comb_pred: f64 = col_sums.iter().map(|&b| comb2(b)).sum();
    let comb_n = comb2(n);

    if comb_n == 0.0 {
        return 1.0; // fewer than two samples: no pairs to disagree on
    }

    let expected = sum_comb_true * sum_comb_pred / comb_n;
    let max_index = 0.5 * (sum_comb_true + sum_comb_pred);
    let denominator = max_index - expected;

    if denominator.abs() < DEGENERATE_DENOM {
        1.0
    } else {
        (sum_comb_cells - expected) / denominator
    }
}

/// Calculates the mean Silhouette Coefficient over all samples under the given distance metric
///
/// For each sample, the silhouette `s = (b - a) / max(a, b)` compares the mean intra-cluster
/// distance `a` with the mean distance `b` to the nearest other cluster; it ranges from `-1`
/// (likely misassigned) through `0` (on a cluster boundary) to `+1` (well clustered). Samples that
/// are the sole member of their cluster contribute `0`. The returned score is the mean over all
/// samples
///
/// Pairwise distances go through [`DistanceCalculationMetric`], the same dispatch point used by the
/// estimators, so any of `Euclidean`, `Manhattan`, or `Minkowski(p)` works. Pass
/// `DistanceCalculationMetric::Euclidean` for the conventional silhouette
///
/// # Parameters
///
/// - `x` - Feature matrix with one sample per row (`n_samples x n_features`)
/// - `labels` - Cluster assignment for each sample
/// - `metric` - Distance metric used for every pairwise distance
///
/// # Returns
///
/// - `f64` - Mean silhouette coefficient in `[-1.0, 1.0]`
///
/// # Panics
///
/// - Panics if the number of rows in `x` differs from the length of `labels`
/// - Panics if the inputs are empty
/// - Panics if the number of distinct clusters is not in `2..=n_samples - 1`
/// - Panics if `metric` is `Minkowski(p)` with `p < 1`
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::silhouette_score;
/// use rustyml::types::DistanceCalculationMetric;
///
/// let x = array![[0.0, 0.0], [0.0, 1.0], [10.0, 10.0], [10.0, 11.0]];
/// let labels = array![0, 0, 1, 1];
/// let score = silhouette_score(&x, &labels, DistanceCalculationMetric::Euclidean);
/// assert!(score > 0.8);
/// ```
pub fn silhouette_score<S1, S2>(
    x: &ArrayBase<S1, Ix2>,
    labels: &ArrayBase<S2, Ix1>,
    metric: DistanceCalculationMetric,
) -> f64
where
    S1: Data<Elem = f64> + Sync,
    S2: Data<Elem = usize>,
{
    let n = x.nrows();
    let labels = to_label_vec(labels);
    let (cluster, k) = validate_clustering_inputs(n, &labels);

    let mut sizes = vec![0usize; k];
    for &c in &cluster {
        sizes[c] += 1;
    }

    // dist_to_cluster[[i, c]] accumulates the total distance from sample i to every sample in cluster c
    let mut dist_to_cluster = Array2::<f64>::zeros((n, k));
    let fill_row = |mut row_acc: ArrayViewMut1<f64>, xi: ArrayView1<f64>| {
        for j in 0..n {
            row_acc[cluster[j]] += metric.distance(xi, x.row(j));
        }
    };
    let fill = Zip::from(dist_to_cluster.rows_mut()).and(x.rows());
    let scan_work = n.saturating_mul(n).saturating_mul(x.ncols());
    if scan_work >= SILHOUETTE_PARALLEL_MIN_ELEMS {
        fill.par_for_each(fill_row);
    } else {
        fill.for_each(fill_row);
    }

    let mut total = 0.0;
    for i in 0..n {
        let own = cluster[i];
        if sizes[own] <= 1 {
            continue; // lone sample contributes a silhouette of 0
        }

        let a = dist_to_cluster[[i, own]] / (sizes[own] - 1) as f64;
        let mut b = f64::INFINITY;
        for c in 0..k {
            if c != own {
                let mean_dist = dist_to_cluster[[i, c]] / sizes[c] as f64;
                if mean_dist < b {
                    b = mean_dist;
                }
            }
        }

        let denominator = a.max(b);
        if denominator > 0.0 {
            total += (b - a) / denominator;
        }
    }

    total / n as f64
}

/// Densifies labels to `0..k` and validates them against an `x` of `n_rows` rows for an internal
/// clustering metric: equal length, non-empty, and `2..=n_rows - 1` distinct clusters. Returns the
/// dense cluster index of each sample and the cluster count `k`
fn validate_clustering_inputs(n_rows: usize, labels: &[usize]) -> (Vec<usize>, usize) {
    if n_rows != labels.len() {
        panic!(
            "dimension mismatch: expected {n_rows}, found {}",
            labels.len()
        );
    }
    if n_rows == 0 {
        panic!("input is empty: x and labels");
    }
    let index = label_index(labels);
    let k = index.len();
    if k < 2 || k >= n_rows {
        panic!(
            "invalid input: number of clusters is {k}, valid range is 2 to n_samples - 1 ({})",
            n_rows - 1
        );
    }
    let cluster = labels.iter().map(|label| index[label]).collect();
    (cluster, k)
}

/// Computes each cluster's centroid (mean of its points) and size, given dense cluster indices
fn cluster_centroids<S>(
    x: &ArrayBase<S, Ix2>,
    cluster: &[usize],
    k: usize,
) -> (Array2<f64>, Vec<usize>)
where
    S: Data<Elem = f64>,
{
    let mut centroids = Array2::<f64>::zeros((k, x.ncols()));
    let mut sizes = vec![0usize; k];
    for (i, &c) in cluster.iter().enumerate() {
        sizes[c] += 1;
        let mut centroid = centroids.row_mut(c);
        centroid += &x.row(i);
    }
    for (mut centroid, &size) in centroids.axis_iter_mut(Axis(0)).zip(sizes.iter()) {
        if size > 0 {
            centroid /= size as f64;
        }
    }
    (centroids, sizes)
}

/// Returns `(homogeneity, completeness)` for two label assignments
///
/// Both reuse the mutual information and entropies already defined above: with `C` the classes
/// (`labels_true`) and `K` the clusters (`labels_pred`), homogeneity is `MI / H(C)` and
/// completeness is `MI / H(K)`. A zero entropy (single cluster) makes its score 1.0
fn homogeneity_completeness(labels_true: &[usize], labels_pred: &[usize], n: usize) -> (f64, f64) {
    let (contingency, row_sums, col_sums) = contingency_matrix(labels_true, labels_pred);
    let mi = mutual_information(&contingency, n, &row_sums, &col_sums);
    let h_classes = entropy_nats(&row_sums, n);
    let h_clusters = entropy_nats(&col_sums, n);

    let homogeneity = if h_classes == 0.0 {
        1.0
    } else {
        mi / h_classes
    };
    let completeness = if h_clusters == 0.0 {
        1.0
    } else {
        mi / h_clusters
    };
    (homogeneity, completeness)
}

/// Calculates the homogeneity of a clustering: the degree to which each cluster contains only
/// members of a single ground-truth class
///
/// Scores range from 0.0 to 1.0, with 1.0 for perfectly homogeneous clusters
///
/// # Parameters
///
/// - `labels_true` - Ground-truth class of each sample
/// - `labels_pred` - Predicted cluster of each sample
///
/// # Returns
///
/// - `f64` - Homogeneity score in `[0.0, 1.0]`
///
/// # Panics
///
/// - Panics if `labels_true` and `labels_pred` have different lengths
/// - Panics if the inputs are empty
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::homogeneity_score;
///
/// let labels_true = array![0, 0, 1, 1];
/// let labels_pred = array![0, 0, 1, 1];
/// assert!((homogeneity_score(&labels_true, &labels_pred) - 1.0).abs() < 1e-12);
/// ```
pub fn homogeneity_score<S>(labels_true: &ArrayBase<S, Ix1>, labels_pred: &ArrayBase<S, Ix1>) -> f64
where
    S: Data<Elem = usize>,
{
    validate_pair(
        labels_true.len(),
        labels_pred.len(),
        "labels_true and labels_pred",
    );
    let n = labels_true.len();
    homogeneity_completeness(&to_label_vec(labels_true), &to_label_vec(labels_pred), n).0
}

/// Calculates the completeness of a clustering: the degree to which all members of a given
/// ground-truth class are assigned to the same cluster
///
/// Scores range from 0.0 to 1.0, with 1.0 for perfectly complete clusters. Completeness is the
/// dual of [`homogeneity_score`] (swapping the roles of the two labelings)
///
/// # Parameters
///
/// - `labels_true` - Ground-truth class of each sample
/// - `labels_pred` - Predicted cluster of each sample
///
/// # Returns
///
/// - `f64` - Completeness score in `[0.0, 1.0]`
///
/// # Panics
///
/// - Panics if `labels_true` and `labels_pred` have different lengths
/// - Panics if the inputs are empty
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::completeness_score;
///
/// let labels_true = array![0, 0, 1, 1];
/// let labels_pred = array![0, 0, 1, 1];
/// assert!((completeness_score(&labels_true, &labels_pred) - 1.0).abs() < 1e-12);
/// ```
pub fn completeness_score<S>(
    labels_true: &ArrayBase<S, Ix1>,
    labels_pred: &ArrayBase<S, Ix1>,
) -> f64
where
    S: Data<Elem = usize>,
{
    validate_pair(
        labels_true.len(),
        labels_pred.len(),
        "labels_true and labels_pred",
    );
    let n = labels_true.len();
    homogeneity_completeness(&to_label_vec(labels_true), &to_label_vec(labels_pred), n).1
}

/// Calculates the V-measure: the harmonic mean of [`homogeneity_score`] and [`completeness_score`]
///
/// V-measure is symmetric in the two labelings and equals the [`normalized_mutual_info`] computed
/// with arithmetic-mean normalization. Scores range from 0.0 to 1.0
///
/// # Parameters
///
/// - `labels_true` - Ground-truth class of each sample
/// - `labels_pred` - Predicted cluster of each sample
///
/// # Returns
///
/// - `f64` - V-measure score in `[0.0, 1.0]`
///
/// # Panics
///
/// - Panics if `labels_true` and `labels_pred` have different lengths
/// - Panics if the inputs are empty
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::v_measure_score;
///
/// let labels_true = array![0, 0, 1, 1, 2, 2];
/// let labels_pred = array![0, 0, 1, 1, 2, 2];
/// assert!((v_measure_score(&labels_true, &labels_pred) - 1.0).abs() < 1e-12);
/// ```
pub fn v_measure_score<S>(labels_true: &ArrayBase<S, Ix1>, labels_pred: &ArrayBase<S, Ix1>) -> f64
where
    S: Data<Elem = usize>,
{
    validate_pair(
        labels_true.len(),
        labels_pred.len(),
        "labels_true and labels_pred",
    );
    let n = labels_true.len();
    let (homogeneity, completeness) =
        homogeneity_completeness(&to_label_vec(labels_true), &to_label_vec(labels_pred), n);

    if homogeneity + completeness == 0.0 {
        0.0
    } else {
        2.0 * homogeneity * completeness / (homogeneity + completeness)
    }
}

/// Calculates the Fowlkes-Mallows index (FMI) between two cluster assignments
///
/// FMI is the geometric mean of the pairwise precision and recall over sample pairs:
/// `TP / sqrt((TP + FP) * (TP + FN))`, where the counts are over pairs grouped together by each
/// clustering. Scores range from 0.0 to 1.0, with 1.0 for identical clusterings
///
/// # Parameters
///
/// - `labels_true` - Ground-truth cluster assignment of each sample
/// - `labels_pred` - Predicted cluster assignment of each sample
///
/// # Returns
///
/// - `f64` - Fowlkes-Mallows index in `[0.0, 1.0]`
///
/// # Panics
///
/// - Panics if `labels_true` and `labels_pred` have different lengths
/// - Panics if the inputs are empty
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::fowlkes_mallows_score;
///
/// let labels_true = array![0, 0, 1, 1];
/// let labels_pred = array![0, 0, 1, 1];
/// assert!((fowlkes_mallows_score(&labels_true, &labels_pred) - 1.0).abs() < 1e-12);
/// ```
pub fn fowlkes_mallows_score<S>(
    labels_true: &ArrayBase<S, Ix1>,
    labels_pred: &ArrayBase<S, Ix1>,
) -> f64
where
    S: Data<Elem = usize>,
{
    validate_pair(
        labels_true.len(),
        labels_pred.len(),
        "labels_true and labels_pred",
    );

    let labels_true = to_label_vec(labels_true);
    let labels_pred = to_label_vec(labels_pred);
    let (contingency, row_sums, col_sums) = contingency_matrix(&labels_true, &labels_pred);

    let comb2 = |size: usize| {
        let size = size as f64;
        size * (size - 1.0) / 2.0
    };
    let tk: f64 = contingency.iter().map(|&n_ij| comb2(n_ij)).sum(); // pairs together in both
    let pk: f64 = col_sums.iter().map(|&b| comb2(b)).sum(); // pairs together in pred
    let qk: f64 = row_sums.iter().map(|&a| comb2(a)).sum(); // pairs together in true

    let denominator = (pk * qk).sqrt();
    if denominator == 0.0 {
        0.0
    } else {
        tk / denominator
    }
}

/// Calculates the Davies-Bouldin index of a clustering using Euclidean distance
///
/// Each cluster's worst-case similarity to another is `(s_i + s_j) / d(c_i, c_j)`, where `s` is the
/// mean distance of a cluster's points to its centroid `c` and `d` is the centroid distance; the
/// index is the average of these maxima. **Lower is better** (0.0 is ideal), making it a cheaper
/// `O(n * k)` complement to [`silhouette_score`] for evaluating a clustering without ground truth
///
/// # Parameters
///
/// - `x` - Feature matrix with one sample per row (`n_samples x n_features`)
/// - `labels` - Cluster assignment of each sample
///
/// # Returns
///
/// - `f64` - Davies-Bouldin index (>= 0.0; lower is better)
///
/// # Panics
///
/// - Panics if the number of rows in `x` differs from the length of `labels`
/// - Panics if the inputs are empty
/// - Panics if the number of distinct clusters is not in `2..=n_samples - 1`
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::davies_bouldin_score;
///
/// let x = array![[0.0, 0.0], [0.1, 0.0], [10.0, 0.0], [10.1, 0.0]];
/// let labels = array![0, 0, 1, 1];
/// assert!(davies_bouldin_score(&x, &labels) < 0.1); // well separated
/// ```
pub fn davies_bouldin_score<S1, S2>(x: &ArrayBase<S1, Ix2>, labels: &ArrayBase<S2, Ix1>) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = usize>,
{
    let labels = to_label_vec(labels);
    let (cluster, k) = validate_clustering_inputs(x.nrows(), &labels);
    let (centroids, sizes) = cluster_centroids(x, &cluster, k);

    // s[c] = mean distance from cluster c's points to its centroid
    let mut s = vec![0.0_f64; k];
    for (i, &c) in cluster.iter().enumerate() {
        s[c] += squared_euclidean_distance_row(&x.row(i), &centroids.row(c)).sqrt();
    }
    for (distance, &size) in s.iter_mut().zip(sizes.iter()) {
        if size > 0 {
            *distance /= size as f64;
        }
    }

    let mut db = 0.0;
    for i in 0..k {
        let mut max_ratio = 0.0_f64;
        for j in 0..k {
            if i != j {
                let centroid_dist =
                    squared_euclidean_distance_row(&centroids.row(i), &centroids.row(j)).sqrt();
                if centroid_dist > 0.0 {
                    max_ratio = max_ratio.max((s[i] + s[j]) / centroid_dist);
                }
            }
        }
        db += max_ratio;
    }
    db / k as f64
}

/// Calculates the Calinski-Harabasz index (variance ratio criterion) of a clustering
///
/// The ratio of between-cluster dispersion to within-cluster dispersion, scaled by
/// `(n - k) / (k - 1)`. **Higher is better**: well-separated, compact clusters score high. Returns
/// 1.0 in the degenerate case where every point coincides with its centroid (zero within-cluster
/// dispersion)
///
/// # Parameters
///
/// - `x` - Feature matrix with one sample per row (`n_samples x n_features`)
/// - `labels` - Cluster assignment of each sample
///
/// # Returns
///
/// - `f64` - Calinski-Harabasz index (>= 0.0; higher is better)
///
/// # Panics
///
/// - Panics if the number of rows in `x` differs from the length of `labels`
/// - Panics if the inputs are empty
/// - Panics if the number of distinct clusters is not in `2..=n_samples - 1`
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::metrics::calinski_harabasz_score;
///
/// let x = array![[0.0, 0.0], [0.1, 0.0], [10.0, 0.0], [10.1, 0.0]];
/// let labels = array![0, 0, 1, 1];
/// assert!(calinski_harabasz_score(&x, &labels) > 100.0); // well separated
/// ```
pub fn calinski_harabasz_score<S1, S2>(x: &ArrayBase<S1, Ix2>, labels: &ArrayBase<S2, Ix1>) -> f64
where
    S1: Data<Elem = f64>,
    S2: Data<Elem = usize>,
{
    let n = x.nrows();
    let labels = to_label_vec(labels);
    let (cluster, k) = validate_clustering_inputs(n, &labels);
    let (centroids, sizes) = cluster_centroids(x, &cluster, k);
    let overall = x.mean_axis(Axis(0)).unwrap(); // n > 0 is guaranteed

    let mut between = 0.0;
    for (centroid, &size) in centroids.axis_iter(Axis(0)).zip(sizes.iter()) {
        between += size as f64 * squared_euclidean_distance_row(&centroid, &overall);
    }
    let mut within = 0.0;
    for (i, &c) in cluster.iter().enumerate() {
        within += squared_euclidean_distance_row(&x.row(i), &centroids.row(c));
    }

    if within == 0.0 {
        return 1.0;
    }
    (between / within) * ((n - k) as f64 / (k - 1) as f64)
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use ndarray::array;

    // label_index

    /// First-appearance dense remap of distinct labels to first-seen ordinal positions
    #[test]
    fn test_label_index_first_appearance_order() {
        let idx = label_index(&[5, 5, 3, 3, 10]);
        assert_eq!(idx.len(), 3, "three distinct labels expected");
        assert_eq!(idx[&5], 0, "5 appears first, should map to 0");
        assert_eq!(idx[&3], 1, "3 appears second, should map to 1");
        assert_eq!(idx[&10], 2, "10 appears third, should map to 2");
    }

    /// A single repeated label maps to index 0 only
    #[test]
    fn test_label_index_single_label() {
        let idx = label_index(&[7, 7, 7]);
        assert_eq!(idx.len(), 1);
        assert_eq!(idx[&7], 0);
    }

    /// All distinct labels each get a unique index in [0, n)
    #[test]
    fn test_label_index_all_distinct() {
        let idx = label_index(&[10, 20, 30]);
        assert_eq!(idx.len(), 3);
        // each appears once in first-seen order: 10->0, 20->1, 30->2
        assert_eq!(idx[&10], 0);
        assert_eq!(idx[&20], 1);
        assert_eq!(idx[&30], 2);
    }

    // contingency_matrix

    /// Every pairing appearing once produces the all-ones 2x2 matrix with row_sums=col_sums=[2,2]
    #[test]
    fn test_contingency_matrix_uniform() {
        let (mat, row_sums, col_sums) = contingency_matrix(&[0, 0, 1, 1], &[0, 1, 0, 1]);
        assert_eq!(mat.shape(), &[2, 2]);
        assert_eq!(mat[[0, 0]], 1);
        assert_eq!(mat[[0, 1]], 1);
        assert_eq!(mat[[1, 0]], 1);
        assert_eq!(mat[[1, 1]], 1);
        assert_eq!(row_sums, vec![2, 2]);
        assert_eq!(col_sums, vec![2, 2]);
    }

    /// Identical labels give a diagonal contingency matrix
    #[test]
    fn test_contingency_matrix_identical_labels() {
        let (mat, row_sums, col_sums) = contingency_matrix(&[0, 0, 1, 1], &[0, 0, 1, 1]);
        assert_eq!(mat.shape(), &[2, 2]);
        assert_eq!(mat[[0, 0]], 2, "class-0 samples all in pred-cluster-0");
        assert_eq!(mat[[0, 1]], 0);
        assert_eq!(mat[[1, 0]], 0);
        assert_eq!(mat[[1, 1]], 2, "class-1 samples all in pred-cluster-1");
        assert_eq!(row_sums, vec![2, 2]);
        assert_eq!(col_sums, vec![2, 2]);
    }

    // entropy_nats

    /// Two equal clusters give H = ln(2)
    #[test]
    fn test_entropy_nats_two_equal_clusters() {
        let h = entropy_nats(&[2, 2], 4);
        assert_abs_diff_eq!(h, std::f64::consts::LN_2, epsilon = 1e-10);
    }

    /// A single cluster covering all samples gives H = 0
    #[test]
    fn test_entropy_nats_single_cluster() {
        let h = entropy_nats(&[4], 4);
        assert_abs_diff_eq!(h, 0.0, epsilon = 1e-10);
    }

    /// Four equal singleton clusters give H = ln(4)
    #[test]
    fn test_entropy_nats_four_equal_clusters() {
        let h = entropy_nats(&[1, 1, 1, 1], 4);
        let expected = (4.0_f64).ln(); // ln(4)
        assert_abs_diff_eq!(h, expected, epsilon = 1e-10);
    }

    /// A zero-count entry contributes nothing to the entropy
    #[test]
    fn test_entropy_nats_zero_count_skipped() {
        let h = entropy_nats(&[0, 4], 4);
        assert_abs_diff_eq!(h, 0.0, epsilon = 1e-10);
    }

    // mutual_information

    /// Independent uniform assignment gives MI = 0
    #[test]
    fn test_mutual_information_independent() {
        let mat = array![[1usize, 1], [1, 1]];
        let mi = mutual_information(&mat, 4, &[2, 2], &[2, 2]);
        assert_abs_diff_eq!(mi, 0.0, epsilon = 1e-10);
    }

    /// Perfect assignment gives MI = H(true) = ln(2)
    #[test]
    fn test_mutual_information_identical() {
        let mat = array![[2usize, 0], [0, 2]];
        let mi = mutual_information(&mat, 4, &[2, 2], &[2, 2]);
        assert_abs_diff_eq!(mi, std::f64::consts::LN_2, epsilon = 1e-10);
    }

    // homogeneity_completeness

    /// Pure clusters (more clusters than classes) give homogeneity 1.0, completeness 0.5, and pin
    /// the tuple ordering (homogeneity first)
    #[test]
    fn test_homogeneity_completeness_pure_clusters() {
        let labels_true = [0usize, 0, 1, 1];
        let labels_pred = [0usize, 1, 2, 3];
        let (h, c) = homogeneity_completeness(&labels_true, &labels_pred, 4);
        assert_abs_diff_eq!(h, 1.0, epsilon = 1e-10);
        assert_abs_diff_eq!(c, 0.5, epsilon = 1e-10);
    }

    /// Identical labels give homogeneity and completeness both 1.0
    #[test]
    fn test_homogeneity_completeness_identical() {
        let labels = [0usize, 0, 1, 1];
        let (h, c) = homogeneity_completeness(&labels, &labels, 4);
        assert_abs_diff_eq!(h, 1.0, epsilon = 1e-10);
        assert_abs_diff_eq!(c, 1.0, epsilon = 1e-10);
    }

    /// Swapping the roles of the pure-clusters case swaps the scores: homogeneity 0.5, completeness 1.0
    #[test]
    fn test_homogeneity_completeness_swapped_roles() {
        let labels_true = [0usize, 1, 2, 3];
        let labels_pred = [0usize, 0, 1, 1];
        let (h, c) = homogeneity_completeness(&labels_true, &labels_pred, 4);
        assert_abs_diff_eq!(h, 0.5, epsilon = 1e-10);
        assert_abs_diff_eq!(c, 1.0, epsilon = 1e-10);
    }

    // entropy_nats: additional edge cases

    /// Unequal clusters [1,3] match the closed-form entropy
    #[test]
    fn test_entropy_nats_unequal_clusters() {
        let h = entropy_nats(&[1, 3], 4);
        // H = -(1/4)*ln(1/4) - (3/4)*ln(3/4)
        let expected =
            -(1.0_f64 / 4.0) * (1.0_f64 / 4.0).ln() - (3.0_f64 / 4.0) * (3.0_f64 / 4.0).ln();
        assert_abs_diff_eq!(h, expected, epsilon = 1e-10);
    }

    // mutual_information: non-trivial case

    /// Four pure clusters over two true classes give MI = ln(2)
    #[test]
    fn test_mutual_information_pure_clusters() {
        // build contingency directly to avoid depending on contingency_matrix correctness
        let mat = array![[1usize, 1, 0, 0], [0, 0, 1, 1]];
        let mi = mutual_information(&mat, 4, &[2, 2], &[1, 1, 1, 1]);
        assert_abs_diff_eq!(mi, std::f64::consts::LN_2, epsilon = 1e-10);
    }
    // expected_mutual_information (EMI hypergeometric kernel)

    /// EMI for the symmetric 2x2 case (row_sums=[2,2], col_sums=[2,2], n=4) equals ln(2)/3
    #[test]
    fn test_expected_mutual_information_symmetric_2x2() {
        let emi = expected_mutual_information(&[2, 2], &[2, 2], 4);
        let expected = std::f64::consts::LN_2 / 3.0;
        assert_abs_diff_eq!(emi, expected, epsilon = 1e-12);
    }
    // ln_factorial_table

    /// table[i] = ln(i!) with length n_max + 1
    #[test]
    fn test_ln_factorial_table_known_values() {
        let table = ln_factorial_table(5);
        assert_eq!(table.len(), 6, "length should be n_max + 1");
        assert_abs_diff_eq!(table[0], 0.0, epsilon = 1e-12); // ln(0!) = ln(1)
        assert_abs_diff_eq!(table[1], 0.0, epsilon = 1e-12); // ln(1!) = ln(1)
        assert_abs_diff_eq!(table[5], (120.0_f64).ln(), epsilon = 1e-10); // ln(5!) = ln(120)
    }

    // cluster_centroids

    /// Per-cluster mean and size are computed from dense cluster indices
    #[test]
    fn test_cluster_centroids_known_means_and_sizes() {
        let x = array![[0.0, 0.0], [2.0, 0.0], [10.0, 10.0]];
        let cluster = [0usize, 0, 1];
        let (centroids, sizes) = cluster_centroids(&x, &cluster, 2);

        assert_eq!(centroids.shape(), &[2, 2]);
        assert_abs_diff_eq!(centroids[[0, 0]], 1.0, epsilon = 1e-12);
        assert_abs_diff_eq!(centroids[[0, 1]], 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(centroids[[1, 0]], 10.0, epsilon = 1e-12);
        assert_abs_diff_eq!(centroids[[1, 1]], 10.0, epsilon = 1e-12);
        assert_eq!(sizes, vec![2, 1]);
    }
}