-
Notifications
You must be signed in to change notification settings - Fork 3
/
code.R
84 lines (69 loc) · 2.52 KB
/
code.R
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
source('functions.R')
######################################################################
## European put ##
######################################################################
s <- 10
k <- 10
r <- .04
t <- .5
sd <- .2
dt <- .002
dx <- c(sd*sqrt(dt), sd*sqrt(3*dt), sd*sqrt(4*dt))
type <- 'put'; style <- 'european'
## Binomial or Black-Scholes-Merton for comparison
if (type == 'put' && style == 'american') {
binom.american.put(s, k, r, t, sd, n=1e3)
} else
bsm.option(s, k, r, t, sd, type)
## Regular implementation.
fde.log(s, k, r, t, sd, type=type, style=style)
fdi.log(s, k, r, t, sd, type=type, style=style)
fdcn.log(s, k, r, t, sd, type=type, style=style)
## Convergence demonstration.
n <- round(t/dt)
m <- round(6*sd*sqrt(t)/dx[1]) # dx = sd*sqrt(dt)
fde.log(s, k, r, t, sd, n, m, type, style)
fdi.log(s, k, r, t, sd, n, m, type, style)
fdcn.log(s, k, r, t, sd, n, m, type, style)
m <- round(6*sd*sqrt(t)/dx[2]) # dx = sd*sqrt(3*dt)
fde.log(s, k, r, t, sd, n, m, type, style)
fdi.log(s, k, r, t, sd, n, m, type, style)
fdcn.log(s, k, r, t, sd, n, m, type, style)
m <- round(6*sd*sqrt(t)/dx[3]) # dx = sd*sqrt(4*dt)
fde.log(s, k, r, t, sd, n, m, type, style)
fdi.log(s, k, r, t, sd, n, m, type, style)
fdcn.log(s, k, r, t, sd, n, m, type, style)
######################################################################
## American put ##
######################################################################
s <- 10
k <- 10
r <- .04
t <- .5
sd <- .2
dt <- .002
ds <- c(0.5, 1, 1.5)
type <- 'put'; style <- 'american'
## Binomial or Black-Scholes-Merton for comparison
if (type == 'put' && style == 'american') {
binom.american.put(s, k, r, t, sd, n=1e3)
} else
bsm.option(s, k, r, t, sd, type)
## Regular implementation.
fde(s, k, r, t, sd, type=type, style=style)
fdi(s, k, r, t, sd, type=type, style=style)
fdcn(s, k, r, t, sd, type=type, style=style)
## Convergence demonstration.
n <- round(t/dt)
m <- round(2*s/ds[1]) # ds = 0.5
fde(s, k, r, t, sd, n, m, type, style)
fdi(s, k, r, t, sd, n, m, type, style)
fdcn(s, k, r, t, sd, n, m, type, style)
m <- round(2*s/ds[2]) # ds = 1
fde(s, k, r, t, sd, n, m, type, style)
fdi(s, k, r, t, sd, n, m, type, style)
fdcn(s, k, r, t, sd, n, m, type, style)
m <- round(2*s/ds[3]) # ds = 1.5
fde(s, k, r, t, sd, n, m, type, style)
fdi(s, k, r, t, sd, n, m, type, style)
fdcn(s, k, r, t, sd, n, m, type, style)