1+ import math
2+
3+ class BSM ():
4+
5+ def pdf (self , x ):
6+ return math .exp (- x ** 2 / 2 ) / math .sqrt (2 * math .pi )
7+
8+ def cdf (self , x ):
9+ return (1 + math .erf (x / math .sqrt (2 ))) / 2
10+
11+ def d1 (self , S , K , V , T ):
12+ return (math .log (S / float (K )) + (V ** 2 / 2 ) * T ) / (V * math .sqrt (T ))
13+
14+ def d2 (self , S , K , V , T ):
15+ return self .d1 (S , K , V , T ) - (V * math .sqrt (T ))
16+
17+ def theo (self , S , K , V , T , dT ):
18+ if dT == 'C' :
19+ return S * self .cdf (self .d1 (S , K , V , T )) - K * self .cdf (self .d2 (S , K , V , T ))
20+ else :
21+ return K * self .cdf (- self .d2 (S , K , V , T )) - S * self .cdf (- self .d1 (S , K , V , T ))
22+
23+ def delta (self , S , K , V , T , dT ):
24+ if dT == 'C' :
25+ delta = self .cdf (self .d1 (S , K , V , T ))
26+ elif dT == 'P' :
27+ delta = self .cdf (self .d1 (S , K , V , T )) - 1
28+ else :
29+ delta = 1
30+ return delta
31+
32+ def vega (self , S , K , V , T ):
33+ vega = (S * math .sqrt (T ) * self .pdf (self .d1 (S , K , V , T ))) / 100
34+ return vega
35+
36+ def theta (self , S , K , V , T ):
37+ theta = - ((S * V * self .pdf (self .d1 (S , K , V , T ))) / (2 * math .sqrt (T ))) / 365
38+ return theta
39+
40+ def gamma (self , S , K , V , T ):
41+ gamma = self .pdf (self .d1 (S , K , V , T ))/ (S * V * math .sqrt (T ))
42+ return gamma
0 commit comments