pub struct Milstein<T: Real, V: State<T>, D: CallBackData> {
pub h: T,
/* private fields */
}Expand description
Milstein Method for solving stochastic differential equations.
The Milstein method is a higher-order method for SDEs that includes an additional term from the Itô-Taylor expansion to achieve better accuracy.
For an SDE of the form: dY = a(t,Y)dt + b(t,Y)dW
The Milstein update is: Y_{n+1} = Y_n + a(t_n, Y_n)Δt + b(t_n, Y_n)ΔW_n + 0.5 * b(t_n, Y_n)^2 * [(ΔW_n)² - Δt]
where ΔW_n is a Wiener process increment.
This implementation is based on the standard form of the Milstein method for scalar SDEs. For geometric Brownian motion, the b(t,Y) term is σY, so the correction term becomes 0.5 * (σY)^2 * [(ΔW)² - Δt].
The Milstein method has strong order of convergence 1.0 (compared to 0.5 for Euler-Maruyama), making it more accurate for SDEs with significant diffusion effects.
§Example
use differential_equations::prelude::*;
use nalgebra::SVector;
use rand::SeedableRng;
use rand_distr::{Distribution, Normal};
struct GBM {
mu: f64, // Drift rate
sigma: f64, // Volatility
rng: rand::rngs::StdRng,
}
impl GBM {
fn new(mu: f64, sigma: f64, seed: u64) -> Self {
Self {
mu,
sigma,
rng: rand::rngs::StdRng::seed_from_u64(seed),
}
}
}
impl SDE<f64, SVector<f64, 1>> for GBM {
fn drift(&self, _t: f64, y: &SVector<f64, 1>, dydt: &mut SVector<f64, 1>) {
dydt[0] = self.mu * y[0];
}
fn diffusion(&self, _t: f64, y: &SVector<f64, 1>, dydw: &mut SVector<f64, 1>) {
dydw[0] = self.sigma * y[0];
}
fn noise(&self, dt: f64, dw: &mut SVector<f64, 1>) {
let normal = Normal::new(0.0, dt.sqrt()).unwrap();
dw[0] = normal.sample(&mut self.rng.clone());
}
}
let t0 = 0.0;
let tf = 1.0;
let y0 = SVector::<f64, 1>::new(100.0);
let mut solver = Milstein::new(0.01);
let gbm = GBM::new(0.1, 0.2, 42);
let gbm_problem = SDEProblem::new(gbm, t0, tf, y0);
// Solve the SDE
let result = gbm_problem.solve(&mut solver);Fields§
§h: TImplementations§
Trait Implementations§
Source§impl<T: Real, V: State<T>, D: CallBackData> Interpolation<T, V> for Milstein<T, V, D>
impl<T: Real, V: State<T>, D: CallBackData> Interpolation<T, V> for Milstein<T, V, D>
Source§impl<T: Real, V: State<T>, D: CallBackData> SDENumericalMethod<T, V, D> for Milstein<T, V, D>
impl<T: Real, V: State<T>, D: CallBackData> SDENumericalMethod<T, V, D> for Milstein<T, V, D>
Source§fn init<F>(
&mut self,
sde: &F,
t0: T,
tf: T,
y0: &V,
) -> Result<Evals, Error<T, V>>where
F: SDE<T, V, D>,
fn init<F>(
&mut self,
sde: &F,
t0: T,
tf: T,
y0: &V,
) -> Result<Evals, Error<T, V>>where
F: SDE<T, V, D>,
Source§fn step<F>(&mut self, sde: &F) -> Result<Evals, Error<T, V>>where
F: SDE<T, V, D>,
fn step<F>(&mut self, sde: &F) -> Result<Evals, Error<T, V>>where
F: SDE<T, V, D>,
Source§fn set_status(&mut self, status: Status<T, V, D>)
fn set_status(&mut self, status: Status<T, V, D>)
Auto Trait Implementations§
impl<T, V, D> Freeze for Milstein<T, V, D>
impl<T, V, D> RefUnwindSafe for Milstein<T, V, D>
impl<T, V, D> Send for Milstein<T, V, D>
impl<T, V, D> Sync for Milstein<T, V, D>
impl<T, V, D> Unpin for Milstein<T, V, D>
impl<T, V, D> UnwindSafe for Milstein<T, V, D>
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
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.