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
//! Graph generation evaluation metrics for Graph Neural Networks
//!
//! This module provides metrics for evaluating graph generation quality,
//! structural similarity, and diversity of generated graphs.

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

use super::core::{DistributionStatistics, SpectralProperties};
use crate::error::{MetricsError, Result};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};

/// Graph generation evaluation metrics
#[derive(Debug, Clone, Default)]
pub struct GraphGenerationMetrics {
    /// Structural similarity metrics
    pub structural_similarity: StructuralSimilarityMetrics,
    /// Statistical similarity metrics
    pub statistical_similarity: StatisticalSimilarityMetrics,
    /// Spectral similarity metrics
    pub spectral_similarity: SpectralSimilarityMetrics,
    /// Generation diversity metrics
    pub diversity_metrics: GenerationDiversityMetrics,
}

/// Structural similarity between generated and reference graphs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructuralSimilarityMetrics {
    /// Degree distribution KL divergence
    pub degree_distribution_kl: f64,
    /// Clustering coefficient difference
    pub clustering_coefficient_diff: f64,
    /// Average path length difference
    pub path_length_diff: f64,
    /// Connected components similarity
    pub components_similarity: f64,
    /// Motif frequency similarity
    pub motif_similarity: f64,
}

/// Statistical similarity metrics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StatisticalSimilarityMetrics {
    /// Node count statistics
    pub node_count_stats: DistributionStatistics,
    /// Edge count statistics
    pub edge_count_stats: DistributionStatistics,
    /// Density statistics
    pub density_stats: DistributionStatistics,
}

/// Spectral similarity metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpectralSimilarityMetrics {
    /// Eigenvalue distribution similarity
    pub eigenvalue_similarity: f64,
    /// Spectral gap similarity
    pub spectral_gap_similarity: f64,
    /// Laplacian spectrum similarity
    pub laplacian_spectrum_similarity: f64,
}

/// Generation diversity metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerationDiversityMetrics {
    /// Pairwise graph edit distance
    pub pairwise_ged: f64,
    /// Structural diversity score
    pub structural_diversity: f64,
    /// Novelty score
    pub novelty_score: f64,
    /// Coverage score
    pub coverage_score: f64,
}

impl Default for StructuralSimilarityMetrics {
    fn default() -> Self {
        Self {
            degree_distribution_kl: 0.0,
            clustering_coefficient_diff: 0.0,
            path_length_diff: 0.0,
            components_similarity: 0.0,
            motif_similarity: 0.0,
        }
    }
}

impl Default for SpectralSimilarityMetrics {
    fn default() -> Self {
        Self {
            eigenvalue_similarity: 0.0,
            spectral_gap_similarity: 0.0,
            laplacian_spectrum_similarity: 0.0,
        }
    }
}

impl Default for GenerationDiversityMetrics {
    fn default() -> Self {
        Self {
            pairwise_ged: 0.0,
            structural_diversity: 0.0,
            novelty_score: 0.0,
            coverage_score: 0.0,
        }
    }
}

impl GraphGenerationMetrics {
    pub fn new() -> Self {
        Self::default()
    }

    /// Evaluate generated graphs against reference graphs
    pub fn evaluate_generation_quality(
        &mut self,
        generated_graphs: &[Vec<Vec<f64>>], // Adjacency matrices
        reference_graphs: &[Vec<Vec<f64>>],
    ) -> Result<()> {
        if generated_graphs.is_empty() || reference_graphs.is_empty() {
            return Err(MetricsError::InvalidInput(
                "Graph sets cannot be empty".to_string(),
            ));
        }

        // Compute structural similarity
        self.compute_structural_similarity(generated_graphs, reference_graphs)?;

        // Compute statistical similarity
        self.compute_statistical_similarity(generated_graphs, reference_graphs)?;

        // Compute diversity metrics
        self.compute_diversity_metrics(generated_graphs)?;

        Ok(())
    }

