Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Excel VBA

- () -

Oct. 2008

(contents)

1. (1)

19.

2. (2)

20. : vs

3. Macro : (1)

21. : (Bisection Method)

4. Macro : (2) VBA

22. : (Bisection Method)

5. VBA
6. Sheet (Layout)

23. : -(Newton-Rapson)

7. KOSPI200
8. Exotic option delta surface

25. Binomial Tree


26. Binomial Tree : 1

9. Black Scholes OPM

27. Binomial Tree : 1

10. Option Greeks : Delta

28. Binomial Tree : 1

11. (delta neutral)

29. Binomial Tree :

12. (delta neutral hedging)

30.
31.
32.
33.
34.
35.
36.
37.
38.

13. (delta neutral hedging)


14. Option Greeks : Gamma
15. Gamma Trading Vs Vega Trading
16. Option Greeks : Vega
17. Option Greeks : Theta
18. Option Greeks : Rho

24. : -(Newton-Rapson)

Binomial Tree Code


MC Simulation
MC Simulation

Cholesky Factorization
Cholesky Factorization Code
Performance
Antithetic Variables
Control Variate Method

1. (1)

EXCEL

VBA


(Spreadsheet
(SpreadsheetModeling)
Modeling)
Use
only
simple
arithmetic
Use only simple arithmetic
Organize
Organizespreadsheet
spreadsheetfor
forbetter
betterpresentation
presentation

(built-in
(built-infuctions)
fuctions)

Make
excel
work
more
efficient
Make excel work more efficient
Powerful
Powerfulexcel
excelbuilt-in
built-infunctions
functions
VBA
VBAI:I:

writing
writingthe
theBlack-Scholes
Black-Scholesfunction
function
pros
prosand
andcons
consof
ofdifferent
differentimplementation
implementation
VBA
VBAII:
II:
Make
Makeexcel
excelwork
workhard,
hard,and
andfancy
fancyusers
usersinput
input
be
beaabetter
betterprogrammer
programmer

2. (2)

(Solver)
(Solver)

(Portfolio optimization)
(Portfolio optimization)

- (Interpolation), ,
- (Interpolation), ,
--(Tree,
(Tree,FDM),
FDM),

(Randon
(RandonVariables)
Variables)

(Monte
(MonteCarlo
CarloSimulation)
Simulation)
Pricing multiple stock options
Pricing multiple stock options


(Risk
(RiskManagement)
Management)
Garch
GarchVolatility
VolatilityModeling
Modeling
Value
at
Risk
Value at Risk

(Interest
(InterestRate
RateDerivatives)
Derivatives)
Modeling interest rate derivatives in Excel
Modeling interest rate derivatives in Excel

C
C

Why need C?
Why need C?
Excel
Exceland
andCC

3. Macro : (1)

?
?

VBA
VBA??Visual
VisualBasic
Basicfor
forApplication
Application,
,vb
vb

4. Macro : (2) VBA

: :

.
.

.
.

: :

: :

,
,

.
.

5. VBA
VBA

(Excel)
xy

x*y

x*y

x^2

x^2

LN(x)

Log(x)

EXP(x)

Exp(x)

SQRT(x)

Sqr(x)

3!

FACT(3)

Application.Fact(3)

NORMSDIST(x)

Application.NormSDist(x)

RAND()

Rnd

x2
loge x

exp(x)

EXP(x)
EXP(x)
,
,exp(x)
exp(x).
.
LN(x)
x

.
LN(x) x .
SQRT(x)
SQRT(x)x
x
.
.
FACT(numbers)
1*2*3**numbers
FACT(numbers) 1*2*3**numbers

NORMDIST

.
NORMDIST .

6. EXCEL Sheet (layout) : , ,

: :

: :

,
,

()
:

() :

: :

[1] VBA

F0 = S 0 e ( r q )T
(S : , r : , q : , T: )

//VBA Code,
Public Function Futures (S As Double, Tau As Double, R As Double, Q As Double)

S= (KOSPI200 )
Tau = ()
Futures = S * Exp((R-Q) * Tau )
End Function

7. KOSPI200



.
. (OTM)
(OTM)

0
0 ,
,
(ITM)
(ITM)

