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
//! Probabilistic Programming and Uncertainty Modeling
//!
//! This module provides Bayesian inference and uncertainty modeling
//! capabilities using the rv and statrs crates.
//!
//! # Features
//! - Probability distributions (Normal, Beta, Bernoulli, etc.)
//! - Bayesian inference primitives
//! - Confidence interval estimation
//! - Particle filtering for state estimation
//!
//! Enable with: `cargo build --features probabilistic`

use serde::{Deserialize, Serialize};

// Re-exports for convenience
pub use rv;
pub use statrs;

use statrs::distribution::{Beta, Continuous, ContinuousCDF, Normal};

/// Confidence level for intervals
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ConfidenceLevel {
    /// 90% confidence
    P90,
    /// 95% confidence
    P95,
    /// 99% confidence
    P99,
}

impl ConfidenceLevel {
    /// Get the alpha value (1 - confidence)
    pub fn alpha(&self) -> f64 {
        match self {
            ConfidenceLevel::P90 => 0.10,
            ConfidenceLevel::P95 => 0.05,
            ConfidenceLevel::P99 => 0.01,
        }
    }

    /// Get the z-score for this confidence level
    pub fn z_score(&self) -> f64 {
        match self {
            ConfidenceLevel::P90 => 1.645,
            ConfidenceLevel::P95 => 1.96,
            ConfidenceLevel::P99 => 2.576,
        }
    }
}

/// A confidence interval
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ConfidenceInterval {
    pub lower: f64,
    pub upper: f64,
    pub level: ConfidenceLevel,
}

impl ConfidenceInterval {
    /// Create a new confidence interval
    pub fn new(lower: f64, upper: f64, level: ConfidenceLevel) -> Self {
        Self {
            lower,
            upper,
            level,
        }
    }

    /// Width of the interval
    pub fn width(&self) -> f64 {
        self.upper - self.lower
    }

    /// Check if a value is within the interval
    pub fn contains(&self, value: f64) -> bool {
        value >= self.lower && value <= self.upper
    }
}

/// Estimate confidence interval from samples
pub fn confidence_interval(samples: &[f64], level: ConfidenceLevel) -> ConfidenceInterval {
    if samples.is_empty() {
        return ConfidenceInterval::new(f64::NAN, f64::NAN, level);
    }

    let mean = samples.iter().sum::<f64>() / samples.len() as f64;
    let variance =
        samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (samples.len() - 1) as f64;
    let std_err = (variance / samples.len() as f64).sqrt();

    let z = level.z_score();
    let lower = mean - z * std_err;
    let upper = mean + z * std_err;

    ConfidenceInterval::new(lower, upper, level)
}

/// Uncertainty estimate for a value
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UncertainValue {
    /// Point estimate (mean)
    pub value: f64,
    /// Standard deviation
    pub uncertainty: f64,
    /// Optional confidence interval
    pub confidence_interval: Option<ConfidenceInterval>,
}

impl UncertainValue {
    /// Create from a normal distribution
    pub fn from_normal(mean: f64, std_dev: f64) -> Self {
        let ci = ConfidenceInterval::new(
            mean - 1.96 * std_dev,
            mean + 1.96 * std_dev,
            ConfidenceLevel::P95,
        );
        Self {
            value: mean,
            uncertainty: std_dev,
            confidence_interval: Some(ci),
        }
    }

    /// Create from samples
    pub fn from_samples(samples: &[f64]) -> Self {
        if samples.is_empty() {
            return Self {
                value: f64::NAN,
                uncertainty: f64::NAN,
                confidence_interval: None,
            };
        }

        let mean = samples.iter().sum::<f64>() / samples.len() as f64;
        let variance =
            samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (samples.len() - 1) as f64;
        let std_dev = variance.sqrt();

        let ci = confidence_interval(samples, ConfidenceLevel::P95);

        Self {
            value: mean,
            uncertainty: std_dev,
            confidence_interval: Some(ci),
        }
    }

