Models#

Heston#

class hestonpy.models.heston.Heston(spot: float, vol_initial: float, r: float, kappa: float, theta: float, drift_emm: float, sigma: float, rho: float, seed: int = 42)[source]#

Heston model for option pricing with stochastic volatility.

This class implements the Heston model for pricing European options with stochastic volatility. It includes methods for simulation, pricing, and hedging.

You can initialize the Heston model parameters under the historical or risk neutral dynamics as you want.

Parameters:
  • spot (float) – Current price of the underlying asset.

  • vol_initial (float) – Initial variance of the underlying asset.

  • r (float) – Risk-free interest rate.

  • kappa (float) – Mean reversion speed of the variance.

  • theta (float) – Long-term mean of the variance.

  • drift_emm (float) – Market price of risk (lambda) for the variance process. Usually set as 0

  • sigma (float) – Volatility of the variance (vol of vol).

  • rho (float) – Correlation between the asset price and its variance.

  • seed (int) – Random seed for reproducibility.

call_delta(strike: array, time_to_maturity: array, s: array | None = None, v: array | None = None)[source]#

Calculate the delta of a European call option.

This method calculates the delta of a European call option using the Heston model.

Parameters:
  • strike (np.array) – Strike prices of the options.

  • time_to_maturity (np.array) – Times to maturity of the options.

  • s (np.array) – Current prices of the underlying asset.

  • v (np.array) – Initial variances of the underlying asset.

Returns:

Delta of the call option.

Return type:

float

call_price(strike: array, time_to_maturity: array, s: array | None = None, v: array | None = None, **kwargs)[source]#

Price a European call option using the Carr-Madan method.

This method prices a European call option using the Carr-Madan Fourier pricing method.

Parameters:
  • strike (np.array) – Strike prices of the options.

  • time_to_maturity (np.array) – Times to maturity of the options.

  • s (np.array) – Current prices of the underlying asset.

  • v (np.array) – Initial variances of the underlying asset.

Returns:

Call option prices.

Return type:

float

call_vega(strike: array, time_to_maturity: array, s: array | None = None, v: array | None = None)[source]#

Calculate the vega of a European call option.

This method calculates the vega of a European call option using the Heston model.

Parameters:
  • strike (np.array) – Strike prices of the options.

  • time_to_maturity (np.array) – Times to maturity of the options.

  • s (np.array) – Current prices of the underlying asset.

  • v (np.array) – Initial variances of the underlying asset.

Returns:

Vega of the call option.

Return type:

float

carr_madan_price(strike: array, time_to_maturity: array, s: array | None = None, v: array | None = None, error_boolean: bool = False, **kwargs)[source]#

Computes the price of a European call option using the Carr-Madan Fourier pricing method.

This method employs the Carr-Madan approach, leveraging the characteristic function to calculate the option price.

Parameters:
  • strike (np.array) – Strike prices of the options.

  • time_to_maturity (np.array) – Times to maturity of the options.

  • s (np.array) – Current prices of the underlying asset.

  • v (np.array) – Initial variances of the underlying asset.

  • error_boolean (bool) – Whether to return the error of the price calculation.

Returns:

Option prices and errors (if error_boolean is True).

Return type:

float or tuple

characteristic(j: int, **kwargs)[source]#

Creates the characteristic function Psi_j(x, v, t; u) for a given (x, v, t).

This function returns the characteristic function based on the index provided.

Parameters:

j (int) – Index of the characteristic function (must be 1 or 2).

Returns:

The characteristic function.

Return type:

callable

Raises:

ValueError – If the index j is not 1 or 2.

delta_vega_hedging(strike: float, strike_hedging: float, maturity: float, maturity_hedging: float, nbr_points: float = 252, nbr_simulations: float = 100)[source]#

Implement a delta-vega hedging strategy for a European option using the Heston model.

This function simulates the hedging process over the lifetime of the option by dynamically rebalancing a portfolio consisting of a risky asset (underlying stock), an option (for vega hedging), and a non-risky asset (bank account). The function assumes that both the pricing and hedging models are based on the Heston stochastic volatility model, but they may use different volatilities for hedging and pricing.

Parameters:
  • strike (float) – Strike price of the option to be hedged.

  • strike_hedging (float) – Strike price of the option used for hedging.

  • maturity (float) – Time to maturity of the option to be hedged.

  • maturity_hedging (float) – Time to maturity of the option used for hedging.

  • nbr_points (int) – Number of time points in the simulation.

  • nbr_simulations (int) – Number of simulations to run.