,
, (ATM)
(ATM)

[2] Black-Scholes Call option function(Surface)


Black-Scholes call option function

60

50

40

30

20

10

150

140

130

120

110

100

90

80

70

60

0
50

0.1

0.2

0.3

0.4

0.5

0.6

T-t

0.7

0.8

0.9

8. Exotic option Delta Surface


Barrier
Barrier Options
Options
1.0000
0.5000
0.0000

 Rebate , Short Delta

-1.0000
-1.5000
8
37

Risk , Participation Rate Rebate


 Barrier Delta, Gamma, Vega Risk

-2.0000

65

-0.5000

-2.5000

94
123

-3.0000

151

 Barrier Option Interest Rate, Repo, Dividend,


Local Volatility, Volatility of Volatility, Jump

118.72

115.76

112.80

109.84

106.88

103.92

98.00

100.96

95.04

92.08

86.15

83.19

180

89.12

-3.5000

Autocall
Autocall Structure
Structure

10
9
8
7

 ELS(6 Chance), Dynamic option Replication


(Delta Greek, )

6
5

 (Buy Low, Sell High)

 , ATM
990
1095

780

675

465

360

255

150

-1
62 68 76
84 92 100
108

45

570

885

Coupon Gamma Risk

[3] Black Scholes OPM

c = SN ( d 1 ) Xe r ( T t ) N ( d 2 )

p = Xe r ( T t ) N ( d 2 ) SN ( d 1 )

9. Black Scholes OPM


//VBA
//VBACode,
Code,-(1973)
-(1973)

Public
PublicFunction
FunctionBlackSholes(CallPutFlag
BlackSholes(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,
r rAs
AsDouble,
Double,TTAs
AsDouble,
Double,vvAs
AsDouble)
Double) As
AsDouble
Double
Dim
Dimd1
d1As
AsDouble,
Double,d2
d2As
AsDouble
Double
d1
d1==(Log(S/X)+(r+v^2/2)*T)/(v*Sqr(T))
(Log(S/X)+(r+v^2/2)*T)/(v*Sqr(T))
d2=d1-v*Sqr(T)
d2=d1-v*Sqr(T)
IfIfCallPutFlag
CallPutFlag==c
cThen
Then
BlackSholes
=
S*Application.NormSDist(d1)-X*Exp(-r*T)*Application.NormSDist(d2)
BlackSholes = S*Application.NormSDist(d1)-X*Exp(-r*T)*Application.NormSDist(d2)
ElseIf
ElseIfCallPutFlag
CallPutFlag==p
pThen
Then
BlackSholes
=
X*Exp(-r*T)*Application.NormSDist(-d2)-S*Application.NormSDist(-d1)
BlackSholes = X*Exp(-r*T)*Application.NormSDist(-d2)-S*Application.NormSDist(-d1)
End
EndIfIf
End
EndFunction
Function
VBA
VBACODE
CODE

1.1.

(
(BlackSholes)
BlackSholes)
2.2.
BlackSholes
BlackSholes

: :String,
String,Double
Double

3.3.Dim
Dim

d1
d1d2
d2

4.4.
If

:
If-Then-ElsIf

If : If-Then-ElsIf
5.5.
(End
(EndFunction)
Function)

10. Option Greeks : Delta

C
= N (d1 )
S

()

c
= e( br )T N (d1 ) > 0
S
p
put =
= e ( b r )T [ N (d1 ) 1] < 0
S
call =

