Skip to main content

trustformers_debug/interpretability/
lime.rs

1//! LIME (Local Interpretable Model-agnostic Explanations) analysis
2//!
3//! This module implements LIME analysis for local model interpretability,
4//! providing local explanations of model predictions through perturbation analysis.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10/// LIME (Local Interpretable Model-agnostic Explanations) analysis result
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct LimeAnalysisResult {
13    /// Analysis timestamp
14    pub timestamp: DateTime<Utc>,
15    /// Local model coefficients
16    pub local_coefficients: HashMap<String, f64>,
17    /// Feature names
18    pub feature_names: Vec<String>,
19    /// Local model R-squared
20    pub local_r_squared: f64,
21    /// Local model intercept
22    pub intercept: f64,
23    /// Feature importance scores
24    pub feature_importance: Vec<FeatureImportance>,
25    /// Perturbation analysis
26    pub perturbation_analysis: PerturbationAnalysis,
27    /// Local neighborhood statistics
28    pub neighborhood_stats: NeighborhoodStats,
29    /// Model fidelity in local region
30    pub local_fidelity: f64,
31}
32
33/// Feature importance from LIME
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct FeatureImportance {
36    /// Feature name
37    pub feature_name: String,
38    /// Importance score
39    pub importance_score: f64,
40    /// Confidence interval
41    pub confidence_interval: (f64, f64),
42    /// Statistical significance
43    pub p_value: f64,
44    /// Feature stability across perturbations
45    pub stability: f64,
46}
47
48/// Perturbation analysis details
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct PerturbationAnalysis {
51    /// Number of perturbations generated
52    pub num_perturbations: usize,
53    /// Perturbation strategy used
54    pub strategy: String,
55    /// Average prediction variance
56    pub prediction_variance: f64,
57    /// Neighborhood coverage
58    pub neighborhood_coverage: f64,
59    /// Most influential perturbations
60    pub influential_perturbations: Vec<PerturbationResult>,
61}
62
63/// Individual perturbation result
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct PerturbationResult {
66    /// Perturbation ID
67    pub id: String,
68    /// Features that were perturbed
69    pub perturbed_features: Vec<String>,
70    /// Original prediction
71    pub original_prediction: f64,
72    /// Perturbed prediction
73    pub perturbed_prediction: f64,
74    /// Prediction change
75    pub prediction_change: f64,
76    /// Distance from original instance
77    pub distance: f64,
78}
79
80/// Local neighborhood statistics
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct NeighborhoodStats {
83    /// Mean prediction in neighborhood
84    pub mean_prediction: f64,
85    /// Standard deviation of predictions
86    pub std_prediction: f64,
87    /// Neighborhood density
88    pub density: f64,
89    /// Feature correlation matrix in neighborhood
90    pub correlation_matrix: HashMap<(String, String), f64>,
91}