    /// Relative uncertainty (coefficient of variation)
    pub fn relative_uncertainty(&self) -> f64 {
        if self.value == 0.0 {
            f64::INFINITY
        } else {
            self.uncertainty / self.value.abs()
        }
    }
}

/// Bayesian belief about a binary outcome
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BinaryBelief {
    /// Number of successes observed
    pub successes: u64,
    /// Number of failures observed
    pub failures: u64,
    /// Prior alpha (successes)
    pub prior_alpha: f64,
    /// Prior beta (failures)
    pub prior_beta: f64,
}

impl BinaryBelief {
    /// Create a new belief with uniform prior
    pub fn uniform_prior() -> Self {
        Self {
            successes: 0,
            failures: 0,
            prior_alpha: 1.0,
            prior_beta: 1.0,
        }
    }

    /// Create with a Jeffreys prior (minimally informative)
    pub fn jeffreys_prior() -> Self {
        Self {
            successes: 0,
            failures: 0,
            prior_alpha: 0.5,
            prior_beta: 0.5,
        }
    }

    /// Create with a specific prior belief
    pub fn with_prior(prior_probability: f64, strength: f64) -> Self {
        let alpha = prior_probability * strength;
        let beta = (1.0 - prior_probability) * strength;
        Self {
            successes: 0,
            failures: 0,
            prior_alpha: alpha,
            prior_beta: beta,
        }
    }

    /// Update belief with a new observation
    pub fn observe(&mut self, success: bool) {
        if success {
            self.successes += 1;
        } else {
            self.failures += 1;
        }
    }

    /// Update with multiple observations
    pub fn observe_batch(&mut self, successes: u64, failures: u64) {
        self.successes += successes;
        self.failures += failures;
    }

    /// Posterior alpha
    pub fn posterior_alpha(&self) -> f64 {
        self.prior_alpha + self.successes as f64
    }

    /// Posterior beta
    pub fn posterior_beta(&self) -> f64 {
        self.prior_beta + self.failures as f64
    }

    /// Mean of the posterior distribution
    pub fn mean(&self) -> f64 {
        let alpha = self.posterior_alpha();
        let beta = self.posterior_beta();
        alpha / (alpha + beta)
    }

    /// Variance of the posterior distribution
    pub fn variance(&self) -> f64 {
        let alpha = self.posterior_alpha();
        let beta = self.posterior_beta();
        let sum = alpha + beta;
        (alpha * beta) / (sum * sum * (sum + 1.0))
    }

    /// Standard deviation of the posterior
    pub fn std_dev(&self) -> f64 {
        self.variance().sqrt()
    }

    /// Get the posterior as an uncertain value
    pub fn to_uncertain(&self) -> UncertainValue {
        UncertainValue::from_normal(self.mean(), self.std_dev())
    }

    /// Credible interval for the probability
    pub fn credible_interval(&self, level: ConfidenceLevel) -> ConfidenceInterval {
        let alpha = self.posterior_alpha();
        let beta_param = self.posterior_beta();
        let dist = Beta::new(alpha, beta_param).unwrap();

        let half_alpha = level.alpha() / 2.0;
        let lower = dist.inverse_cdf(half_alpha);
        let upper = dist.inverse_cdf(1.0 - half_alpha);

        ConfidenceInterval::new(lower, upper, level)
    }
}

impl Default for BinaryBelief {
    fn default() -> Self {
        Self::uniform_prior()
    }
}

/// Particle filter for state estimation
pub struct ParticleFilter {
    /// Particle states
    particles: Vec<f64>,
    /// Particle weights
    weights: Vec<f64>,
    /// Process noise standard deviation
    process_noise: f64,
}