//VBA
//VBACode,
Code,(Delta)
(Delta): :()
()
Public
Function
Delta(CallPutFlag
Public Function Delta(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,_r_rAs
As
Double,
v
As
Double)
As
Double
Double, v As Double) As Double
Dim
Dimd1
d1As
AsDouble
Double
d1
=
(Log(S
d1 = (Log(S/ /X)
X)++(r(r++vv^^22/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
IfIfCallPutFlag
=
"c"
Then
CallPutFlag = "c" Then
Delta
Delta==Exp((-r)
Exp((-r)* *T)
T)* *Application.NormSDist(d1)
Application.NormSDist(d1)
ElseIf
CallPutFlag
=
"p"
ElseIf CallPutFlag = "p"Then
Then
Delta
=
Exp((-r)
*
T)
*
(Application.NormSDist(d1)
Delta = Exp((-r) * T) * (Application.NormSDist(d1)- -1)1)
End
EndIfIf
End
EndFunction
Function

11. (Delta Neutral Trading)


(Delta Neutral Trading)
.
. , (Directional risk) .
0 ,
(theta)
(vega) . ,
(short strangle) : (time decay+ implied volatility)
:
-
- (mean reverting)
-
:
- : (, )
-
- :
:
, , ,
. ,
.
- (jump)
- (transaction cost)
- (stochastic volatility) :
- : (discrete hedging)

12. (Delta Neutral Hedging)


( )
Delta
Deltaof
ofOption
Option
Gamma
of
Gamma ofOption
Option
Theta
of
Option
Theta of Option
Rho
Rhoof
ofOption
Option
Vega
Vegaof
ofOption
Option

== Delta
Deltaof
ofHedge
HedgePortfolio
Portfolio
== Gamma
of
Hedge
Gamma of HedgePortfolio
Portfolio
== Theta
of
Hedge
Portfolio
Theta of Hedge Portfolio
== Rho
Rhoof
ofHedge
HedgePortfolio
Portfolio
== Vega
Vegaof
ofHedge
HedgePortfolio
Portfolio

( )

Delta
Deltaof
ofOption
Option

== Delta
Deltaof
ofHedge
HedgePortfolio
Portfolio(
(

)
)
c.f
c.f: :

Delta Hedging of Large Portfolio


Delta
Deltaof
ofLarge
LargePortfolio
Portfolio == Delta
Deltaof
ofHedge
HedgePortfolio
Portfolio
Gamma
of
Large
Portfolio

0
Gamma of Large Portfolio 0
Vega
Vegaof
ofLarge
LargePortfolio
Portfolio 00

13. (Delta Neutral Hedging)

Black-Scholes
Black-Scholes

Black-Scholes
Black-Scholes

(
(

)
)

!
!
B-S
B-S

:
:
1
1++

Delta:
Delta:(
(
)/(
)/()
)
Option
price

Slope =
B

Stock price

14. Option Greeks : Gamma

call , put

2 c 2 p n(d1)e ( b r )T
= 2 =
=
>0
S
S 2
S T

//VBA
//VBACode,
Code,(Gamma)
(Gamma): :()
()
Public
PublicFunction
FunctionGamma(S
Gamma(SAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,r rAs
AsDouble,
Double,vvAs
AsDouble)
Double)As
AsDouble
Double
Dim
Dimd1
d1As
AsDouble
Double
d1
d1==(Log(S
(Log(S/ /X)
X)++(r(r++vv^^22/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
Gamma
Gamma==Exp((-r)
Exp((-r)* *T)
T)* *ND(d1)
ND(d1)/ /(S
(S* *vv* *Sqr(T))
Sqr(T))
End
EndFunction
Function

15. Gamma Trading vs Vega Trading


VS (gamma vs vega trading)
(realized volatility) . (short gamma)
,
, . ,
,
.

(long gamma) : > (IV )


(short gamma): < (IV )

, (implied volatility) ,
.

(long vega) : (IV )


(short vega): (IV )

, .
, ,
.

16. Option Greeks : Vega

Vegacall . put =

c
p
= Se (b r )T n(d1) T > 0
=

//VBA
//VBACode,
Code,(Vega)
(Vega): :()
()
Public
PublicFunction
FunctionVega(S
Vega(SAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,r rAs
AsDouble,
Double,vvAs
AsDouble)
Double)As
AsDouble
Double
Dim
d1
As
Double
Dim d1 As Double
d1
d1==(Log(S
(Log(S/ /X)
X)++(r(r++vv^^22/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
Vega
=
S
*
Exp((-r)
*
T)
*
ND(d1)
*
Sqr(T)
Vega = S * Exp((-r) * T) * ND(d1) * Sqr(T)
End
EndFunction
Function

17. Option Greeks : Theta


call

Se (b r )T n(d1)
c
=
=
(b r ) Se ( b r )T N ( d1 ) rXe rT N (d 2 ) 0
T
2 T

put

p
Se (b r )T n(d1)
= =
+ (b r ) Se (b r )T N (d1 ) + rXe rT N (d 2 ) 0
T
2 T

//VBA
//VBACode,
Code,(Vega)
(Vega): :()
()
Public
Function
Theta(CallPutFlag
Public Function Theta(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,
r rAs
Double,
v
As
Double)
As
Double
As Double, v As Double) As Double
Dim
Dimd1
d1As
AsDouble,
Double,d2
d2As
AsDouble
Double
d1
=
(Log(S
/
X)
+
(r
+
v
^
2
d1 = (Log(S / X) + (r + v ^ 2/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
d2
=
d1
v
*
Sqr(T)
d2 = d1 - v * Sqr(T)
IfIfCallPutFlag
CallPutFlag=="c"
"c"Then
Then
Theta
=
-S
*
Exp((-r)
Theta = -S * Exp((-r)* *T)
T)* *Application.NormSDist(d1)
Application.NormSDist(d1)* *vv/ /(2(2* *Sqr(T))
Sqr(T))- -(-r)
(-r)* *SS* *Exp((-r)
Exp((-r)* *T)
T)
* *Application.NormSDist(d1)
Application.NormSDist(d1)
- -r r* *XX* *Exp(-r
Exp(-r* *T)
T)* *Application.NormSDist(d2)
Application.NormSDist(d2)
ElseIf
CallPutFlag
=
"p"
ElseIf CallPutFlag = "p"Then
Then
Theta
=
-S
*
Exp((-r)
*
T)
Theta = -S * Exp((-r) * T)* *Application.NormSDist(d1)
Application.NormSDist(d1)* *vv/ /(2(2* *Sqr(T))
Sqr(T))++(-r)
(-r)* *SS* *Exp((-r)
Exp((-r)* *T)
T)
* *Application.NormSDist(-d1)
Application.NormSDist(-d1)
++r r* *XX* *Exp(-r
Exp(-r* *T)
T)* *Application.NormSDist(-d2)
Application.NormSDist(-d2)
End
EndIfIf
End
EndFunction
Function

18. Option Greeks : Rho

//VBA
//VBACode,
Code,(Rho)
(Rho): :()
()
Public
PublicFunction
FunctionRho(CallPutFlag
Rho(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,r rAs
As
Double,
v
As
Double)
As
Double
Double, v As Double) As Double
Dim
Dimd1
d1As
AsDouble,
Double,d2
d2As
AsDouble
Double
d1
=
(Log(S
/
X)
+
(r
+
v
^
2
d1 = (Log(S / X) + (r + v ^ 2/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
d2
=
d1
v
*
Sqr(T)
d2 = d1 - v * Sqr(T)
IfIfCallPutFlag
CallPutFlag=="c"
"c"Then
Then
Rho
=
X
*
T
*
Exp(-r
Rho = X * T * Exp(-r* *T)
T)* *Application.NormSDist(d2)
Application.NormSDist(d2)
ElseIf
CallPutFlag
=
"p"
Then
ElseIf CallPutFlag = "p" Then
Rho
Rho==-X
-X* *TT* *Exp(-r
Exp(-r* *T)
T)* *Application.NormSDist(-d2)
Application.NormSDist(-d2)
End
If
End If
End
EndFunction
Function

[4]
.

Check

, , , kospi200,/,
, ,

, /, , ,,,
, /

, , ,

10

, ,

Cell Data Cell

, , Check ?

Worksheet ?

Worksheet ?

19. (Numerical Method)

(closed
(closedform
formsolution)
solution)
,
,

, ,

.
.,
,

.
.
,
,

(Binomial
(Binomial) )

(Trinomial
(TrinomialTree),
Tree),
(FDM)
(FDM)

.
.

(Tree)

(FDM)

20. : vs

.
.

.
.

.
.

(argument)
(argument)

. ,

.
, .
,
,=f(,,,,)
=f(,,,,),
,=g(,,
=g(,,
,
,
)

, , ) . .
.

21. : (Bisection Method)


f(x)

x0
f(x)

x1

11
x0) f(x0)*f(x1) < 0
::
[x
[x0,0,xx1]1] (,
(, xx1<
1< x0) f(x0)*f(x1) < 0
2

2 ,
,





[x
[x0,0,xx1]
1]



 


x 0 + x1
22
x2 = 2

::
:

|
x
x
|
>

33

4
4


xx2
: | x2 2 - x0 0| > 
2
44
f(x
f(x0)*f(x
)*f(x2)) << 00
xx1
xx2
,
,

xx0
xx2

::
0

22. : (Bisection Method)


Public
PublicFunction
Functionimvol(CallPutFlag
imvol(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,TT
As
AsDOUBLE,
DOUBLE,r rAs
AsDouble,
Double,Price
PriceAs
AsDouble)
Double)
Dim
vLow
As
Double,
vHigh
As
Double,
vi
As
Double
Dim vLow As Double, vHigh As Double, vi As Double
Dim
DimcLow
cLowAs
AsDouble,
Double,cHigh
cHighAs
AsDouble,
Double,Epsilon
EpsilonAs
AsDouble
Double
vLow
vLow==0.01
0.01

vHigh
=
2

vHigh = 2

Epsilon
=
0.000001
Epsilon = 0.000001

cLow
cLow==BS(CallPutFlag,
BS(CallPutFlag,S,S,X,X,T,T,r,r,vLow)
vLow)
cHigh
=
BS(CallPutFlag,
S,
X,T,
r,
vHigh)
cHigh = BS(CallPutFlag, S, X,T, r, vHigh)
ViVi==vLow
vLow++(price
(pricecLow)*(vHigh-vLow)
cLow)*(vHigh-vLow)/ /(cHigh
(cHighcLow)
cLow)
Do
DoWhile
WhileAbs(priceAbs(price-BS(CallPutFlag,
BS(CallPutFlag,S,S,X,X,T,T,r,r,vi))
vi))>>Epsilon
Epsilon
IfIf BS(CallPutFlag,
S,
X,
T,
r,
vi)
<
Price
Then
BS(CallPutFlag, S, X, T, r, vi) < Price Then
vLow
vLow==vivi
Else
Else
vHihg
vHihg==vivi
cLow
cLow==BS(CallPutFlag,
BS(CallPutFlag,S,S,X,X,T,T,r,r,vLow)
vLow)
cHigh
=
BS(CallPutFlag,
S,
X,
T,
r,
vHihg)
cHigh = BS(CallPutFlag, S, X, T, r, vHihg)
vivi==vLow
vLow++(price
(pricecLow)
cLow)* *(vHihg
(vHihgvLow
vLow) )/ /(cHihg
(cHihgcLow)
cLow)
Loop
Loop
Imvol
Imvol==vivi
End
EndFunction
Function

23. : -(Newton Raphson Method)


f(x )

f ( x i)

f(x

i+ 1

[x

i,

( x i )]

i+ 2

i+ 1

-
-




.
.



.
.



Xi
Xi
f(Xi)
f(Xi)
g(X)
g(X)

g(x)=0
g(x)=0
Xi+1
Xi+1 .
.




,
,



.
.

24. : -(Newton Raphson Method)


Function
FunctionImpliedVolatility(Spot,
ImpliedVolatility(Spot,Strike,
Strike,RiskFreeRate,
RiskFreeRate,Tau,
Tau,Erroer,
Erroer,PutCall,
PutCall,MarketPrice,
MarketPrice,MaxTrial)
MaxTrial)
Dim
InitialValue
As
Double
Dim InitialValue As Double
Dim
DimTempError
TempErrorAs
AsDouble
Double
Dim
TempPrice
As
Double
Dim TempPrice As Double
InitialValue
InitialValue==Sqr(Abs(Log(Spot
Sqr(Abs(Log(Spot/ /Strike)
Strike)++__
Risk
RiskFreeRate
FreeRate* *Tau
Tau) )*2*2/ /Tau)
Tau)
For
ForI I== 11To
ToMaxTrial
MaxTrial
TempPrice
TempPrice==BlackScholes(Spot,
BlackScholes(Spot,Strike,
Strike,RiskFreeRate,
RiskFreeRate,__
0,0,Tau,
Tau,InitialValue,
InitialValue,PutCALL)
PutCALL)
TempError
=
TempPrice

MarketPrice
TempError = TempPrice MarketPrice
IfIfTempError
TempError<=
<=Error
ErrorThen
Then
ImpliedVolatility

InitialValue
ImpliedVolatility InitialValue
Exit
ExitFunction
Function
Else
Else
InitialValue
InitialValue==InitialValue
InitialValueTempError
TempError/ /__
BlackSchole(Spot,
BlackSchole(Spot,Strike,
Strike,RiskFreeRate,
RiskFreeRate,0,0,Tau,
Tau,InitialValue,
InitialValue,PutCall
PutCall&&vega)
vega)
End
If
End If
Next
NextI I
ImpliedVolatility
ImpliedVolatility==00
End
EndFunction
Function

25. Binomial Tree

1979,
1979,John
JohnCox,
Cox,Stephen
StephenRoss,
Ross,Mark
MarkRubinstein
Rubinstein

Risk-Neutral
Risk-NeutralValuation
Valuation

Real
RealWorld
WorldRisk
RiskNeutral
NeutralWorld
World

Path-Dependent
Path-Dependent

Early
EarlyExercise
Exercise

Local
LocalVolatility
Volatility
Yield
YieldCurve
Curve

=>
=>Trinomial
TrinomialModel,
Model,Equal
EqualProbability
ProbabilityModel
Model

26. Binomial Tree : 1

S, f, r
u, d, , d < r < u
Su, Sd
fu, fd
1

S0u
fu
S
f
S0d
fd

27. Binomial Tree : 1


: , : 1

: Su - fu
: Sd fd
: S - f

Su - fu = Sd fd
//
= (fu fd)/(Su Sd) //
S - f = e-rT (Su - fu ) //
f = S (1 u e-rT) + fu e-rT
= e-rT[ p fu + (1-p)fd]
, p = (e-rT d) / ( u d)
, 0 < p < 1
, p Real World Risk
Neutral World . (f)
/ =>

28. Binomial Tree : 1

One step in the binomial model


Asset
Volatility

100
0.2

u
v
Risk-neutral p

1.02
0.98
0.5125

Interest rate

0.05

Timestep
Discount factor

0.01
0.9995

Strike

100

One timestep
before expiry

Expiry
102
2

100
0.5
1.024488
98

Color coding

Stock
Delta
Option

29. Binomial Tree :

The tree
Asset
Volatility

100
0.2

u
v
Risk-neutral p

1.02
0.98
0.5125

Interest rate

0.05

Timestep
Discount factor

0.01
0.9995

Strike

100

1.0005

Color coding

Three timesteps
before expiry

Two timesteps
before expiry

One timestep
before expiry

Expiry
106.1208
6.1208

104.04
1
4.089975
102
0.756467
2.584069
100
0.517497
1.574163

101.9592
1.9592
99.96
0.489996
1.003588

98
0.256017
0.514082
Stock
Delta
Option

97.9608
0
96.04
0
0
94.1192
0

30. Binomial Code


Function Price(Asset As Double, Volatility As Double, IntRate As Double, _
Strike As Double, Expiry As Double, NoSteps As Integer)
ReDim s(0 To NoSteps)
ReDim V(0 To NoSteps)
timestep = Expiry / NoSteps
DiscountFactor = Exp(-IntRate * timestep)
temp1 = Exp((IntRate + Volatility * Volatility) * timestep)
temp2 = 0.5 * (DiscountFactor + temp1)
u = temp2 + Sqr(temp2 * temp2 - 1)
d=1/u
p = (Exp(IntRate * timestep) - d) / (u - d)

Function USPrice(Asset As Double, Volatility As Double, IntRate As Double, _


Strike As Double, Expiry As Double, NoSteps As Integer)
ReDim s(0 To NoSteps, 0 To NoSteps)
ReDim V(0 To NoSteps, 0 To NoSteps)
timestep = Expiry / NoSteps
DiscountFactor = Exp(-IntRate * timestep)
temp1 = Exp((IntRate + Volatility * Volatility) * timestep)
temp2 = 0.5 * (DiscountFactor + temp1)
u = temp2 + Sqr(temp2 * temp2 - 1)
d=1/u
p = (Exp(IntRate * timestep) - d) / (u - d)

s(0) = Asset
For n = 1 To NoSteps
For j = n To 1 Step -1
s(j) = u * s(j - 1)
Next j
s(0) = d * s(0)
Next n

s(0, 0) = Asset
For n = 1 To NoSteps
For j = n To 1 Step -1
s(j, n) = u * s(j - 1, n - 1)
Next j
s(0, n) = d * s(0, n - 1)
Next n

For j = 0 To NoSteps
V(j) = Payoff(s(j), Strike)
Next j

For j = 0 To NoSteps
V(j, NoSteps) = Payoff(s(j, NoSteps), Strike)
Next j

For n = NoSteps To 1 Step -1


For j = 0 To NoSteps - 1
V(j) = (p * V(j + 1) + (1 - p) * V(j)) _
* DiscountFactor
Next j
Next n
Price = V(0)
End Function

For n = NoSteps To 1 Step -1


For j = 0 To NoSteps - 1
V(j, n - 1) = Application.Max((p * V(j + 1, n) + (1 - p) * V(j, n)) _
* DiscountFactor, Payoff(s(j, n - 1), Strike))
Next j
Next n
USPrice = V(0, 0)
End Function

Function Payoff(s, e)
If s < e Then Payoff = e - s
End Function

31. MC Simulation


PayOff
PayOff


Log-normal
S(t+1) = S(t) * Exp((r-d-.5*Vol^2)*dt + *Vol*sqr(dt))
()
: NormsInv(Rand())
Box-Muller Algorithm

32. MC Simulation
Function BoxMuller()
Randomize
Do
X = 2 * Rnd() - 1
Y = 2 * Rnd() - 1
dist = X * X + Y * Y
Loop Until dist < 1
BoxMuller = X * Sqr(-2 * Log(dist) / dist)
End Function

[5] Vanilla Option

33.

Cholesky Factorization
Correlated Random Variable
() (MMT )
= M Correlated Random Variable Vector

E( i j) = ij
= M
MMT =
T = M T MT
E( T) = ME[ T ] MT = MMT =
, : correlation matrix
Matrix LU
=> Correlation Matrix
,

34. Cholesky Factorization

Input symmetric (positive definite) correlation matrix


0.45
0.03
0.13
0.06
0.09

Matrix M

0.67082
0.044721
0.193793
0.089443
0.134164

Product of M
and M^T

0.45
0.03
0.13
0.06
0.09

0.03
0.34
0
-0.09
0.12

0.13
0
0.28
0.001
-0.13

0.06
-0.09
0.001
0.41
0.03

0.09
0.12
-0.13
0.03
0.32

0
0
0
0
0.581378
0
0
0
-0.014907 0.492161
0
0
-0.161685 -0.038084 0.611889
0
0.196086 -0.31103 0.061872 0.403711

0.03
0.34
0
-0.09
0.12

0.13
0
0.28
0.001
-0.13

0.06
-0.09
0.001
0.41
0.03

0.09
0.12
-0.13
0.03
0.32

35. Cholesky Factorization Code


Function cholesky(Sigma As Object)
Dim n As Integer
Dim k As Integer
Dim i As Integer
Dim j As Integer
Dim X As Double
Dim a() As Double
Dim M() As Double
n = Sigma.Columns.Count
ReDim a(1 To n, 1 To n)
ReDim M(1 To n, 1 To n)
For i = 1 To n
For j = 1 To n
a(i, j) = Sigma.Cells(i, j).Value
M(i, j) = 0
Next j
Next i
For i = 1 To n
For j = i To n
X = a(i, j)
For k = 1 To (i - 1)
X = X - M(i, k) * M(j, k)
Next k
If j = i Then
M(i, i) = Sqr(X)
Else
M(j, i) = X / M(i, i)
End If
Next j
Next i
cholesky = M
End Function

36. Performance

Issue
MC Simulations (Sqrt() )

Antithetic Variables
Control Variate Method

37. Antithetic Variables


( )


Drift

38. Control Variate Method

, ( )

( , closed form solution)
,
, (?)

V1 = V1 V2 + V2
, V1: V1 , V2: V2 , V2: V2

End

You might also like