File: BasicAmericanOptions.R

package info (click to toggle)
foptions 260.72-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 428 kB
  • ctags: 62
  • sloc: fortran: 1,262; makefile: 13
file content (399 lines) | stat: -rwxr-xr-x 12,253 bytes parent folder | download | duplicates (4)
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

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General 
# Public License along with this library; if not, write to the 
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA

# Copyrights (C)
# for this R-port: 
#   1999 - 2004, Diethelm Wuertz, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   info@rmetrics.org
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# FUNCTION:                  DESCRIPTION:
#  RollGeskeWhaleyOption      Roll-Geske-Whaley Calls on Dividend Paying Stocks
#  BAWAmericanApproxOption    Barone-Adesi and Whaley Approximation
#  BSAmericanApproxOption     Bjerksund and Stensland Approximation
################################################################################


RollGeskeWhaleyOption = 
function(S, X, time1, Time2, r, D, sigma, title = NULL, description = NULL) 
{   # A function implemented by Diethelm Wuertz
 
    # Description:
    #   Calculates the option price of an American call on a stock
    #   paying a single dividend with specified time to divident
    #   payout. The option valuation formula derived by Roll, Geske 
    #   and Whaley is used.
    
    # References:
    #   Haug E.G., The Complete Guide to Option Pricing Formulas
    
    # FUNCTION:
    
    # Settings:
    big = 100000000
    eps = 1.0e-5
    t1 = time1
    T2 = Time2
    
    # Compute:
    Sx = S - D * exp(-r * t1)
    if(D <= X * (1 - exp(-r*(T2-t1)))) {         
        result = GBSOption("c", Sx, X, T2, r, b=r, sigma)@price
        cat("\nWarning: Not optimal to exercise\n")
        return(result) }
    ci = GBSOption("c", S, X, T2-t1, r, b=r, sigma)@price
    HighS = S
    while ( ci-HighS-D+X > 0 && HighS < big ) {
        HighS = HighS * 2
        ci = GBSOption("c", HighS, X, T2-t1, r, b=r, sigma)@price }
    if(HighS > big) {
        result = GBSOption("c", Sx, X, T2, r, b=r, sigma)@price
        stop()}
    LowS = 0
    I = HighS * 0.5
    ci = GBSOption("c", I, X, T2-t1, r, b=r, sigma)@price 
    # Search algorithm to find the critical stock price I
    while ( abs(ci-I-D+X) > eps && HighS - LowS > eps ) {
         if(ci-I-D+X < 0 ) { HighS = I }
        else { LowS = I }
        I = (HighS + LowS) / 2
        ci = GBSOption("c", I, X, T2-t1, r, b=r, sigma)@price }
    a1 = (log(Sx/X) + (r+sigma^2/2)*T2) / (sigma*sqrt(T2))
    a2 = a1 - sigma*sqrt(T2)
    b1 = (log(Sx/I) + (r+sigma^2/2)*t1) / (sigma*sqrt(t1))
    b2 = b1 - sigma*sqrt(t1)
    result = Sx*CND(b1) + Sx*CBND(a1,-b1,-sqrt(t1/T2)) -
        X*exp(-r*T2)*CBND(a2,-b2,-sqrt(t1/T2)) - 
            (X-D)*exp(-r*t1)*CND(b2)
    
    # Parameters:
    # S, X, time1, Time2, r, D, sigma
    param = list()
    param$S = S
    param$X = X
    param$time1 = time1
    param$Time2 = Time2
    param$r = r
    param$D = D
    param$sigma = sigma
    
    # Add title and description:
    if(is.null(title)) title = "Roll Geske Whaley Option"
    if(is.null(description)) description = as.character(date())
    
    # Return Value:
    new("fOPTION", 
        call = match.call(),
        parameters = param,
        price = result, 
        title = title,
        description = description
        )      
}


# ******************************************************************************


BAWAmericanApproxOption = 
function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma, title = NULL, 
description = NULL)
{   # A function implemented by Diethelm Wuertz
 
    # Description:
    #   Calculates the option price of an American call or put
    #   option on an underlying asset for a given cost-of-carry rate.
    #   The quadratic approximation method by Barone-Adesi and
    #   Whaley is used.

    # References:
    #   Haug E.G., The Complete Guide to Option Pricing Formulas
    
    # FUNCTION:
    
    # Settings:
    TypeFlag = TypeFlag[1]
    
    # Compute:
    if(TypeFlag == "c") {
        result = .BAWAmCallApproxOption(S, X, Time, r, b, sigma) }
    if(TypeFlag == "p") {      
        result = .BAWAmPutApproxOption(S, X, Time, r, b, sigma) }
       
    # Parameters:
    # TypeFlag = c("c", "p"), S, X, Time, r, b, sigma
    param = list()
    param$TypeFlag = TypeFlag
    param$S = S
    param$X = X
    param$Time = Time
    param$r = r
    param$b = b
    param$sigma = sigma
    
    # Add title and description:
    if(is.null(title)) title = "BAW American Approximated Option"
    if(is.null(description)) description = as.character(date())
    
    # Return Value:
    new("fOPTION", 
        call = match.call(),
        parameters = param,
        price = result, 
        title = title,
        description = description
        )      
}


