1
+ {
2
+ "cells" : [
3
+ {
4
+ "cell_type" : " markdown" ,
5
+ "metadata" : {},
6
+ "source" : [
7
+ " # European option pricing in R \n " ,
8
+ " \n " ,
9
+ " Based on [Pricing of EU Options](https://www.r-bloggers.com/2020/12/pricing-of-european-options-with-monte-carlo/)"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type" : " markdown" ,
14
+ "metadata" : {},
15
+ "source" : [
16
+ " ## Black-Scholes"
17
+ ]
18
+ },
19
+ {
20
+ "cell_type" : " code" ,
21
+ "execution_count" : 1 ,
22
+ "metadata" : {},
23
+ "outputs" : [
24
+ {
25
+ "data" : {
26
+ "text/html" : " <ol class=list-inline>\n\t <li>7.28815118277163</li>\n\t <li>4.29313455768843</li>\n </ol>\n " ,
27
+ "text/latex" : " \\ begin{enumerate*}\n\\ item 7.28815118277163\n\\ item 4.29313455768843\n\\ end{enumerate*}\n " ,
28
+ "text/markdown" : " 1. 7.28815118277163\n 2. 4.29313455768843\n\n\n " ,
29
+ "text/plain" : " [1] 7.288151 4.293135"
30
+ },
31
+ "metadata" : {},
32
+ "output_type" : " display_data"
33
+ }
34
+ ],
35
+ "source" : [
36
+ " K = 100\n " ,
37
+ " r = 0.02\n " ,
38
+ " sigma = 0.2\n " ,
39
+ " T = 0.5\n " ,
40
+ " S0 = 102\n " ,
41
+ " \n " ,
42
+ " # call option\n " ,
43
+ " d1 <- (log(S0/K) + (r + sigma^2/2) * T)/(sigma * sqrt(T))\n " ,
44
+ " d2 <- d1 - sigma * sqrt(T)\n " ,
45
+ " phid1 <- pnorm(d1)\n " ,
46
+ " call_price <- S0 * phid1 - K * exp(-r * T) * pnorm(d2)\n " ,
47
+ " \n " ,
48
+ " # put option\n " ,
49
+ " d1 <- (log(S0/K) + (r + sigma^2/2) * T)/(sigma * sqrt(T))\n " ,
50
+ " d2 <- d1 - sigma * sqrt(T)\n " ,
51
+ " phimd1 <- pnorm(-d1)\n " ,
52
+ " put_price <- -S0 * phimd1 + K * exp(-r * T) * pnorm(-d2)\n " ,
53
+ " \n " ,
54
+ " c(call_price, put_price)"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type" : " markdown" ,
59
+ "metadata" : {},
60
+ "source" : [
61
+ " ## Monte-Carlo simulation "
62
+ ]
63
+ },
64
+ {
65
+ "cell_type" : " code" ,
66
+ "execution_count" : 5 ,
67
+ "metadata" : {},
68
+ "outputs" : [
69
+ {
70
+ "data" : {
71
+ "text/html" : [
72
+ " <dl>\n " ,
73
+ " \t <dt>$price_call</dt>\n " ,
74
+ " \t\t <dd>7.29073833912655</dd>\n " ,
75
+ " \t <dt>$sterr_call</dt>\n " ,
76
+ " \t\t <dd>0.0101347588868935</dd>\n " ,
77
+ " \t <dt>$price_put</dt>\n " ,
78
+ " \t\t <dd>4.29468276035008</dd>\n " ,
79
+ " \t <dt>$sterr_put</dt>\n " ,
80
+ " \t\t <dd>0.00670090190916396</dd>\n " ,
81
+ " </dl>\n "
82
+ ],
83
+ "text/latex" : [
84
+ " \\ begin{description}\n " ,
85
+ " \\ item[\\ $price\\ _call] 7.29073833912655\n " ,
86
+ " \\ item[\\ $sterr\\ _call] 0.0101347588868935\n " ,
87
+ " \\ item[\\ $price\\ _put] 4.29468276035008\n " ,
88
+ " \\ item[\\ $sterr\\ _put] 0.00670090190916396\n " ,
89
+ " \\ end{description}\n "
90
+ ],
91
+ "text/markdown" : [
92
+ " $price_call\n " ,
93
+ " : 7.29073833912655\n " ,
94
+ " $sterr_call\n " ,
95
+ " : 0.0101347588868935\n " ,
96
+ " $price_put\n " ,
97
+ " : 4.29468276035008\n " ,
98
+ " $sterr_put\n " ,
99
+ " : 0.00670090190916396\n " ,
100
+ " \n " ,
101
+ " \n "
102
+ ],
103
+ "text/plain" : [
104
+ " $price_call\n " ,
105
+ " [1] 7.290738\n " ,
106
+ " \n " ,
107
+ " $sterr_call\n " ,
108
+ " [1] 0.01013476\n " ,
109
+ " \n " ,
110
+ " $price_put\n " ,
111
+ " [1] 4.294683\n " ,
112
+ " \n " ,
113
+ " $sterr_put\n " ,
114
+ " [1] 0.006700902\n "
115
+ ]
116
+ },
117
+ "metadata" : {},
118
+ "output_type" : " display_data"
119
+ }
120
+ ],
121
+ "source" : [
122
+ " # call put option monte carlo\n " ,
123
+ " call_put_mc<-function(nSim=1000000, tau, r, sigma, S0, K) {\n " ,
124
+ " \n " ,
125
+ " Z <- rnorm(nSim, mean=0, sd=1)\n " ,
126
+ " WT <- sqrt(tau) * Z\n " ,
127
+ " ST = S0*exp((r - 0.5*sigma^2)*tau + sigma*WT)\n " ,
128
+ " \n " ,
129
+ " # price and standard error of call option\n " ,
130
+ " simulated_call_payoffs <- exp(-r*tau)*pmax(ST-K,0)\n " ,
131
+ " price_call <- mean(simulated_call_payoffs)\n " ,
132
+ " sterr_call <- sd(simulated_call_payoffs)/sqrt(nSim)\n " ,
133
+ " # price and standard error of put option\n " ,
134
+ " simulated_put_payoffs <- exp(-r*tau)*pmax(K-ST,0)\n " ,
135
+ " price_put <- mean(simulated_put_payoffs)\n " ,
136
+ " sterr_put <- sd(simulated_put_payoffs)/sqrt(nSim)\n " ,
137
+ " \n " ,
138
+ " output<-list(price_call=price_call, sterr_call=sterr_call, \n " ,
139
+ " price_put=price_put, sterr_put=sterr_put)\n " ,
140
+ " return(output)\n " ,
141
+ " \n " ,
142
+ " }\n " ,
143
+ " set.seed(1)\n " ,
144
+ " results<-call_put_mc(n=1000000, tau=0.5, r=0.02, sigma=0.2, S0=102, K=100)\n " ,
145
+ " results"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type" : " code" ,
150
+ "execution_count" : 6 ,
151
+ "metadata" : {},
152
+ "outputs" : [
153
+ {
154
+ "data" : {
155
+ "text/html" : [
156
+ " <dl>\n " ,
157
+ " \t <dt>$price_call</dt>\n " ,
158
+ " \t\t <dd>7.2901930740815</dd>\n " ,
159
+ " \t <dt>$sterr_call</dt>\n " ,
160
+ " \t\t <dd>0.00499340311846781</dd>\n " ,
161
+ " \t <dt>$price_put</dt>\n " ,
162
+ " \t\t <dd>4.29481225823472</dd>\n " ,
163
+ " \t <dt>$sterr_put</dt>\n " ,
164
+ " \t\t <dd>0.00363647856411944</dd>\n " ,
165
+ " </dl>\n "
166
+ ],
167
+ "text/latex" : [
168
+ " \\ begin{description}\n " ,
169
+ " \\ item[\\ $price\\ _call] 7.2901930740815\n " ,
170
+ " \\ item[\\ $sterr\\ _call] 0.00499340311846781\n " ,
171
+ " \\ item[\\ $price\\ _put] 4.29481225823472\n " ,
172
+ " \\ item[\\ $sterr\\ _put] 0.00363647856411944\n " ,
173
+ " \\ end{description}\n "
174
+ ],
175
+ "text/markdown" : [
176
+ " $price_call\n " ,
177
+ " : 7.2901930740815\n " ,
178
+ " $sterr_call\n " ,
179
+ " : 0.00499340311846781\n " ,
180
+ " $price_put\n " ,
181
+ " : 4.29481225823472\n " ,
182
+ " $sterr_put\n " ,
183
+ " : 0.00363647856411944\n " ,
184
+ " \n " ,
185
+ " \n "
186
+ ],
187
+ "text/plain" : [
188
+ " $price_call\n " ,
189
+ " [1] 7.290193\n " ,
190
+ " \n " ,
191
+ " $sterr_call\n " ,
192
+ " [1] 0.004993403\n " ,
193
+ " \n " ,
194
+ " $price_put\n " ,
195
+ " [1] 4.294812\n " ,
196
+ " \n " ,
197
+ " $sterr_put\n " ,
198
+ " [1] 0.003636479\n "
199
+ ]
200
+ },
201
+ "metadata" : {},
202
+ "output_type" : " display_data"
203
+ }
204
+ ],
205
+ "source" : [
206
+ " antithetic_call_put_mc<-function(nSim, tau, r, sigma, S0, K) {\n " ,
207
+ " \n " ,
208
+ " Z <- rnorm(nSim, mean=0, sd=1)\n " ,
209
+ " \n " ,
210
+ " WT <- sqrt(tau) * Z\n " ,
211
+ " # ST1 and ST2 and the antithetic variates\n " ,
212
+ " ST1 = (S0*exp((r - 0.5*sigma^2)*tau + sigma*WT))\n " ,
213
+ " ST2 = (S0*exp((r - 0.5*sigma^2)*tau + sigma*(-WT)))\n " ,
214
+ " \n " ,
215
+ " # call option price and standard error\n " ,
216
+ " simulated_call_payoffs1 <- exp(-r*tau)*pmax(ST1-K,0)\n " ,
217
+ " simulated_call_payoffs2 <- exp(-r*tau)*pmax(ST2-K,0)\n " ,
218
+ " # get the average\n " ,
219
+ " simulated_call_payoffs <- ( simulated_call_payoffs1 + simulated_call_payoffs2)/2\n " ,
220
+ " price_call <- mean(simulated_call_payoffs)\n " ,
221
+ " sterr_call <- sd(simulated_call_payoffs)/sqrt(nSim)\n " ,
222
+ " \n " ,
223
+ " \n " ,
224
+ " # put option price and standard error\n " ,
225
+ " simulated_put_payoffs1 <- exp(-r*tau)*pmax(K-ST1,0)\n " ,
226
+ " simulated_put_payoffs2 <- exp(-r*tau)*pmax(K-ST2,0)\n " ,
227
+ " # get the average\n " ,
228
+ " simulated_put_payoffs <- (simulated_put_payoffs1+simulated_put_payoffs2)/2\n " ,
229
+ " price_put <- mean(simulated_put_payoffs)\n " ,
230
+ " sterr_put <- sd(simulated_put_payoffs)/sqrt(nSim)\n " ,
231
+ " \n " ,
232
+ " output<-list(price_call=price_call, sterr_call=sterr_call, \n " ,
233
+ " price_put=price_put, sterr_put=sterr_put )\n " ,
234
+ " return(output)\n " ,
235
+ " \n " ,
236
+ " }\n " ,
237
+ " \n " ,
238
+ " set.seed(1)\n " ,
239
+ " results<-antithetic_call_put_mc(n=1000000, tau=0.5, r=0.02, sigma=0.2, S0=102, K=100)\n " ,
240
+ " \n " ,
241
+ " results"
242
+ ]
243
+ },
244
+ {
245
+ "cell_type" : " code" ,
246
+ "execution_count" : 11 ,
247
+ "metadata" : {},
248
+ "outputs" : [],
249
+ "source" : [
250
+ " importance_call_put_mc<-function(nSim, tau, r, sigma, S0, K) {\n " ,
251
+ " \n " ,
252
+ " Z <- rnorm(nSim, mean=0, sd=1)\n " ,
253
+ " WT <- sqrt(tau) * Z\n " ,
254
+ " ST = S0*exp((r - 0.5*sigma^2)*tau + sigma*WT)\n " ,
255
+ " \n " ,
256
+ " # call option price and standard error\n " ,
257
+ " simulated_call_payoffs <- (exp(-r*tau)*pmax(ST-K,0))[ST>K]\n " ,
258
+ " price_call <- mean(simulated_call_payoffs*mean(ST>K))\n " ,
259
+ " sterr_call <- sd(simulated_call_payoffs*mean(ST>K))/sqrt(nSim)\n " ,
260
+ " \n " ,
261
+ " # put option price and standard error\n " ,
262
+ " simulated_put_payoffs <- (exp(-r*tau)*pmax(K-ST,0))[ST<k] \n " ,
263
+ " price_put <- mean(simulated_put_payoffs*mean(st<k))\n " ,
264
+ " sterr_put <- sd(simulated_put_payoffs*mean(st<k))/sqrt(nSim)\n " ,
265
+ " }"
266
+ ]
267
+ },
268
+ {
269
+ "cell_type" : " code" ,
270
+ "execution_count" : null ,
271
+ "metadata" : {},
272
+ "outputs" : [],
273
+ "source" : []
274
+ }
275
+ ],
276
+ "metadata" : {
277
+ "language_info" : {},
278
+ "orig_nbformat" : 3
279
+ },
280
+ "nbformat" : 4 ,
281
+ "nbformat_minor" : 2
282
+ }
0 commit comments