Returns:

Portfolio values, underlying asset prices, variances, and option prices.

Return type:

tuple

fourier_transform_price(strike: array, time_to_maturity: array, s: array | None = None, v: array | None = None, error_boolean: bool = False, **kwargs)[source]#

Price a European option using the Fourier transform method.

This method prices a European option using the Fourier transform method with the Heston model.

Parameters:
  • strike (np.array) – Strike prices of the options.

  • time_to_maturity (np.array) – Times to maturity of the options.

  • s (np.array) – Current prices of the underlying asset.

  • v (np.array) – Initial variances of the underlying asset.

  • error_boolean (bool) – Whether to return the error of the price calculation.

Returns:

Option prices and errors (if error_boolean is True).

Return type:

float or tuple

monte_carlo_price(strike: float, time_to_maturity: float, scheme: str = typing.Literal['euler', 'milstein'], nbr_points: int = 100, nbr_simulations: int = 1000)[source]#

Price a European option using Monte Carlo simulation.

This method prices a European option using Monte Carlo simulation with the Heston model.

Parameters:
  • strike (float) – Strike price of the option.

  • time_to_maturity (float) – Time to maturity of the option in years.

  • scheme (str) – Discretization scheme to use (‘euler’ or ‘milstein’).

  • nbr_points (int) – Number of time points in each simulation.

  • nbr_simulations (int) – Number of simulations to run.

Returns:

Option price and confidence interval.

Return type:

namedtuple

plot_simulation(time_to_maturity: float = 1, scheme: str = typing.Literal['euler', 'milstein'], nbr_points: int = 100) tuple[source]#

Plot a single simulation of the asset price and variance paths.

This method simulates and plots the paths of the underlying asset price and its variance using the specified discretization scheme.

Parameters:
  • time_to_maturity (float) – Time to maturity of the option in years.

  • scheme (str) – Discretization scheme to use (‘euler’ or ‘milstein’).

  • nbr_points (int) – Number of time points in the simulation.

Returns:

Simulated asset prices and variances.

Return type:

tuple

price_surface()[source]#

Plot the call price surface as a function of strike and time to maturity.

This method generates a 3D plot of the call option prices for a range of strikes and maturities.

Returns:

None

simulate(time_to_maturity: float = 1, scheme: str = typing.Literal['euler', 'milstein'], nbr_points: int = 100, nbr_simulations: int = 1000) tuple[source]#

Simulate asset price and variance paths using the Heston model.

This method simulates the paths of the underlying asset price and its variance using either the Euler or Milstein discretization scheme.

Parameters:
  • time_to_maturity (float) – Time to maturity of the option in years.

  • scheme (str) – Discretization scheme to use (‘euler’ or ‘milstein’).

  • nbr_points (int) – Number of time points in each simulation.

  • nbr_simulations (int) – Number of simulations to run.

Returns:

Simulated asset prices, variances, and count of null variances.

Return type:

tuple

Bates#

class hestonpy.models.bates.Bates(spot: float, vol_initial: float, r: float, kappa: float, theta: float, drift_emm: float, sigma: float, rho: float, lambda_jump: float, mu_J: float, sigma_J: float, seed: int = 42)[source]#

Bates model for option pricing with stochastic volatility and jumps.

This class implements the Bates model, which extends the Heston model by incorporating jumps in the underlying asset price. It is used for pricing European options with stochastic volatility and jump diffusion.

Parameters:
  • spot (float) – The current price of the underlying asset.

  • vol_initial (float) – The initial variance of the underlying asset.

  • r (float) – The risk-free interest rate.

  • kappa (float) – The rate at which the variance reverts to the long-term mean.

  • theta (float) – The long-term mean of the variance.

  • drift_emm (float) – The market price of risk for the variance process.

  • sigma (float) – The volatility of the variance process.

  • rho (float) – The correlation between the asset price and its variance.

  • lambda_jump (float) – The intensity of jumps.

  • mu_J (float) – The mean of the jump size.

  • sigma_J (float) – The volatility of the jump size.

  • seed (int) – Seed for the random number generator.

call_price(strike: array, time_to_maturity: array, s: array | None = None, v: array | None = None, **kwargs)[source]#

