Implementation:Avhz RustQuant ModelParameter
| Knowledge Sources | |
|---|---|
| Domains | Stochastic_Processes, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Core parameter wrapper type used throughout the RustQuant stochastic processes module.
Description
ModelParameter is a newtype wrapper around a boxed closure Box<dyn Fn(f64) -> f64 + Send + Sync> that unifies constant values and time-dependent functions into a single type. This allows all stochastic process parameters to be either:
- Constant -- A fixed f64 value (e.g., 0.05)
- Time-dependent -- A function of time Fn(f64) -> f64 (e.g., |t| 0.05 * t)
The struct implements From<f64> (wrapping a constant into a closure that ignores its argument) and From<F> for any F: Fn(f64) -> f64 + Send + Sync (wrapping a function directly). This design enables ergonomic construction of stochastic processes with either constant or time-varying parameters.
Usage
ModelParameter is used as the field type for all stochastic process struct parameters (mu, sigma, theta, etc.). Users typically do not construct ModelParameter directly; instead, they pass constants or closures to process constructors, and the Into<ModelParameter> conversion handles the wrapping automatically.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_stochastics/src/model_parameter.rs
- Lines: 1-57
Signature
pub struct ModelParameter(pub Box<dyn Fn(f64) -> f64 + Send + Sync>);
impl fmt::Debug for ModelParameter {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result
}
impl From<f64> for ModelParameter {
fn from(x: f64) -> Self
}
impl<F> From<F> for ModelParameter
where
F: Fn(f64) -> f64 + 'static + Send + Sync,
{
fn from(func: F) -> Self
}
Import
use RustQuant::stochastics::ModelParameter;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (constant) | f64 | No | A constant value, converted to a closure that returns x for any input |
| (function) | Fn(f64) -> f64 + Send + Sync | No | A time-dependent function taking time t and returning a parameter value |
Outputs
| Name | Type | Description |
|---|---|---|
| .0(t) | f64 | Evaluates the wrapped function at time t and returns the parameter value |
Usage Examples
use RustQuant::stochastics::ModelParameter;
use RustQuant::stochastics::ArithmeticBrownianMotion;
// Using a constant parameter -- automatically converted via From<f64>
let abm_const = ArithmeticBrownianMotion::new(0.05, 0.9);
// Using a time-dependent parameter -- automatically converted via From<F>
let abm_td = ArithmeticBrownianMotion::new(
|t: f64| 0.05 + 0.01 * t, // time-varying drift
|t: f64| 0.9 * (-0.1 * t).exp(), // decaying volatility
);
// Direct construction of ModelParameter
let param = ModelParameter::from(0.05);
let value = param.0(1.0); // returns 0.05 regardless of time
let param_fn = ModelParameter::from(|t: f64| 0.05 * t);
let value_at_2 = param_fn.0(2.0); // returns 0.1