Techniques¶
The techniques
package provides the various numerical and analytical
methods for pricing options.
AmericanMonteCarloTechnique ¶
AmericanMonteCarloTechnique(
*,
n_paths: int = 20000,
n_steps: int = 100,
antithetic: bool = True,
seed: int | None = None,
lsm_degree: int = 2,
)
Bases: BaseTechnique
, GreekMixin
, IVMixin
Prices American options by simulating full SDE paths and applying the Longstaff-Schwartz algorithm.
Source code in src/optpricing/techniques/american_monte_carlo.py
BaseTechnique ¶
Bases: ABC
Abstract base class for all pricing methodologies.
A technique defines the algorithm used to compute a price from the core 'atoms' (Option, Stock, Rate) and a given financial 'Model'.
price
abstractmethod
¶
price(
option: Option | ndarray,
stock: Stock,
model: BaseModel,
rate: Rate,
**kwargs,
) -> PricingResult | np.ndarray
Calculate the price of an option.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option | ndarray
|
The option contract(s) to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use for the calculation. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
**kwargs
|
Any
|
Additional keyword arguments required by specific techniques or models. |
{}
|
Returns:
Type | Description |
---|---|
PricingResult
|
An object containing the calculated price and potentially other metrics. |
Source code in src/optpricing/techniques/base/base_technique.py
CRRTechnique ¶
Bases: LatticeTechnique
Cox-Ross-Rubinstein binomial lattice technique.
Source code in src/optpricing/techniques/base/lattice_technique.py
ClosedFormTechnique ¶
Bases: BaseTechnique
, GreekMixin
, IVMixin
A pricing technique for models that provide a closed-form solution.
This class acts as a generic wrapper. It calls the price_closed_form
method on a given model. It also intelligently uses analytic Greeks if the
model provides them, otherwise falling back to the finite-difference methods
from GreekMixin
.
Initializes the technique.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_analytic_greeks
|
bool
|
If True, the technique will use the model's specific analytic Greek
methods (e.g., |
True
|
Source code in src/optpricing/techniques/closed_form.py
delta ¶
Overrides GreekMixin to use analytic delta if available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option | ZeroCouponBond
|
The instrument to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. For rate models, |
required |
model
|
BaseModel
|
The financial model to use. Must have |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Source code in src/optpricing/techniques/closed_form.py
gamma ¶
Overrides GreekMixin to use analytic gamma if available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option | ZeroCouponBond
|
The instrument to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. For rate models, |
required |
model
|
BaseModel
|
The financial model to use. Must have |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Source code in src/optpricing/techniques/closed_form.py
price ¶
price(
option: Option | ZeroCouponBond,
stock: Stock,
model: BaseModel,
rate: Rate,
**kwargs: Any,
) -> PricingResult
Prices the instrument using the model's closed-form solution.
This method dynamically builds the required parameters based on the
type of instrument being priced (e.g., Option or ZeroCouponBond) and
calls the model's price_closed_form
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option | ZeroCouponBond
|
The instrument to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. For rate models, |
required |
model
|
BaseModel
|
The financial model to use. Must have |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
PricingResult
|
An object containing the calculated price. |
Raises:
Type | Description |
---|---|
TypeError
|
If the model does not have a closed-form solution or if the instrument type is not supported. |
Source code in src/optpricing/techniques/closed_form.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 |
|
rho ¶
Overrides GreekMixin to use analytic rho if available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option | ZeroCouponBond
|
The instrument to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. For rate models, |
required |
model
|
BaseModel
|
The financial model to use. Must have |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Source code in src/optpricing/techniques/closed_form.py
theta ¶
Overrides GreekMixin to use analytic theta if available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option | ZeroCouponBond
|
The instrument to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. For rate models, |
required |
model
|
BaseModel
|
The financial model to use. Must have |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Source code in src/optpricing/techniques/closed_form.py
vega ¶
Overrides GreekMixin to use analytic vega if available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option | ZeroCouponBond
|
The instrument to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. For rate models, |
required |
model
|
BaseModel
|
The financial model to use. Must have |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Source code in src/optpricing/techniques/closed_form.py
FFTTechnique ¶
Bases: BaseTechnique
, GreekMixin
, IVMixin
Fast Fourier Transform (FFT) pricer based on the Carr-Madan formula, preserving the original tuned logic for grid and parameter selection.
Initializes the FFT solver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
The exponent for the number of grid points (N = 2^n), by default 12. |
12
|
eta
|
float
|
The spacing of the grid in the frequency domain, by default 0.25. |
0.25
|
alpha
|
float | None
|
The dampening parameter. If None, it is auto-tuned based on a volatility proxy from the model. Defaults to None. |
None
|
Source code in src/optpricing/techniques/fft.py
price ¶
price(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
**kwargs: Any,
) -> PricingResult
Calculates the option price using the FFT method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use. Must support a characteristic function. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
PricingResult
|
An object containing the calculated price. |
Source code in src/optpricing/techniques/fft.py
GreekMixin ¶
Provides finite-difference calculations for Greeks.
This mixin is designed to be side-effect-free. It creates modified copies
of the input objects for shifted calculations rather than mutating them in place.
It also supports Common Random Numbers (CRN) for variance reduction in
Monte Carlo-based calculations by checking for a self.rng
attribute.
delta ¶
delta(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
h_frac: float = 0.001,
**kwargs: Any,
) -> float
Calculates delta using a central difference formula.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use for the calculation. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
h_frac
|
float
|
The fractional step size for shifting the spot price, by default 1e-3. |
0.001
|
Returns:
Type | Description |
---|---|
float
|
The calculated delta. |
Source code in src/optpricing/techniques/base/greek_mixin.py
gamma ¶
gamma(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
h_frac: float = 0.001,
**kw: Any,
) -> float
Calculates gamma using a central difference formula.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use for the calculation. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
h_frac
|
float
|
The fractional step size for shifting the spot price, by default 1e-3. |
0.001
|
Returns:
Type | Description |
---|---|
float
|
The calculated gamma. |
Source code in src/optpricing/techniques/base/greek_mixin.py
rho ¶
rho(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
h: float = 0.0001,
**kw: Any,
) -> float
Calculates rho using a central difference formula.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use for the calculation. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
h
|
float
|
The absolute step size for shifting the interest rate, by default 1e-4. |
0.0001
|
Returns:
Type | Description |
---|---|
float
|
The calculated rho. |
Source code in src/optpricing/techniques/base/greek_mixin.py
theta ¶
theta(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
h: float = 1e-05,
**kw: Any,
) -> float
Calculates theta using a central difference formula.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use for the calculation. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
h
|
float
|
The absolute step size for shifting maturity, by default 1e-5. |
1e-05
|
Returns:
Type | Description |
---|---|
float
|
The calculated theta. |
Source code in src/optpricing/techniques/base/greek_mixin.py
vega ¶
vega(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
h: float = 0.0001,
**kw: Any,
) -> float
Calculates vega using a central difference formula.
Returns np.nan
if the model does not have a 'sigma' parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use for the calculation. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
h
|
float
|
The absolute step size for shifting volatility, by default 1e-4. |
0.0001
|
Returns:
Type | Description |
---|---|
float
|
The calculated vega. |
Source code in src/optpricing/techniques/base/greek_mixin.py
IVMixin ¶
Calculates Black-Scholes implied volatility for a given price using a root-finding algorithm.
This implementation uses Brent's method for speed and precision, with a fallback to a more robust Secant method if the initial search fails.
implied_volatility ¶
implied_volatility(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
target_price: float,
low: float = 1e-06,
high: float = 5.0,
tol: float = 1e-06,
**kwargs: Any,
) -> float
Calculates the implied volatility for a given option price.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The model to use for pricing. Note: IV is always calculated relative to the Black-Scholes-Merton model. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
target_price
|
float
|
The market price of the option for which to find the IV. |
required |
low
|
float
|
The lower bound for the volatility search, by default 1e-6. |
1e-06
|
high
|
float
|
The upper bound for the volatility search, by default 5.0. |
5.0
|
tol
|
float
|
The tolerance for the root-finding algorithm, by default 1e-6. |
1e-06
|
Returns:
Type | Description |
---|---|
float
|
The implied volatility, or |
Source code in src/optpricing/techniques/base/iv_mixin.py
IntegrationTechnique ¶
IntegrationTechnique(
*,
upper_bound: float = 200.0,
limit: int = 200,
epsabs: float = 1e-09,
epsrel: float = 1e-09,
)
Bases: BaseTechnique
, GreekMixin
, IVMixin
Prices options using the Gil-Pelaez inversion formula via numerical quadrature.
This technique leverages the model's characteristic function (CF) to price options. It is particularly useful for models where a closed-form solution is unavailable but the CF is known (e.g., Heston, Bates, VG, NIG).
It provides an analytic delta as a "free" byproduct of the pricing calculation.
Initializes the numerical integration solver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
upper_bound
|
float
|
The upper limit of the integration, by default 200.0. |
200.0
|
limit
|
int
|
The maximum number of sub-intervals for the integration, by default 200. |
200
|
epsabs
|
float
|
The absolute error tolerance for the integration, by default 1e-9. |
1e-09
|
epsrel
|
float
|
The relative error tolerance for the integration, by default 1e-9. |
1e-09
|
Source code in src/optpricing/techniques/integration.py
delta ¶
Returns the 'free' delta calculated during the pricing call.
If the cache is empty or the analytic delta calculation failed, it
falls back to the numerical finite difference method from GreekMixin
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use. Must support a characteristic function. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
float
|
Delta of the option. |
Source code in src/optpricing/techniques/integration.py
price ¶
price(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
**kwargs: Any,
) -> PricingResult
Calculates the option price and caches the 'free' analytic delta.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use. Must support a characteristic function. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
PricingResult
|
An object containing the calculated price. |
Source code in src/optpricing/techniques/integration.py
LeisenReimerTechnique ¶
Bases: LatticeTechnique
Leisen-Reimer binomial lattice technique with Peizer-Pratt inversion.
Source code in src/optpricing/techniques/base/lattice_technique.py
MonteCarloTechnique ¶
MonteCarloTechnique(
*,
n_paths: int = 20000,
n_steps: int = 100,
antithetic: bool = True,
seed: int | None = None,
)
Bases: BaseTechnique
, GreekMixin
, IVMixin
A universal Monte Carlo engine that dispatches to specialized, JIT-compiled kernels for different model types.
Initializes the Monte Carlo engine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_paths
|
int
|
The number of simulation paths, by default 20_000. |
20000
|
n_steps
|
int
|
The number of time steps in each path, by default 100. |
100
|
antithetic
|
bool
|
Whether to use antithetic variates for variance reduction, by default True. |
True
|
seed
|
int | None
|
Seed for the random number generator for reproducibility, by default None. |
None
|
Source code in src/optpricing/techniques/monte_carlo.py
price ¶
price(
option: Option,
stock: Stock,
model: BaseModel,
rate: Rate,
**kwargs: Any,
) -> PricingResult
Prices an option using the appropriate Monte Carlo simulation method.
This method acts as a dispatcher, selecting the correct simulation strategy (SDE path, pure Levy, or exact sampler) based on the capabilities of the provided model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use for the simulation. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
PricingResult
|
An object containing the calculated price. |
Source code in src/optpricing/techniques/monte_carlo.py
PDETechnique ¶
Bases: BaseTechnique
, GreekMixin
, IVMixin
Prices options by solving the Black-Scholes PDE with a Crank-Nicolson scheme.
This technique is optimized for the BSM model and calculates the price, delta, and gamma in a single pass by building a grid of asset prices and time steps.
Initializes the PDE solver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
S_max_mult
|
float
|
Multiplier for the initial spot price to set the maximum grid boundary, by default 3.0. |
3.0
|
M
|
int
|
Number of asset price steps (grid columns), by default 200. |
200
|
N
|
int
|
Number of time steps (grid rows), by default 200. |
200
|
Source code in src/optpricing/techniques/pde.py
delta ¶
Returns the cached delta from the PDE grid.
If the cache is empty, it first runs the pricing calculation.
Source code in src/optpricing/techniques/pde.py
gamma ¶
Returns the cached gamma from the PDE grid.
If the cache is empty, it first runs the pricing calculation.
Source code in src/optpricing/techniques/pde.py
price ¶
Calculates the option price and caches grid-based Greeks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
option
|
Option
|
The option contract to be priced. |
required |
stock
|
Stock
|
The underlying asset's properties. |
required |
model
|
BaseModel
|
The financial model to use. Must be a BSMModel. |
required |
rate
|
Rate
|
The risk-free rate structure. |
required |
Returns:
Type | Description |
---|---|
PricingResult
|
An object containing the calculated price. |
Source code in src/optpricing/techniques/pde.py
PricingResult
dataclass
¶
A container for the results of a pricing operation.
Attributes:
Name | Type | Description |
---|---|---|
price |
float
|
The calculated price of the instrument. |
greeks |
(dict[str, float], optional)
|
A dictionary containing calculated Greek values (e.g., 'delta', 'gamma'). |
implied_vol |
(float, optional)
|
The calculated Black-Scholes implied volatility. |
TOPMTechnique ¶
Bases: LatticeTechnique
Kamrad-Ritchken trinomial lattice technique.