reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Statistical Significance Testing for Vibe Protocol
//!
//! Implements bootstrap resampling, confidence intervals, and statistical significance
//! testing for multi-model validation with rigorous statistical methodology.

use crate::vibe::{VIBEError, ValidationResult};
use rand::prelude::*;
use rand::rngs::StdRng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Statistical test configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalConfig {
    /// Number of bootstrap samples
    pub bootstrap_samples: usize,

    /// Confidence level for intervals (e.g., 0.95 for 95%)
    pub confidence_level: f32,

    /// Significance threshold for p-values
    pub significance_threshold: f32,

    /// Enable robust statistics (resistant to outliers)
    pub enable_robust_statistics: bool,

    /// Use Bayesian methods for uncertainty estimation
    pub enable_bayesian_methods: bool,
}

impl Default for StatisticalConfig {
    fn default() -> Self {
        Self {
            bootstrap_samples: 1000,
            confidence_level: 0.95,
            significance_threshold: 0.05,
            enable_robust_statistics: true,
            enable_bayesian_methods: false,
        }
    }
}

/// Statistical test result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalResult {
    /// Calculated p-value
    pub p_value: f32,

    /// Statistical significance (p < threshold)
    pub is_significant: bool,

    /// Effect size measure
    pub effect_size: f32,

    /// Confidence intervals
    pub confidence_interval: (f32, f32),

    /// Power of the test
    pub test_power: f32,

    /// Method used for analysis
    pub method: StatisticalMethod,
}

/// Statistical methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StatisticalMethod {
    BootstrapResampling,
    TTest,
    MannWhitneyU,
    BayesianInference,
    PermutationTest,
}

/// Statistical testing engine
pub struct StatisticalEngine {
    config: StatisticalConfig,
    rng: StdRng,
}

impl StatisticalEngine {
    /// Create new statistical engine
    pub fn new(config: StatisticalConfig) -> Self {
        Self {
            config,
            rng: StdRng::from_entropy(),
        }
    }

    /// Create with default configuration
    pub fn default() -> Self {
        Self::new(StatisticalConfig::default())
    }

    /// Perform bootstrap resampling significance test
    pub fn bootstrap_significance_test(
        &mut self,
        validation_results: &[ValidationResult],
        baseline_score: f32,
    ) -> Result<StatisticalResult, VIBEError> {
        if validation_results.is_empty() {
            return Err(VIBEError::ValidationError(
                "No validation results for statistical testing".to_string(),
            ));
        }

        // Extract scores from validation results
        let scores: Vec<f32> = validation_results
            .iter()
            .map(|result| result.overall_score / 100.0)
            .collect();

        // Perform bootstrap resampling
        let bootstrap_distribution = self.bootstrap_resampling(&scores)?;

        // Calculate p-value
        let p_value = self.calculate_p_value(baseline_score, &bootstrap_distribution);

        // Calculate effect size
        let effect_size = self.calculate_effect_size(&scores, baseline_score);

        // Calculate confidence intervals
        let confidence_interval = self.calculate_confidence_interval(&bootstrap_distribution);

        // Calculate test power
        let test_power = self.calculate_power(&scores, baseline_score);

        Ok(StatisticalResult {
            p_value,
            is_significant: p_value < self.config.significance_threshold,
            effect_size,
            confidence_interval,
            test_power,
            method: StatisticalMethod::BootstrapResampling,
        })
    }

    /// Perform multi-model consistency testing
    pub fn multi_model_consistency_test(
        &mut self,
        model_scores: &HashMap<String, f32>,
    ) -> Result<StatisticalResult, VIBEError> {
        if model_scores.is_empty() {
            return Err(VIBEError::ValidationError(
                "No model scores for consistency testing".to_string(),
            ));
        }

        let scores: Vec<f32> = model_scores.values().copied().collect();

        // Use mean score as baseline
        let mean_score = scores.iter().sum::<f32>() / scores.len() as f32;

        // Calculate variance as indicator of consistency
        let variance = scores
            .iter()
            .map(|score| (score - mean_score).powi(2))
            .sum::<f32>()
            / scores.len() as f32;

        // Lower variance indicates better consistency
        let consistency_score = 1.0 - variance.min(1.0);

        // For consistency testing, we want low variance (high consistency)
        let p_value = variance; // Simplified: variance as p-value

        Ok(StatisticalResult {
            p_value,
            is_significant: variance < 0.1, // Low variance is significant
            effect_size: consistency_score,
            confidence_interval: (mean_score - variance.sqrt(), mean_score + variance.sqrt()),
            test_power: 0.8, // Assuming reasonable power
            method: StatisticalMethod::BootstrapResampling,
        })
    }