.BAWAmCallApproxOption <- 
function(S, X, Time, r, b, sigma) 
{
    # Internal Function - The Call:
        
    # Compute:
    if(b >= r) {
        result = GBSOption("c", S, X, Time, r, b, sigma)@price }
    else {
        Sk = .bawKc(X, Time, r, b, sigma)
        n = 2*b/sigma^2
        K = 2*r/(sigma^2*(1-exp(-r*Time)))
        d1 = (log(Sk/X)+(b+sigma^2/2)*Time)/(sigma*sqrt(Time))
        Q2 = (-(n-1)+sqrt((n-1)^2+4*K))/2
        a2 = (Sk/Q2)*(1-exp((b-r)*Time)*CND(d1))
        if(S < Sk) {
            result = GBSOption("c", S, X, Time, r, b, sigma)@price +
                a2*(S/Sk)^Q2 
        } else {
            result = S-X 
        } 
    }
    
    # Return Value:
    result 
}


.bawKc <- 
function(X, Time, r, b, sigma) 
{   
    # Newton Raphson algorithm to solve for the critical commodity 
    # price for a Call.
    # Calculation of seed value, Si
    n = 2*b/sigma^2
    m = 2*r/sigma^2
    q2u = (-(n-1)+sqrt((n-1)^2+4*m))/2
    Su = X/(1-1/q2u)
    h2 = -(b*Time+2*sigma*sqrt(Time))*X/(Su-X)
    Si = X+(Su-X)*(1-exp(h2))
    K = 2*r/(sigma^2*(1-exp(-r*Time)))
    d1 = (log(Si/X)+(b+sigma^2/2)*Time)/(sigma*sqrt(Time))
    Q2 = (-(n-1)+sqrt((n-1)^2+4*K))/2
    LHS = Si-X
    RHS = GBSOption("c", Si, X, Time, r, b, sigma)@price + 
        (1-exp((b-r)*Time)*CND(d1))*Si/Q2
    bi = exp((b-r)*Time)*CND(d1)*(1-1/Q2) +
        (1-exp((b-r)*Time)*CND(d1)/(sigma*sqrt(Time)))/Q2
    E = 0.000001
    
    # Newton Raphson algorithm for finding critical price Si
    while (abs(LHS-RHS)/X > E) {
        Si = (X+RHS-bi*Si)/(1-bi)
        d1 = (log(Si/X)+(b+sigma^2/2)*Time)/(sigma*sqrt(Time))
        LHS = Si-X
        RHS = GBSOption("c", Si, X, Time, r, b, sigma)@price + 
            (1-exp((b-r)*Time)*CND(d1))*Si/Q2
        bi = exp((b-r)*Time)*CND(d1)*(1-1/Q2) + 
        (   1-exp((b-r)*Time)*CND(d1)/(sigma*sqrt(Time)))/Q2 }
    
    # Return Value:
    Si
}


.BAWAmPutApproxOption <- 
function(S, X, Time, r, b, sigma) 
{
    # Internal Function - The Put:
    
    # Compute:
    Sk = .bawKp(X, Time, r, b, sigma)
    n = 2*b/sigma^2
    K = 2*r/(sigma^2*(1-exp(-r*Time)))
    d1 = (log(Sk/X)+(b+sigma^2/2)*Time)/(sigma*sqrt(Time))
    Q1 = (-(n-1)-sqrt((n-1)^2+4*K))/2
    a1 = -(Sk/Q1)*(1-exp((b-r)*Time)*CND(-d1))
    if(S > Sk) {
        result = GBSOption("p", S, X, Time, r, b, sigma)@price + a1*(S/Sk)^Q1 
    } else {
        result = X-S 
    }  
    
    # Return Value:
    result
}


