1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/******************************************************************************
use positive::pos_or_panic;
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 14/8/24
******************************************************************************/
//! # Volatility Module
//!
//! This module provides comprehensive volatility calculation and modeling tools
//! for financial applications, including historical, implied, and stochastic
//! volatility models.
//!
//! ## Core Features
//!
//! ### Volatility Calculation Methods
//!
//! - Constant Volatility
//! - Historical Volatility (Moving Window)
//! - EWMA (Exponentially Weighted Moving Average)
//! - GARCH(1,1)
//! - Heston Stochastic Volatility
//! - Implied Volatility
//! - Uncertain Volatility Bounds
//! - Volatility Surface Interpolation
//!
//! ## Usage Examples
//!
//! ### Basic Volatility Calculations
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use positive::Positive;
//! use optionstratlib::volatility::constant_volatility;
//!
//! let returns = [dec!(0.02), dec!(0.02), dec!(0.02), dec!(0.02)];
//! let vol = constant_volatility(&returns);
//! ```
//!
//! ### Implied Volatility Calculation
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use optionstratlib::{ExpirationDate, Options};
//! use optionstratlib::model::types::{ OptionStyle, OptionType, Side};
//! use optionstratlib::volatility::implied_volatility;
//! use positive::Positive;
//! use positive::pos_or_panic;
//!
//! let mut option = Options::new(
//! OptionType::European,
//! Side::Long,
//! "STOCK".to_string(),
//! Positive::HUNDRED, // Strike price
//! ExpirationDate::Days(pos_or_panic!(30.0)),
//! pos_or_panic!(0.2), // Initial volatility guess
//! Positive::ONE, // Quantity
//! Positive::HUNDRED, // Current price
//! dec!(0.05), // Risk-free rate
//! OptionStyle::Call,
//! Positive::ZERO, // Dividend yield
//! None, // Exotic parameters
//! );
//!
//! let market_price = pos_or_panic!(30.0);
//! let iv = implied_volatility(market_price, &mut option, 100);
//! ```
//!
//! ### Historical Volatility with Moving Window
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use optionstratlib::volatility::historical_volatility;
//!
//! let returns = [dec!(0.02), dec!(0.02), dec!(-0.02), dec!(0.02)];
//! let window_size = 3;
//! let hist_vol = historical_volatility(&returns, window_size);
//! ```
//!
//! ### EWMA Volatility
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use optionstratlib::volatility::ewma_volatility;
//!
//! let returns = vec![dec!(0.01), dec!(-0.02), dec!(0.015), dec!(-0.01)];
//! let lambda = dec!(0.94); // Standard decay factor for daily data
//! let ewma_vol = ewma_volatility(&returns, lambda);
//! ```
//!
//!
//! ## Mathematical Models
//!
//! ### GARCH(1,1)
//!
//! The GARCH(1,1) model is implemented as:
//! σ²(t) = ω + α * r²(t-1) + β * σ²(t-1)
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use optionstratlib::volatility::garch_volatility;
//!
//! let returns = vec![dec!(0.01), dec!(-0.02), dec!(0.015)];
//! let omega = dec!(0.1); // Long-term variance weight
//! let alpha = dec!(0.2); // Recent shock weight
//! let beta = dec!(0.7); // Previous variance weight
//! let garch_vol = garch_volatility(&returns, omega, alpha, beta);
//! ```
//!
//! ### Heston Stochastic Volatility
//!
//! Implements the Heston model:
//! dv(t) = κ(θ - v(t))dt + ξ√v(t)dW(t)
//!
//! ```rust
//! use rust_decimal::Decimal;
//! use rust_decimal_macros::dec;
//! use optionstratlib::assert_decimal_eq;
//! use optionstratlib::volatility::simulate_heston_volatility;
//!
//! let kappa = dec!(2.0); // Mean reversion speed
//! let theta = dec!(0.04); // Long-term variance
//! let xi = dec!(0.3); // Volatility of volatility
//! let v0 = dec!(0.04); // Initial variance
//! let dt = Decimal::ONE / dec!(252.0); // Daily time step
//! let steps = 252; // Number of steps
//!
//! let heston_vol = simulate_heston_volatility(kappa, theta, xi, v0, dt, steps);
//! ```
//!
//! ## Time Frame Handling
//!
//! The module includes utilities for converting between different time frames:
//!
//! ```rust
//! # fn main() -> Result<(), optionstratlib::error::Error> {
//! use positive::pos_or_panic;
//! use optionstratlib::utils::time::TimeFrame;
//! use optionstratlib::volatility::{annualized_volatility, de_annualized_volatility};
//!
//! let daily_vol = pos_or_panic!(0.01);
//! let annual_vol = annualized_volatility(daily_vol, TimeFrame::Day)?;
//! let daily_vol_again = de_annualized_volatility(annual_vol, TimeFrame::Day);
//! # Ok(())
//! # }
//! ```
//!
//! ## Performance Considerations
//!
//! - Implied volatility calculation: O(n) where n is max_iterations
//! - Historical volatility: O(n*w) where n is returns length and w is window size
//! - EWMA: O(n) where n is returns length
//! - GARCH: O(n) where n is returns length
//! - Heston simulation: O(n) where n is number of steps
//!
//! ## Implementation Notes
//!
//! - All volatility calculations ensure non-negative results
//! - Implied volatility uses Newton-Raphson method with bounds
//! - Surface interpolation uses bilinear interpolation
//! - Time scaling follows the square root of time rule
//! - Numerical stability is ensured through bounds checking
//!
//! ## References
//!
//! The implementations are based on standard financial mathematics literature:
//! - Black-Scholes-Merton option pricing model
//! - RiskMetrics™ Technical Document for EWMA
//! - Heston (1993) stochastic volatility model
//! - GARCH by Bollerslev (1986)
pub use ;
pub use ;