This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Option pricing (vp.finance)

Option pricing.

  • Black-Scholes prices, Greeks by reverse-mode differentiation, and implied volatility
  • Cox-Ross-Rubinstein binomial trees
  • Heston prices by Fourier-cosine series
  • American puts by Crank-Nicolson

Each is checked against a published value or an independent method.

Part of the distribution vipyr; it runs from a checkout of the repository. Planned on PyPI as pip install vipyr.

Functions

vp.finance.bs_greeks
Black-Scholes delta, gamma and vega of one option. Delta and vega are `grad` of bs_price; gamma is vega / (S^2 sigma T), which holds for Black-Scholes calls and puts alike.
vp.finance.bs_price
The Black-Scholes price of a European option on a stock with continuous dividend yield q. An in-the-money option is priced as the out-of-the-money one plus the discounted forward intrinsic value (put-call parity).
vp.finance.cn_american_put
The American put by Crank-Nicolson on M space intervals over [0, Smax] and Nt time steps, of which the first `implicit` are fully implicit (two half-size steps each).
vp.finance.crr_price
The CRR value with N steps: u = exp(sigma sqrt(dt)), d = 1/u, the risk-neutral up probability p = (exp((r - q) dt) - d) / (u - d), values rolled back from maturity; an American option takes the larger of continuing and exercising at every node.
vp.finance.heston_cos
Heston prices of European options on one stock for an array of strikes, with N cosine terms and the truncation half-width L standard deviations. The put comes from the series (its payoff is bounded) and the call from put-call parity, as Fang and Oosterlee recommend. is_call: 1 call, 0 put.
vp.finance.implied_vol
The implied volatility of one quote, or NaN when the price is outside the no-arbitrage bounds. Newton steps on log(otm(s)) - log(target) in s = sigma sqrt(T), kept inside a bracket [lo, hi] that every evaluation shrinks; a step that leaves it is replaced by bisection (or doubling while hi is unknown). Stops when a step is below 4e-16 of s.

Examples in the gallery