Struct rgsl::types::monte_carlo::PlainMonteCarlo
source · [−]pub struct PlainMonteCarlo { /* private fields */ }
Expand description
The plain Monte Carlo algorithm samples points randomly from the integration region to estimate the integral and its error. Using this algorithm the estimate of the integral E(f; N) for N randomly distributed points x_i is given by,
E(f; N) = = V <f> = (V / N) sum_i^N f(x_i)
where V is the volume of the integration region. The error on this estimate sigma(E;N)
is
calculated from the estimated variance of the mean,
sigma^2 (E; N) = (V^2 / N^2) sum_i^N (f(x_i) - <f>)^2.
For large N this variance decreases asymptotically as Var(f)/N
, where Var(f)
is the true
variance of the function over the integration region. The error estimate itself should decrease as
sigma(f)/sqrt{N}
. The familiar law of errors decreasing as 1/sqrt{N}
applies-to reduce the
error by a factor of 10 requires a 100-fold increase in the number of sample points.
Implementations
sourceimpl PlainMonteCarlo
impl PlainMonteCarlo
sourcepub fn new(dim: usize) -> Option<PlainMonteCarlo>
pub fn new(dim: usize) -> Option<PlainMonteCarlo>
This function allocates and initializes a workspace for Monte Carlo integration in dim dimensions.
sourcepub fn init(&mut self) -> Value
pub fn init(&mut self) -> Value
This function initializes a previously allocated integration state. This allows an existing workspace to be reused for different integrations.
sourcepub fn integrate<F: FnMut(&[f64]) -> f64>(
&mut self,
f: F,
xl: &[f64],
xu: &[f64],
t_calls: usize,
r: &mut Rng
) -> (Value, f64, f64)
pub fn integrate<F: FnMut(&[f64]) -> f64>(
&mut self,
f: F,
xl: &[f64],
xu: &[f64],
t_calls: usize,
r: &mut Rng
) -> (Value, f64, f64)
This routines uses the plain Monte Carlo algorithm to integrate the function f over the dim-dimensional hypercubic region defined by the lower and upper limits in the arrays xl and xu, each of the same size. The integration uses a fixed number of function calls calls, and obtains random sampling points using the random number generator r. A previously allocated workspace s must be supplied. The result of the integration is returned in result, with an estimated absolute error abserr.
In C, the function takes a gsl_monte_function
as first argument. In here, you have to
pass the dim
argument and the function pointer (which became a closure) directly to the
function.
It returns either Ok((result, abserr)) or Err(Value).
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for PlainMonteCarlo
impl !Send for PlainMonteCarlo
impl !Sync for PlainMonteCarlo
impl Unpin for PlainMonteCarlo
impl UnwindSafe for PlainMonteCarlo
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more