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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! # Jaeckel
//!
//! A Rust port of Peter Jäckel's algorithms on http://www.jaeckel.org
//!
//! ## Let's Be Rational
//! The Rust crate is based on the latest (2024) version of the C++ reference implementation
//! The conditionally compiled features in the C++ reference implementation are not included in this crate
//! The PJ-2024-Inverse-Normal algorithm are used to inverse the normal cumulative distribution funciton and the AS241 algorighm is omitted
//!
//! ### Example
//!
//! ```rust
//! use jaeckel::{black, implied_black_volatility};
//!
//! // Calculate option price
//! let forward = 100.0;
//! let strike = 110.0;
//! let volatility = 0.2;
//! let time_to_expiry = 1.0;
//! let is_call = 1.0; // 1.0 for call, -1.0 for put
//!
//! let price = black(forward, strike, volatility, time_to_expiry, is_call);
//!
//! // Calculate implied volatility from price
//! let implied_vol = implied_black_volatility(price, forward, strike, time_to_expiry, is_call);
//! assert!((implied_vol - volatility).abs() < 1e-14);
//! ```
//!
//! ## Copyright
//!
//! Original C++ implementation Copyright © 2013-2023 Peter Jäckel.
//!
//! Permission to use, copy, modify, and distribute this software is freely granted,
//! provided that this notice is preserved.
// Internal modules - not exposed in public API
/// Error function using Cody's algorithm.
///
/// Computes erf(x) = (2/√π) ∫₀ˣ e^(-t²) dt
///
/// # Arguments
/// * `x` - Input value
///
/// # Returns
/// The error function value
pub use crateerf_cody;
/// Complementary error function using Cody's algorithm.
///
/// Computes erfc(x) = 1 - erf(x) = (2/√π) ∫ₓ^∞ e^(-t²) dt
///
/// # Arguments
/// * `x` - Input value
///
/// # Returns
/// The complementary error function value
pub use crateerfc_cody;
/// Scaled complementary error function using Cody's algorithm.
///
/// Computes erfcx(x) = e^(x²) × erfc(x)
///
/// # Arguments
/// * `x` - Input value
///
/// # Returns
/// The scaled complementary error function value
pub use crateerfcx_cody;
/// Inverse error function.
///
/// Computes the inverse of erf(x), i.e., finds y such that erf(y) = x
///
/// # Arguments
/// * `x` - Input value in range (-1, 1)
///
/// # Returns
/// The inverse error function value
///
/// # Panics
/// Panics if |x| >= 1
pub use crateerfinv;
/// Inverse cumulative normal distribution function.
///
/// Computes the inverse of the standard normal CDF, also known as the quantile function
/// or probit function.
///
/// # Arguments
/// * `u` - Probability value in range (0, 1)
///
/// # Returns
/// The value x such that P(X ≤ x) = u, where X ~ N(0,1)
pub use crateinverse_norm_cdf;
/// Cumulative normal distribution function.
///
/// Computes P(X ≤ x) where X ~ N(0,1)
///
/// # Arguments
/// * `x` - Input value
///
/// # Returns
/// The cumulative probability
pub use cratenorm_cdf;
/// Standard normal probability density function.
///
/// Computes φ(x) = (1/√(2π)) × e^(-x²/2)
///
/// # Arguments
/// * `x` - Input value
///
/// # Returns
/// The probability density value
pub use cratenorm_pdf;
/// Rational cubic interpolation function.
///
/// Performs monotone convex interpolation using rational cubic splines.
/// This is used internally for implied volatility calculations.
///
/// # Arguments
/// * `x` - Evaluation point
/// * `x_l` - Left node x-coordinate
/// * `x_r` - Right node x-coordinate
/// * `y_l` - Left node y-coordinate
/// * `y_r` - Right node y-coordinate
/// * `d_l` - Left node derivative
/// * `d_r` - Right node derivative
/// * `r` - Control parameter for shape
///
/// # Returns
/// Interpolated value at x
pub use craterational_cubic_interpolation;
/// Calculate control parameter to fit second derivative at left side.
///
/// # Arguments
/// * `x_l` - Left node x-coordinate
/// * `x_r` - Right node x-coordinate
/// * `y_l` - Left node y-coordinate
/// * `y_r` - Right node y-coordinate
/// * `d_l` - Left node derivative
/// * `d_r` - Right node derivative
/// * `second_derivative_l` - Target second derivative at left side
///
/// # Returns
/// Control parameter r for rational cubic interpolation
pub use craterational_cubic_control_parameter_to_fit_second_derivative_at_left_side;
/// Calculate control parameter to fit second derivative at right side.
///
/// # Arguments
/// * `x_l` - Left node x-coordinate
/// * `x_r` - Right node x-coordinate
/// * `y_l` - Left node y-coordinate
/// * `y_r` - Right node y-coordinate
/// * `d_l` - Left node derivative
/// * `d_r` - Right node derivative
/// * `second_derivative_r` - Target second derivative at right side
///
/// # Returns
/// Control parameter r for rational cubic interpolation
pub use craterational_cubic_control_parameter_to_fit_second_derivative_at_right_side;
/// Calculate minimum rational cubic control parameter.
///
/// # Arguments
/// * `d_l` - Left derivative
/// * `d_r` - Right derivative
/// * `s` - Slope between nodes
/// * `prefer_shape_preservation_over_smoothness` - Prioritize shape preservation
///
/// # Returns
/// Minimum control parameter to preserve desired shape properties
pub use crateminimum_rational_cubic_control_parameter;
/// Calculate convex rational cubic control parameter to fit second derivative at left side.
///
/// # Arguments
/// * `x_l` - Left node x-coordinate
/// * `x_r` - Right node x-coordinate
/// * `y_l` - Left node y-coordinate
/// * `y_r` - Right node y-coordinate
/// * `d_l` - Left node derivative
/// * `d_r` - Right node derivative
/// * `second_derivative_l` - Target second derivative at left side
/// * `prefer_shape_preservation_over_smoothness` - Prioritize shape preservation
///
/// # Returns
/// Control parameter ensuring both second derivative fit and convexity
pub use crateconvex_rational_cubic_control_parameter_to_fit_second_derivative_at_left_side;
/// Calculate convex rational cubic control parameter to fit second derivative at right side.
///
/// # Arguments
/// * `x_l` - Left node x-coordinate
/// * `x_r` - Right node x-coordinate
/// * `y_l` - Left node y-coordinate
/// * `y_r` - Right node y-coordinate
/// * `d_l` - Left node derivative
/// * `d_r` - Right node derivative
/// * `second_derivative_r` - Target second derivative at right side
/// * `prefer_shape_preservation_over_smoothness` - Prioritize shape preservation
///
/// # Returns
/// Control parameter ensuring both second derivative fit and convexity
pub use crateconvex_rational_cubic_control_parameter_to_fit_second_derivative_at_right_side;
/// Calculate Black-Scholes option price.
///
/// Computes the undiscounted Black-Scholes price for a European option.
///
/// # Arguments
/// * `forward` - Forward price of the underlying asset
/// * `strike` - Strike price of the option
/// * `volatility` - Implied volatility (annualized)
/// * `time` - Time to expiration in years
/// * `theta` - Option type: 1.0 for call, -1.0 for put
///
/// # Returns
/// The undiscounted option price
///
/// # Example
/// ```
/// use jaeckel::black;
///
/// let call_price = black(100.0, 110.0, 0.2, 1.0, 1.0);
/// let put_price = black(100.0, 110.0, 0.2, 1.0, -1.0);
/// ```
pub use crateBlack as black;
/// Calculate Black-Scholes vega (price sensitivity to volatility).
///
/// Computes ∂Price/∂σ for the Black-Scholes formula.
///
/// # Arguments
/// * `forward` - Forward price of the underlying asset
/// * `strike` - Strike price of the option
/// * `volatility` - Implied volatility (annualized)
/// * `time` - Time to expiration in years
///
/// # Returns
/// Vega (always positive for both calls and puts)
pub use crateVega as vega;
/// Calculate Black-Scholes volga (second derivative with respect to volatility).
///
/// Computes ∂²Price/∂σ² for the Black-Scholes formula.
///
/// # Arguments
/// * `forward` - Forward price of the underlying asset
/// * `strike` - Strike price of the option
/// * `volatility` - Implied volatility (annualized)
/// * `time` - Time to expiration in years
///
/// # Returns
/// Volga (convexity of price with respect to volatility)
pub use crateVolga as volga;
/// Calculate implied volatility from Black-Scholes price.
///
/// Uses Peter Jäckel's "Let's Be Rational" algorithm for extremely accurate
/// and fast implied volatility calculations.
///
/// # Arguments
/// * `price` - Option price (undiscounted)
/// * `forward` - Forward price of the underlying asset
/// * `strike` - Strike price of the option
/// * `time` - Time to expiration in years
/// * `theta` - Option type: 1.0 for call, -1.0 for put
///
/// # Returns
/// Implied volatility. Special values:
/// - Returns `f64::MAX` if price is above maximum possible
/// - Returns `-f64::MAX` if price is below intrinsic value
/// - Returns 0.0 if price equals intrinsic value (no time value)
///
/// # Example
/// ```
/// use jaeckel::{black, implied_black_volatility};
///
/// let price = black(100.0, 110.0, 0.2, 1.0, 1.0);
/// let iv = implied_black_volatility(price, 100.0, 110.0, 1.0, 1.0);
/// ```
pub use crateImpliedBlackVolatility as implied_black_volatility;
/// Calculate normalised Black-Scholes price.
///
/// Computes the Black-Scholes price in normalised form where the forward
/// is 1 and moneyness is expressed as x = ln(F/K).
///
/// # Arguments
/// * `x` - Log-moneyness: ln(forward/strike)
/// * `s` - Total volatility: σ√t
///
/// # Returns
/// Normalised option price
pub use crateNormalisedBlack as normalised_black;
/// Calculate normalised vega.
///
/// Computes vega in normalised form.
///
/// # Arguments
/// * `x` - Log-moneyness: ln(forward/strike)
/// * `s` - Total volatility: σ√t
///
/// # Returns
/// Normalised vega
pub use crateNormalisedVega as normalised_vega;
/// Calculate normalised volga.
///
/// Computes volga in normalised form.
///
/// # Arguments
/// * `x` - Log-moneyness: ln(forward/strike)
/// * `s` - Total volatility: σ√t
///
/// # Returns
/// Normalised volga
pub use crateNormalisedVolga as normalised_volga;
/// Calculate implied volatility from normalised Black-Scholes price.
///
/// # Arguments
/// * `beta` - Normalised option price
/// * `x` - Log-moneyness: ln(forward/strike)
/// * `theta` - Option type: 1.0 for call, -1.0 for put
///
/// # Returns
/// Total volatility s = σ√t
pub use crateNormalisedImpliedBlackVolatility as normalised_implied_black_volatility;
/// Householder's third-order convergence factor for root finding.
///
/// # Arguments
/// * `nu` - Newton step: -f/f'
/// * `h_2` - Second derivative ratio: f''/f'
/// * `h_3` - Third derivative ratio: f'''/f'
///
/// # Returns
/// Householder convergence factor
pub use cratehouseholder3_factor;
/// Householder's fourth-order convergence factor for root finding.
///
/// # Arguments
/// * `nu` - Newton step: -f/f'
/// * `h_2` - Second derivative ratio: f''/f'
/// * `h_3` - Third derivative ratio: f'''/f'
/// * `h_4` - Fourth derivative ratio: f''''/f'
///
/// # Returns
/// Householder convergence factor
pub use cratehouseholder4_factor;