σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
QuantLib skill›Module 2 · Rates, bonds and options›Topic 2.3·~50 minIntermediate

Vanilla options with analytic engines

Skill positionModule 2 of 5 · Topic 3 of 3
2.3 Vanilla options with analytic engines
—Lab score— / 100Total XP—Streak—
Lecture0% read

9 min readCode examples: 5

  1. 01The Black-Scholes-Merton formula
  2. 02Put-call parity
  3. 03The Greeks
  4. 04From a price to a volatility
  5. 05Options on SPY on three days
  6. 06What you will build

A one-year at-the-money call on SPY, priced on 21 August 2026 with last year's volatility of 12.9%, costs 49.31 dollars, and its delta is 0.60: if SPY rises by one dollar the call gains about 60 cents. Feed that price back into the formula and you get the same 12.9% volatility again. Formulas for the price, the Greeks and the implied volatility make one family, and in QuantLib that family is an analytic engine. This topic writes it as plain functions.

01

The Black-Scholes-Merton formula

Where we are. The bond of the last topic was priced by a function of its dates, coupon and yield. An option is priced the same way, by a function of its inputs, and for the simplest options the formula is exact. A call gives the right to buy a share at the strike on the expiry date; a put gives the right to sell. These are vanilla options. The Black-Scholes-Merton model assumes that the share moves randomly with a constant volatility, that money earns a constant interest rate, and that the share pays a continuous dividend yield.

The formula needs two numbers, d1 and d2. Read d2 as a kind of z-score of the share finishing above the strike, and the price as the discounted average of what the option pays. The share enters reduced by the dividend yield, because a buyer of the option does not receive the dividends, and the strike enters discounted at the interest rate.

call = S e^(−qT) N(d1) − K e^(−rT) N(d2), put = K e^(−rT) N(−d2) − S e^(−qT) N(−d1), d1 = ( ln(S/K) + (r − q + σ²/2) T ) ÷ ( σ √T ), d2 = d1 − σ √T(1)S is the share price, K the strike, r the interest rate, q the dividend yield, σ the volatility, T the time to expiry in years and N the standard normal distribution function

Worked example 1. *Problem:* price a one-year at-the-money option (spot 100, strike 100) with a 5% rate, a 2% dividend yield and a 20% volatility, as a call and as a put. Then see what changes with no dividend, with a 4% dividend, and with a volatility of 40%. *Plan:* use d1_d2 and bsm_price. *Steps:* below.

main.pyPython
print([round(v, 4) for v in d1_d2(100.0, 100.0, 0.05, 0.02, 0.2, 1.0)])1print(round(bsm_price(100.0, 100.0, 0.05, 0.02, 0.2, 1.0, "call"), 4), round(bsm_price(100.0, 100.0, 0.05, 0.02, 0.2, 1.0, "put"), 4))2print(round(bsm_price(100.0, 100.0, 0.05, 0.0, 0.2, 1.0, "call"), 4), round(bsm_price(100.0, 100.0, 0.05, 0.04, 0.2, 1.0, "call"), 4), round(bsm_price(100.0, 100.0, 0.05, 0.04, 0.2, 1.0, "put"), 4))3print(round(bsm_price(100.0, 100.0, 0.05, 0.02, 0.4, 1.0, "call"), 4))4
[0.25, 0.05]
9.227 6.3301
10.4506 8.1026 7.1466
16.7994
  1. 1d1 and d2.
  2. 2Call and put.
  3. 3No dividend, then a 4% dividend.
  4. 4Double the volatility.

*Check:* d1 is 0.25 and d2 is 0.05. The call is worth 9.227 and the put 6.3301. With no dividend the call is 10.4506, the plain Black-Scholes price, and with a 4% dividend it is 8.1026, while the put rises from 6.3301 to 7.1466. With a volatility of 40% the call costs 16.7994. *What to notice:* a dividend makes the call cheaper and the put dearer, because the share is expected to fall by the dividend; and volatility makes both dearer, because the option pays only on one side.

