numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
Documentation
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! # Probabilistic Programming Module
//!
//! This module provides comprehensive probabilistic programming infrastructure for NumRS2,
//! including advanced inference algorithms, Bayesian utilities, and probabilistic graphical models.
//!
//! ## Overview
//!
//! The probabilistic module offers production-ready implementations of:
//!
//! - **MCMC Inference**: Metropolis-Hastings, Gibbs sampling, Hamiltonian Monte Carlo (HMC),
//!   No-U-Turn Sampler (NUTS), and Parallel Tempering
//! - **Variational Inference**: Mean-field VI, Automatic Differentiation VI (ADVI), ELBO optimization
//! - **Bayesian Utilities**: Conjugate priors, posterior computation, model comparison (BIC, DIC, WAIC),
//!   credible intervals, hypothesis testing
//! - **Graphical Models**: Bayesian networks, Markov Random Fields, Hidden Markov Models,
//!   Gaussian Processes
//! - **Extended Distributions**: Beta, Gamma, Dirichlet, Student's t, Wishart, Inverse-Wishart,
//!   Von Mises, and more
//!
//! ## Mathematical Background
//!
//! ### Bayesian Inference
//!
//! Bayesian inference provides a principled framework for updating beliefs about parameters θ
//! given observed data D using Bayes' theorem:
//!
//! ```text
//! p(θ|D) = p(D|θ)p(θ) / p(D)
//! ```
//!
//! where:
//! - p(θ|D) is the posterior distribution
//! - p(D|θ) is the likelihood
//! - p(θ) is the prior distribution
//! - p(D) is the marginal likelihood (evidence)
//!
//! ### Markov Chain Monte Carlo (MCMC)
//!
//! When the posterior distribution cannot be computed analytically, MCMC methods construct
//! a Markov chain whose stationary distribution is the target posterior. Common algorithms include:
//!
//! - **Metropolis-Hastings**: Generic MCMC using proposal distributions
//! - **Gibbs Sampling**: Samples from conditional distributions
//! - **Hamiltonian Monte Carlo**: Uses gradient information for efficient exploration
//! - **NUTS**: Adaptive HMC with automatic step size tuning
//!
//! ### Variational Inference
//!
//! Variational inference approximates the posterior p(θ|D) with a simpler distribution q(θ)
//! by minimizing the Kullback-Leibler divergence:
//!
//! ```text
//! KL(q||p) = ∫ q(θ) log(q(θ)/p(θ|D)) dθ
//! ```
//!
//! This is equivalent to maximizing the Evidence Lower BOund (ELBO):
//!
//! ```text
//! ELBO = E_q[log p(D,θ)] - E_q[log q(θ)]
//! ```
//!
//! ## SCIRS2 Policy Compliance
//!
//! This module strictly follows SCIRS2 ecosystem policies:
//!
//! - **Random Number Generation**: ALWAYS use `scirs2_core::random` (NEVER direct rand/rand_distr)
//! - **Array Operations**: ALWAYS use `scirs2_core::ndarray` (NEVER direct ndarray)
//! - **Parallel Processing**: ALWAYS use `scirs2_core::parallel_ops` (NEVER direct rayon)
//! - **Statistical Functions**: Use `scirs2_stats` for statistical computations
//! - **Linear Algebra**: Use `scirs2_linalg` for matrix operations (Pure Rust via OxiBLAS)
//!
//! ## Usage Examples
//!
//! ### Example 1: Metropolis-Hastings Sampling
//!
//! ```rust,ignore
//! use numrs2::new_modules::probabilistic::{MetropolisHastings, GaussianProposal};
//! use scirs2_core::random::default_rng;
//!
//! // Define log-posterior function
//! let log_posterior = |theta: &[f64]| -> f64 {
//!     // Log-likelihood + log-prior
//!     -0.5 * theta[0].powi(2) // Standard normal prior
//! };
//!
//! // Create sampler with Gaussian proposal
//! let mut rng = default_rng();
//! let proposal = GaussianProposal::new(0.5)?; // Proposal std dev
//! let mut sampler = MetropolisHastings::new(log_posterior, proposal);
//!
//! // Run MCMC for 10,000 iterations
//! let initial_state = vec![0.0];
//! let samples = sampler.sample(&initial_state, 10000, 1000, &mut rng)?;
//! ```
//!
//! ### Example 2: Bayesian Linear Regression
//!
//! ```rust,ignore
//! use numrs2::new_modules::probabilistic::{BayesianLinearRegression, NormalInverseGammaPrior};
//! use numrs2::prelude::*;
//!
//! // Data: y = 2*x + 1 + noise
//! let x = linspace(0.0, 10.0, 100).reshape(&[100, 1]);
//! let y = x.multiply_scalar(2.0).add_scalar(1.0).add(&randn(&[100]));
//!
//! // Set up conjugate prior
//! let prior = NormalInverseGammaPrior::default();
//!
//! // Compute posterior
//! let posterior = prior.update(&x, &y)?;
//!
//! // Sample from posterior predictive
//! let x_new = linspace(10.0, 15.0, 50).reshape(&[50, 1]);
//! let y_pred = posterior.predict(&x_new)?;
//! ```
//!
//! ### Example 3: Model Comparison with WAIC
//!
//! ```rust,ignore
//! use numrs2::new_modules::probabilistic::{ModelComparison, waic};
//!
//! // Compute WAIC for model selection
//! let log_likelihood_samples = /* MCMC samples of log-likelihood */;
//! let waic_score = waic(&log_likelihood_samples)?;
//! println!("WAIC: {}", waic_score.waic);
//! println!("Effective parameters: {}", waic_score.p_waic);
//! ```
//!
//! ## Performance Considerations
//!
//! - **SIMD Optimization**: Distribution operations use SIMD when applicable
//! - **Parallel MCMC**: Multiple chains can run in parallel using `scirs2_core::parallel_ops`
//! - **Memory Efficiency**: Streaming algorithms for large-scale inference
//! - **Numerical Stability**: Log-space computations to prevent underflow
//!
//! ## References
//!
//! - Gelman, A., et al. (2013). *Bayesian Data Analysis* (3rd ed.). Chapman and Hall/CRC.
//! - Neal, R. M. (2011). MCMC using Hamiltonian dynamics. *Handbook of Markov Chain Monte Carlo*.
//! - Hoffman, M. D., & Gelman, A. (2014). The No-U-Turn Sampler. *Journal of Machine Learning Research*.
//! - Blei, D. M., Kucukelbir, A., & McAuliffe, J. D. (2017). Variational Inference: A Review for Statisticians.
//! - Bishop, C. M. (2006). *Pattern Recognition and Machine Learning*. Springer.

