optionstratlib_simulation/lib.rs
1#![deny(missing_docs, rustdoc::broken_intra_doc_links)]
2// Per rules/global_rules.md §Error Handling, unchecked `[]` / slicing is
3// banned in production code.
4#![deny(clippy::indexing_slicing)]
5// Unit tests routinely index into `Vec`s they just pushed into, so the lint
6// is silenced in `#[cfg(test)]` only.
7#![cfg_attr(test, allow(clippy::indexing_slicing))]
8// Per rules/global_rules.md §Arithmetic, an operator that overflows, divides
9// by zero or breaks the `Positive` invariant aborts the caller, so every
10// operation on `Decimal`, `Positive` and the integers goes through its
11// checked form (#788, #808). `clippy.toml` exempts the unary minus on
12// `Decimal`, which cannot overflow. Casts that truncate, wrap or drop the
13// sign are denied for the same reason. Unit tests are exempt, as above.
14#![deny(
15 clippy::arithmetic_side_effects,
16 clippy::cast_possible_truncation,
17 clippy::cast_possible_wrap,
18 clippy::cast_sign_loss
19)]
20#![cfg_attr(
21 test,
22 allow(
23 clippy::arithmetic_side_effects,
24 clippy::cast_possible_truncation,
25 clippy::cast_possible_wrap,
26 clippy::cast_sign_loss
27 )
28)]
29// An error or a default built eagerly in `ok_or` / `unwrap_or` is paid on
30// the success path too (#857), so it is built in a closure.
31#![deny(clippy::or_fun_call)]
32
33//! # optionstratlib-simulation
34//!
35//! The simulation engine of OptionStratLib: random walks, stochastic
36//! processes, simulators, exit policies and the generic evaluation and
37//! statistics of simulated paths. It depends on `optionstratlib-core` and
38//! `optionstratlib-pricing`, and on no option chain, strategy, backtesting,
39//! plotting, I/O or terminal-presentation code.
40//!
41//! - [`simulation`]: the stochastic processes ([`simulation::WalkType`]:
42//! Brownian, geometric Brownian, log-returns, mean-reverting, jump
43//! diffusion, GARCH, Heston, telegraph, custom and historical) and their
44//! default kernels ([`simulation::WalkTypeAble`]); walk parameters and
45//! steps ([`simulation::WalkParams`], [`simulation::steps::Step`],
46//! [`simulation::steps::Xstep`], [`simulation::steps::Ystep`]); the walk
47//! driver shared by every generator ([`simulation::walk_steps`],
48//! [`simulation::walk_steps_par`], [`simulation::generator_positive`]);
49//! [`simulation::randomwalk::RandomWalk`] and
50//! [`simulation::simulator::Simulator`]; the Ornstein-Uhlenbeck process
51//! ([`simulation::generate_ou_process`]); the multi-factor quadratic rough
52//! Heston model and its Monte Carlo vanilla prices
53//! ([`simulation::QuadraticRoughHestonParams`],
54//! [`simulation::simulate_quadratic_rough_heston_path`],
55//! [`simulation::price_vanilla_quadratic_rough_heston`]); exit policies
56//! ([`simulation::ExitPolicy`], [`simulation::check_exit_policy`]); and the
57//! generic path evaluation ([`simulation::PathEvaluator`],
58//! [`simulation::PathOutcome`], [`simulation::PathStatistics`],
59//! [`simulation::evaluate_paths`]).
60//! - [`error`]: [`error::SimulationError`], [`error::SimulationResult`] and
61//! [`error::QuadraticRoughHestonError`].
62//!
63//! Prices along a path are `Positive`, drifts and statistics `Decimal`;
64//! `f64` stays inside the kernels, and a non-finite value at an
65//! `f64` → `Decimal` boundary is reported as
66//! [`error::SimulationError::NonFinite`].
67//!
68//! The evaluation of a strategy over simulated paths, its statistics report
69//! and the charts of a walk stay with the backtesting and visualization
70//! layers, which the `optionstratlib` facade provides; the chain and series
71//! generators built on these walks are in `optionstratlib-market`, behind
72//! its `synthetic` feature.
73//!
74//! ## Imports
75//!
76//! There is no prelude, as in the other component crates (ADR-0001 D7): the
77//! [`simulation`] module root re-exports the capability, and the
78//! `optionstratlib` facade prelude serves broad imports.
79//!
80//! ## Place in the workspace
81//!
82//! - **Depends on** `optionstratlib-core` and `optionstratlib-pricing`.
83//! - **Must not depend on** `optionstratlib-market`, `-analytics`, `-strategies`,
84//! `-backtest` and `-visualization`; `make check-graph` enforces the layering
85//! (ADR-0001 D9).
86//! - **In the facade:** `optionstratlib::simulation` and `SimulationError` /
87//! `SimulationResult` / `QuadraticRoughHestonError` in `optionstratlib::error`,
88//! under the facade feature
89//! `simulation`. The facade paths are the same types as the paths here; the
90//! [ownership
91//! map](https://github.com/joaquinbejar/OptionStratLib/blob/main/docs/ownership.md)
92//! lists every one with its feature.
93//!
94//! Moving from 0.21? The [0.22 architecture and adoption
95//! guide](https://github.com/joaquinbejar/OptionStratLib/blob/main/docs/migration-0.22.md)
96//! maps each redesign to its replacement workflow; 0.22 keeps no
97//! compatibility with 0.21.
98//!
99//! ## Minimal example
100//!
101//! ```rust
102//! use optionstratlib_core::pos_or_panic;
103//! use optionstratlib_simulation::simulation::generate_ou_process;
104//!
105//! // An Ornstein-Uhlenbeck path of 50 steps mean-reverting to 100.
106//! let path = generate_ou_process(
107//! pos_or_panic!(100.0),
108//! pos_or_panic!(100.0),
109//! pos_or_panic!(0.5),
110//! pos_or_panic!(0.2),
111//! pos_or_panic!(0.01),
112//! 50,
113//! )?;
114//! assert_eq!(path.len(), 50);
115//! # Ok::<(), optionstratlib_simulation::error::SimulationError>(())
116//! ```
117//!
118//! ## Runnable example
119//!
120//! A runnable program that depends on this crate directly, with the smallest
121//! dependency set and no facade, is [`osl-example-direct-simulation`](https://github.com/joaquinbejar/OptionStratLib/tree/main/examples/direct/simulation); `make tree-example-direct-simulation`
122//! asserts its resolved graph.
123//!
124//! ## Features
125//!
126//! - `schema` (off by default): derives `utoipa::ToSchema` on the simulation
127//! types and enables `schema` in core and pricing.
128
129/// Random walks, stochastic processes, simulators, exit policies and the
130/// generic evaluation of simulated paths.
131pub mod simulation;
132
133/// Errors raised by the simulation layer.
134pub mod error;
135
136/// Version of the `optionstratlib-simulation` crate.
137pub const VERSION: &str = env!("CARGO_PKG_VERSION");
138
139/// Compiles the Rust examples of this crate's `README.md` as doctests, so the
140/// README cannot drift from the API (#554). Exists only under `cfg(doctest)`.
141#[cfg(doctest)]
142#[doc = include_str!("../README.md")]
143pub struct ReadmeDoctests;