Calibration#
Calibration methods on implied volatility smile.
VolatilitySmile#
- class hestonpy.models.calibration.volatilitySmile.VolatilitySmile(strikes: array, time_to_maturity: array, atm: float, market_prices: array | None = None, market_ivs: array | None = None, r: float = 0)[source]#
Represents a volatility smile constructed from market prices or implied volatilities.
This class handles the conversion between option prices and implied volatilities using the Black-Scholes model. It supports calibration of Heston and Bates models to fit the observed volatility smile.
- Parameters:
strikes (np.array) – Array of option strike prices.
time_to_maturity (np.array) – Array of time to maturity for the options.
atm (float) – At-the-money forward price.
market_prices (np.array) – Array of market option prices.
market_ivs (np.array) – Array of market implied volatilities.
r (float) – Risk-free interest rate.
- calibration(price_function, initial_guess, guess_correlation_sign: Literal['positive', 'negative', 'unknown'] = 'unknown', speed: Literal['local', 'global'] = 'local', power: Literal['rmse', 'mae', 'mse'] = 'mse', method: Literal['L-BFGS-B', 'SLSQP', 'trust-constr'] = 'L-BFGS-B', weights: array | None = None, relative_errors: bool = False)[source]#
Calibrates a Heston model (parameters: kappa, theta, sigma, rho) or a Bates model (parameters: kappa, theta, sigma, rho, lambda_jump, mu_J, sigma_J) to fit the observed volatility smile.
The initial variance is set to the closest ATM implied volatility from the data to reduce dimensionality.
Two calibration schemes are available:
‘local’: A fast but less robust method, sensitive to market noise.
‘global’: A more robust but slower method.
The user can specify a prior belief about the sign of the correlation:
‘positive’: Constrains rho to [0,1].
‘negative’: Constrains rho to [-1,0].
‘unknown’: Allows rho to vary in [-1,1].
If a correlation sign is provided, the function ensures the initial guess for rho has the correct sign.
- Parameters:
price_function (callable) – Function to compute option prices under the Heston model or Bates model.
initial_guess (list) – Initial parameters, [kappa, theta, sigma, rho] for Heston models or [kappa, theta, sigma, rho, lambda_jump, mu_J, sigma_J] for Bates models.
guess_correlation_sign (str) – Assumption on the correlation sign (‘positive’, ‘negative’, ‘unknown’).
speed (str) – Calibration method (‘local’ for fast, ‘global’ for robust optimization).
weights (np.array) – Array of weights applied to each observation in the calibration. If None, uniform weights are used.
power (str) – Defines the loss function’s exponentiation method (‘mse’, ‘mae’, ‘rmse’).
relative_errors (bool) – If True, the calibration minimizes relative errors instead of absolute errors.
method (str) – Local optimization algorithm to use (“L-BFGS-B”, “SLSQP” or “trust-constr”).
- Returns:
Dictionary containing the calibrated Heston parameters.
- Return type:
dict
- compute_smile(prices: array | None = None, strikes: array | None = None) array [source]#
Computes implied volatilities from option prices using the Black-Scholes model.
- Parameters:
prices (np.array) – Option prices corresponding to the strikes. If None, uses self.market_prices.
strikes (np.array) – Strike prices. If None, uses self.strikes.
- Returns:
Implied volatilities computed from the option prices.
- Return type:
np.array
- evaluate_calibration(model_values: array, metric_type: Literal['price', 'iv'] = 'price')[source]#
Evaluates the quality of the calibration by calculating RMSE, MSE, and MAE either on prices or implied volatilities (IVs).
- Parameters:
model_values (np.array) – Values estimated by the model (prices or IVs).
metric_type (str) – ‘price’ to compare prices, ‘iv’ to compare IVs. Default is ‘price’.
- Returns:
Dictionary containing the absolute and relative error metrics.
- Return type:
Dict[str, float]
- filters(full_market_data: DataFrame, select_mid_ivs: bool = True) DataFrame [source]#
Filters market data based on volume, mid-price, bid-ask spread, and moneyness.
- Parameters:
full_market_data (pd.DataFrame) – DataFrame containing market data with columns: ‘Strike’, ‘Volume’, ‘Bid’, ‘Ask’.
select_mid_ivs (bool) – If True, selects mid implied volatilities. Default is True.
- Returns:
Filtered market data with additional columns: ‘Mid ivs’, ‘Ask ivs’, ‘Bid ivs’, ‘Mid Price’.
- Return type:
pd.DataFrame
- plot(calibrated_ivs: array | None = None, calibrated_prices: array | None = None, bid_prices: array | None = None, bid_ivs: array | None = None, ask_prices: array | None = None, ask_ivs: array | None = None, maturity: str | None = None)[source]#
Plots the volatility smile.
This function can either: * Plot the smile using only the market data provided in the constructor. * Plot the smile with additional calibrated data (either calibrated implied volatilities or prices).
If calibrated_prices is provided, the function computes the corresponding implied volatilities before plotting.
- Parameters:
calibrated_ivs (np.array) – Calibrated implied volatilities. If provided, they will be plotted.
calibrated_prices (np.array) – Calibrated option prices. If provided, they will be converted to IVs before plotting.
bid_prices (np.array) – Bid prices. If provided, they will be converted to IVs before plotting.
bid_ivs (np.array) – Bid implied volatilities. If provided, they will be plotted.
ask_prices (np.array) – Ask prices. If provided, they will be converted to IVs before plotting.
ask_ivs (np.array) – Ask implied volatilities. If provided, they will be plotted.
maturity (str) – Maturity date in the format ‘YYYY-MM-DD’. If provided, it will be used in the plot title.
- reverse_smile(ivs: array | None = None) array [source]#
Computes option prices from implied volatilities using the Black-Scholes model.
- Parameters:
ivs (np.array) – Implied volatilities corresponding to the strikes. If None, uses self.market_ivs.
- Returns:
Option prices computed from the implied volatilities.
- Return type:
np.array
- svi_smooth(select_svi_ivs: bool = False)[source]#
Smooths the volatility smile using a raw SVI model.
- Parameters:
select_svi_ivs (bool) – If True, selects the calibrated SVI implied volatilities. Default is False.
- Returns:
Dictionary of calibrated SVI parameters and array of calibrated implied volatilities.
- Return type:
Tuple[Dict[str, float], np.array]
StochasticVolatilityInspired#
- class hestonpy.models.calibration.svi.StochasticVolatilityInspired(time_to_maturity: float)[source]#
Implements the SVI (Stochastic Volatility Inspired) parameterization in its raw formulation, as presented in Jim Gatheral’s paper.
This class allows for the calculation of the total implied volatility according to the SVI formulation and performs calibration to market data.
- Parameters:
time_to_maturity (float) – Time to maturity of the option in years.
- calibration(strikes: array, market_ivs: array, forward: float, x0: list = [0.5, 0.5, 0.5, 0.5, 0.5], method: str = 'SLSQP')[source]#
Calibrates the SVI model to market implied volatilities by minimizing the squared error between the model’s total implied variance and the market’s total implied variance.
- Parameters:
strikes (np.array) – Array of option strike prices.
market_ivs (np.array) – Array of market implied volatilities.
forward (float) – Forward price of the underlying asset. Often np.exp(r * time_to_mat) * spot.
x0 (list) – Initial values of the SVI parameters (a, b, rho, m, sigma, respectively). Default is [0.5, 0.5, 0.5, 0.5, 0.5].
method (str) – Optimization algorithm used. Default is ‘SLSQP’.
- Returns:
Tuple containing a dictionary of calibrated SVI parameters and an array of model implied volatilities after calibration.
- Return type:
tuple (dict, np.array)
- raw_formulation(k, a: float, b: float, rho: float, m: float, sigma: float)[source]#
Computes the total implied variance according to the raw SVI formulation.
- Parameters:
k (float) – Log-moneyness (log(strike / forward)).
a (float) – Vertical shift parameter.
b (float) – Curvature parameter.
rho (float) – Correlation parameter (-1 < rho < 1).
m (float) – Horizontal translation parameter.
sigma (float) – Scale volatility parameter.
- Returns:
Total implied variance associated with log-moneyness k.
- Return type:
float