scirs2-metrics 0.3.0

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
//! Self-supervised learning metrics
//!
//! This module provides evaluation metrics for self-supervised learning
//! including linear probing, clustering analysis, and representation quality.

#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]

use crate::error::{MetricsError, Result};
use scirs2_core::ndarray::{Array1, Array2, Axis};
use scirs2_core::numeric::Float;
use std::iter::Sum;

use super::results::{ClusteringResult, LinearProbingResult, RepresentationRankResult};

/// Self-supervised learning metrics
pub struct SelfSupervisedMetrics<F: Float> {
    /// Number of linear probing epochs
    pub n_probe_epochs: usize,
    /// Learning rate for linear probing
    pub probe_learning_rate: F,
    /// Number of clustering attempts
    pub n_clustering_runs: usize,
    _phantom: std::marker::PhantomData<F>,
}

impl<
        F: Float + scirs2_core::numeric::FromPrimitive + Sum + scirs2_core::ndarray::ScalarOperand,
    > Default for SelfSupervisedMetrics<F>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<
        F: Float + scirs2_core::numeric::FromPrimitive + Sum + scirs2_core::ndarray::ScalarOperand,
    > SelfSupervisedMetrics<F>
{
    /// Create new self-supervised learning metrics
    pub fn new() -> Self {
        Self {
            n_probe_epochs: 100,
            probe_learning_rate: F::from(0.001).expect("Failed to convert constant to float"),
            n_clustering_runs: 5,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set linear probing parameters
    pub fn with_linear_probe(mut self, epochs: usize, lr: F) -> Self {
        self.n_probe_epochs = epochs;
        self.probe_learning_rate = lr;
        self
    }

    /// Set clustering parameters
    pub fn with_clustering(mut self, runs: usize) -> Self {
        self.n_clustering_runs = runs;
        self
    }

    /// Compute linear probing accuracy
    pub fn linear_probing_accuracy(
        &self,
        representations: &Array2<F>,
        labels: &Array1<usize>,
        test_representations: &Array2<F>,
        test_labels: &Array1<usize>,
    ) -> Result<LinearProbingResult<F>> {
        if representations.nrows() != labels.len() {
            return Err(MetricsError::InvalidInput(
                "Mismatched representations and labels".to_string(),
            ));
        }

        if test_representations.nrows() != test_labels.len() {
            return Err(MetricsError::InvalidInput(
                "Mismatched test representations and labels".to_string(),
            ));
        }

        // Get number of classes
        let n_classes = labels.iter().max().unwrap_or(&0) + 1;
        let n_features = representations.ncols();

        // Initialize linear classifier weights (simplified to centroid-based)
        let mut class_centroids = Array2::zeros((n_classes, n_features));
        let mut class_counts = vec![0; n_classes];

        // Compute class centroids
        for (i, &label) in labels.iter().enumerate() {
            for j in 0..n_features {
                class_centroids[[label, j]] = class_centroids[[label, j]] + representations[[i, j]];
            }
            class_counts[label] += 1;
        }

        // Normalize centroids
        for class in 0..n_classes {
            if class_counts[class] > 0 {
                let count = F::from(class_counts[class]).expect("Failed to convert to float");
                for j in 0..n_features {
                    class_centroids[[class, j]] = class_centroids[[class, j]] / count;
                }
            }
        }

        // Evaluate on test set
        let mut correct_predictions = 0;
        let mut per_class_correct = vec![0; n_classes];
        let mut per_class_total = vec![0; n_classes];

        for (i, &true_label) in test_labels.iter().enumerate() {
            let test_sample = test_representations.row(i);

            // Find closest centroid
            let mut best_distance = F::infinity();
            let mut predicted_class = 0;

            for class in 0..n_classes {
                if class_counts[class] > 0 {
                    let centroid = class_centroids.row(class);
                    let distance =
                        self.euclidean_distance(&test_sample.to_owned(), &centroid.to_owned())?;

                    if distance < best_distance {
                        best_distance = distance;
                        predicted_class = class;
                    }
                }
            }

            per_class_total[true_label] += 1;
            if predicted_class == true_label {
                correct_predictions += 1;
                per_class_correct[true_label] += 1;
            }
        }

        let overall_accuracy = F::from(correct_predictions).expect("Failed to convert to float")
            / F::from(test_labels.len()).expect("Operation failed");

        // Compute per-class accuracies
        let mut per_class_accuracies = Vec::with_capacity(n_classes);
        for class in 0..n_classes {
            if per_class_total[class] > 0 {
                let acc = F::from(per_class_correct[class]).expect("Failed to convert to float")
                    / F::from(per_class_total[class]).expect("Failed to convert to float");
                per_class_accuracies.push(acc);
            } else {
                per_class_accuracies.push(F::zero());
            }
        }

        let balanced_accuracy = per_class_accuracies.iter().copied().sum::<F>()
            / F::from(n_classes).expect("Failed to convert to float");

        Ok(LinearProbingResult {
            overall_accuracy,
            balanced_accuracy,
            per_class_accuracies,
            n_classes,
            n_test_samples: test_labels.len(),
        })
    }

    /// Compute representation rank (effective dimensionality)
    pub fn representation_rank(
        &self,
        representations: &Array2<F>,
        threshold: F,
    ) -> Result<RepresentationRankResult<F>> {
        if representations.is_empty() {
            return Err(MetricsError::InvalidInput(
                "Empty representations".to_string(),
            ));
        }

        // Compute covariance matrix
        let cov = self.compute_covariance_matrix(representations)?;

        // Compute eigenvalues (simplified approximation using diagonal)
        let mut eigenvalues: Vec<F> = (0..cov.nrows()).map(|i| cov[[i, i]]).collect();
        eigenvalues.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));

        // Compute total variance
        let total_variance: F = eigenvalues.iter().copied().sum();

        // Find effective rank
        let mut cumulative_variance = F::zero();
        let mut effective_rank = 0;

        for &eigenval in &eigenvalues {
            cumulative_variance = cumulative_variance + eigenval;
            effective_rank += 1;

            if cumulative_variance / total_variance >= threshold {
                break;
            }
        }

        // Compute participation ratio
        let sum_eigenvals: F = eigenvalues.iter().copied().sum();
        let sum_eigenvals_sq: F = eigenvalues.iter().map(|&x| x * x).sum();
        let participation_ratio = if sum_eigenvals_sq > F::zero() {
            (sum_eigenvals * sum_eigenvals) / sum_eigenvals_sq
        } else {
            F::zero()
        };

        Ok(RepresentationRankResult {
            effective_rank,
            participation_ratio,
            eigenvalues,
            total_variance,
            explained_variance_ratio: cumulative_variance / total_variance,
        })
    }

    /// Compute clustering-based evaluation (simplified NMI)
    pub fn clustering_normalized_mutual_information(
        &self,
        representations: &Array2<F>,
        true_labels: &Array1<usize>,
        n_clusters: usize,
    ) -> Result<ClusteringResult<F>> {
        if representations.nrows() != true_labels.len() {
            return Err(MetricsError::InvalidInput(
                "Mismatched representations and labels".to_string(),
            ));
        }

        if representations.is_empty() {
            return Err(MetricsError::InvalidInput(
                "Empty representations".to_string(),
            ));
        }

        // Perform simple k-means clustering (simplified centroid-based)
        let cluster_assignments = self.simple_kmeans(representations, n_clusters)?;

        // Compute normalized mutual information
        let nmi = self.compute_normalized_mutual_information(true_labels, &cluster_assignments)?;

        // Compute adjusted rand index (simplified)
        let ari = self.compute_adjusted_rand_index(true_labels, &cluster_assignments)?;

        // Compute silhouette score
        let silhouette = self.compute_silhouette_score(representations, &cluster_assignments)?;

        Ok(ClusteringResult {
            normalized_mutual_information: nmi,
            adjusted_rand_index: ari,
            silhouette_score: silhouette,
            cluster_assignments,
            n_clusters,
        })
    }

    /// Compute Euclidean distance
    fn euclidean_distance(&self, a: &Array1<F>, b: &Array1<F>) -> Result<F> {
        if a.len() != b.len() {
            return Err(MetricsError::InvalidInput(
                "Vector dimension mismatch".to_string(),
            ));
        }

        let distance_sq: F = a
            .iter()
            .zip(b.iter())
            .map(|(&x, &y)| {
                let diff = x - y;
                diff * diff
            })
            .sum();

        Ok(distance_sq.sqrt())
    }

    /// Compute covariance matrix
    fn compute_covariance_matrix(&self, data: &Array2<F>) -> Result<Array2<F>> {
        let n_samples = data.nrows();
        let n_features = data.ncols();

        if n_samples < 2 {
            return Err(MetricsError::InvalidInput(
                "Need at least 2 samples".to_string(),
            ));
        }

        // Center the data
        let mean = data.mean_axis(Axis(0)).expect("Operation failed");
        let centered = data - &mean.insert_axis(Axis(0));

        // Compute covariance
        let mut cov = Array2::zeros((n_features, n_features));

        for i in 0..n_features {
            for j in i..n_features {
                let mut sum = F::zero();
                for k in 0..n_samples {
                    sum = sum + centered[[k, i]] * centered[[k, j]];
                }
                let cov_val = sum / F::from(n_samples - 1).expect("Failed to convert to float");
                cov[[i, j]] = cov_val;
                if i != j {
                    cov[[j, i]] = cov_val;
                }
            }
        }

        Ok(cov)
    }

    /// Simple k-means clustering
    fn simple_kmeans(&self, data: &Array2<F>, k: usize) -> Result<Vec<usize>> {
        let n_samples = data.nrows();
        let n_features = data.ncols();

        if k == 0 || k > n_samples {
            return Err(MetricsError::InvalidInput(
                "Invalid number of clusters".to_string(),
            ));
        }

        // Initialize centroids (use first k samples)
        let mut centroids = Array2::zeros((k, n_features));
        for i in 0..k {
            for j in 0..n_features {
                centroids[[i, j]] = data[[i % n_samples, j]];
            }
        }

        let mut assignments = vec![0; n_samples];
        let max_iterations = 100;

        for _ in 0..max_iterations {
            let mut changed = false;

            // Assign points to nearest centroids
            for i in 0..n_samples {
                let mut best_distance = F::infinity();
                let mut best_cluster = 0;

                for j in 0..k {
                    let distance = self.euclidean_distance(
                        &data.row(i).to_owned(),
                        &centroids.row(j).to_owned(),
                    )?;

                    if distance < best_distance {
                        best_distance = distance;
                        best_cluster = j;
                    }
                }

                if assignments[i] != best_cluster {
                    assignments[i] = best_cluster;
                    changed = true;
                }
            }

            if !changed {
                break;
            }

            // Update centroids
            let mut cluster_counts = vec![0; k];
            centroids.fill(F::zero());

            for i in 0..n_samples {
                let cluster = assignments[i];
                cluster_counts[cluster] += 1;

                for j in 0..n_features {
                    centroids[[cluster, j]] = centroids[[cluster, j]] + data[[i, j]];
                }
            }

            // Normalize centroids
            for i in 0..k {
                if cluster_counts[i] > 0 {
                    let count = F::from(cluster_counts[i]).expect("Failed to convert to float");
                    for j in 0..n_features {
                        centroids[[i, j]] = centroids[[i, j]] / count;
                    }
                }
            }
        }

        Ok(assignments)
    }

    /// Compute normalized mutual information
    fn compute_normalized_mutual_information(
        &self,
        true_labels: &Array1<usize>,
        pred_labels: &[usize],
    ) -> Result<F> {
        if true_labels.len() != pred_labels.len() {
            return Err(MetricsError::InvalidInput(
                "Label length mismatch".to_string(),
            ));
        }

        let n = true_labels.len();
        if n == 0 {
            return Ok(F::zero());
        }

        // Build contingency table
        let max_true = *true_labels.iter().max().unwrap_or(&0) + 1;
        let max_pred = *pred_labels.iter().max().unwrap_or(&0) + 1;

        let mut contingency = vec![vec![0; max_pred]; max_true];
        for i in 0..n {
            contingency[true_labels[i]][pred_labels[i]] += 1;
        }

        // Compute marginals
        let mut true_marginal = vec![0; max_true];
        let mut pred_marginal = vec![0; max_pred];

        for i in 0..max_true {
            for j in 0..max_pred {
                true_marginal[i] += contingency[i][j];
                pred_marginal[j] += contingency[i][j];
            }
        }

        // Compute mutual information
        let mut mi = F::zero();
        for i in 0..max_true {
            for j in 0..max_pred {
                if contingency[i][j] > 0 {
                    let p_ij = F::from(contingency[i][j]).expect("Failed to convert to float")
                        / F::from(n).expect("Failed to convert to float");
                    let p_i = F::from(true_marginal[i]).expect("Failed to convert to float")
                        / F::from(n).expect("Failed to convert to float");
                    let p_j = F::from(pred_marginal[j]).expect("Failed to convert to float")
                        / F::from(n).expect("Failed to convert to float");

                    if p_i > F::zero() && p_j > F::zero() {
                        mi = mi + p_ij * (p_ij / (p_i * p_j)).ln();
                    }
                }
            }
        }

        // Compute entropies for normalization
        let mut h_true = F::zero();
        let mut h_pred = F::zero();

        for i in 0..max_true {
            if true_marginal[i] > 0 {
                let p_i = F::from(true_marginal[i]).expect("Failed to convert to float")
                    / F::from(n).expect("Failed to convert to float");
                h_true = h_true - p_i * p_i.ln();
            }
        }

        for j in 0..max_pred {
            if pred_marginal[j] > 0 {
                let p_j = F::from(pred_marginal[j]).expect("Failed to convert to float")
                    / F::from(n).expect("Failed to convert to float");
                h_pred = h_pred - p_j * p_j.ln();
            }
        }

        // Normalize
        let denominator =
            (h_true + h_pred) / F::from(2.0).expect("Failed to convert constant to float");
        if denominator > F::zero() {
            Ok(mi / denominator)
        } else {
            Ok(F::zero())
        }
    }

    /// Compute adjusted rand index (simplified)
    fn compute_adjusted_rand_index(
        &self,
        true_labels: &Array1<usize>,
        pred_labels: &[usize],
    ) -> Result<F> {
        if true_labels.len() != pred_labels.len() {
            return Err(MetricsError::InvalidInput(
                "Label length mismatch".to_string(),
            ));
        }

        let n = true_labels.len();
        if n == 0 {
            return Ok(F::zero());
        }

        // Count agreements
        let mut agreements = 0;
        for i in 0..n {
            for j in (i + 1)..n {
                let same_true = true_labels[i] == true_labels[j];
                let same_pred = pred_labels[i] == pred_labels[j];

                if same_true == same_pred {
                    agreements += 1;
                }
            }
        }

        let total_pairs = n * (n - 1) / 2;
        if total_pairs == 0 {
            return Ok(F::zero());
        }

        // Simplified ARI (just agreement ratio)
        Ok(F::from(agreements).expect("Failed to convert to float")
            / F::from(total_pairs).expect("Failed to convert to float"))
    }

    /// Compute silhouette score
    fn compute_silhouette_score(
        &self,
        data: &Array2<F>,
        cluster_assignments: &[usize],
    ) -> Result<F> {
        let n_samples = data.nrows();
        if n_samples != cluster_assignments.len() {
            return Err(MetricsError::InvalidInput(
                "Data and assignments length mismatch".to_string(),
            ));
        }

        if n_samples < 2 {
            return Ok(F::zero());
        }

        let mut total_silhouette = F::zero();
        let mut valid_samples = 0;

        for i in 0..n_samples {
            let cluster_i = cluster_assignments[i];

            // Compute average intra-cluster distance
            let mut intra_distance = F::zero();
            let mut intra_count = 0;

            for j in 0..n_samples {
                if i != j && cluster_assignments[j] == cluster_i {
                    let distance =
                        self.euclidean_distance(&data.row(i).to_owned(), &data.row(j).to_owned())?;
                    intra_distance = intra_distance + distance;
                    intra_count += 1;
                }
            }

            if intra_count > 0 {
                intra_distance =
                    intra_distance / F::from(intra_count).expect("Failed to convert to float");
            }

            // Compute minimum average inter-cluster distance
            let mut min_inter_distance = F::infinity();
            let max_cluster = *cluster_assignments.iter().max().unwrap_or(&0);

            for other_cluster in 0..=max_cluster {
                if other_cluster != cluster_i {
                    let mut inter_distance = F::zero();
                    let mut inter_count = 0;

                    for j in 0..n_samples {
                        if cluster_assignments[j] == other_cluster {
                            let distance = self.euclidean_distance(
                                &data.row(i).to_owned(),
                                &data.row(j).to_owned(),
                            )?;
                            inter_distance = inter_distance + distance;
                            inter_count += 1;
                        }
                    }

                    if inter_count > 0 {
                        inter_distance = inter_distance
                            / F::from(inter_count).expect("Failed to convert to float");
                        min_inter_distance = min_inter_distance.min(inter_distance);
                    }
                }
            }

            // Compute silhouette for this sample
            if min_inter_distance != F::infinity() {
                let max_distance = intra_distance.max(min_inter_distance);
                if max_distance > F::zero() {
                    let silhouette = (min_inter_distance - intra_distance) / max_distance;
                    total_silhouette = total_silhouette + silhouette;
                    valid_samples += 1;
                }
            }
        }

        if valid_samples > 0 {
            Ok(total_silhouette / F::from(valid_samples).expect("Failed to convert to float"))
        } else {
            Ok(F::zero())
        }
    }
}