Skip to main content

shape_runtime/statistics/
mod.rs

1//! Advanced statistics module for Shape pattern analysis
2//!
3//! This module provides comprehensive statistical analysis capabilities
4//! for pattern matching results, including advanced metrics and visualizations.
5
6mod correlations;
7mod descriptive;
8mod distributions;
9mod patterns;
10mod temporal;
11pub mod types;
12
13use crate::query_result::QueryResult;
14use shape_ast::error::Result;
15
16// Re-export public types
17pub use types::*;
18
19// Import internal functions
20use descriptive::calculate_basic_statistics;
21use distributions::{
22    calculate_confidence_interval, match_rate_confidence_interval, value_confidence_interval,
23};
24use patterns::calculate_pattern_statistics;
25use temporal::calculate_temporal_statistics;
26
27/// Advanced statistics calculator for pattern analysis
28pub struct StatisticsCalculator {
29    /// Confidence level for statistical calculations (e.g., 0.95 for 95% CI)
30    confidence_level: f64,
31}
32
33impl StatisticsCalculator {
34    /// Create a new statistics calculator
35    pub fn new() -> Self {
36        Self {
37            confidence_level: 0.95,
38        }
39    }
40
41    /// Calculate confidence interval for a set of values
42    /// Uses t-distribution approximation for small samples
43    pub fn calculate_confidence_interval(&self, values: &[f64]) -> ConfidenceInterval {
44        calculate_confidence_interval(values, self.confidence_level)
45    }
46
47    /// Calculate confidence interval for values
48    pub fn value_confidence_interval(&self, result: &QueryResult) -> ConfidenceInterval {
49        value_confidence_interval(result, self.confidence_level)
50    }
51
52    /// Calculate confidence interval for match rate using Wilson score interval
53    pub fn match_rate_confidence_interval(
54        &self,
55        matches: usize,
56        total: usize,
57    ) -> ConfidenceInterval {
58        match_rate_confidence_interval(matches, total, self.confidence_level)
59    }
60
61    /// Generate comprehensive statistics report
62    pub fn generate_report(&self, query_result: &QueryResult) -> Result<StatisticsReport> {
63        let basic = calculate_basic_statistics(query_result)?;
64        let patterns = calculate_pattern_statistics(query_result)?;
65        let temporal = calculate_temporal_statistics(query_result)?;
66
67        Ok(StatisticsReport {
68            basic,
69            patterns,
70            temporal,
71        })
72    }
73}
74
75impl Default for StatisticsCalculator {
76    fn default() -> Self {
77        Self::new()
78    }
79}