    fn compute_structural_similarity(
        &mut self,
        generated_graphs: &[Vec<Vec<f64>>],
        reference_graphs: &[Vec<Vec<f64>>],
    ) -> Result<()> {
        // Compute degree distributions
        let gen_degree_dist = self.compute_degree_distribution(generated_graphs);
        let ref_degree_dist = self.compute_degree_distribution(reference_graphs);

        // KL divergence between degree distributions
        self.structural_similarity.degree_distribution_kl =
            self.compute_kl_divergence(&gen_degree_dist, &ref_degree_dist);

        // Clustering coefficient difference
        let gen_clustering = generated_graphs
            .iter()
            .map(|g| self.compute_clustering_coefficient(g))
            .collect::<Vec<_>>();
        let ref_clustering = reference_graphs
            .iter()
            .map(|g| self.compute_clustering_coefficient(g))
            .collect::<Vec<_>>();

        let gen_avg_clustering = gen_clustering.iter().sum::<f64>() / gen_clustering.len() as f64;
        let ref_avg_clustering = ref_clustering.iter().sum::<f64>() / ref_clustering.len() as f64;

        self.structural_similarity.clustering_coefficient_diff =
            (gen_avg_clustering - ref_avg_clustering).abs();

        Ok(())
    }

    fn compute_statistical_similarity(
        &mut self,
        generated_graphs: &[Vec<Vec<f64>>],
        reference_graphs: &[Vec<Vec<f64>>],
    ) -> Result<()> {
        // Node count statistics
        let gen_node_counts: Vec<f64> = generated_graphs.iter().map(|g| g.len() as f64).collect();
        let ref_node_counts: Vec<f64> = reference_graphs.iter().map(|g| g.len() as f64).collect();

        self.statistical_similarity.node_count_stats =
            self.compute_distribution_stats(&gen_node_counts, &ref_node_counts);

        // Edge count statistics
        let gen_edge_counts: Vec<f64> = generated_graphs
            .iter()
            .map(|g| self.count_edges(g) as f64)
            .collect();
        let ref_edge_counts: Vec<f64> = reference_graphs
            .iter()
            .map(|g| self.count_edges(g) as f64)
            .collect();

        self.statistical_similarity.edge_count_stats =
            self.compute_distribution_stats(&gen_edge_counts, &ref_edge_counts);

        // Density statistics
        let gen_densities: Vec<f64> = generated_graphs
            .iter()
            .map(|g| self.compute_density(g))
            .collect();
        let ref_densities: Vec<f64> = reference_graphs
            .iter()
            .map(|g| self.compute_density(g))
            .collect();

        self.statistical_similarity.density_stats =
            self.compute_distribution_stats(&gen_densities, &ref_densities);

        Ok(())
    }

    fn compute_diversity_metrics(&mut self, generated_graphs: &[Vec<Vec<f64>>]) -> Result<()> {
        if generated_graphs.len() < 2 {
            return Ok(());
        }

        // Compute pairwise distances
        let mut total_distance = 0.0;
        let mut pair_count = 0;

        for i in 0..generated_graphs.len() {
            for j in (i + 1)..generated_graphs.len() {
                let distance =
                    self.compute_graph_edit_distance(&generated_graphs[i], &generated_graphs[j]);
                total_distance += distance;
                pair_count += 1;
            }
        }

        self.diversity_metrics.pairwise_ged = if pair_count > 0 {
            total_distance / pair_count as f64
        } else {
            0.0
        };

        // Structural diversity (simplified)
        let degree_variances: Vec<f64> = generated_graphs
            .iter()
            .map(|g| self.compute_degree_variance(g))
            .collect();

        self.diversity_metrics.structural_diversity =
            degree_variances.iter().sum::<f64>() / degree_variances.len() as f64;

        Ok(())
    }

    fn compute_degree_distribution(&self, graphs: &[Vec<Vec<f64>>]) -> HashMap<usize, f64> {
        let mut degree_counts: HashMap<usize, usize> = HashMap::new();
        let mut total_nodes = 0;

        for graph in graphs {
            for i in 0..graph.len() {
                let degree = graph[i].iter().filter(|&&x| x > 0.0).count();
                *degree_counts.entry(degree).or_insert(0) += 1;
                total_nodes += 1;
            }
        }

        degree_counts
            .into_iter()
            .map(|(degree, count)| (degree, count as f64 / total_nodes as f64))
            .collect()
    }

    fn compute_kl_divergence(
        &self,
        dist1: &HashMap<usize, f64>,
        dist2: &HashMap<usize, f64>,
    ) -> f64 {
        let mut kl_div = 0.0;
        let epsilon = 1e-10;

        let all_keys: std::collections::HashSet<usize> =
            dist1.keys().chain(dist2.keys()).cloned().collect();

        for key in all_keys {
            let p = dist1.get(&key).unwrap_or(&epsilon);
            let q = dist2.get(&key).unwrap_or(&epsilon);

            if *p > 0.0 && *q > 0.0 {
                kl_div += p * (p / q).ln();
            }
        }

        kl_div
    }