Calculate the price of a European call option using the Bates model.

This method computes the price of a European call option by leveraging the Carr-Madan Fourier pricing method.

Parameters:
  • strike (np.array) – The strike price of the option.

  • time_to_maturity (np.array) – Time to maturity of the option in years.

  • s (np.array) – The current price of the underlying asset.

  • v (np.array) – The initial variance of the underlying asset.

  • kwargs – Additional keyword arguments for model parameters.

Returns:

The price of the call option.

Return type:

float

carr_madan_price(strike: array, time_to_maturity: array, s: array | None = None, v: array | None = None, error_boolean: bool = False, **kwargs)[source]#

Computes the price of a European call option using the Carr-Madan Fourier pricing method.

This method employs the Carr-Madan approach, leveraging the characteristic function to calculate the option price.

Parameters:
  • strike (np.array) – The strike price of the option.

  • time_to_maturity (np.array) – Time to maturity of the option in years.

  • s (np.array) – The current price of the underlying asset.

  • v (np.array) – The initial variance of the underlying asset.

  • error_boolean (bool) – Flag to return the error associated with the price.

  • kwargs – Additional keyword arguments for model parameters.

Returns:

The calculated option price and optionally the error.

Return type:

float or tuple

characteristic(j: int, **kwargs) float[source]#

Extends the characteristic function to include jumps in the Heston model.

This method calculates the characteristic function for the Bates model, which includes both stochastic volatility and jumps.

Parameters:
  • j (int) – Indicator for the characteristic function component (1 or 2).

  • kwargs – Additional keyword arguments for model parameters.

Returns:

The characteristic function.

Return type:

float

BlackScholes#

class hestonpy.models.blackScholes.BlackScholes(spot: float, r: float, mu: float, volatility: float, seed: int = 42)[source]#

Black-Scholes model for option pricing.

This class implements the Black-Scholes model for pricing European options. It includes methods for simulating asset paths, calculating option prices, and plotting Greeks.

Parameters:
  • spot (float) – The current price of the underlying asset.

  • r (float) – The risk-free interest rate.

  • mu (float) – The expected return of the underlying asset.

  • volatility (float) – The volatility of the underlying asset.

  • seed (int) – Seed for the random number generator.

call_price(strike: float, time_to_maturity: float = 1, spot: float | None = None, r: float | None = None, volatility: float | None = None)[source]#

Calculate the price of a European call option.

This method calculates the price of a European call option using the Black-Scholes formula.

Parameters:
  • strike (float) – The strike price of the option.

  • time_to_maturity (float) – Time to maturity of the option in years.

  • spot (float) – The current price of the underlying asset.

  • r (float) – The risk-free interest rate.

  • volatility (float) – The volatility of the underlying asset.

Returns:

The price of the call option.

Return type:

float

delta(strike: float, time_to_maturity: float, flag_option: Literal['call', 'put'], spot: float | None = None, r: float | None = None, volatility: float | None = None)[source]#

Calculate the Delta of a European option.

Delta measures the sensitivity of the option price to changes in the underlying asset’s price.

Parameters:
  • strike (float) – The strike price of the option.

  • time_to_maturity (float) – Time to maturity of the option in years.

  • flag_option (str) – Type of option (“call” or “put”).

  • spot (float) – The current price of the underlying asset.

  • r (float) – The risk-free interest rate.

  • volatility (float) – The volatility of the underlying asset.

Returns:

The Delta of the option.

Return type:

float

delta_hedging(strike: float, time_to_maturity: float, flag_option: Literal['call', 'put'], hedging_volatility: float, pricing_volatility: float | None = None, nbr_hedges: float = 252, nbr_simulations: float = 100)[source]#

Implement a delta hedging strategy for a European option.

This method simulates a delta hedging strategy using both a risky asset (underlying asset) and a non-risky asset.

Parameters:
  • flag_option (str) – Type of option (“call” or “put”).

  • strike (float) – The strike price of the option.

  • hedging_volatility (float) – The volatility used for hedging purposes.

  • pricing_volatility (float) – The volatility used for pricing the option.

  • nbr_hedges (int) – The number of hedging intervals.

  • nbr_simulations (int) – The number of simulations.

Returns:

A tuple containing the portfolio value and simulated asset prices.

Return type:

tuple

delta_surface(flag_option: Literal['call', 'put'])[source]#