impl ParticleFilter {
    /// Create a new particle filter
    pub fn new(
        num_particles: usize,
        initial_state: f64,
        initial_spread: f64,
        process_noise: f64,
    ) -> Self {
        let dist = Normal::new(initial_state, initial_spread).unwrap();
        let particles: Vec<f64> = (0..num_particles)
            .map(|_| {
                let u: f64 = rand::random();
                dist.inverse_cdf(u)
            })
            .collect();

        let uniform_weight = 1.0 / num_particles as f64;
        let weights = vec![uniform_weight; num_particles];

        Self {
            particles,
            weights,
            process_noise,
        }
    }

    /// Predict step - propagate particles forward
    pub fn predict(&mut self) {
        let noise_dist = Normal::new(0.0, self.process_noise).unwrap();
        for particle in &mut self.particles {
            let u: f64 = rand::random();
            *particle += noise_dist.inverse_cdf(u);
        }
    }

    /// Update step - weight particles by likelihood of observation
    pub fn update(&mut self, observation: f64, observation_noise: f64) {
        let obs_dist = Normal::new(0.0, observation_noise).unwrap();

        for (i, particle) in self.particles.iter().enumerate() {
            let residual = observation - particle;
            self.weights[i] *= obs_dist.pdf(residual);
        }

        // Normalize weights
        let sum: f64 = self.weights.iter().sum();
        if sum > 0.0 {
            for w in &mut self.weights {
                *w /= sum;
            }
        }
    }

    /// Get the weighted mean estimate
    pub fn estimate(&self) -> f64 {
        self.particles
            .iter()
            .zip(self.weights.iter())
            .map(|(p, w)| p * w)
            .sum()
    }

    /// Get the uncertainty estimate
    pub fn uncertainty(&self) -> UncertainValue {
        let mean = self.estimate();
        let variance: f64 = self
            .particles
            .iter()
            .zip(self.weights.iter())
            .map(|(p, w)| w * (p - mean).powi(2))
            .sum();

        UncertainValue::from_normal(mean, variance.sqrt())
    }

    /// Resample particles to avoid degeneracy
    pub fn resample(&mut self) {
        let n = self.particles.len();
        let mut cumsum = vec![0.0; n];
        cumsum[0] = self.weights[0];
        for i in 1..n {
            cumsum[i] = cumsum[i - 1] + self.weights[i];
        }

        let mut new_particles = Vec::with_capacity(n);
        for _ in 0..n {
            let u: f64 = rand::random();
            let idx = cumsum.iter().position(|&c| c >= u).unwrap_or(n - 1);
            new_particles.push(self.particles[idx]);
        }

        self.particles = new_particles;
        self.weights = vec![1.0 / n as f64; n];
    }

    /// Effective sample size
    pub fn effective_sample_size(&self) -> f64 {
        let sum_sq: f64 = self.weights.iter().map(|w| w * w).sum();
        if sum_sq > 0.0 {
            1.0 / sum_sq
        } else {
            0.0
        }
    }
}

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

    #[test]
    fn test_confidence_interval() {
        let samples: Vec<f64> = (0..100).map(|i| i as f64).collect();
        let ci = confidence_interval(&samples, ConfidenceLevel::P95);
        assert!(ci.lower < ci.upper);
        assert!(ci.contains(50.0));
    }

    #[test]
    fn test_binary_belief() {
        let mut belief = BinaryBelief::uniform_prior();
        belief.observe_batch(8, 2);

        let mean = belief.mean();
        assert!(mean > 0.7 && mean < 0.9);

        let ci = belief.credible_interval(ConfidenceLevel::P95);
        assert!(ci.contains(mean));
    }

    #[test]
    fn test_uncertain_value() {
        let samples = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let uncertain = UncertainValue::from_samples(&samples);

        assert!((uncertain.value - 3.0).abs() < 0.01);
        assert!(uncertain.uncertainty > 0.0);
    }

    #[test]
    fn test_particle_filter() {
        let mut pf = ParticleFilter::new(1000, 0.0, 1.0, 0.1);
        pf.predict();
        pf.update(1.0, 0.5);

        let estimate = pf.estimate();
        // Estimate should move toward observation
        assert!(estimate > 0.0);
    }
}