    fn compute_clustering_coefficient(&self, graph: &[Vec<f64>]) -> f64 {
        let n = graph.len();
        if n < 3 {
            return 0.0;
        }

        let mut total_clustering = 0.0;
        let mut node_count = 0;

        for i in 0..n {
            let neighbors: Vec<usize> = (0..n).filter(|&j| i != j && graph[i][j] > 0.0).collect();

            if neighbors.len() < 2 {
                continue;
            }

            let mut triangle_count = 0;
            for &u in &neighbors {
                for &v in &neighbors {
                    if u < v && graph[u][v] > 0.0 {
                        triangle_count += 1;
                    }
                }
            }

            let possible_triangles = neighbors.len() * (neighbors.len() - 1) / 2;
            if possible_triangles > 0 {
                total_clustering += triangle_count as f64 / possible_triangles as f64;
                node_count += 1;
            }
        }

        if node_count > 0 {
            total_clustering / node_count as f64
        } else {
            0.0
        }
    }

    fn count_edges(&self, graph: &[Vec<f64>]) -> usize {
        let mut edge_count = 0;
        for i in 0..graph.len() {
            for j in (i + 1)..graph.len() {
                if graph[i][j] > 0.0 {
                    edge_count += 1;
                }
            }
        }
        edge_count
    }

    fn compute_density(&self, graph: &[Vec<f64>]) -> f64 {
        let n = graph.len();
        if n <= 1 {
            return 0.0;
        }

        let edge_count = self.count_edges(graph);
        let max_edges = n * (n - 1) / 2;

        if max_edges > 0 {
            edge_count as f64 / max_edges as f64
        } else {
            0.0
        }
    }

    fn compute_distribution_stats(&self, dist1: &[f64], dist2: &[f64]) -> DistributionStatistics {
        // Simplified implementation - compute basic statistics difference
        let mean1 = dist1.iter().sum::<f64>() / dist1.len() as f64;
        let mean2 = dist2.iter().sum::<f64>() / dist2.len() as f64;

        let var1 = dist1.iter().map(|x| (x - mean1).powi(2)).sum::<f64>() / dist1.len() as f64;
        let var2 = dist2.iter().map(|x| (x - mean2).powi(2)).sum::<f64>() / dist2.len() as f64;

        DistributionStatistics {
            mean: (mean1 - mean2).abs(),
            std_dev: (var1.sqrt() - var2.sqrt()).abs(),
            skewness: 0.0, // Simplified
            kurtosis: 0.0, // Simplified
            min: dist1.iter().fold(f64::INFINITY, |a, &b| a.min(b)),
            max: dist1.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b)),
            median: 0.0, // Simplified
            percentiles: BTreeMap::new(),
        }
    }

    fn compute_graph_edit_distance(&self, graph1: &[Vec<f64>], graph2: &[Vec<f64>]) -> f64 {
        // Simplified GED approximation based on structural differences
        let n1 = graph1.len();
        let n2 = graph2.len();

        let node_diff = (n1 as i32 - n2 as i32).abs() as f64;
        let edge_diff =
            (self.count_edges(graph1) as i32 - self.count_edges(graph2) as i32).abs() as f64;

        node_diff + edge_diff
    }

    fn compute_degree_variance(&self, graph: &[Vec<f64>]) -> f64 {
        let degrees: Vec<f64> = (0..graph.len())
            .map(|i| graph[i].iter().filter(|&&x| x > 0.0).count() as f64)
            .collect();

        if degrees.is_empty() {
            return 0.0;
        }

        let mean = degrees.iter().sum::<f64>() / degrees.len() as f64;
        degrees.iter().map(|d| (d - mean).powi(2)).sum::<f64>() / degrees.len() as f64
    }
}

impl StructuralSimilarityMetrics {
    pub fn new() -> Self {
        Self::default()
    }
}

impl StatisticalSimilarityMetrics {
    pub fn new() -> Self {
        Self::default()
    }
}

impl SpectralSimilarityMetrics {
    pub fn new() -> Self {
        Self::default()
    }
}

impl GenerationDiversityMetrics {
    pub fn new() -> Self {
        Self::default()
    }
}