    /// Perform cross-validation stability test
    pub fn cross_validation_stability_test(
        &mut self,
        validation_folds: &[Vec<ValidationResult>],
    ) -> Result<StatisticalResult, VIBEError> {
        if validation_folds.is_empty() {
            return Err(VIBEError::ValidationError(
                "No validation folds for stability testing".to_string(),
            ));
        }

        // Calculate mean scores for each fold
        let fold_means: Vec<f32> = validation_folds
            .iter()
            .map(|fold| {
                fold.iter()
                    .map(|result| result.overall_score / 100.0)
                    .sum::<f32>()
                    / fold.len() as f32
            })
            .collect();

        // Calculate stability metric (lower variance = higher stability)
        let overall_mean = fold_means.iter().sum::<f32>() / fold_means.len() as f32;
        let variance = fold_means
            .iter()
            .map(|mean| (mean - overall_mean).powi(2))
            .sum::<f32>()
            / fold_means.len() as f32;

        let stability_score = 1.0 - variance.min(1.0);

        Ok(StatisticalResult {
            p_value: variance,
            is_significant: variance < 0.05, // Low variance indicates stability
            effect_size: stability_score,
            confidence_interval: (overall_mean - variance.sqrt(), overall_mean + variance.sqrt()),
            test_power: 0.8,
            method: StatisticalMethod::BootstrapResampling,
        })
    }

    /// Validate statistical significance across cultures/languages
    pub fn cross_cultural_significance_test(
        &mut self,
        cultural_validation_results: &HashMap<String, Vec<ValidationResult>>,
    ) -> Result<StatisticalResult, VIBEError> {
        if cultural_validation_results.is_empty() {
            return Err(VIBEError::ValidationError(
                "No cultural validation results for testing".to_string(),
            ));
        }

        // Calculate average scores per culture
        let cultural_means: HashMap<String, f32> = cultural_validation_results
            .iter()
            .map(|(culture, results)| {
                let mean = results
                    .iter()
                    .map(|result| result.overall_score / 100.0)
                    .sum::<f32>()
                    / results.len() as f32;
                (culture.clone(), mean)
            })
            .collect();

        // Calculate overall mean and variance across cultures
        let cultural_scores: Vec<f32> = cultural_means.values().copied().collect();
        let overall_mean = cultural_scores.iter().sum::<f32>() / cultural_scores.len() as f32;
        let variance = cultural_scores
            .iter()
            .map(|score| (score - overall_mean).powi(2))
            .sum::<f32>()
            / cultural_scores.len() as f32;

        // For cross-cultural testing, we want low variance (consistent performance)
        let cultural_consistency = 1.0 - variance.min(1.0);

        Ok(StatisticalResult {
            p_value: variance,
            is_significant: variance < 0.1, // Low variance indicates cultural consistency
            effect_size: cultural_consistency,
            confidence_interval: (overall_mean - variance.sqrt(), overall_mean + variance.sqrt()),
            test_power: 0.7,
            method: StatisticalMethod::BootstrapResampling,
        })
    }

    /// Helper: Bootstrap resampling implementation
    fn bootstrap_resampling(&mut self, scores: &[f32]) -> Result<Vec<f32>, VIBEError> {
        let n = scores.len();
        let mut bootstrap_means = Vec::with_capacity(self.config.bootstrap_samples);

        for _ in 0..self.config.bootstrap_samples {
            // Sample with replacement
            let sample: Vec<f32> = (0..n)
                .map(|_| scores[self.rng.gen_range(0..n)])
                .collect();

            let mean = sample.iter().sum::<f32>() / sample.len() as f32;
            bootstrap_means.push(mean);
        }

        bootstrap_means.sort_by(|a, b| a.partial_cmp(b).unwrap());
        Ok(bootstrap_means)
    }

    /// Helper: Calculate p-value from bootstrap distribution
    fn calculate_p_value(&self, baseline: f32, bootstrap_distribution: &[f32]) -> f32 {
        // Count how many bootstrap samples are as extreme as or more extreme than baseline
        let extreme_count = bootstrap_distribution
            .iter()
            .filter(|&score| score >= baseline)
            .count();

        extreme_count as f32 / bootstrap_distribution.len() as f32
    }

    /// Helper: Calculate effect size
    fn calculate_effect_size(&self, scores: &[f32], baseline: f32) -> f32 {
        let sample_mean = scores.iter().sum::<f32>() / scores.len() as f32;
        let sample_std = self.calculate_standard_deviation(scores);

        if sample_std == 0.0 {
            return 0.0;
        }

        (sample_mean - baseline) / sample_std
    }