use crate::array::Array;
use crate::error::NumRs2Error;
use std::fmt;

// Module declarations
pub mod bayesian;
pub mod distributions;
pub mod graphical;
pub mod inference;

#[cfg(test)]
mod tests;

// Re-exports from submodules
pub use bayesian::*;
pub use distributions::*;
pub use graphical::*;
pub use inference::*;

/// Result type for probabilistic operations
pub type Result<T> = std::result::Result<T, ProbabilisticError>;

/// Comprehensive error type for probabilistic programming operations
#[derive(Debug, Clone)]
pub enum ProbabilisticError {
    /// Invalid parameter value
    InvalidParameter { parameter: String, message: String },

    /// Dimension mismatch in array operations
    DimensionMismatch {
        expected: Vec<usize>,
        actual: Vec<usize>,
        operation: String,
    },

    /// Numerical error (overflow, underflow, NaN, etc.)
    NumericalError { message: String },

    /// Convergence failure in iterative algorithms
    ConvergenceError {
        algorithm: String,
        iterations: usize,
        message: String,
    },

    /// Invalid probability distribution
    InvalidDistribution {
        distribution: String,
        reason: String,
    },

    /// MCMC sampling error
    SamplingError {
        sampler: String,
        iteration: usize,
        message: String,
    },

    /// Variational inference error
    VariationalInferenceError { message: String },

