Models¶
The models
package contains all financial models for valuing options and rates.
It provides the abstract BaseModel
and a suite of concrete implementations,
from the standard Black-Scholes-Merton to advanced stochastic volatility
and jump-diffusion models.
BSMModel ¶
Bases: BaseModel
Black-Scholes-Merton (BSM) model for pricing European options.
This model assumes the underlying asset follows a geometric Brownian motion with constant volatility and risk-free rate.
Initializes the BSM model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/bsm.py
delta_analytic ¶
delta_analytic(
*,
spot: float,
strike: float,
r: float,
q: float,
t: float,
call: bool = True,
) -> float
Analytic delta for the BSM model.
Source code in src/optpricing/models/bsm.py
gamma_analytic ¶
Analytic gamma for the BSM model.
Source code in src/optpricing/models/bsm.py
rho_analytic ¶
rho_analytic(
*,
spot: float,
strike: float,
r: float,
q: float,
t: float,
call: bool = True,
) -> float
Analytic rho for the BSM model.
Source code in src/optpricing/models/bsm.py
theta_analytic ¶
theta_analytic(
*,
spot: float,
strike: float,
r: float,
q: float,
t: float,
call: bool = True,
) -> float
Analytic theta for the BSM model.
Source code in src/optpricing/models/bsm.py
vega_analytic ¶
Analytic vega for the BSM model.
Source code in src/optpricing/models/bsm.py
BaseModel ¶
Bases: ABC
Abstract base class for all financial pricing models.
This class defines a common interface for all models, including parameter validation, metadata flags for supported features (e.g., characteristic function, SDE), and methods for creating modified model instances.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
A string identifier for the model (e.g., "Black-Scholes-Merton"). |
params |
dict[str, float]
|
A dictionary holding the model's parameters. |
supports_cf |
bool
|
Flag indicating if the model implements a characteristic function. |
supports_sde |
bool
|
Flag indicating if the model implements an SDE simulation path. |
supports_pde |
bool
|
Flag indicating if the model provides PDE coefficients. |
has_closed_form |
bool
|
Flag indicating if a closed-form solution is available. |
has_variance_process |
bool
|
Flag for stochastic volatility models (e.g., Heston, SABR). |
is_pure_levy |
bool
|
Flag for pure Levy models where the terminal value can be sampled directly. |
has_jumps |
bool
|
Flag for models that include a jump component. |
Initializes the model and validates its parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float]
|
A dictionary of parameter names to values for the model. |
required |
Source code in src/optpricing/models/base/base_model.py
__eq__ ¶
Check for equality based on class type and parameters.
__hash__ ¶
Provide a hash for the model, making it usable in sets and dict keys.
__repr__ ¶
Provide a formal string representation of the model.
cf ¶
Return the characteristic function of the log-price process.
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the model does not support a characteristic function. |
Source code in src/optpricing/models/base/base_model.py
get_pde_coeffs ¶
Return the coefficients for the pricing PDE.
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the model does not support PDE solving. |
Source code in src/optpricing/models/base/base_model.py
get_sde_sampler ¶
Return a function that can be used to sample paths from the model's SDE.
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the model does not support SDE sampling. |
Source code in src/optpricing/models/base/base_model.py
price_closed_form ¶
Compute the option price using a closed-form solution, if available.
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the model does not have a closed-form solution. |
Source code in src/optpricing/models/base/base_model.py
with_params ¶
Create a new model instance with updated parameters.
This is useful for calibration and sensitivity analysis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**updated_params
|
float
|
Keyword arguments for the parameters to update. |
{}
|
Returns:
Type | Description |
---|---|
BaseModel
|
A new instance of the model with the updated parameters. |
Source code in src/optpricing/models/base/base_model.py
BatesModel ¶
Bases: BaseModel
Bates stochastic volatility jump-diffusion model.
This model combines the Heston stochastic volatility process with Merton's log-normal jump-diffusion process, providing a rich framework for capturing both volatility smiles and sudden market shocks.
Initializes the Bates model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/bates.py
get_sde_stepper ¶
Returns the SDE stepper function for the Bates model.
Source code in src/optpricing/models/bates.py
BlacksApproxModel ¶
Bases: BaseModel
Black's approximation for an American call on a stock with discrete dividends.
Initializes the Black's Approximation model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
required |
Source code in src/optpricing/models/blacks_approx.py
CEVModel ¶
Bases: BaseModel
Constant Elasticity of Variance (CEV) model.
Initializes the CEV model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/cev.py
sample_terminal_spot ¶
Exact simulation of the terminal spot price via Non-Central Chi-Squared.
This method provides a way to sample directly from the terminal distribution of the CEV process, which is more efficient than path-based simulation for pricing European options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
S0
|
float
|
The initial spot price of the asset. |
required |
r
|
float
|
The continuously compounded risk-free rate. |
required |
T
|
float
|
The time to maturity, in years. |
required |
size
|
int
|
The number of samples to generate. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of simulated terminal spot prices. |
Source code in src/optpricing/models/cev.py
CGMYModel ¶
Bases: BaseModel
CGMY (Carr, Geman, Madan, Yor, 2002) pure-jump Levy model.
This is a flexible four-parameter model that can capture skewness and kurtosis. It has a known characteristic function for all valid parameters. Monte Carlo simulation is supported for the special case Y=1 (Variance Gamma process).
Initializes the CGMY model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/cgmy.py
raw_cf ¶
Returns the CF of the raw CGMY process, without drift or spot.
This function represents the characteristic function of the process before the risk-neutral drift adjustment is applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
float
|
The time horizon. |
required |
Returns:
Type | Description |
---|---|
Callable
|
The raw characteristic function. |
Source code in src/optpricing/models/cgmy.py
sample_terminal_log_return ¶
Generates exact samples of the terminal log-return for the CGMY process.
NOTE: This is only implemented for the special case Y=1, where the process is a difference of two time-changed Brownian motions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
T
|
float
|
The time to maturity, in years. |
required |
size
|
int
|
The number of samples to generate. |
required |
rng
|
Generator
|
A random number generator instance for reproducibility. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of simulated terminal log-returns. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the model parameter Y is not equal to 1. |
Source code in src/optpricing/models/cgmy.py
CIRModel ¶
Bases: BaseModel
Cox-Ingersoll-Ross (1985) mean-reverting short rate model. dr_t = kappa * (theta - r_t) * dt + sigma * sqrt(r_t) * dW_t
Initializes the CIR model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/cir.py
DupireLocalVolModel ¶
Bases: BaseModel
Dupire Local Volatility model.
Initializes the Dupire Local Volatility model.
The key parameter for this model is 'vol_surface', a callable function that takes maturity (T) and strike (K) and returns the local volatility.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, Any] | None
|
A dictionary of model parameters. If None, a default constant volatility surface is used. Defaults to None. |
None
|
Source code in src/optpricing/models/dupire_local.py
__repr__ ¶
Custom representation to handle the vol_surface function.
Source code in src/optpricing/models/dupire_local.py
HestonModel ¶
Bases: BaseModel
Heston stochastic volatility model.
This model describes the evolution of an asset's price where the volatility itself is a random process, following a Cox-Ingersoll-Ross (CIR) process. It is widely used as it can capture volatility smiles and skews.
Initializes the Heston model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/heston.py
get_sde_stepper ¶
Returns the SDE stepper function for the Heston model.
This is not used by the JIT-compiled kernel but is kept for potential future use with non-compiled or more complex simulation loops.
Source code in src/optpricing/models/heston.py
HyperbolicModel ¶
Bases: BaseModel
Hyperbolic pure-jump Levy model.
Initializes the Hyperbolic model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/hyperbolic.py
raw_cf ¶
Returns the CF of the raw Hyperbolic process, without drift or spot.
This function represents the characteristic function of the process before the risk-neutral drift adjustment is applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
float
|
The time horizon. |
required |
Returns:
Type | Description |
---|---|
Callable
|
The raw characteristic function. |
Source code in src/optpricing/models/hyperbolic.py
sample_terminal_log_return ¶
Generates exact samples of the terminal log-return for the Hyperbolic process.
This uses the scipy.stats.genhyperbolic
distribution, which is the
exact distribution of the process at a given time horizon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
T
|
float
|
The time to maturity, in years. |
required |
size
|
int
|
The number of samples to generate. |
required |
rng
|
Generator
|
A random number generator instance for reproducibility. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of simulated terminal log-returns. |
Source code in src/optpricing/models/hyperbolic.py
KouModel ¶
Bases: BaseModel
Kou double-exponential jump-diffusion model.
Initializes the Kou Double-Exponential Jump model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/kou.py
MertonJumpModel ¶
Bases: BaseModel
Merton Jump-Diffusion model, implemented as a Poisson-weighted sum of BSM prices.
Initializes the Merton Jump-Diffusion model. Uses BSMModel instance for the core diffusion
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float]
|
A dictionary of model parameters. |
required |
Source code in src/optpricing/models/merton_jump.py
NIGModel ¶
Bases: BaseModel
Normal Inverse Gaussian (NIG) model, a pure-jump Levy process.
Initializes the NIG model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/nig.py
raw_cf ¶
Returns the CF of the raw NIG process, without drift or spot.
This function represents the characteristic function of the process before the risk-neutral drift adjustment is applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
float
|
The time horizon. |
required |
Returns:
Type | Description |
---|---|
Callable
|
The raw characteristic function. |
Source code in src/optpricing/models/nig.py
sample_terminal_log_return ¶
Generates exact samples of the terminal log-return for the NIG process.
This leverages the representation of the NIG process as a Brownian motion with drift, time-changed by an Inverse Gaussian process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
T
|
float
|
The time to maturity, in years. |
required |
size
|
int
|
The number of samples to generate. |
required |
rng
|
Generator
|
A random number generator instance for reproducibility. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of simulated terminal log-returns. |
Source code in src/optpricing/models/nig.py
ParamValidator ¶
A utility class containing static methods for model parameter validation.
This class is not meant to be instantiated. It serves as a namespace
for common validation logic used by BaseModel
subclasses.
bounded
staticmethod
¶
Check if a parameter is within a specified inclusive range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float]
|
The dictionary of parameters to validate. |
required |
key
|
str
|
The name of the parameter to check. |
required |
low
|
float
|
The lower bound of the valid range. |
required |
high
|
float
|
The upper bound of the valid range. |
required |
model
|
str
|
The name of the model performing the validation, for error messages. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the parameter is outside the [low, high] range. |
Source code in src/optpricing/models/base/validators.py
positive
staticmethod
¶
Check if specified parameters are strictly positive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float]
|
The dictionary of parameters to validate. |
required |
keys
|
list[str]
|
A list of parameter names that must be positive. |
required |
model
|
str
|
The name of the model performing the validation, for error messages. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the specified parameters are not strictly positive. |
Source code in src/optpricing/models/base/validators.py
require
staticmethod
¶
Check for the presence of required parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float]
|
The dictionary of parameters to validate. |
required |
required
|
list[str]
|
A list of parameter names that must be present in |
required |
model
|
str
|
The name of the model performing the validation, for error messages. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If any of the required parameters are missing. |
Source code in src/optpricing/models/base/validators.py
PerpetualPutModel ¶
Bases: BaseModel
Provides a closed-form price for a perpetual American put option.
A perpetual option has no expiry date. This model assumes the holder will exercise optimally. The risk-free rate and volatility are considered intrinsic parameters of the model itself.
Initializes the model with its parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary requiring 'sigma' and 'rate'. If None, |
required |
Source code in src/optpricing/models/perpetual_put.py
SABRJumpModel ¶
Bases: BaseModel
SABR model with an added log-normal jump component on the spot process.
Initializes the SABR with Jumps model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/sabr_jump.py
SABRModel ¶
Bases: BaseModel
Stochastic Alpha, Beta, Rho (SABR) model from Hagan et al. (2002).
Initializes the SABR model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|
Source code in src/optpricing/models/sabr.py
VarianceGammaModel ¶
Bases: BaseModel
Variance Gamma (VG) model, a pure-jump Levy process.
Initializes the Variance Gamma (VG) model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float]
|
A dictionary of model parameters. |
None
|
Source code in src/optpricing/models/vg.py
raw_cf ¶
Returns the raw characteristic function of the VG process itself.
This function represents the characteristic function of the process before the risk-neutral drift adjustment is applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
float
|
The time horizon. |
required |
Returns:
Type | Description |
---|---|
Callable
|
The raw characteristic function. |
Source code in src/optpricing/models/vg.py
sample_terminal_log_return ¶
Generates exact samples of the terminal log-return for the VG process.
This leverages the representation of the VG process as a Brownian motion with drift, time-changed by a Gamma process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
T
|
float
|
The time to maturity, in years. |
required |
size
|
int
|
The number of samples to generate. |
required |
rng
|
Generator
|
A random number generator instance for reproducibility. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of simulated terminal log-returns. |
Source code in src/optpricing/models/vg.py
VasicekModel ¶
Bases: BaseModel
Vasicek mean-reverting short rate model. dr_t = kappa * (theta - r_t) * dt + sigma * dW_t
Initializes the Vasicek model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
dict[str, float] | None
|
A dictionary of model parameters. If None, |
None
|