pub struct MonteCarloEngine { /* private fields */ }Expand description
Monte Carlo engine for stochastic simulation.
Implementations§
Source§impl MonteCarloEngine
impl MonteCarloEngine
Sourcepub const fn new(
n_samples: usize,
variance_reduction: VarianceReduction,
) -> Self
pub const fn new( n_samples: usize, variance_reduction: VarianceReduction, ) -> Self
Create a new Monte Carlo engine.
Sourcepub const fn with_samples(n_samples: usize) -> Self
pub const fn with_samples(n_samples: usize) -> Self
Create engine with default settings.
Sourcepub fn run<F>(&self, f: F, rng: &mut SimRng) -> MonteCarloResult
pub fn run<F>(&self, f: F, rng: &mut SimRng) -> MonteCarloResult
Run Monte Carlo simulation with a function f: [0,1] -> R.
§Example
use simular::domains::monte_carlo::{MonteCarloEngine, VarianceReduction};
use simular::engine::rng::SimRng;
let engine = MonteCarloEngine::with_samples(10000);
let mut rng = SimRng::new(42);
// Estimate integral of x^2 from 0 to 1 (true value = 1/3)
let result = engine.run(|x| x * x, &mut rng);
assert!((result.estimate - 1.0/3.0).abs() < 0.01);Sourcepub fn run_nd<F>(&self, dim: usize, f: F, rng: &mut SimRng) -> MonteCarloResult
pub fn run_nd<F>(&self, dim: usize, f: F, rng: &mut SimRng) -> MonteCarloResult
Run multi-dimensional Monte Carlo.
§Example
use simular::domains::monte_carlo::MonteCarloEngine;
use simular::engine::rng::SimRng;
let engine = MonteCarloEngine::with_samples(10000);
let mut rng = SimRng::new(42);
// Estimate volume of unit sphere in 3D (true value ≈ 4.189)
let result = engine.run_nd(3, |x| {
let r2 = x.iter().map(|&xi| xi * xi).sum::<f64>();
if r2 <= 1.0 { 8.0 } else { 0.0 } // 8 = 2^3 for [-1,1]^3 domain
}, &mut rng);Trait Implementations§
Auto Trait Implementations§
impl Freeze for MonteCarloEngine
impl RefUnwindSafe for MonteCarloEngine
impl Send for MonteCarloEngine
impl Sync for MonteCarloEngine
impl Unpin for MonteCarloEngine
impl UnsafeUnpin for MonteCarloEngine
impl UnwindSafe for MonteCarloEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more