-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSM.py
733 lines (559 loc) · 20 KB
/
BSM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# Black Scholes Merton for Python (from Excel VBA)
# George Fisher MIT Spring 2012
#
# I created BSM routines for Excel first and then converted to Python
# I drew upon published work by Back, Benninga, Hull and Wilmott but the majority is my own original work
#
# d1, d2
# N (en/phi) std normal CDF
# N' (nprime) std normal PDF
#
# Binary Options
# Euro Call & Put
# Greeks
# Implied Volatility
# Put/Call Parity
# American_Call_Dividend
# American_Put_Binomial
#
# Also includes
# risk-neutral prob
# forward prices & rates
# CAGR
# randn/randn_ssdt
# convert discrete to continuous interest rate
# Interest is
# risk-free rate
# domestic risk-free rate for currencies
# Yield is
# dividend yield for stock
# lease rate for commodities
# foreign currency risk-free rate for currencies
# Sigma is the standard deviation of the underlying asset
# Time is a year fraction: for 3-months ... Time = 3/12
# Stock is S_0
# Exercise is K
# => Interest, Yield, Sigma, Time are all annual numbers
# => Time = 0 is the value at maturity
# most of the functions accomodate this
# for some, it's infinity or otherwise meaningless
# => Sigma = 0 is also accomodated in most functions
##
## Utilities
## ---------
##
# N: the standard-normal CDF
def en(x):
return phi(x)
def phi(x):
import math
# constants
a1 = 0.254829592
a2 = -0.284496736
a3 = 1.421413741
a4 = -1.453152027
a5 = 1.061405429
p = 0.3275911
# Save the sign of x
sign = 1
if x < 0:
sign = -1
x = abs(x)/math.sqrt(2.0)
# A&S formula 7.1.26
t = 1.0/(1.0 + p*x)
y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x)
return 0.5*(1.0 + sign*y)
# N': the first derivative of N(x) ... the standard normal PDF
def nprime(x):
import math
return math.exp(-0.5 * x * x) / math.sqrt(2 * 3.1415926)
# Random Normal (epsilon)
def RandN():
# produces a standard normal random variable epsilon
import random
return random.gauss(0,1)
def RandN_ssdt(ssdt):
# produces a standard normal random variable epsilon times sigma*sqrt(deltaT)
import random
return random.gauss(0,ssdt)
# binomial tree risk-neutral probability (Hull 7th edition Ch 19 P 409)
def RiskNeutralProb(Interest, Yield, sigma, deltaT):
import math
u = math.exp( sigma * math.sqrt(deltaT))
d = math.exp(-sigma * math.sqrt(deltaT))
a = math.exp((Interest - Yield) * deltaT)
numerator = a - d
denominator = u - d
return numerator / denominator
# Call & Put prices derived from put-call parity
# ---------------
def CallParity(Stock, Exercise, Time, Interest, Yield, Put_price):
import math
return Put_price + Stock * math.exp(-Yield * Time) - Exercise * math.exp(-Interest * Time)
def PutParity(Stock, Exercise, Time, Interest, Yield, Call_price):
import math
return Call_price + Exercise * math.exp(-Interest * Time) - Stock * math.exp(-Yield * Time)
# forward price
def ForwardPrice(Spot, Time, Interest, Yield):
import math
return Spot * math.exp((Interest - Yield) * Time)
# forward rate from Time1 to Time2 (discrete compounding)
def ForwardRate(SpotInterest1, Time1, SpotInterest2, Time2):
numerator = (1 + SpotInterest2) ** Time2
denominator = (1 + SpotInterest1) ** Time1
return ((numerator / denominator) ** (1 / (Time2 - Time1))) - 1
# CAGR
def CAGRd(Starting_value, Ending_Value, Number_of_years):
# discrete CAGR
return ((Ending_Value / Starting_value) ** (1 / Number_of_years)) - 1
# Convert TO continuous compounding FROM discrete
def r_continuous(r_discrete, compounding_periods_per_year):
import math
m = compounding_periods_per_year
return m * math.log(1 + r_discrete / m)
# Convert TO discrete compounding FROM continuous
#
# t_discrete = m * (exp(r_continuous / m) - 1)
#
# where m is the number of compounding periods per year
#
def r_discrete(r_continuous, compounding_periods_per_year):
import math
m = compounding_periods_per_year
return m * (math.exp(r_continuous / m) - 1)
# --------------------------------------------------------------------------------
##
## Black Scholes
## -------------
##
def dOne(Stock, Exercise, Time, Interest, Yield, sigma):
import math
return (math.log(Stock / Exercise) + (Interest - Yield + 0.5 * sigma * sigma) * Time) / (sigma * math.sqrt(Time))
def dTwo(Stock, Exercise, Time, Interest, Yield, sigma):
import math
return (math.log(Stock / Exercise) + (Interest - Yield - 0.5 * sigma * sigma) * Time) / (sigma * math.sqrt(Time))
#
# Binary Options
#
# Digital: Cash or Nothing
def CashCall(Stock, Exercise, Time, Interest, Yield, sigma):
import math
if Time < 0.000000005:
if Stock >= Exercise:
return 1
else:
return 0
if sigma == 0:
sigma = 0.0000000001
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
Nd2 = phi(d2_)
return math.exp(-Interest * Time) * Nd2
def CashPut(Stock, Exercise, Time, Interest, Yield, sigma):
if Time < 0.000000005:
if Stock >= Exercise:
return 0
else:
return 1
if sigma == 0:
sigma = 0.0000000001
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
Nminusd2 = phi(-d2_)
return math.exp(-Interest * Time) * Nminusd2
# Asset or Nothing
def AssetCall(Stock, Exercise, Time, Interest, Yield, sigma):
import math
if Time < 0.000000005:
if Stock >= Exercise:
return Stock
else:
return 0
if sigma == 0:
sigma = 0.0000000001
if Exercise < 0.000000005:
Nd1 = 1
else:
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
Nd1 = phi(d1_)
return Stock * math.exp(-Yield * Time) * Nd1
def AssetPut(Stock, Exercise, Time, Interest, Yield, sigma):
import math
if Time < 0.000000005:
if Stock >= Exercise:
return 0
else:
return Stock
if sigma == 0:
sigma = 0.0000000001
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
Nminusd1 = phi(-d1_)
return Stock * math.exp(-Yield * Time) * Nminusd1
#
# European Call and Put
# ---------------------
#
def EuroCall(Stock, Exercise, Time, Interest, Yield, sigma):
import math
if Time == 0:
return max(0, Stock - Exercise)
if sigma == 0:
return max(0, math.exp(-Yield * Time) * Stock - math.exp(-Interest * Time) * Exercise)
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
return Stock * math.exp(-Yield * Time) * phi(d1_) - Exercise * math.exp(-Interest * Time) * phi(d2_)
def EuroPut(Stock, Exercise, Time, Interest, Yield, sigma):
import math
if Time == 0:
return max(0, Exercise - Stock)
if sigma == 0:
return max(0, math.exp(-Interest * Time) * Exercise - math.exp(-Yield * Time) * Stock)
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
return Exercise * math.exp(-Interest * Time) * phi(-d2_) - Stock * math.exp(-Yield * Time) * phi(-d1_)
#
# American Put
# ------------
# Per Kerry Back Chapt5.bas
#
def American_Put_Binomial(S0, K, r, sigma, q, T, N):
import math
"""
#
# Inputs are S0 = initial stock price
# K = strike price
# r = risk-free rate
# sigma = volatility
# q = dividend yield
# T = time to maturity
# N = number of time periods
#
"""
dt = T / N # length of time period
u = math.exp(sigma * math.sqrt(dt)) # size of up step
d = 1 / u # size of down step
pu = (math.exp((r - q) * dt) - d) / (u - d) # probability of up step
dpu = math.exp(-r * dt) * pu # one-period discount x prob of up step
dpd = math.exp(-r * dt) * (1 - pu) # one-period discount x prob of down step
u2 = u * u
S = S0 * d ** N # stock price at bottom node at last date
PutV[0] = max(K - S, 0) # put value at bottom node at last date
for j in range(1,N+1):
S = S * u2
PutV[j] = max(K - S, 0)
for i in range(N - 1, 0, -1): # back up in time to date 0
S = S0 * d ** i # stock price at bottom node at date i
PutV[0] = max(K - S, dpd * PutV(0) + dpu * PutV(1))
for j in range(1,i+1): # step up over nodes at date i
S = S * u2
PutV[j] = max(K - S, dpd * PutV(j) + dpu * PutV(j + 1))
return PutV[0] # put value at bottom node at date 0
#
# Greeks from Hull (Edition 7) Chapter 17 p378
# --------------------------------------------
#
# per the Black Scholes PDE for a portfolio of options
# on a single non-dividend-paying underlying stock
#
# Theta + Delta * S * r + Gamma * 0.5 * sigma**2 * S**2 = r * Portfolio_Value
# Per Hull: for large option portfolios, usually created by banks in the
# course of buying and selling OTC options to clients, the portfolio is
# Delta hedged every day and Gamma/Vega hedged as needed
#
# Delta Gamma Vega
# Portfolio P_delta P_gamma P_vega
# Option1 w1*1_delta w1*1_gamma w1*1_vega
# Option2 w2*2_delta w2*2_gamma w2*2_vega
#
# Set the columns equal to zero and solve the simultaneous equations
# Most OTC options are sold close to the money; high gamma and vega
# as (if) the price of the underlying move away gamma and vega decline
# Delta
# -----
#
# If a bank sells a call to a client (short a call)
# ... it hedges itself with a synthetic long call
#
# Synthetic long call = Delta * Stock_price - bond
# ie., borrow the money to buy Delta shares of the stock
#
def DeltaCall(Stock, Exercise, Time, Interest, Yield, sigma):
import math
if Time == 0:
if Stock > Exercise:
return 1
else:
return 0
if sigma == 0:
sigma = 0.0000000001
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
return math.exp(-Yield * Time) * phi(d1_)
def DeltaPut(Stock, Exercise, Time, Interest, Yield, sigma):
import math
if Time == 0:
if Stock < Exercise:
return -1
else:
return 0
if sigma == 0:
sigma = 0.0000000001
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
return math.exp(-Yield * Time) * (phi(d1_) - 1)
#
# Gamma the convexity
# -----
#
def OptionGamma(Stock, Exercise, Time, Interest, Yield, sigma):
If sigma = 0 Then
sigma = 0.0000000001
End If
Dim d1_
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
OptionGamma = nprime(d1_) * math.exp(-Yield * Time) _
/ (Stock * sigma * math.sqrt(Time))
#
# Theta the decay in the value of an option/portfolio of options as time passes
# -----
#
# divide by 365 for "per calendar day"; 252 for "per trading day"
#
# In a delta-neutral portfolio, Theta is a proxy for Gamma
#
def ThetaCall(Stock, Exercise, Time, Interest, Yield, sigma):
If sigma = 0 Then
sigma = 0.0000000001
End If
Dim d1_
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
Dim d2_
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
Dim Nd1_
Nd1_ = phi(d1_)
Dim Nd2_
Nd2_ = phi(d2_)
ThetaCall = -Stock * nprime(d1_) * sigma * math.exp(-Yield * Time) / (2 * math.sqrt(Time)) _
+ Yield * Stock * Nd1_ * math.exp(-Yield * Time) _
- Interest * Exercise * math.exp(-Interest * Time) * Nd2_
def ThetaPut(Stock, Exercise, Time, Interest, Yield, sigma):
If sigma = 0 Then
sigma = 0.0000000001
End If
Dim d1_
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
Dim d2_
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
Dim Nminusd1_
Nminusd1_ = phi(-d1_)
Dim Nminusd2_
Nminusd2_ = phi(-d2_)
ThetaPut = -Stock * nprime(d1_) * sigma * math.exp(-Yield * Time) / (2 * math.sqrt(Time)) _
- Yield * Stock * Nminusd1_ * math.exp(-Yield * Time) _
+ Interest * Exercise * math.exp(-Interest * Time) * Nminusd2_
#
# Vega the sensitivity to changes in the volatility of the underlying
# ----
#
def Vega(Stock, Exercise, Time, Interest, Yield, sigma):
If sigma = 0 Then
sigma = 0.0000000001
End If
Dim d1_
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
Vega = Stock * math.sqrt(Time) * nprime(d1_) * math.exp(-Yield * Time)
#
# Rho the sensitivity to changes in the interest rate
# ---
#
#
# Note the various Rho calculations see Hull 7th Edition Ch 17 P378
#
def RhoFuturesCall(Stock, Exercise, Time, Interest, Yield, sigma):
RhoFuturesCall = -EuroCall(Stock, Exercise, Time, Interest, Yield, sigma) * Time
def RhoFuturesPut(Stock, Exercise, Time, Interest, Yield, sigma):
RhoFuturesPut = -EuroPut(Stock, Exercise, Time, Interest, Yield, sigma) * Time
#
# The Rho corresponding to the domestic interest rate is RhoCall/Put, below
# foreign interest rate is RhoFXCall/Put, shown here
#
def RhoFXCall(Stock, Exercise, Time, Interest, Yield, sigma):
Dim d1_
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
Dim Nd1_
Nd1_ = phi(d1_)
RhoFXCall = -Time * math.exp(-Yield * Time) * Stock * Nd1_
def RhoFXPut(Stock, Exercise, Time, Interest, Yield, sigma):
Dim d1_
d1_ = dOne(Stock, Exercise, Time, Interest, Yield, sigma)
Dim Nminusd1_
Nminusd1_ = phi(-d1_)
RhoFXPut = Time * math.exp(-Yield * Time) * Stock * Nminusd1_
#
# "Standard" Rhos
#
def RhoCall(Stock, Exercise, Time, Interest, Yield, sigma):
If sigma = 0 Then
sigma = 0.0000000001
End If
Dim d2_
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
Dim Nd2_
Nd2_ = phi(d2_)
RhoCall = Exercise * Time * math.exp(-Interest * Time) * Nd2_
def RhoPut(Stock, Exercise, Time, Interest, Yield, sigma):
If sigma = 0 Then
sigma = 0.0000000001
End If
Dim d2_
d2_ = dTwo(Stock, Exercise, Time, Interest, Yield, sigma)
Dim Nminusd2_
Nminusd2_ = phi(-d2_)
RhoPut = -Exercise * Time * math.exp(-Interest * Time) * Nminusd2_
#
# Since Bennigna and Back produce identical numbers
# and MATLAB produced numbers that are +/- 2%, I'm
# inclined to go with these numbers
#
#
# Implied Volatility from Benningna
# ---------------------------------
#
def EuroCallVol(Stock, Exercise, Time, Interest, Yield, Call_price):
Dim High, Low
High = 2
Low = 0
Do While (High - Low) > 0.000001
If EuroCall(Stock, Exercise, Time, Interest, Yield, (High + Low) / 2) > _
Call_price Then
High = (High + Low) / 2
Else: Low = (High + Low) / 2
End If
Loop
EuroCallVol = (High + Low) / 2
def EuroPutVol(Stock, Exercise, Time, Interest, Yield, Put_price):
Dim High, Low
High = 2
Low = 0
Do While (High - Low) > 0.000001
If EuroPut(Stock, Exercise, Time, Interest, Yield, (High + Low) / 2) > _
Put_price Then
High = (High + Low) / 2
Else: Low = (High + Low) / 2
End If
Loop
EuroPutVol = (High + Low) / 2
#
# Implied Volatility from Kerry Back p64
# Chapt3.bas Newton Raphson technique
# Answer IDENTICAL to Bennigna (EuroCallVol)
#
def Black_Scholes_Call(S, K, r, sigma, q, T):
Black_Scholes_Call = EuroCall(S, K, T, r, q, sigma)
def Black_Scholes_Call_Implied_Vol(S, K, r, q, T, CallPrice):
#
# Inputs are S = initial stock price
# K = strike price
# r = risk-free rate
# q = dividend yield
# T = time to maturity
# CallPrice = call price
#
Dim tol, lower, flower, upper, fupper, guess, fguess
If CallPrice < math.exp(-q * T) * S - math.exp(-r * T) * K Then
MsgBox ("Option price violates the arbitrage bound.")
Exit Function
End If
tol = 10 ^ -6
lower = 0
flower = Black_Scholes_Call(S, K, r, lower, q, T) - CallPrice
upper = 1
fupper = Black_Scholes_Call(S, K, r, upper, q, T) - CallPrice
Do While fupper < 0 # double upper until it is an upper bound
upper = 2 * upper
fupper = Black_Scholes_Call(S, K, r, upper, q, T) - CallPrice
Loop
guess = 0.5 * lower + 0.5 * upper
fguess = Black_Scholes_Call(S, K, r, guess, q, T) - CallPrice
Do While upper - lower > tol # until root is bracketed within tol
If fupper * fguess < 0 Then # root is between guess and upper
lower = guess # make guess the new lower bound
flower = fguess
guess = 0.5 * lower + 0.5 * upper # new guess = bi-section
fguess = Black_Scholes_Call(S, K, r, guess, q, T) - CallPrice
Else # root is between lower and guess
upper = guess # make guess the new upper bound
fupper = fguess
guess = 0.5 * lower + 0.5 * upper # new guess = bi-section
fguess = Black_Scholes_Call(S, K, r, guess, q, T) - CallPrice
End If
Loop
Black_Scholes_Call_Implied_Vol = guess
#
# Implied Volatility from Wilmott Into Ch 8 p192 Newton Raphson***NOT DEBUGGED***
#
def ImpVolCall(Stock, Exercise, Time, Interest, Yield, Call_price):
Volatility = 0.2
epsilon = 0.0001
dv = epsilon + 1
While Abs(dv) > epsilon
PriceError = EuroCall(Stock, Exercise, Time, Interest, Yield, Volatility) - Call_price
dv = PriceError / Vega(Stock, Exercise, Time, Interest, Yield, Volatility)
Volatility = Volatility - dv
Wend
ImpVolCall = Volatility
#
# from Kerry Back Chapt8.bas ... need Python's "BiNormalProb"
#
def American_Call_Dividend(S, K, r, sigma, Div, TDiv, TCall):
import math
"""
#
# Inputs are S = initial stock price
# K = strike price
# r = risk-free rate
# sigma = volatility
# Div = cash dividend
# TDiv = time until dividend payment
# TCall = time until option matures >= TDiv
#
"""
LessDiv = S - math.exp(-r * TDiv) * Div # stock value excluding dividend
If Div / K <= 1 - math.exp(-r * (TCall - TDiv)): # early exercise cannot be optimal
return Black_Scholes_Call(LessDiv, K, r, sigma, 0, TCall)
#
# Now we find an upper bound for the bisection.
#
upper = K
while upper + Div - K < Black_Scholes_Call(upper, K, r, sigma, 0, TCall - TDiv):
upper = 2 * upper
#
# Now we use bisection to compute Zstar = LessDivStar.
#
tol = 10 ** -6
lower = 0
flower = Div - K
fupper = upper + Div - K - Black_Scholes_Call(upper, K, r, sigma, 0, TCall - TDiv)
guess = 0.5 * lower + 0.5 * upper
fguess = guess + Div - K - Black_Scholes_Call(guess, K, r, sigma, 0, TCall - TDiv)
while upper - lower > tol:
if fupper * fguess < 0:
lower = guess
flower = fguess
guess = 0.5 * lower + 0.5 * upper
fguess = guess + Div - K - Black_Scholes_Call(guess, K, r, sigma, 0, TCall - TDiv)
else:
upper = guess
fupper = fguess
guess = 0.5 * lower + 0.5 * upper
fguess = guess + Div - K - Black_Scholes_Call(guess, K, r, sigma, 0, TCall - TDiv)
LessDivStar = guess
#
# Now we calculate the probabilities and the option value.
#
d1 = (math.log(LessDiv / LessDivStar) + (r + sigma ** 2 / 2) * TDiv) / (sigma * math.sqrt(TDiv))
d2 = d1 - sigma * math.sqrt(TDiv)
d1prime = (math.log(LessDiv / K) + (r + sigma ** 2 / 2) * TCall) / (sigma * math.sqrt(TCall))
d2prime = d1prime - sigma * math.sqrt(TCall)
rho = -math.sqrt(TDiv / TCall)
N1 = phi(d1)
N2 = phi(d2)
M1 = BiNormalProb(-d1, d1prime, rho)
M2 = BiNormalProb(-d2, d2prime, rho)
return LessDiv * N1 + math.exp(-r * TDiv) * (Div - K) * N2 + LessDiv * M1 - math.exp(-r * TCall) * K * M2