.bawKp <- 
function(X, Time, r, b, sigma) 
{   
    # Internal Function - used for the Put:
    
    # Newton Raphson algorithm to solve for the critical commodity 
    # price for a Put.
    # Calculation of seed value, Si
    n = 2*b/sigma^2
    m = 2*r/sigma^2
    q1u = (-(n-1)-sqrt((n-1)^2+4*m))/2
    Su = X/(1-1/q1u)
    h1 = (b*Time-2*sigma*sqrt(Time))*X/(X-Su)
    Si = Su+(X-Su)*exp(h1) 
    K = 2*r/(sigma^2*(1-exp(-r*Time)))
    d1 = (log(Si/X)+(b+sigma^2/2)*Time)/(sigma*sqrt(Time))
    Q1 = (-(n-1)-sqrt((n-1)^2+4*K))/2
    LHS = X-Si
    RHS = GBSOption("p", Si, X, Time, r, b, sigma)@price -
        (1-exp((b-r)*Time)*CND(-d1))*Si/Q1
    bi = -exp((b-r)*Time)*CND(-d1)*(1-1/Q1) -
        (1+exp((b-r)*Time)*CND(-d1)/(sigma*sqrt(Time)))/Q1
    E = 0.000001
    # Newton Raphson algorithm for finding critical price Si
    while (abs(LHS-RHS)/X > E ) {
        Si = (X-RHS+bi*Si)/(1+bi)
        d1 = (log(Si/X)+(b+sigma^2/2)*Time)/(sigma*sqrt(Time))
        LHS = X-Si
        RHS = GBSOption("p", Si, X, Time, r, b, sigma)@price -
            (1-exp((b-r)*Time)*CND(-d1))*Si/Q1
        bi = -exp((b-r)*Time)*CND(-d1)*(1-1/Q1) -
            (1+exp((b-r)*Time)*CND(-d1)/(sigma*sqrt(Time)))/Q1 }
    # Return Value:
    Si
}


# ------------------------------------------------------------------------------


BSAmericanApproxOption = 
function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma, title = NULL, 
description = NULL)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Calculates the option price of an American call or 
    #   put option stocks, futures, and currencies. The 
    #   approximation method by Bjerksund and Stensland is used.
    
    # References:
    #   Haug E.G., The Complete Guide to Option Pricing Formulas
    
    # FUNCTION:
    
    # Settings:
    TypeFlag = TypeFlag[1]
    
    # The Bjerksund and Stensland (1993) American approximation:
    if(TypeFlag == "c") {
      result = .BSAmericanCallApprox(S, X, Time, r, b, sigma) }
    if(TypeFlag == "p") {
      # Use the Bjerksund and Stensland put-call transformation
      result = .BSAmericanCallApprox(X, S, Time, r - b, -b, sigma) }
    
    # Parameters:
    # TypeFlag = c("c", "p"), S, X, Time, r, b, sigma
    param = list()
    param$TypeFlag = TypeFlag
    param$S = S
    param$X = X
    param$Time = Time
    param$r = r
    param$b = b
    param$sigma = sigma
    if(!is.na(result$TriggerPrice)) param$TrigerPrice = result$TriggerPrice 
    
    # Add title and description:
    if(is.null(title)) title = "BS American Approximated Option"
    if(is.null(description)) description = as.character(date())
    
    # Return Value:
    new("fOPTION", 
        call = match.call(),
        parameters = param,
        price = result$Premium, 
        title = title,
        description = description
        )      
}


.BSAmericanCallApprox <- 
function(S, X, Time, r, b, sigma) 
{ 
    # Call Approximation:
    
    if(b >= r) { 
        # Never optimal to exersice before maturity
        result = list(
            Premium = GBSOption("c", S, X, Time, r, b, sigma)@price,
            TriggerPrice = NA)
    } else {
    Beta = (1/2 - b/sigma^2) + sqrt((b/sigma^2 - 1/2)^2 + 2*r/sigma^2)
    BInfinity = Beta/(Beta-1) * X
    B0 = max(X, r/(r-b) * X)
    ht = -(b*Time + 2*sigma*sqrt(Time)) * B0/(BInfinity-B0)
    # Trigger Price I:
    I = B0 + (BInfinity-B0) * (1 - exp(ht))
    alpha = (I-X) * I^(-Beta)
    if(S >= I) { 
        result = list(
            Premium = S-X, 
            TriggerPrice = I) }
    else {
        result = list(
            Premium = alpha*S^Beta - alpha*.bsPhi(S,Time,Beta,I,I,r,b,sigma) + 
            .bsPhi(S,Time,1,I,I,r,b,sigma) - .bsPhi(S,Time,1,X,I,r,b,sigma) - 
            X*.bsPhi(S,Time,0,I,I,r,b,sigma) + X*.bsPhi(S,Time,0,X,I,r,b,sigma), 
            TriggerPrice = I) } }
    result}
      

.bsPhi <- 
function(S, Time, gamma, H, I, r, b, sigma) 
{
    # Utility function phi:

    lambda = (-r + gamma*b + 0.5*gamma * (gamma-1)*sigma^2) * Time
    d = -(log(S/H) + (b + (gamma-0.5)*sigma^2)*Time) / 
        (sigma*sqrt(Time))
    kappa = 2 * b / (sigma^2) + (2*gamma - 1)
    result = exp(lambda)*S^gamma * 
    (CND(d)-(I/S)^kappa*CND(d-2*log(I/S)/(sigma*sqrt(Time))))
    
    # Return Value:
    result 
}


################################################################################