    /// Graphical model error
    GraphicalModelError { model_type: String, message: String },

    /// Integration error with NumRS2
    NumRs2IntegrationError { source: Box<NumRs2Error> },

    /// Generic error with custom message
    Other { message: String },
}

impl fmt::Display for ProbabilisticError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProbabilisticError::InvalidParameter { parameter, message } => {
                write!(f, "Invalid parameter '{}': {}", parameter, message)
            }
            ProbabilisticError::DimensionMismatch {
                expected,
                actual,
                operation,
            } => {
                write!(
                    f,
                    "Dimension mismatch in {}: expected {:?}, got {:?}",
                    operation, expected, actual
                )
            }
            ProbabilisticError::NumericalError { message } => {
                write!(f, "Numerical error: {}", message)
            }
            ProbabilisticError::ConvergenceError {
                algorithm,
                iterations,
                message,
            } => {
                write!(
                    f,
                    "Convergence failure in {} after {} iterations: {}",
                    algorithm, iterations, message
                )
            }
            ProbabilisticError::InvalidDistribution {
                distribution,
                reason,
            } => {
                write!(f, "Invalid distribution '{}': {}", distribution, reason)
            }
            ProbabilisticError::SamplingError {
                sampler,
                iteration,
                message,
            } => {
                write!(
                    f,
                    "Sampling error in {} at iteration {}: {}",
                    sampler, iteration, message
                )
            }
            ProbabilisticError::VariationalInferenceError { message } => {
                write!(f, "Variational inference error: {}", message)
            }
            ProbabilisticError::GraphicalModelError {
                model_type,
                message,
            } => {
                write!(f, "Graphical model error in {}: {}", model_type, message)
            }
            ProbabilisticError::NumRs2IntegrationError { source } => {
                write!(f, "NumRS2 integration error: {}", source)
            }
            ProbabilisticError::Other { message } => {
                write!(f, "Probabilistic error: {}", message)
            }
        }
    }
}

impl std::error::Error for ProbabilisticError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ProbabilisticError::NumRs2IntegrationError { source } => Some(source),
            _ => None,
        }
    }
}

impl From<NumRs2Error> for ProbabilisticError {
    fn from(error: NumRs2Error) -> Self {
        ProbabilisticError::NumRs2IntegrationError {
            source: Box::new(error),
        }
    }
}

/// Helper function to validate probability values
///
/// Ensures that a probability is in the valid range [0, 1].
///
/// # Arguments
///
/// * `p` - Probability value to validate
/// * `name` - Parameter name for error messages
///
/// # Returns
///
/// * `Ok(())` if the probability is valid
/// * `Err(ProbabilisticError)` if the probability is invalid
///
/// # Examples
///
/// ```rust,ignore
/// validate_probability(0.5, "p")?; // OK
/// validate_probability(-0.1, "p")?; // Error: probability out of range
/// validate_probability(1.5, "p")?; // Error: probability out of range
/// ```
pub fn validate_probability(p: f64, name: &str) -> Result<()> {
    if !p.is_finite() {
        return Err(ProbabilisticError::InvalidParameter {
            parameter: name.to_string(),
            message: format!("probability must be finite, got {}", p),
        });
    }
    if !(0.0..=1.0).contains(&p) {
        return Err(ProbabilisticError::InvalidParameter {
            parameter: name.to_string(),
            message: format!("probability must be in [0, 1], got {}", p),
        });
    }
    Ok(())
}

/// Helper function to validate positive parameter values
///
/// Ensures that a parameter is strictly positive.
///
/// # Arguments
///
/// * `value` - Parameter value to validate
/// * `name` - Parameter name for error messages
///
/// # Returns
///
/// * `Ok(())` if the value is valid
/// * `Err(ProbabilisticError)` if the value is invalid
pub fn validate_positive(value: f64, name: &str) -> Result<()> {
    if !value.is_finite() {
        return Err(ProbabilisticError::InvalidParameter {
            parameter: name.to_string(),
            message: format!("value must be finite, got {}", value),
        });
    }
    if value <= 0.0 {
        return Err(ProbabilisticError::InvalidParameter {
            parameter: name.to_string(),
            message: format!("value must be positive, got {}", value),
        });
    }
    Ok(())
}