02

Put-call parity

A call and a put with the same strike and expiry are tied together by a no-arbitrage relation, put-call parity. Buy a call and sell a put: you receive the share at the strike in every case, whatever happens, so the difference of the prices is the price of that forward position, the share reduced by its dividends minus the discounted strike. The relation does not depend on the volatility or on the model, which makes it a check for any pricing code: if it fails, there is a bug.

call − put = S e^(−qT) − K e^(−rT)(2)parity: the difference of a call and a put is the value of buying the share at the strike at expiry

Worked example 2. *Problem:* check the parity of the prices of example 1, then of the same pair with the call raised by 0.5. *Plan:* use parity_gap. *Steps:* below.

main.pyPython
call = bsm_price(100.0, 100.0, 0.05, 0.02, 0.2, 1.0, "call")put = bsm_price(100.0, 100.0, 0.05, 0.02, 0.2, 1.0, "put")print(round(parity_gap(call, put, 100.0, 100.0, 0.05, 0.02, 1.0), 10), round(parity_gap(call + 0.5, put, 100.0, 100.0, 0.05, 0.02, 1.0), 6))1print(round(call - put, 4), round(100.0 * math.exp(-0.02) - 100.0 * math.exp(-0.05), 4))2
0.0 0.5
2.8969 2.8969
  1. 1Zero for the prices of one model; the shift shows in full.
  2. 2Call minus put is the forward minus the discounted strike.

*Check:* the gap is 0.0 for the consistent pair and 0.5 for the shifted one. The call minus the put is 2.8969, and so is 100 × e^(−0.02) − 100 × e^(−0.05). *What to notice:* parity holds for any volatility; this is why a trader can read a put price from a call price, and why a pricing library tests it on every new engine.

03

The Greeks

A trader wants to know not only the price but how it changes when something moves. The answers are the Greeks, the derivatives of the price. Delta is the change for a one-unit move of the share. Gamma is the change of the delta, the curvature. Vega is the change for a move of the volatility (here for a move of 1.00, so divide by 100 for one point). Theta is the change as time passes (here per year). Rho is the change for a move of the interest rate (here for 1.00). All five have closed forms in the Black-Scholes-Merton model.

Worked example 3. *Problem:* find the five Greeks of the call and of the put of example 1. Then, for calls with strikes of 80, 100 and 120, find the price, the delta, the gamma and the vega. Finally raise the volatility from 20% to 21% and compare the change of the price with the vega. *Plan:* use bsm_greeks. *Steps:* below.

main.pyPython
for kind in ("call", "put"):    print(kind, {name: round(value, 4) for name, value in bsm_greeks(100.0, 100.0, 0.05, 0.02, 0.2, 1.0, kind).items()})1for strike in (80.0, 100.0, 120.0):    greeks = bsm_greeks(100.0, strike, 0.05, 0.02, 0.2, 1.0, "call")    print(strike, round(bsm_price(100.0, strike, 0.05, 0.02, 0.2, 1.0, "call"), 4), round(greeks["delta"], 4), round(greeks["gamma"], 5), round(greeks["vega"], 3))2print(round(bsm_price(100.0, 100.0, 0.05, 0.02, 0.21, 1.0) - bsm_price(100.0, 100.0, 0.05, 0.02, 0.2, 1.0), 4))3
call {'delta': 0.5869, 'gamma': 0.019, 'vega': 37.9012, 'theta': -5.0893, 'rho': 49.4581}
put {'delta': -0.3933, 'gamma': 0.019, 'vega': 37.9012, 'theta': -2.2936, 'rho': -45.6648}
80.0 22.7641 0.8959 0.00769 15.389
100.0 9.227 0.5869 0.01895 37.901
120.0 2.7118 0.2491 0.01571 31.418
0.3791
  1. 1Five numbers for each option.
  2. 2Price, delta, gamma and vega at three strikes.
  3. 3One point of volatility, compared with the vega.

