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, n_jobs: int | None = None, chunk_size: int = DEFAULT_CHUNK_SIZE, variance_scheme: str = 'qe')
Bases: BaseTechnique, GreekMixin, IVMixin
Prices American options by simulating full SDE paths and applying the Longstaff-Schwartz algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_paths
|
int
|
As for |
20000
|
n_steps
|
int
|
As for |
20000
|
antithetic
|
int
|
As for |
20000
|
seed
|
int
|
As for |
20000
|
variance_scheme
|
int
|
As for |
20000
|
lsm_degree
|
int
|
Polynomial degree of the Longstaff-Schwartz regression, by default 2. |
2
|
n_jobs
|
int | None
|
Maximum number of threads simulating path chunks concurrently; results do not depend on it. |
None
|
chunk_size
|
int
|
Paths per chunk and random stream, by default 8192. |
DEFAULT_CHUNK_SIZE
|
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
COSTechnique ¶
COSTechnique(*, width: float = 10.0, n_terms: int | None = None, tol: float = 1e-12, max_terms: int = 1 << 16)
Bases: BaseTechnique, GreekMixin, IVMixin
Prices European options with the COS method (Fang & Oosterlee 2008).
Works for every model with a characteristic function. The series converges exponentially for smooth densities and, unlike Gil-Pelaez quadrature on a fixed grid, stays accurate when the characteristic function decays slowly (strong Feller violation in Heston, Variance Gamma and CGMY at short maturities), because the number of terms adapts to the decay. The delta is a by-product of the pricing series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
float
|
Half-width |
10.0
|
n_terms
|
int
|
Fixed number of cosine terms. By default it is chosen per option
as the smallest power of two (at least 64) whose neglected tail is
below |
None
|
tol
|
float
|
Bound on the neglected tail relative to the strike, by default 1e-12. |
1e-12
|
max_terms
|
int
|
Cap on the adaptive number of terms, by default 2**16. |
1 << 16
|
Source code in src/optpricing/techniques/cos.py
delta ¶
Returns the delta computed with the price, pricing first if the inputs
differ from the last price call.
Source code in src/optpricing/techniques/cos.py
price ¶
Calculates the option price and caches the series 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/cos.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 134 135 136 | |
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.
Initializes the FFT solver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The exponent for the minimum 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.125 (0.25 left a 3e-5 discretisation error on ATM prices). |
0.125
|
alpha
|
float | None
|
The dampening parameter. If None, it is chosen per option by the
Lord & Kahl (2007) rule inside the strip where |
None
|
Source code in src/optpricing/techniques/fft.py
price ¶
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 = 1000.0, limit: int = 200, epsabs: float = 1e-09, epsrel: float = 1e-09, method: str = 'gauss', n_nodes: int = 24, adaptive: bool = True)
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 1000.0. Short maturities need the long tail: 200 left errors of 1e-5 (Heston, 10 days) to 5e-4 (NIG, T = 0.1). |
1000.0
|
limit
|
int
|
Maximum number of sub-intervals for the adaptive method, by default 200. |
200
|
epsabs
|
float
|
Absolute error tolerance of the adaptive method, by default 1e-9. |
1e-09
|
epsrel
|
float
|
Relative error tolerance of the adaptive method, by default 1e-9. |
1e-09
|
method
|
('gauss', 'quad')
|
|
"gauss"
|
n_nodes
|
int
|
Gauss-Legendre nodes per panel, by default 24. |
24
|
adaptive
|
bool
|
For |
True
|
Source code in src/optpricing/techniques/integration.py
delta ¶
Returns the 'free' delta calculated during the pricing call.
The delta of the last price call is reused if it was computed for the
same inputs; otherwise the option is priced first. If the analytic delta
is unavailable, it falls back to the 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
gauss_nodes
classmethod
¶
Composite Gauss-Legendre nodes and weights on geometric panels of
[0, upper_bound] (cached).
Source code in src/optpricing/techniques/integration.py
price ¶
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, n_jobs: int | None = None, chunk_size: int = DEFAULT_CHUNK_SIZE, variance_scheme: str = 'qe')
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
|
n_jobs
|
int | None
|
Maximum number of threads simulating path chunks concurrently. None
uses |
None
|
chunk_size
|
int
|
Paths per chunk (and per random stream), by default 8192. Results depend on the seed and on this value only. |
DEFAULT_CHUNK_SIZE
|
variance_scheme
|
('qe', 'euler')
|
Discretisation of the Heston/Bates variance process: Andersen's Quadratic-Exponential scheme with martingale correction (default, accurate with ~50 steps even when the Feller condition fails) or full-truncation Euler. |
"qe"
|
Source code in src/optpricing/techniques/monte_carlo.py
price ¶
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. The grid spacing is chosen so that the spot lies exactly on a node. |
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
|
rannacher_steps
|
int
|
Number of initial Crank-Nicolson steps replaced by pairs of fully implicit half-steps (Rannacher 1984 start-up). This damps the oscillations the non-smooth payoff otherwise induces in gamma. Default 2; set 0 for plain Crank-Nicolson. |
2
|
Source code in src/optpricing/techniques/pde.py
delta ¶
Returns the delta from the PDE grid.
The grid of the last price call is reused if it was built for the
same inputs; otherwise the option is priced first.
Source code in src/optpricing/techniques/pde.py
gamma ¶
Returns the gamma from the PDE grid.
The grid of the last price call is reused if it was built for the
same inputs; otherwise the option is priced first.
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.