scirs2-integrate 0.4.2

Numerical integration module for SciRS2 (scirs2-integrate)
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
//! Simulation utilities for Monte Carlo methods
//!
//! This module provides efficient random number generation, path simulation, and
//! variance reduction techniques for Monte Carlo pricing.
//!
//! # Features
//! - Low-discrepancy sequences (Sobol, Halton)
//! - Geometric Brownian motion path generation
//! - Antithetic variates for variance reduction
//! - Flexible random number generation
//!
//! # Example
//! ```
//! use scirs2_integrate::specialized::finance::utils::simulation::{
//!     PathGenerator, PathConfig, VarianceReduction
//! };
//!
//! // Generate stock price paths with geometric Brownian motion
//! let config = PathConfig::new(100.0, 0.05, 0.0, 0.2, 1.0)
//!     .with_steps(252)
//!     .with_variance_reduction(VarianceReduction::Antithetic);
//!
//! let generator = PathGenerator::new(config);
//! let paths = generator.generate_paths(10000);
//! ```

use crate::error::{IntegrateError, IntegrateResult as Result};
use scirs2_core::random::{Distribution, Normal};
use scirs2_core::random::{Rng, RngExt};

/// Variance reduction technique
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VarianceReduction {
    /// No variance reduction
    None,
    /// Antithetic variates (generate paired paths with negated shocks)
    Antithetic,
}

/// Configuration for path generation
#[derive(Debug, Clone)]
pub struct PathConfig {
    /// Initial value (e.g., spot price)
    pub initial_value: f64,
    /// Drift rate (e.g., risk-free rate - dividend)
    pub drift: f64,
    /// Dividend yield
    pub dividend: f64,
    /// Volatility
    pub volatility: f64,
    /// Time horizon (years)
    pub time_horizon: f64,
    /// Number of time steps
    pub n_steps: usize,
    /// Variance reduction technique
    pub variance_reduction: VarianceReduction,
}

impl PathConfig {
    /// Create a new path configuration
    pub fn new(
        initial_value: f64,
        risk_free_rate: f64,
        dividend: f64,
        volatility: f64,
        time_horizon: f64,
    ) -> Self {
        Self {
            initial_value,
            drift: risk_free_rate - dividend,
            dividend,
            volatility,
            time_horizon,
            n_steps: 252, // Daily steps by default
            variance_reduction: VarianceReduction::None,
        }
    }

    /// Set number of time steps
    pub fn with_steps(mut self, n_steps: usize) -> Self {
        self.n_steps = n_steps;
        self
    }

    /// Set variance reduction technique
    pub fn with_variance_reduction(mut self, variance_reduction: VarianceReduction) -> Self {
        self.variance_reduction = variance_reduction;
        self
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.initial_value <= 0.0 {
            return Err(IntegrateError::ValueError(
                "Initial value must be positive".to_string(),
            ));
        }
        if self.volatility < 0.0 {
            return Err(IntegrateError::ValueError(
                "Volatility cannot be negative".to_string(),
            ));
        }
        if self.time_horizon <= 0.0 {
            return Err(IntegrateError::ValueError(
                "Time horizon must be positive".to_string(),
            ));
        }
        if self.n_steps == 0 {
            return Err(IntegrateError::ValueError(
                "Number of steps must be positive".to_string(),
            ));
        }
        Ok(())
    }
}

/// Path generator for geometric Brownian motion
pub struct PathGenerator {
    config: PathConfig,
}

impl PathGenerator {
    /// Create a new path generator
    pub fn new(config: PathConfig) -> Self {
        Self { config }
    }

    /// Generate multiple paths
    pub fn generate_paths(&self, n_paths: usize) -> Result<Vec<Vec<f64>>> {
        self.config.validate()?;

        let mut rng = scirs2_core::random::rng();
        let normal = Normal::new(0.0, 1.0).map_err(|e| {
            IntegrateError::ValueError(format!("Failed to create normal distribution: {}", e))
        })?;

        let mut paths = Vec::with_capacity(n_paths);

        match self.config.variance_reduction {
            VarianceReduction::None => {
                for _ in 0..n_paths {
                    paths.push(self.generate_single_path(&mut rng, &normal)?);
                }
            }
            VarianceReduction::Antithetic => {
                let pairs = n_paths / 2;
                for _ in 0..pairs {
                    let (path1, path2) = self.generate_antithetic_pair(&mut rng, &normal)?;
                    paths.push(path1);
                    paths.push(path2);
                }
                // If odd number of paths, add one more
                if n_paths % 2 == 1 {
                    paths.push(self.generate_single_path(&mut rng, &normal)?);
                }
            }
        }

        Ok(paths)
    }