*Check:* the call has a delta of 0.5869, a gamma of 0.019, a vega of 37.9012, a theta of -5.0893 and a rho of 49.4581; the put has a delta of -0.3933, a theta of -2.2936 and a rho of -45.6648, with the same gamma and vega. By strike, the call prices are 22.7641, 9.227 and 2.7118, the deltas 0.8959, 0.5869 and 0.2491, the gammas 0.00769, 0.01895 and 0.01571, and the vegas 15.389, 37.901 and 31.418. A rise of the volatility from 20% to 21% adds 0.3791 to the price, and the vega divided by 100 is 0.379. *What to notice:* gamma and vega are the largest near the money; the call delta goes from near 1 (deep in the money) to near 0 (far out of the money); and the vega is a good estimate of a small change of the price.

04

From a price to a volatility

In practice the volatility is not known; the price is. The implied volatility is the volatility that, put in the formula, gives the quoted price. Like the yield of a bond, it has no formula, but the price rises with the volatility, so bisection finds it: if the price at the middle of the interval is above the quote, the volatility must be lower. A historical volatility is the other way to get a number: it is the standard deviation of the daily log returns of the share over the last year, multiplied by √252 to turn a daily figure into an annual one.

Worked example 4. *Problem:* find the implied volatility of a quoted call price of 10.0 on the option of example 1, and recover the volatility of 45% from the price of a six-month put at strike 110 with a 3% rate and no dividend. *Plan:* use implied_vol. *Steps:* below.

main.pyPython
print(round(implied_vol(10.0, 100.0, 100.0, 0.05, 0.02, 1.0), 6))1price = bsm_price(100.0, 110.0, 0.03, 0.0, 0.45, 0.5, "put")print(round(price, 4), round(implied_vol(price, 100.0, 110.0, 0.03, 0.0, 0.5, "put"), 6))2
0.220385
17.7641 0.45
  1. 1A quote of 10.0 is above the 9.227 of a 20% volatility.
  2. 2The volatility that made the price comes back.

*Check:* a call quoted at 10.0 implies a volatility of 0.220385, higher than the 0.2 that gives 9.227. The six-month put at strike 110 is worth 17.7641 at a volatility of 45%, and implied_vol returns 0.45. *What to notice:* the round trip is the best test of the two functions together; and a market quote tells you what volatility the market is using, not what the share will do.

05

Options on SPY on three days

Worked example 5. *Problem:* price a one-year at-the-money option on SPY on three days, with the volatility of the previous year, the 1-year Treasury yield and a dividend yield of 1.3%. For each day find the call and the put, the five Greeks of the call, the implied volatility of the call price, the parity gap, and the delta by a finite difference with a bump of 1% of the spot. *Plan:* study combines the functions. *Steps:* below.

main.pyPython
out = study(load_data())for date, row in out.items():    print(date, round(row["vol"], 4), round(row["call"], 3), round(row["put"], 3), round(row["delta"], 4), round(row["vega"], 2), round(row["theta"], 2), round(row["iv_back"], 4), round(row["delta_fd"], 5))1
2022-01-03 0.1302 21.482 25.549 0.492 179.49 -9.58 0.1302 0.49191
2023-07-03 0.1932 41.658 24.343 0.6138 162.7 -24.45 0.1932 0.61374
2026-08-21 0.1287 49.305 28.95 0.601 290.23 -29.26 0.1287 0.60087
  1. 1One row per day.