Plot the Delta surface of the option as a function of strike and time to maturity.

This method generates a 3D plot of the Delta for a range of strikes and times to maturity.

Parameters:

flag_option (str) – Type of option (“call” or “put”).

gamma(strike: float, time_to_maturity: float, spot: float | None = None, r: float | None = None, volatility: float | None = None)[source]#

Calculate the Gamma of a European option.

Gamma measures the rate of change of Delta with respect to the underlying asset’s price.

Parameters:
  • strike (float) – The strike price of the option.

  • time_to_maturity (float) – Time to maturity of the option in years.

  • spot (float) – The current price of the underlying asset.

  • r (float) – The risk-free interest rate.

  • volatility (float) – The volatility of the underlying asset.

Returns:

The Gamma of the option.

Return type:

float

gamma_surface()[source]#

Plot the Gamma surface of the option as a function of strike and time to maturity.

This method generates a 3D plot of the Gamma for a range of strikes and times to maturity.

plot_simulation(scheme: str = typing.Literal['euler', 'milstein'], nbr_points: int = 1000, time_to_maturity: float = 1) array[source]#

Plot a simulated asset price path.

This method plots a single simulated price path of the underlying asset.

Parameters:
  • scheme (str) – Discretization scheme to use (“euler” or “milstein”).

  • nbr_points (int) – Number of time points in the simulation.

  • time_to_maturity (float) – Time to maturity of the option in years.

Returns:

Simulated asset price path.

Return type:

np.array

put_price(strike: float, time_to_maturity: float, spot: float | None = None, r: float | None = None, volatility: float | None = None)[source]#

Calculate the price of a European put option.

This method calculates the price of a European put option using the Black-Scholes formula.

Parameters:
  • strike (float) – The strike price of the option.

  • time_to_maturity (float) – Time to maturity of the option in years.

  • spot (float) – The current price of the underlying asset.

  • r (float) – The risk-free interest rate.

  • volatility (float) – The volatility of the underlying asset.

Returns:

The price of the put option.

Return type:

float

simulate(time_to_maturity: float = 1, scheme: str = typing.Literal['euler', 'milstein'], nbr_points: int = 100, nbr_simulations: int = 1000) array[source]#

Simulate asset price paths using the Black-Scholes model.

This method simulates the price paths of the underlying asset using either the Euler or Milstein discretization scheme.

Parameters:
  • time_to_maturity (float) – Time to maturity of the option in years.

  • scheme (str) – Discretization scheme to use (“euler” or “milstein”).

  • nbr_points (int) – Number of time points in each simulation.

  • nbr_simulations (int) – Number of simulations to run.

Returns:

Simulated asset price paths.

Return type:

np.array

vega(strike: float, time_to_maturity: float, spot: float | None = None, r: float | None = None, volatility: float | None = None)[source]#

Calculate the Vega of a European option.

Vega measures the sensitivity of the option price to changes in volatility.

Parameters:
  • strike (float) – The strike price of the option.

  • time_to_maturity (float) – Time to maturity of the option in years.

  • spot (float) – The current price of the underlying asset.

  • r (float) – The risk-free interest rate.

  • volatility (float) – The volatility of the underlying asset.

Returns:

The Vega of the option.

Return type:

float

volatility_arbitrage(strike: float, time_to_maturity: float, flag_option: Literal['call'], hedging_volatility: float, pricing_volatility: float | None = None, nbr_hedges: float = 1000, nbr_simulations: float = 100)[source]#

Implement a volatility arbitrage strategy by buying an underpriced option and dynamically delta hedging it to expiry.

This function models a scenario where the implied volatility of an option (used for pricing) differs from the trader’s forecasted actual volatility (used for hedging). If the trader’s forecast turns out to be correct, a profit can be realized by buying the option and dynamically delta hedging it. The delta for hedging is calculated using the trader’s forecasted volatility, while the option price reflects the implied volatility.

Parameters:
  • flag_option (str) – Type of option (“call”).

  • strike (float) – The strike price of the option.

  • hedging_volatility (float) – The volatility used for delta hedging.

  • pricing_volatility (float) – The implied volatility used for pricing.

  • nbr_hedges (int) – The number of hedging intervals.

  • nbr_simulations (int) – The number of simulations.

Returns:

A tuple containing the portfolio value and simulated asset prices.

Return type:

tuple