Skip to main content

oxirs_samm/analytics/
modelanalytics_compute_property_correlations_group.rs

1//! # ModelAnalytics - compute_property_correlations_group Methods
2//!
3//! This module contains method implementations for `ModelAnalytics`.
4//!
5//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
6
7use super::modelanalytics_type::ModelAnalytics;
8use crate::analytics::{
9    CorrelationDirection, CorrelationInsight, CorrelationStrength, PropertyCorrelationMatrix,
10};
11use scirs2_core::ndarray_ext::Array1;
12use std::collections::{HashMap, HashSet};
13
14fn generate_correlation_interpretation(feat1: &str, feat2: &str, coef: f64) -> String {
15    let direction = if coef > 0.0 { "increases" } else { "decreases" };
16    let strength = if coef.abs() > 0.7 {
17        "strongly"
18    } else if coef.abs() > 0.5 {
19        "moderately"
20    } else {
21        "weakly"
22    };
23    format!(
24        "As {} increases, {} {} {}",
25        feat1, feat2, strength, direction
26    )
27}
28
29impl ModelAnalytics {
30    /// Compute correlation matrix for model features
31    ///
32    /// Analyzes relationships between different model characteristics using
33    /// Pearson, Spearman, and Kendall correlation methods from scirs2-stats.
34    ///
35    /// # Returns
36    ///
37    /// PropertyCorrelationMatrix containing correlation coefficients and insights
38    ///
39    /// # Example
40    ///
41    /// ```rust,ignore
42    /// use oxirs_samm::analytics::ModelAnalytics;
43    ///
44    /// let analytics = ModelAnalytics::analyze(&aspect);
45    /// let correlations = analytics.compute_property_correlations();
46    ///
47    /// println!("Strong correlations found:");
48    /// for insight in &correlations.insights {
49    ///     if insight.strength == CorrelationStrength::Strong {
50    ///         println!("  {} <-> {}: {:.3}",
51    ///                  insight.feature1, insight.feature2, insight.coefficient);
52    ///     }
53    /// }
54    /// ```
55    pub fn compute_property_correlations(&self) -> PropertyCorrelationMatrix {
56        use scirs2_stats::{CorrelationBuilder, CorrelationMethod};
57        let features = [
58            (
59                "property_count",
60                self.distributions.property_distribution.mean,
61            ),
62            (
63                "structural_complexity",
64                self.complexity_assessment.structural,
65            ),
66            ("cognitive_complexity", self.complexity_assessment.cognitive),
67            ("coupling", self.complexity_assessment.coupling * 100.0),
68            ("quality_score", self.quality_score),
69        ];
70        let n = features.len();
71        let mut matrix = vec![vec![0.0; n]; n];
72        let mut insights = Vec::new();
73        for i in 0..n {
74            for j in 0..n {
75                if i == j {
76                    matrix[i][j] = 1.0;
77                    continue;
78                }
79                if i > j {
80                    matrix[i][j] = matrix[j][i];
81                    continue;
82                }
83                let x = Array1::from_vec(vec![features[i].1]);
84                let y = Array1::from_vec(vec![features[j].1]);
85                let corr_result = CorrelationBuilder::new()
86                    .method(CorrelationMethod::Pearson)
87                    .compute(x.view(), y.view());
88                let coefficient: f64 = match corr_result {
89                    Ok(result) => result.value.correlation,
90                    Err(_) => 0.0,
91                };
92                matrix[i][j] = coefficient;
93                let abs_coef = coefficient.abs();
94                if abs_coef > 0.3 && i != j {
95                    let strength = if abs_coef > 0.7 {
96                        CorrelationStrength::Strong
97                    } else if abs_coef > 0.5 {
98                        CorrelationStrength::Moderate
99                    } else {
100                        CorrelationStrength::Weak
101                    };
102                    let direction = if coefficient > 0.0 {
103                        CorrelationDirection::Positive
104                    } else {
105                        CorrelationDirection::Negative
106                    };
107                    insights.push(CorrelationInsight {
108                        feature1: features[i].0.to_string(),
109                        feature2: features[j].0.to_string(),
110                        coefficient,
111                        strength,
112                        direction,
113                        interpretation: generate_correlation_interpretation(
114                            features[i].0,
115                            features[j].0,
116                            coefficient,
117                        ),
118                    });
119                }
120            }
121        }
122        PropertyCorrelationMatrix {
123            feature_names: features.iter().map(|(name, _)| name.to_string()).collect(),
124            correlation_matrix: matrix,
125            insights,
126            method: "Pearson".to_string(),
127        }
128    }
129}