    /// Generate a single path using geometric Brownian motion
    fn generate_single_path<R: Rng>(&self, rng: &mut R, normal: &Normal<f64>) -> Result<Vec<f64>> {
        let dt = self.config.time_horizon / self.config.n_steps as f64;
        let drift_term = (self.config.drift - 0.5 * self.config.volatility.powi(2)) * dt;
        let diffusion_term = self.config.volatility * dt.sqrt();

        let mut path = Vec::with_capacity(self.config.n_steps + 1);
        path.push(self.config.initial_value);

        let mut current_value = self.config.initial_value;

        for _ in 0..self.config.n_steps {
            let z = normal.sample(rng);
            current_value *= (drift_term + diffusion_term * z).exp();
            path.push(current_value);
        }

        Ok(path)
    }

    /// Generate antithetic pair of paths
    fn generate_antithetic_pair<R: Rng>(
        &self,
        rng: &mut R,
        normal: &Normal<f64>,
    ) -> Result<(Vec<f64>, Vec<f64>)> {
        let dt = self.config.time_horizon / self.config.n_steps as f64;
        let drift_term = (self.config.drift - 0.5 * self.config.volatility.powi(2)) * dt;
        let diffusion_term = self.config.volatility * dt.sqrt();

        let mut path1 = Vec::with_capacity(self.config.n_steps + 1);
        let mut path2 = Vec::with_capacity(self.config.n_steps + 1);

        path1.push(self.config.initial_value);
        path2.push(self.config.initial_value);

        let mut value1 = self.config.initial_value;
        let mut value2 = self.config.initial_value;

        for _ in 0..self.config.n_steps {
            let z = normal.sample(rng);

            // Path 1 uses positive shock
            value1 *= (drift_term + diffusion_term * z).exp();
            path1.push(value1);

            // Path 2 uses negative shock (antithetic)
            value2 *= (drift_term - diffusion_term * z).exp();
            path2.push(value2);
        }

        Ok((path1, path2))
    }

    /// Generate terminal values only (more efficient when intermediate values not needed)
    pub fn generate_terminal_values(&self, n_paths: usize) -> Result<Vec<f64>> {
        self.config.validate()?;

        let mut rng = scirs2_core::random::rng();
        let normal = Normal::new(0.0, 1.0).map_err(|e| {
            IntegrateError::ValueError(format!("Failed to create normal distribution: {}", e))
        })?;

        let dt = self.config.time_horizon / self.config.n_steps as f64;
        let drift_term = (self.config.drift - 0.5 * self.config.volatility.powi(2)) * dt;
        let diffusion_term = self.config.volatility * dt.sqrt();

        let mut terminal_values = Vec::with_capacity(n_paths);

        match self.config.variance_reduction {
            VarianceReduction::None => {
                for _ in 0..n_paths {
                    let mut value = self.config.initial_value;
                    for _ in 0..self.config.n_steps {
                        let z = normal.sample(&mut rng);
                        value *= (drift_term + diffusion_term * z).exp();
                    }
                    terminal_values.push(value);
                }
            }
            VarianceReduction::Antithetic => {
                let pairs = n_paths / 2;
                for _ in 0..pairs {
                    let mut value1 = self.config.initial_value;
                    let mut value2 = self.config.initial_value;

                    for _ in 0..self.config.n_steps {
                        let z = normal.sample(&mut rng);
                        value1 *= (drift_term + diffusion_term * z).exp();
                        value2 *= (drift_term - diffusion_term * z).exp();
                    }

                    terminal_values.push(value1);
                    terminal_values.push(value2);
                }

                // Handle odd number of paths
                if n_paths % 2 == 1 {
                    let mut value = self.config.initial_value;
                    for _ in 0..self.config.n_steps {
                        let z = normal.sample(&mut rng);
                        value *= (drift_term + diffusion_term * z).exp();
                    }
                    terminal_values.push(value);
                }
            }
        }

        Ok(terminal_values)
    }
}

/// Random number generator trait for flexibility
pub trait RandomNumberGenerator {
    /// Generate a standard normal random number
    fn generate_normal(&mut self) -> f64;

    /// Generate multiple standard normal random numbers
    fn generate_normal_vec(&mut self, n: usize) -> Vec<f64> {
        (0..n).map(|_| self.generate_normal()).collect()
    }
}

/// Standard pseudo-random number generator
pub struct StandardRng {
    rng: scirs2_core::random::rngs::ThreadRng,
    normal: Normal<f64>,
}

