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
moment_strip ¶
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. |
vectorized_closed_form |
bool
|
Flag indicating that |
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
moment_strip ¶
Open interval of s with E[(S_t / S_0)^s] < inf for every t,
or None if unknown or maturity-dependent (e.g. Heston's moment
explosion).
Fourier techniques use it to stay inside the region where cf at
imaginary arguments is a genuine moment: beyond it the closed forms are
continued analytically and can return plausible-looking values (past
Kou's pole the "moments" stay positive and log-convex when the jump
intensity is small).
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, dS = (r-q) S dt + sigma S^gamma dW.
European options have the Cox (1975) / Schroder (1989) closed form in terms
of the noncentral chi-squared distribution (price_closed_form); the
terminal spot can be sampled exactly (sample_terminal_spot). For
gamma < 1 the origin is absorbing, which both methods honour.
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 ¶
sample_terminal_spot(S0: float, r: float, T: float, size: int, q: float = 0.0, rng: Generator | None = None) -> np.ndarray
Exact simulation of the terminal spot of dS = (r-q)S dt + sigma S^gamma dW.
The drifted CEV process is a deterministic time change of the driftless
one, S_T = exp((r-q)T) F_tau with
tau = (exp(2(r-q)(gamma-1)T) - 1) / (2(r-q)(gamma-1)), and
X = F^(2(1-gamma)) / ((1-gamma)^2 sigma^2) is a squared Bessel
process of dimension delta = (1-2 gamma)/(1-gamma).
gamma < 1(delta < 2): the origin is attainable and absorbing;X_tauis drawn from the absorbed BESQ law (Makarov & Glew 2010): withlam = X_0 / (2 tau)anda = 1 - delta/2, drawG ~ Gamma(a, 1); the path is absorbed iffG >= lam(probabilityQ(a, lam)), otherwiseN ~ Poisson(lam - G)andX_tau ~ Gamma(N + 1, 2 tau). This reproduces Cox's (1975) / Schroder's (1989) pricing formula and keepsSa true martingale.gamma > 1(delta > 2): the origin is unattainable andX_tau / tauis noncentral chi-squared withdeltadegrees of freedom and non-centralityX_0 / tau.gamma == 1reduces to geometric Brownian motion.
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 |
q
|
float
|
The continuously compounded dividend yield, by default 0.0. |
0.0
|
rng
|
Generator | None
|
Random number generator; a fresh default generator is used if None. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
An array of simulated terminal spot prices (zeros for absorbed paths). |
Source code in src/optpricing/models/cev.py
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 | |
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 (Y < 2),
including the limiting cases Y = 0 (difference of two gamma processes,
i.e. Variance Gamma) and Y = 1. Monte Carlo sampling is exact for Y = 0
and uses inverse-transform sampling of the terminal characteristic function
otherwise.
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
moment_strip ¶
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 samples of the terminal log-return of the raw CGMY process.
For Y = 0 the process is the difference of two independent gamma
processes with shape C*T and rates M and G (exact). For any
other Y the terminal law is sampled by inverse transform of the
characteristic function (COS expansion), see
:func:optpricing.models.base.sample_from_cf.
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/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 scalar function
sigma(t, S) of time and spot returning the local volatility (the
Dupire surface sigma(T, K) evaluated along the path at T = t, K = S).
Monte Carlo compiles it with numba.njit, so it must be
nopython-compatible (plain arithmetic, math, NumPy scalars).
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 Levy motion (Eberlein & Keller 1995).
X_1 follows the hyperbolic distribution (generalized hyperbolic with
lambda = 1) with parameters alpha, beta, delta, mu; the process is the
Levy process generated by that law, so phi_t(u) = phi_1(u)^t. Note that
X_t is not hyperbolic for t != 1 (the class is not closed under
convolution), hence terminal sampling for t != 1 inverts the
characteristic function numerically.
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
moment_strip ¶
|beta + s| < alpha (Eberlein & Keller 1995).
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 samples of the terminal log-return of the raw process.
For T == 1 the law is the hyperbolic distribution itself and
scipy.stats.genhyperbolic is sampled exactly (scipy's shape parameters
are the dimensionless a = alpha*delta and b = beta*delta). For any
other horizon the terminal law has no closed form and is sampled by
inverse transform of the characteristic function, see
:func:optpricing.models.base.sample_from_cf.
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
moment_strip ¶
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
moment_strip ¶
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
moment_strip ¶
|beta + s| < alpha (Barndorff-Nielsen 1997).
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 forward dynamics (see :class:~optpricing.models.SABRModel) with
compensated log-normal jumps on the forward:
dF/F- = -lambda k dt + sigma_t F^(beta-1) dW + (e^J - 1) dN,
J ~ N(mu_j, sigma_j^2), k = E[e^J] - 1.
Monte Carlo only; the three-parameter legacy form (alpha as vol of vol)
is mapped to nu = alpha with a DeprecationWarning.
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
SABR model of Hagan, Kumar, Lesniewski & Woodward (2002).
The forward F and its volatility follow
dF = sigma_t F^beta dW, d sigma_t = nu sigma_t dZ, d<W, Z> = rho dt,
with sigma_0 = alpha. Spot inputs are converted to the forward
F = S exp((r - q) T); an optional shift prices F + shift (shifted
SABR, for negative or near-zero forwards).
Pricing: the closed form is Black-76 with Hagan's lognormal implied
volatility (an asymptotic expansion, accurate for moderate nu^2 T and
strikes not too far from the forward); Monte Carlo simulates the forward
with an Euler step for F and the exact lognormal step for sigma_t.
Notes
Before v2.5 the model had three parameters and used alpha as the
volatility of volatility (the initial volatility defaulted to the same
number). Such parameter sets are still accepted with a
DeprecationWarning and mapped to nu = alpha.
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
implied_volatility ¶
implied_volatility(forward: float | ndarray, strike: float | ndarray, t: float) -> float | np.ndarray
Hagan lognormal implied volatility for (shifted) forward and strike(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forward
|
float | ndarray
|
Forward price(s) (unshifted). |
required |
strike
|
float | ndarray
|
Strike(s) (unshifted). |
required |
t
|
float
|
Time to expiry in years. |
required |
Returns:
| Type | Description |
|---|---|
float | ndarray
|
Black implied volatility of the shifted forward |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the (shifted) forward or strike is not positive, or the expansion yields a non-positive / non-finite volatility. |
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
moment_strip ¶
Roots of 1 - theta nu s - sigma^2 nu s^2 / 2 (Madan, Carr & Chang
1998): the moment generating function is that base to -t/nu.
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
|