*Check:* the historical volatility is 13.0% on 3 January 2022, 19.3% on 3 July 2023 and 12.9% on 21 August 2026. The at-the-money call costs 21.482, 41.658 and 49.305, with deltas of 0.4920, 0.6138 and 0.6010; the 1-year yield was close to zero in 2022 and 5.43% in 2023, which explains why the put costs more than the call in 2022 and less in 2023. The implied volatilities come back to the historical ones (0.1302, 0.1932 and 0.1287), the parity gaps are 0, and the finite-difference delta of 21 August 2026 is 0.60087, against 0.60102 for the formula. *What to notice:* the finite difference is not exactly the delta, because the bump is large and the price is curved; a smaller bump would be closer.

Historical volatility is not implied volatility

The volatility of last year is a guess for the next year, and the options market can disagree with it by a wide margin. Here the historical volatility is only a convenient input, to turn real SPY prices into option prices. The market's own figure is the implied volatility, read from real option quotes; the data of this course has none.

  • FunctionQuestion it answers
  • d1_d2(spot, strike, rate, div, vol, t)What are the two arguments of the formula?the inputs of every price
  • bsm_price(spot, strike, rate, div, vol, t, kind)What is a European option worth?uses d1_d2QuantLib: AnalyticEuropeanEngine
  • bsm_greeks(spot, strike, rate, div, vol, t, kind)How does the price move?uses d1_d2QuantLib: option.delta(), gamma(), vega()
  • parity_gap(call, put, spot, strike, rate, div, t)Are the call and the put consistent?a check, not a price
  • implied_vol(price, spot, strike, rate, div, t, kind)Which volatility gives this quote?uses bsm_priceQuantLib: option.impliedVolatility
  • historical_vol(closes, window)How volatile was the share?from the log returns
What the real data saysOne-year at-the-money options on SPY, priced with the volatility of the previous year and the 1-year Treasury yield
Historical volatility, 21 Aug 202612.9%19.3% on 3 July 2023
Call price, 21 Aug 202649.31put 28.95; parity gap 0
Delta of the call, 21 Aug 20260.601finite difference 0.6009
Vega of the call, 21 Aug 2026290.2162.7 on 3 July 2023
Theta of the call, 21 Aug 2026−29.3a yearthe option loses about 0.08 a day

Key takeaways

  • An analytic engine is a formula. For vanilla options the Black-Scholes-Merton model gives the price and all the Greeks in closed form; the share is reduced by the dividend yield, the strike discounted by the rate.
  • Parity is a free test. A call minus a put must equal the share (reduced by dividends) minus the discounted strike, for any volatility.
  • Greeks are derivatives. Delta, gamma, vega, theta and rho can each be checked against a finite difference of the price.
  • A volatility can be read from a price. The price rises with the volatility, so bisection finds the implied volatility; a historical volatility comes from the returns instead.
Quick check
Your turn. A call costs 12.00 and the same-strike put costs 7.00 on a share paying no dividends, with a discounted strike of 95. What is the share price implied by put-call parity?
Pick an answer.
06

What you will build

The lab follows the lecture. Task 1 is the pair d1 and d2, and task 2 the price of a call or a put with a dividend yield. Task 3 is the five Greeks and task 4 the put-call parity gap. Task 5 finds the implied volatility of a quote by bisection, and task 6 the historical volatility of a price history. Task 7 prices at-the-money options on SPY on three real days.

Next topic: Yield term structures and bootstrapping. Look at the code you now have: the bond function takes a yield and the dates, the option function takes a rate, a dividend yield and a volatility. Each one repeats the same work of discounting by hand, and each time you want a different rate you must change the arguments of every call. A single rate is also not a curve. The next topic builds a curve of discount factors from market quotes, so that every price can ask it for the discount factor of any date; the topic after that gives this repeated pricing code a common design.

Lab · what's graded

7 tasks · 100 pts
  • d1 and d28 pts
  • The option price14 pts
  • The Greeks22 pts
  • Put-call parity6 pts
  • Implied volatility16 pts
  • Historical volatility10 pts
  • Options on SPY24 pts
Explorer · lab-sqt-05
▾ lab-sqt-05
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sqt-05$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sqt-05 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes