Calibration¶
The calibration
package provides tools for fitting financial models to
market data, estimating parameters from historical data, and calculating
implied volatility surfaces.
BSMIVSolver ¶
High-performance, vectorized Newton-Raphson solver for BSM implied volatility.
This solver is designed to calculate the implied volatility for a large number of options simultaneously, leveraging NumPy for vectorized operations.
Initializes the BSM implied volatility solver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_iter
|
int
|
The maximum number of iterations for the Newton-Raphson method, by default 20. |
20
|
tolerance
|
float
|
The error tolerance to determine convergence, by default 1e-6. |
1e-06
|
Source code in src/optpricing/calibration/vectorized_bsm_iv.py
solve ¶
Calculates implied volatility for an array of options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_prices
|
ndarray
|
An array of market prices for which to find the implied volatility. |
required |
options
|
DataFrame
|
A DataFrame of option contracts, must include 'strike', 'maturity', and 'optionType' columns. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of calculated implied volatilities corresponding to the target prices. |
Source code in src/optpricing/calibration/vectorized_bsm_iv.py
Calibrator ¶
A generic class for calibrating financial models to market data.
This class orchestrates the process of finding the model parameters that minimize the difference between model prices and observed market prices.
Initializes the Calibrator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
BaseModel
|
The financial model to be calibrated (e.g., HestonModel). |
required |
market_data
|
DataFrame
|
A DataFrame containing market prices of options. Must include 'strike', 'maturity', 'optionType', and 'marketPrice' columns. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Source code in src/optpricing/calibration/calibrator.py
fit ¶
fit(
initial_guess: dict[str, float],
bounds: dict[str, tuple],
frozen_params: dict[str, float] = None,
) -> dict[str, float]
Performs the calibration using an optimization algorithm.
This method uses scipy.optimize.minimize
(or minimize_scalar
for
a single parameter) to find the optimal set of parameters that
minimizes the objective function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_guess
|
dict[str, float]
|
A dictionary of initial guesses for the parameters to be fitted. |
required |
bounds
|
dict[str, tuple]
|
A dictionary mapping parameter names to their (min, max) bounds. |
required |
frozen_params
|
dict[str, float] | None
|
A dictionary of parameters to hold constant during the optimization. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
A dictionary containing the full set of calibrated and frozen parameters. |
Source code in src/optpricing/calibration/calibrator.py
VectorizedIntegrationIVSolver ¶
VectorizedIntegrationIVSolver(
max_iter: int = 20,
tolerance: float = 1e-07,
upper_bound: float = 200.0,
)
A high-performance, vectorized Secant method solver for implied volatility for any model that supports a characteristic function.
Initializes the vectorized IV solver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_iter
|
int
|
Maximum number of iterations for the Secant method, by default 20. |
20
|
tolerance
|
float
|
Error tolerance for convergence, by default 1e-7. |
1e-07
|
upper_bound
|
float
|
The upper limit for the numerical integration, by default 200.0. |
200.0
|
Source code in src/optpricing/calibration/vectorized_integration_iv.py
solve ¶
Calculates implied volatility for an array of options and prices.
This method uses a vectorized Secant root-finding algorithm. The pricing at each step is performed using a vectorized version of the Gil-Pelaez inversion formula.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_prices
|
ndarray
|
An array of market prices for which to find the implied volatility. |
required |
options
|
DataFrame
|
A DataFrame of option contracts. |
required |
model
|
BaseModel
|
The financial model to use for pricing. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of calculated implied volatilities. |
Source code in src/optpricing/calibration/vectorized_integration_iv.py
VolatilitySurface ¶
A class to compute and hold market and model-implied volatility surfaces.
This class takes a DataFrame of option market data and provides methods to calculate the Black-Scholes implied volatility (IV) for each option, either from its market price or from a price generated by a financial model.
Initializes the VolatilitySurface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option_data
|
DataFrame
|
A DataFrame containing market prices of options. Must include 'strike', 'maturity', 'marketPrice', 'optionType', and 'expiry' columns. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the required columns are missing from |
Source code in src/optpricing/calibration/iv_surface.py
calculate_market_iv ¶
Calculates the market implied volatility surface from market prices.
This method inverts the Black-Scholes formula for each option's market
price to find the corresponding implied volatility. The results are
stored in the self.surface
DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stock
|
Stock
|
The underlying asset's properties. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
VolatilitySurface
|
The same instance of the class, allowing for method chaining. |
Source code in src/optpricing/calibration/iv_surface.py
calculate_model_iv ¶
calculate_model_iv(
stock: Stock,
rate: Rate,
model: BaseModel,
technique: BaseTechnique = None,
) -> VolatilitySurface
Calculates a model's implied volatility surface.
This method first prices every option in the dataset using the provided model and technique. It then inverts the Black-Scholes formula for each of these model prices to generate the model-implied volatility surface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stock
|
Stock
|
The underlying asset's properties. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
model
|
BaseModel
|
The financial model to generate prices from. |
required |
technique
|
BaseTechnique
|
The pricing technique to use with the model. |
None
|
Returns:
Type | Description |
---|---|
VolatilitySurface
|
The same instance of the class, allowing for method chaining. |
Source code in src/optpricing/calibration/iv_surface.py
find_atm_options ¶
Finds the closest at-the-money (ATM) call-put pair for each expiry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
calls
|
DataFrame
|
A DataFrame of call options. |
required |
puts
|
DataFrame
|
A DataFrame of put options. |
required |
spot
|
float
|
The current spot price of the underlying. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
A DataFrame containing the merged ATM call-put pairs. |
Source code in src/optpricing/calibration/fit_market_params.py
fit_jump_params_from_history ¶
Estimates jump parameters and diffusion volatility from historical returns.
This function separates historical log returns into a "diffusion" component (normal daily movements) and a "jump" component (extreme movements) based on a standard deviation threshold. It then calculates the annualized parameters for a jump-diffusion model like Merton's.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_returns
|
Series
|
A pandas Series of daily log returns. |
required |
threshold_stds
|
float
|
The number of standard deviations to use as a threshold for identifying jumps, by default 3.0. |
3.0
|
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the estimated parameters: 'sigma', 'lambda', 'mu_j', and 'sigma_j'. |
Source code in src/optpricing/calibration/fit_jump_parameters.py
fit_rate_and_dividend ¶
fit_rate_and_dividend(
calls: DataFrame,
puts: DataFrame,
spot: float,
r_fixed: float | None = None,
q_fixed: float | None = None,
) -> tuple[float, float]
Fits the risk-free rate (r) and dividend yield (q) from put-call parity.
This function uses the prices of at-the-money (ATM) call-put pairs to
solve for the r
and q
that minimize the parity pricing error.
Parameters can be held fixed or fitted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
calls
|
DataFrame
|
A DataFrame of call options with 'strike', 'maturity', 'marketPrice'. |
required |
puts
|
DataFrame
|
A DataFrame of put options with 'strike', 'maturity', 'marketPrice'. |
required |
spot
|
float
|
The current spot price of the underlying. |
required |
r_fixed
|
float | None
|
If provided, the risk-free rate is held fixed at this value. Defaults to None. |
None
|
q_fixed
|
float | None
|
If provided, the dividend yield is held fixed at this value. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
tuple[float, float]
|
A tuple containing the estimated (or fixed) risk-free rate and dividend yield. |
Source code in src/optpricing/calibration/fit_market_params.py
price_options_vectorized ¶
price_options_vectorized(
options_df: DataFrame,
stock: Stock,
model: BaseModel,
rate: Rate,
*,
upper_bound: float = 200.0,
**kwargs: Any,
) -> np.ndarray
Vectorised integral pricer (Carr-Madan representation).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options_df
|
DataFrame
|
Must contain |
required |
stock
|
Stock
|
Underlying description. |
required |
model
|
BaseModel
|
Any model exposing a |
required |
rate
|
Rate
|
Continuous zero-curve. |
required |
upper_bound
|
float
|
Integration truncation limit (works for double precision). |
200
|
Returns:
Type | Description |
---|---|
ndarray
|
Model prices - aligned with the row order of options_df. |
Source code in src/optpricing/calibration/vectorized_pricer.py
select_fastest_technique ¶
Selects the fastest available pricing technique for a given model.
The function checks the capabilities of the model in a specific order of preference, which generally corresponds to computational speed.
The order of preference is: 1. Closed-Form 2. Fast Fourier Transform (FFT) 3. Monte Carlo
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
BaseModel
|
The financial model for which to select a technique. |
required |
Returns:
Type | Description |
---|---|
BaseTechnique
|
An instance of the fastest suitable pricing technique. |
Raises:
Type | Description |
---|---|
TypeError
|
If no suitable pricing technique can be found for the model. |