    /// Helper: Calculate confidence intervals
    fn calculate_confidence_interval(&self, bootstrap_distribution: &[f32]) -> (f32, f32) {
        let n = bootstrap_distribution.len();
        let lower_index = ((1.0 - self.config.confidence_level) / 2.0 * n as f32) as usize;
        let upper_index = n - lower_index - 1;

        (
            bootstrap_distribution[lower_index],
            bootstrap_distribution[upper_index],
        )
    }

    /// Helper: Calculate test power
    fn calculate_power(&self, scores: </f32>, baseline: f32) -> f32 {
        // Simplified power calculation
        let effect_size = self.calculate_effect_size(scores, baseline);
        let n = scores.len();

        if n == 0 {
            return 0.0;
        }

        // Cohen's approximate power calculation
        let power = 1.0 - (-effect_size.abs() * (n as f32).sqrt()).exp();
        power.min(1.0).max(0.0)
    }

    /// Helper: Calculate standard deviation
    fn calculate_standard_deviation(&self, data: &[f32]) -> f32 {
        if data.is_empty() {
            return 0.0;
        }

        let mean = data.iter().sum::<f32>() / data.len() as f32;
        let variance = data
            .iter()
            .map(|value| (value - mean).powi(2))
            .sum::<f32>()
            / data.len() as f32;

        variance.sqrt()
    }
}

/// Bayesian statistical methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BayesianAnalyzer {
    /// Prior distribution parameters
    prior_alpha: f32,
    prior_beta: f32,
}

impl BayesianAnalyzer {
    /// Create new Bayesian analyzer
    pub fn new(prior_alpha: f32, prior_beta: f32) -> Self {
        Self {
            prior_alpha,
            prior_beta,
        }
    }

    /// Perform Bayesian inference on validation results
    pub fn bayesian_inference(
        &self,
        successes: usize,
        total: usize,
    ) -> (f32, f32) {
        // Beta-Binomial conjugate prior update
        let posterior_alpha = self.prior_alpha + successes as f32;
        let posterior_beta = self.prior_beta + (total - successes) as f32;

        // Calculate posterior mean and credible interval
        let posterior_mean = posterior_alpha / (posterior_alpha + posterior_beta);
        let posterior_variance = (posterior_alpha * posterior_beta)
            / ((posterior_alpha + posterior_beta).powi(2)
            * (posterior_alpha + posterior_beta + 1.0));

        // 95% credible interval (approximate)
        let credible_interval = (
            posterior_mean - 1.96 * posterior_variance.sqrt(),
            posterior_mean + 1.96 * posterior_variance.sqrt(),
        );

        (posterior_mean, credible_interval.1)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vibe::ValidationResult;

    #[test]
    fn test_statistical_engine_creation() {
        let engine = StatisticalEngine::default();
        assert_eq!(engine.config.bootstrap_samples, 1000);
        assert_eq!(engine.config.confidence_level, 0.95);
    }

    #[test]
    fn test_bootstrap_significance_test() {
        let mut engine = StatisticalEngine::default();
        let validation_results = vec![
            ValidationResult {
                overall_score: 85.0,
                platform_scores: HashMap::new(),
                confidence_interval: None,
                status: crate::vibe::validation::ValidationStatus::Validated,
                detailed_results: HashMap::new(),
                validation_time_ms: 100,
                issues: Vec::new(),
                recommendations: Vec::new(),
                timestamp: chrono::Utc::now(),
                protocol_id: uuid::Uuid::new_v4(),
            },
            ValidationResult {
                overall_score: 90.0,
                platform_scores: HashMap::new(),
                confidence_interval: None,
                status: crate::vibe::validation::ValidationStatus::Validated,
                detailed_results: HashMap::new(),
                validation_time_ms: 100,
                issues: Vec::new(),
                recommendations: Vec::new(),
                timestamp: chrono::Utc::now(),
                protocol_id: uuid::Uuid::new_v4(),
            },
        ];

        let result = engine
            .bootstrap_significance_test(&validation_results, 0.7)
            .unwrap();

        assert!(result.p_value >= 0.0);
        assert!(result.p_value <= 1.0);
    }

    #[test]
    fn test_bayesian_inference() {
        let analyzer = BayesianAnalyzer::new(1.0, 1.0); // Uniform prior
        let (mean, _) = analyzer.bayesian_inference(80, 100);

        assert!(mean > 0.7 && mean < 0.9);
    }
}