Analytical and semi-analytical pricing for European vanilla and barrier options. Pure Python, no black-box dependencies — every formula derives directly from the mathematics.
Closed-form pricing, first-order Greeks, and implied volatility inversion for European calls and puts.
| Output | Formula |
|---|---|
| Price | |
| Delta | |
| Gamma | |
| Vega | |
| Theta |
where
Implied volatility is recovered via Brent's method with arbitrage-bound checks.
import black_scholes as bs
result = bs.greeks(S=100, K=105, T=0.5, r=0.05, sigma=0.20, option_type="call")
# BSResult(price=5.14, delta=0.398, gamma=0.019, vega=0.271, theta=-0.017, rho=0.089)
iv = bs.implied_vol(market_price=5.14, S=100, K=105, T=0.5, r=0.05)
# 0.19999...Semi-analytical pricing via Gil-Pelaez Fourier inversion of the Heston (1993) characteristic function.
The characteristic function is evaluated using the Albrecher et al. (2007) formulation to avoid branch-cut discontinuities. The option price is:
Calibration to a strip of market prices via L-BFGS-B (minimises sum of squared relative errors).
from heston import HestonParams, price, calibrate
params = HestonParams(v0=0.04, kappa=2.0, theta=0.04, sigma_v=0.30, rho=-0.70)
c = price(S=100, K=105, T=0.5, r=0.05, params=params)
fitted = calibrate(market_prices, strikes, S=100, T=0.5, r=0.05)All eight standard single-barrier types under Black-Scholes dynamics (Reiner & Rubinstein, 1991).
| Barrier | Call | Put |
|---|---|---|
| Down-and-Out | ✓ | ✓ |
| Down-and-In | ✓ | ✓ |
| Up-and-Out | ✓ | ✓ |
| Up-and-In | ✓ | ✓ |
In-Out parity check_in_out_parity.
Optional cash rebate on knock-out.
import barrier_options as barrier
p = barrier.price(S=100, K=100, H=90, T=0.5, r=0.05, sigma=0.20,
barrier_type="down-and-out", option_type="call")
assert barrier.check_in_out_parity(S=100, K=100, H=90, T=0.5, r=0.05, sigma=0.20)pip install -r requirements.txt
python examples/demo.py- Black, F. & Scholes, M. (1973). The Pricing of Options and Corporate Liabilities. Journal of Political Economy.
- Heston, S. L. (1993). A Closed-Form Solution for Options with Stochastic Volatility. Review of Financial Studies.
- Reiner, E. & Rubinstein, M. (1991). Breaking Down the Barriers. Risk Magazine.
- Albrecher, H., Mayer, P., Schachermayer, W. & Teichmann, J. (2007). The Little Heston Trap. Wilmott Magazine.