stochastic-rs
Concepts

Traits overview

The trait surface that organises the library — FloatExt, ProcessExt, DistributionExt, PricerExt, ModelPricer, Calibrator, GreeksExt, and friends.

Traits overview

The library's organising principle is a small set of traits. Each trait captures one abstraction, with concrete types implementing the trait generically over the float type via FloatExt.

The trait map

TraitCratePurpose
FloatExtstochastic-rs-distributionsFoundational float trait bound (f32 / f64)
SimdFloatExtstochastic-rs-distributionsSIMD-friendly subset of FloatExt
ProcessExt<T>stochastic-rs-stochasticStochastic process simulation (sample, sample_par)
DistributionExt<T>stochastic-rs-distributionsClosed-form pdf / cdf / cf / moments
BivariateExt<T>stochastic-rs-copulasBivariate copulas
MultivariateExt<T>stochastic-rs-copulasMultivariate copulas (openblas-only)
PricerExt<T>stochastic-rs-quantOption pricing with date support
ModelPricer<T>stochastic-rs-quantConcrete-typed pricer (no &dyn)
ToModel, ToShortRateModelstochastic-rs-quantCalibrator → Concrete pricer bridge
FourierModelExt<T>stochastic-rs-quantCharacteristic function models
Calibrator<T>stochastic-rs-quantResult-based calibration (type Params, type Error)
Instrument, PricingEnginestochastic-rs-quantQuantLib-style decoupling of payoff vs engine
GreeksExt<T>stochastic-rs-quantFirst- + second-order Greeks aggregator
MalliavinExt<T>stochastic-rs-stochasticFinite-difference Malliavin Greeks
CalendarExtstochastic-rs-quantPluggable holiday calendars
TimeExtstochastic-rs-quantDay-count-aware maturity (tau_with_dcc)

FloatExt

The trait every numerical type is generic over:

pub trait FloatExt: Float + …  {
    // arithmetic, comparison, conversions
    // closed-form math: erf, erfc, gamma, lgamma, beta, …
    // RNG-friendly: ZERO, ONE, TWO, PI, TAU, EPSILON, …
}

f32 and f64 are blanket-impl'd; user code rarely names the trait explicitly except as a bound:

pub fn my_pricer<T: FloatExt>(s0: T, k: T, t: T) -> T { /* … */ }

The SimdFloatExt subset is what bulk samplers rely on for f32x8 / f64x4 operations.

ProcessExt<T>

Every stochastic process implements ProcessExt<T>:

pub trait ProcessExt<T: FloatExt> {
    fn sample(&self) -> Array1<T>;
    fn sample_par(&self, m: usize) -> Array2<T>;
    // optional: malliavin-style methods via MalliavinExt
}

sample() produces a single path of length n (the steps); sample_par(m) produces m independent paths in parallel via Rayon.

DistributionExt<T>

Closed-form moments / pdf / cdf / characteristic function — implemented by 18/19 distributions in the library (5 named no-closed-form unimplemented!() cases on specific moments per the project status memo).

pub trait DistributionExt<T: FloatExt> {
    fn pdf(&self, x: T) -> T;
    fn cdf(&self, x: T) -> T;
    fn characteristic_fn(&self, t: T) -> Complex<T>;
    fn mean(&self) -> T;
    fn variance(&self) -> T;
    fn skewness(&self) -> T { unimplemented!("not implemented for {}", type_name) }
    fn kurtosis(&self) -> T { unimplemented!("not implemented for {}", type_name) }
}

Default behaviour: unimplemented moments panic with a helpful message — never silently return zero. This is a hard project rule.

Calibrator<T> and CalibrationResult

Calibration is Result-based with associated types for the parameter struct and error:

pub trait Calibrator<T: FloatExt> {
    type Params;
    type Error;
    fn calibrate(&self, market: &MarketData<T>) -> Result<CalibrationResult<Self>, Self::Error>;
}

CalibrationResult carries the calibrated Params, the final RMSE, iteration count, and a success flag. See the Quant catalog for the calibrators that ship today.

GreeksExt<T> and the Greeks aggregator

All pricers that derive from ModelPricer<T> automatically get a default greeks() implementation via finite differences. MC pricers override the default with a single-pass computation that re-uses the path bundle.

pub struct Greeks<T> {
    pub delta: T, pub gamma: T,
    pub vega: T,  pub theta: T, pub rho: T,
    pub vanna: T, pub charm: T,
    pub volga: T, pub veta: T,
}

See Quant for the pricers that expose Greeks.

On this page