pub struct SDEProblem<T, V, D, F>{
pub sde: F,
pub t0: T,
pub tf: T,
pub y0: V,
/* private fields */
}Expand description
Initial Value Problem for Stochastic Differential Equations (SDEProblem)
The Initial Value Problem takes the form: dY = a(t, Y)dt + b(t, Y)dW, t0 <= t <= tf, Y(t0) = y0
where:
- a(t, Y) is the drift term (deterministic part)
- b(t, Y) is the diffusion term (stochastic part)
- dW represents a Wiener process increment
§Overview
The SDEProblem struct provides a simple interface for solving stochastic differential equations:
§Example
use differential_equations::prelude::*;
use nalgebra::SVector;
use rand::SeedableRng;
use rand_distr::{Distribution, Normal};
struct GBM {
rng: rand::rngs::StdRng,
}
impl GBM {
fn new(seed: u64) -> Self {
Self {
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] = 0.1 * y[0]; // μS
}
fn diffusion(&self, _t: f64, y: &SVector<f64, 1>, dydw: &mut SVector<f64, 1>) {
dydw[0] = 0.2 * y[0]; // σS
}
fn noise(&mut self, dt: f64, dw: &mut SVector<f64, 1>) {
let normal = Normal::new(0.0, dt.sqrt()).unwrap();
dw[0] = normal.sample(&mut self.rng);
}
}
let t0 = 0.0;
let tf = 1.0;
let y0 = SVector::<f64, 1>::new(100.0);
let mut solver = ExplicitRungeKutta::three_eighths(0.01);
let gbm = GBM::new(42);
let mut gbm_problem = SDEProblem::new(gbm, t0, tf, y0);
// Solve the SDE
let result = gbm_problem.solve(&mut solver);§Fields
sde- SDE implementing the stochastic differential equationt0- Initial timetf- Final timey0- Initial state vector
§Basic Usage
new(sde, t0, tf, y0)- Create a new SDE Problemsolve(&mut solver)- Solve using default output (solver step points)
§Output Control Methods
These methods configure how solution points are generated and returned:
even(dt)- Generate evenly spaced output points with intervaldtdense(n)- Includeninterpolated points between each solver stept_eval(points)- Evaluate solution at specific time pointssolout(custom_solout)- Use a custom output handlerseed(u64)- Set a specific random seed for reproducible simulations
Fields§
§sde: F§t0: T§tf: T§y0: VImplementations§
Source§impl<T, V, D, F> SDEProblem<T, V, D, F>
impl<T, V, D, F> SDEProblem<T, V, D, F>
Sourcepub fn solve<S>(
&mut self,
solver: &mut S,
) -> Result<Solution<T, V, D>, Error<T, V>>where
S: StochasticNumericalMethod<T, V, D> + Interpolation<T, V>,
pub fn solve<S>(
&mut self,
solver: &mut S,
) -> Result<Solution<T, V, D>, Error<T, V>>where
S: StochasticNumericalMethod<T, V, D> + Interpolation<T, V>,
Solve the SDE Problem using a default solout, e.g. outputting solutions at calculated steps
§Returns
Result<Solution<T, V, D>, Error<T, V>>-Ok(Solution)if successful or interrupted by events,Err(Error)if errors or issues are encountered
Sourcepub fn solout<'a, O: Solout<T, V, D>>(
&'a mut self,
solout: &'a mut O,
) -> SDEProblemMutRefSoloutPair<'a, T, V, D, F, O>
pub fn solout<'a, O: Solout<T, V, D>>( &'a mut self, solout: &'a mut O, ) -> SDEProblemMutRefSoloutPair<'a, T, V, D, F, O>
Returns an SDE Problem with the provided solout function for outputting points
§Returns
- SDE Problem with the provided solout function ready for .solve() method
Sourcepub fn even(
&mut self,
dt: T,
) -> SDEProblemSoloutPair<'_, T, V, D, F, EvenSolout<T>>
pub fn even( &mut self, dt: T, ) -> SDEProblemSoloutPair<'_, T, V, D, F, EvenSolout<T>>
Sourcepub fn dense(
&mut self,
n: usize,
) -> SDEProblemSoloutPair<'_, T, V, D, F, DenseSolout>
pub fn dense( &mut self, n: usize, ) -> SDEProblemSoloutPair<'_, T, V, D, F, DenseSolout>
Sourcepub fn t_eval(
&mut self,
points: Vec<T>,
) -> SDEProblemSoloutPair<'_, T, V, D, F, TEvalSolout<T>>
pub fn t_eval( &mut self, points: Vec<T>, ) -> SDEProblemSoloutPair<'_, T, V, D, F, TEvalSolout<T>>
Sourcepub fn crossing(
&mut self,
component_idx: usize,
threshold: T,
direction: CrossingDirection,
) -> SDEProblemSoloutPair<'_, T, V, D, F, CrossingSolout<T>>
pub fn crossing( &mut self, component_idx: usize, threshold: T, direction: CrossingDirection, ) -> SDEProblemSoloutPair<'_, T, V, D, F, CrossingSolout<T>>
Uses the CrossingSolout method to output points when a specific component crosses a threshold Note this does not include the solution of the calculated steps
§Arguments
component_idx- Index of the component to monitor for crossingthreshold- Value to crossdirection- Direction of crossing (positive or negative)
§Returns
- SDE Problem with CrossingSolout function ready for .solve() method
Sourcepub fn hyperplane_crossing<V1>(
&mut self,
point: V1,
normal: V1,
extractor: fn(&V) -> V1,
direction: CrossingDirection,
) -> SDEProblemSoloutPair<'_, T, V, D, F, HyperplaneCrossingSolout<T, V1, V>>where
V1: State<T>,
pub fn hyperplane_crossing<V1>(
&mut self,
point: V1,
normal: V1,
extractor: fn(&V) -> V1,
direction: CrossingDirection,
) -> SDEProblemSoloutPair<'_, T, V, D, F, HyperplaneCrossingSolout<T, V1, V>>where
V1: State<T>,
Uses the HyperplaneCrossingSolout method to output points when a specific hyperplane is crossed Note this does not include the solution of the calculated steps
§Arguments
point- Point on the hyperplanenormal- Normal vector of the hyperplaneextractor- Function to extract the component from the state vectordirection- Direction of crossing (positive or negative)
§Returns
- SDE Problem with HyperplaneCrossingSolout function ready for .solve() method
Trait Implementations§
Source§impl<T, V, D, F> Clone for SDEProblem<T, V, D, F>
impl<T, V, D, F> Clone for SDEProblem<T, V, D, F>
Source§fn clone(&self) -> SDEProblem<T, V, D, F>
fn clone(&self) -> SDEProblem<T, V, D, F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<T, V, D, F> Freeze for SDEProblem<T, V, D, F>
impl<T, V, D, F> RefUnwindSafe for SDEProblem<T, V, D, F>
impl<T, V, D, F> Send for SDEProblem<T, V, D, F>
impl<T, V, D, F> Sync for SDEProblem<T, V, D, F>
impl<T, V, D, F> Unpin for SDEProblem<T, V, D, F>
impl<T, V, D, F> UnwindSafe for SDEProblem<T, V, D, F>
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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.