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
use SQRT_2PI;
use crate::;
// NOTE: black has to be private. If is public then `calc_rational_iv` is decreased benches dramatically.
// For internal validation/benchmarks we sometimes need direct access to
// specific helpers from `black.rs`. To avoid permanently degrading
// optimisations on the critical IV path, these are only re-exported
// under a dedicated feature.
pub use ;
pub
const IMPLIED_VOLATILITY_MAXIMUM_ITERATIONS: i32 = 2;
pub const DENORMALISATION_CUTOFF: f64 = 0.0;
pub const ONE_OVER_SQRT_TWO_PI: f64 = 1.0 / SQRT_2PI;
/// Calculates the price of a European option using the Black model.
///
/// This function computes the theoretical price of a European call or put option based on the Black model, which is an extension of the Black-Scholes model for futures contracts. The model assumes that the price of the underlying asset follows a geometric Brownian motion and that markets are frictionless.
///
/// # Arguments
/// * `forward_price` - The forward price of the underlying asset.
/// * `strike_price` - The strike price of the option.
/// * `sigma` - The volatility of the underlying asset's returns.
/// * `time_to_maturity` - The time to maturity of the option, in years.
/// * `option_type` - The type of the option (call or put), represented by `OptionType`.
///
/// # Returns
/// The theoretical price of the option as a `f64`.
///
/// # Examples
/// ```
/// use blackscholes::{OptionType, lets_be_rational::black};
///
/// let forward_price = 100.0;
/// let strike_price = 95.0;
/// let sigma = 0.2;
/// let time_to_maturity = 1.0;
/// let option_type = OptionType::Call; // For a call option
///
/// let price = black(forward_price, strike_price, sigma, time_to_maturity, option_type);
/// println!("The price of the option is: {}", price);
/// ```
///
/// # Note
/// The function uses the natural logarithm of the forward price over the strike price,
/// multiplies it by the square root of time to maturity, and applies the option type
/// to determine the final price. It's suitable for European options *only*.
/// Calculates the implied volatility of an option using a rational guess.
///
/// This function estimates the implied volatility of a European call or put option based on the market price, forward price, strike price, time to maturity, and option type. It uses a rational guess approach with limited iterations (2) to find the implied volatility.
///
/// # Arguments
/// * `market_price` - The market price of the option.
/// * `forward_price` - The forward price of the underlying asset.
/// * `strike_price` - The strike price of the option.
/// * `time_to_maturity` - The time to maturity of the option, in years.
/// * `option_type` - The type of the option (call or put), represented by `OptionType`.
///
/// # Returns
/// The implied volatility of the option as a `f64`.
///
/// # Examples
/// ```
/// use blackscholes::{OptionType, lets_be_rational::implied_volatility_from_a_transformed_rational_guess};
///
/// let market_price = 10.0;
/// let forward_price = 100.0;
/// let strike_price = 95.0;
/// let time_to_maturity = 1.0;
/// let option_type = OptionType::Call;
///
/// let implied_volatility = implied_volatility_from_a_transformed_rational_guess(
/// market_price,
/// forward_price,
/// strike_price,
/// time_to_maturity,
/// option_type,
/// );
/// println!("The implied volatility of the option is: {}", implied_volatility);
/// ```
///
/// # Note
/// This function is suitable for European options *only* and uses a rational guess approach with limited iterations (2) to estimate the implied volatility.