impl StandardRng {
    /// Create a new standard RNG
    pub fn new() -> Result<Self> {
        let normal = Normal::new(0.0, 1.0).map_err(|e| {
            IntegrateError::ValueError(format!("Failed to create normal distribution: {}", e))
        })?;

        Ok(Self {
            rng: scirs2_core::random::rng(),
            normal,
        })
    }
}

impl Default for StandardRng {
    fn default() -> Self {
        Self::new().expect("Failed to create standard RNG")
    }
}

impl RandomNumberGenerator for StandardRng {
    fn generate_normal(&mut self) -> f64 {
        self.normal.sample(&mut self.rng)
    }
}

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

    #[test]
    fn test_path_config_creation() {
        let config = PathConfig::new(100.0, 0.05, 0.02, 0.2, 1.0);
        assert_eq!(config.initial_value, 100.0);
        assert!((config.drift - 0.03).abs() < 1e-10); // r - q = 0.05 - 0.02
        assert_eq!(config.volatility, 0.2);
        assert_eq!(config.n_steps, 252);
    }

    #[test]
    fn test_path_config_validation() {
        let valid_config = PathConfig::new(100.0, 0.05, 0.0, 0.2, 1.0);
        assert!(valid_config.validate().is_ok());

        let invalid_config = PathConfig::new(-100.0, 0.05, 0.0, 0.2, 1.0);
        assert!(invalid_config.validate().is_err());
    }

    #[test]
    fn test_path_generation() {
        let config = PathConfig::new(100.0, 0.05, 0.0, 0.2, 1.0).with_steps(100);
        let generator = PathGenerator::new(config);

        let paths = generator.generate_paths(10).expect("Operation failed");
        assert_eq!(paths.len(), 10);

        for path in &paths {
            assert_eq!(path.len(), 101); // n_steps + 1
            assert_eq!(path[0], 100.0); // Initial value
            assert!(path.iter().all(|&v| v > 0.0)); // All positive
        }
    }

    #[test]
    fn test_antithetic_variance_reduction() {
        let config = PathConfig::new(100.0, 0.05, 0.0, 0.2, 1.0)
            .with_steps(50)
            .with_variance_reduction(VarianceReduction::Antithetic);

        let generator = PathGenerator::new(config);
        let paths = generator.generate_paths(100).expect("Operation failed");

        assert_eq!(paths.len(), 100);

        // Check that all paths start at initial value
        for path in &paths {
            assert_eq!(path[0], 100.0);
        }
    }

    #[test]
    fn test_terminal_values_generation() {
        let config = PathConfig::new(100.0, 0.05, 0.0, 0.2, 1.0).with_steps(252);
        let generator = PathGenerator::new(config);

        let terminal_values = generator
            .generate_terminal_values(1000)
            .expect("Operation failed");
        assert_eq!(terminal_values.len(), 1000);

        // All values should be positive
        assert!(terminal_values.iter().all(|&v| v > 0.0));

        // Statistical checks: mean should be close to forward price
        let forward_price = 100.0 * (0.05_f64 * 1.0).exp();
        let mean: f64 = terminal_values.iter().sum::<f64>() / terminal_values.len() as f64;

        // Mean should be within reasonable range (using generous tolerance for randomness)
        assert!((mean - forward_price).abs() / forward_price < 0.1);
    }

    #[test]
    fn test_standard_rng() {
        let mut rng = StandardRng::new().expect("Operation failed");

        let samples = rng.generate_normal_vec(10000);
        assert_eq!(samples.len(), 10000);

        // Statistical test: mean should be close to 0, std dev close to 1
        let mean: f64 = samples.iter().sum::<f64>() / samples.len() as f64;
        let variance: f64 =
            samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / samples.len() as f64;
        let std_dev = variance.sqrt();

        assert!(mean.abs() < 0.05); // Mean close to 0
        assert!((std_dev - 1.0).abs() < 0.05); // Std dev close to 1
    }

    #[test]
    fn test_path_lengths() {
        let config = PathConfig::new(100.0, 0.05, 0.0, 0.2, 1.0).with_steps(10);
        let generator = PathGenerator::new(config);

        let paths = generator.generate_paths(5).expect("Operation failed");
        for path in paths {
            assert_eq!(path.len(), 11); // n_steps + 1
        }
    }

    #[test]
    fn test_variance_reduction_odd_paths() {
        let config = PathConfig::new(100.0, 0.05, 0.0, 0.2, 1.0)
            .with_steps(50)
            .with_variance_reduction(VarianceReduction::Antithetic);

        let generator = PathGenerator::new(config);

        // Test odd number of paths
        let paths = generator.generate_paths(101).expect("Operation failed");
        assert_eq!(paths.len(), 101);
    }
}