/// Helper function to validate non-negative parameter values
///
/// Ensures that a parameter is non-negative.
///
/// # Arguments
///
/// * `value` - Parameter value to validate
/// * `name` - Parameter name for error messages
///
/// # Returns
///
/// * `Ok(())` if the value is valid
/// * `Err(ProbabilisticError)` if the value is invalid
pub fn validate_non_negative(value: f64, name: &str) -> Result<()> {
    if !value.is_finite() {
        return Err(ProbabilisticError::InvalidParameter {
            parameter: name.to_string(),
            message: format!("value must be finite, got {}", value),
        });
    }
    if value < 0.0 {
        return Err(ProbabilisticError::InvalidParameter {
            parameter: name.to_string(),
            message: format!("value must be non-negative, got {}", value),
        });
    }
    Ok(())
}

/// Helper function to validate array shapes match
///
/// # Arguments
///
/// * `expected` - Expected shape
/// * `actual` - Actual shape
/// * `operation` - Operation name for error messages
///
/// # Returns
///
/// * `Ok(())` if shapes match
/// * `Err(ProbabilisticError)` if shapes don't match
pub fn validate_shape(expected: &[usize], actual: &[usize], operation: &str) -> Result<()> {
    if expected != actual {
        return Err(ProbabilisticError::DimensionMismatch {
            expected: expected.to_vec(),
            actual: actual.to_vec(),
            operation: operation.to_string(),
        });
    }
    Ok(())
}

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

    #[test]
    fn test_validate_probability() {
        assert!(validate_probability(0.0, "p").is_ok());
        assert!(validate_probability(0.5, "p").is_ok());
        assert!(validate_probability(1.0, "p").is_ok());
        assert!(validate_probability(-0.1, "p").is_err());
        assert!(validate_probability(1.1, "p").is_err());
        assert!(validate_probability(f64::NAN, "p").is_err());
        assert!(validate_probability(f64::INFINITY, "p").is_err());
    }

    #[test]
    fn test_validate_positive() {
        assert!(validate_positive(0.1, "x").is_ok());
        assert!(validate_positive(1.0, "x").is_ok());
        assert!(validate_positive(100.0, "x").is_ok());
        assert!(validate_positive(0.0, "x").is_err());
        assert!(validate_positive(-1.0, "x").is_err());
        assert!(validate_positive(f64::NAN, "x").is_err());
    }

    #[test]
    fn test_validate_non_negative() {
        assert!(validate_non_negative(0.0, "x").is_ok());
        assert!(validate_non_negative(0.1, "x").is_ok());
        assert!(validate_non_negative(1.0, "x").is_ok());
        assert!(validate_non_negative(-0.1, "x").is_err());
        assert!(validate_non_negative(f64::NAN, "x").is_err());
    }

    #[test]
    fn test_validate_shape() {
        assert!(validate_shape(&[2, 3], &[2, 3], "test").is_ok());
        assert!(validate_shape(&[2], &[2], "test").is_ok());
        assert!(validate_shape(&[2, 3], &[3, 2], "test").is_err());
        assert!(validate_shape(&[2, 3], &[2], "test").is_err());
    }

    #[test]
    fn test_error_display() {
        let err = ProbabilisticError::InvalidParameter {
            parameter: "alpha".to_string(),
            message: "must be positive".to_string(),
        };
        let display = format!("{}", err);
        assert!(display.contains("alpha"));
        assert!(display.contains("positive"));
    }

    #[test]
    fn test_error_from_numrs2() {
        let numrs2_err = NumRs2Error::DimensionMismatch("expected 2x3, got 3x2".to_string());
        let prob_err: ProbabilisticError = numrs2_err.into();

        match prob_err {
            ProbabilisticError::NumRs2IntegrationError { .. } => {}
            _ => panic!("Expected NumRs2IntegrationError"),
        }
    }
}