scipy.special collects the named functions that appear again and again in formulas. The most useful for finance is the normal cumulative distribution Φ(x), available as special.ndtr. It is tied to the error function by Φ(x)=21(1+erf(x/2)). The Black–Scholes price of a call with spot S, strike K, rate r, volatility σ and time T uses it twice: C=SΦ(d1)−Ke−rTΦ(d2), where d1=σTln(S/K)+(r+σ2/2)T and d2=d1−σT. The sensitivity of the price to volatility is called vega, and it equals Sφ(d1)T, where φ is the normal density. Vega is what a Newton solver needs to find an implied volatility.
The other group of special functions is about numbers that are too big or too small. A factorial grows so fast that 171! already overflows a normal decimal number in a computer, but lnn! is easy: special.gammaln(n+1). special.comb(n, k) counts the paths with k up-moves in an n-step tree. special.logsumexp computes ln∑exi safely by pulling out the largest x, so that ln(e1000+e1000)=1000+ln2 does not overflow. special.expit(x)=1/(1+e^{-x}) turns any score into a probability between 0 and 1, as in a logistic model. The rule is: work in logs when a number can be huge or tiny, and exponentiate only at the end.
A call price and an implied-volatility stepTake S=K=100, r=0, σ=0.2 and T=1. Then d1=0.1 and d2=−0.1, and Φ(0.1)=0.5398, Φ(−0.1)=0.4602. So C=100⋅(0.5398−0.4602)=7.97. The normal density at 0.1 is 0.3970, so vega is 100⋅0.3970⋅1=39.70. If the market price is 9.00, the price is too low by 1.03, and one Newton step raises the volatility to 0.2+1.03/39.70=0.2261. For Φ(1) you can use erf(0.7071)=0.6827, so Φ(1)=21(1+0.6827)=0.8413. In the small tools, expit(2)=0.8808, ln4!=3.178, and (310)=120. On real data, SPY's daily return has a standard deviation of 1.196%. A fall of 5% is z=−4.21 standard deviations, and Φ(−4.21)=1.26⋅10−5, which a normal model says happens once in about 315 years. In the 5,403 days of data, it happened on 20 days.
Name the function: ndtr, erf, expit, gammaln, comb and logsumexp replace pages of hand-written maths.
Work in logs for extremes: use gammaln and logsumexp instead of a factorial or a sum of huge exponentials.
Vega drives the solver: a Newton step for implied volatility is the price gap divided by vega.
Lab · what's graded
The normal function from erf10 pts
A Black–Scholes call20 pts
Vega10 pts
An implied-volatility step15 pts
What each function is for15 pts
The safe way to add10 pts
Why compute factorials through logs20 pts
0/ 7 answered
1Numeric answer10 ptsNot answered
Given erf(0.7071)=0.6827, compute Φ(1)=21(1+erf(1/2)). Give four decimals.
p=accepted: ± 0.0005
2Numeric answer20 ptsNot answered
With S=K=100, r=0, σ=0.2, T=1 we get d1=0.1, d2=−0.1, Φ(0.1)=0.5398 and Φ(−0.1)=0.4602. What is C=SΦ(d1)−KΦ(d2)? Give two decimals.
c=accepted: ± 0.03
3Numeric answer10 ptsNot answered
The normal density at d1=0.1 is φ(0.1)=0.3970. With S=100 and T=1, what is vega =Sφ(d1)T? Give two decimals.
v=accepted: ± 0.1
4Numeric answer15 ptsNot answered
The market price is 9.00 and the model price at σ=0.2 is 7.9656, with vega 39.70. What is the new volatility σ+(market−model)/vega? Give four decimals.
s=accepted: ± 0.0005
5Match15 ptsNot answered
Match each scipy.special function with its job.
Pick the partner of each item on the left.
ndtr
expit
gammaln
logsumexp
comb
6Choice10 ptsNot answered
You need ln(e1000+e1002) on a computer. Which way is safe?
7Explain in words20 ptsNot answered
A binomial probability for a 200-step tree contains 200!. In one or two sentences, explain why you should compute it with gammaln and logs.
Math modeKeyboard or LaTeX inputTask 1 of 7 · Ctrl ⏎ to check