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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/******************************************************************************
use positive::pos_or_panic;
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 30/11/24
******************************************************************************/
//! # Probability Analysis Module
//!
//! This module provides comprehensive probability and risk analysis tools for
//! option strategies, including profit probability calculations, risk metrics,
//! and price movement analysis.
//!
//! ## Core Components
//!
//! ### Strategy Probability Analysis
//!
//! ```rust
//! use positive::Positive;
//!
//! pub struct StrategyProbabilityAnalysis {
//! pub probability_of_profit: Positive,
//! pub probability_of_max_profit: Positive,
//! pub probability_of_max_loss: Positive,
//! pub expected_value: Positive,
//! pub break_even_points: Vec<Positive>,
//! pub risk_reward_ratio: Positive,
//! }
//! ```
//!
//! ### Probability Analysis Trait
//!
//! ```rust
//! use positive::Positive;
//! use optionstratlib::pricing::Profit;
//! use optionstratlib::strategies::Strategies;
//! use optionstratlib::error::ProbabilityError;
//!
//! use optionstratlib::strategies::probabilities::{PriceTrend, StrategyProbabilityAnalysis, VolatilityAdjustment};
//!
//! pub trait ProbabilityAnalysis: Strategies + Profit {
//! fn analyze_probabilities(
//! &self,
//! volatility_adj: Option<VolatilityAdjustment>,
//! trend: Option<PriceTrend>
//! ) -> Result<StrategyProbabilityAnalysis, ProbabilityError>;
//!
//! fn expected_value(
//! &self,
//! volatility_adj: Option<VolatilityAdjustment>,
//! trend: Option<PriceTrend>
//! ) -> Result<Positive, ProbabilityError>;
//! }
//! ```
//!
//! ## Usage Examples
//!
//! ### Basic Strategy Analysis
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use tracing::info;
//! use optionstratlib::model::types::{ OptionStyle, OptionType, Side};
//! use optionstratlib::strategies::probabilities::{ProbabilityAnalysis, VolatilityAdjustment, PriceTrend, StrategyProbabilityAnalysis};
//! use optionstratlib::ExpirationDate;use positive::Positive;
//! use positive::pos_or_panic;
//! use optionstratlib::strategies::bear_call_spread::BearCallSpread;
//!
//! let strategy = BearCallSpread::new(
//! "SP500".to_string(),
//! pos_or_panic!(5781.88), // underlying_price
//! pos_or_panic!(5750.0), // long_strike_itm
//! pos_or_panic!(5820.0), // short_strike
//! ExpirationDate::Days(Positive::TWO),
//! pos_or_panic!(0.18), // implied_volatility
//! dec!(0.05), // risk_free_rate
//! Positive::ZERO, // dividend_yield
//! Positive::TWO, // long quantity
//! pos_or_panic!(85.04), // premium_long
//! pos_or_panic!(29.85), // premium_short
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.73), // close_fee_long
//! pos_or_panic!(0.73), // close_fee_short
//! );
//! let analysis = strategy.analyze_probabilities(None, None);
//!
//! info!("Analysis: {:?}", analysis);
//! ```
//!
//! ### Analysis with Volatility Adjustment
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use optionstratlib::ExpirationDate;
//! use optionstratlib::strategies::probabilities::{ProbabilityAnalysis, VolatilityAdjustment};
//! use positive::Positive;
//! use positive::pos_or_panic;
//! use optionstratlib::strategies::bear_call_spread::BearCallSpread;
//!
//! let strategy = BearCallSpread::new(
//! "SP500".to_string(),
//! pos_or_panic!(5781.88), // underlying_price
//! pos_or_panic!(5750.0), // long_strike_itm
//! pos_or_panic!(5820.0), // short_strike
//! ExpirationDate::Days(Positive::TWO),
//! pos_or_panic!(0.18), // implied_volatility
//! dec!(0.05), // risk_free_rate
//! Positive::ZERO, // dividend_yield
//! Positive::TWO, // long quantity
//! pos_or_panic!(85.04), // premium_long
//! pos_or_panic!(29.85), // premium_short
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.73), // close_fee_long
//! pos_or_panic!(0.73), // close_fee_short
//! );
//!
//! let vol_adj = Some(VolatilityAdjustment {
//! base_volatility: pos_or_panic!(0.20), // 20% base volatility
//! std_dev_adjustment: pos_or_panic!(0.10), // 10% adjustment
//! });
//!
//! let analysis = strategy.analyze_probabilities(vol_adj, None);
//! ```
//!
//! ### Analysis with Price Trend
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use optionstratlib::ExpirationDate;
//! use positive::Positive;
//! use positive::pos_or_panic;
//! use optionstratlib::strategies::bear_call_spread::BearCallSpread;
//! use optionstratlib::strategies::probabilities::{PriceTrend, ProbabilityAnalysis};
//! let strategy = BearCallSpread::new(
//! "SP500".to_string(),
//! pos_or_panic!(5781.88), // underlying_price
//! pos_or_panic!(5750.0), // long_strike_itm
//! pos_or_panic!(5820.0), // short_strike
//! ExpirationDate::Days(Positive::TWO),
//! pos_or_panic!(0.18), // implied_volatility
//! dec!(0.05), // risk_free_rate
//! Positive::ZERO, // dividend_yield
//! Positive::TWO, // long quantity
//! pos_or_panic!(85.04), // premium_long
//! pos_or_panic!(29.85), // premium_short
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.78), // open_fee_long
//! pos_or_panic!(0.73), // close_fee_long
//! pos_or_panic!(0.73), // close_fee_short
//! );
//! let trend = Some(PriceTrend {
//! drift_rate: 0.05, // 5% annual drift
//! confidence: 0.95, // 95% confidence level
//! });
//!
//! let analysis = strategy.analyze_probabilities(None, trend).unwrap();
//! ```
//!
//! ### Price Range Probability Analysis
//!
//! ```rust
//! use tracing::info;
//! use optionstratlib::strategies::probabilities::calculate_price_probability;
//! use optionstratlib::ExpirationDate;
//! use positive::Positive;
//! use positive::pos_or_panic;
//!
//! let (prob_below, prob_in_range, prob_above) = calculate_price_probability(
//! &Positive::HUNDRED, // current price
//! &pos_or_panic!(95.0), // lower bound
//! &pos_or_panic!(105.0), // upper bound
//! None, // volatility adjustment
//! None, // trend
//! &ExpirationDate::Days(pos_or_panic!(30.0)),
//! None // risk-free rate
//! ).unwrap();
//! info!("Probabilities: {}, {}, {}", prob_below, prob_in_range, prob_above);
//! ```
//!
//! ## Mathematical Models
//!
//! ### Expected Value Calculation
//!
//! The expected value is calculated using:
//! ```text
//! E[V] = Σ P(Si) * V(Si)
//! ```
//! where:
//! - Si: Price scenario i
//! - P(Si): Probability of scenario i
//! - V(Si): Value at scenario i
//!
//! ### Price Movement Probability
//!
//! Uses log-normal distribution with drift:
//! ```text
//! ln(ST/S0) ~ N(μT, σ²T)
//! ```
//! where:
//! - ST: Price at time T
//! - S0: Current price
//! - μ: Drift rate
//! - σ: Volatility
//! - T: Time to expiration
//!
//! ## Performance Considerations
//!
//! - Probability calculations: O(n) where n is the number of price points
//! - Expected value calculation: O(n) for n scenarios
//! - Memory usage: O(1) for single point calculations
//! - Cache results when analyzing multiple scenarios
//!
//! ## Implementation Notes
//!
//! - All probabilities are strictly positive (Positive)
//! - Volatility adjustments affect both mean and standard deviation
//! - Price trends are incorporated through drift adjustment
//! - Break-even points are calculated numerically
//! - Risk metrics use absolute values for consistency
//!
//! ## Error Handling
//!
//! The module returns Result types for all main functions, with errors for:
//! - Invalid time parameters (negative or zero time to expiry)
//! - Invalid volatility (zero or negative)
//! - Invalid probability bounds (outside [\0,1\])
//! - Invalid price ranges (upper < lower bound)
pub
pub
pub use StrategyProbabilityAnalysis;
pub use ProbabilityAnalysis;
pub use ;