-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathRQuantlib.Rout.save
367 lines (362 loc) · 14.1 KB
/
RQuantlib.Rout.save
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
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
> stopifnot(require(RQuantLib))
Loading required package: RQuantLib
>
> ## values from Quantlib's test-suite
> ## Reference: Haug, Option Pricing Formulas, McGraw-Hill, 1998
> ##
> ## and generally sourced from the code in the test-suite/
> ## directory of the QuantLib distribution
>
> ## europeanoption.cpp: call value == 2.1334
> print(EuropeanOption("call", underlying=60, strike=65, div=0, riskFree=0.08,
+ maturity=0.25, vol=0.3), digits=5)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
2.13337 0.37248 0.04204 11.35154 -8.42817 5.05390 -5.58724
> ## europeanoption.cpp: put value == 2.4648
> print(EuropeanOption("put", underlying=100, strike=95, div=0.05, riskFree=0.1,
+ maturity=0.5, vol=0.2), digits=5)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
2.46479 -0.26418 0.02284 22.83957 -3.00053 -14.44147 13.20908
>
> ## europeanoption.cpp: call delta == 0.5946
> print(EuropeanOption("call", underlying=105, strike=100,div=0.1,riskFree=0.1,
+ maturity=0.5, vol=0.36), digits=4)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
12.4328 0.5946 0.0135 26.7781 -8.3968 25.0016 -31.2180
> ## europeanoption.cpp: put delta == -0.3566
> print(EuropeanOption("put", underlying=105, strike=100,div=0.1,riskFree=0.1,
+ maturity=0.5, vol=0.36), digits=4)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
7.6767 -0.3566 0.0135 26.7781 -8.8725 -22.5599 18.7215
>
> ## europeanoption.cpp: call gamma == 0.0278
> print(EuropeanOption("call", underlying=55, strike=60,div=0.0,riskFree=0.1,
+ maturity=0.75, vol=0.30), digits=4)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
5.3928 0.5333 0.0278 18.9358 -6.1813 17.9557 -22.0003
> ## europeanoption.cpp: put gamma == 0.0278
> print(EuropeanOption("put", underlying=55, strike=60,div=0.0,riskFree=0.1,
+ maturity=0.75, vol=0.30), digits=4)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
6.0574 -0.4667 0.0278 18.9358 -0.6148 -23.7927 19.2497
>
> ## europeanoption.cpp: call vega == 18.9358
> print(EuropeanOption("call", underlying=55, strike=60,div=0.0,riskFree=0.1,
+ maturity=0.75, vol=0.30), digits=4)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
5.3928 0.5333 0.0278 18.9358 -6.1813 17.9557 -22.0003
> ## europeanoption.cpp: put vega == 18.9358
> print(EuropeanOption("put", underlying=55, strike=60,div=0.0,riskFree=0.1,
+ maturity=0.75, vol=0.30), digits=4)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
6.0574 -0.4667 0.0278 18.9358 -0.6148 -23.7927 19.2497
>
>
> ## americanoption.cpp: call value == 10.0089 -- we show 10.00606
> print(AmericanOption("call", underlying=110, strike=100, div=0.1, riskFree=0.1,
+ maturity=0.1, vol=0.15), digits=5)
Concise summary of valuation for AmericanOption
value delta gamma vega theta rho divRho
10.00606 NA NA NA NA NA NA
> ## americanoption.cpp: put value == 0.3159
> print(AmericanOption("call", underlying=90, strike=100, div=0.1, riskFree=0.1,
+ maturity=0.1, vol=0.25), digits=5)
Concise summary of valuation for AmericanOption
value delta gamma vega theta rho divRho
0.3159 NA NA NA NA NA NA
>
>
> # Discrete dividend
> ## europeanoption.cpp: call value == 3.67
> ## Reference pg. 253 - Hull 5th ed Exercise 12.8 - From QuantLib tests
> print(EuropeanOption("call", underlying=40, strike=40, div=0, riskFree=0.09,
+ maturity=0.5, vol=0.3,
+ discreteDividends = c(0.5, 0.5),
+ discreteDividendsTimeUntil = c(2/12, 5/12)), digits=5)
Concise summary of valuation for EuropeanOption
value delta gamma vega theta rho divRho
3.67123 0.58003 0.04722 10.78672 -4.99372 9.64649 NA
>
> ## americanoption.cpp: call value == 3.72 (Hull) -- we show 3.75
> ## Reference p. 256 - Hull 5th ed. Exercise 12.9 using (flawed) Roll, Geske, Whaley formula
> print(AmericanOption("call", underlying=40, strike=40, div=0, riskFree=0.09,
+ maturity=0.5, vol=0.3,
+ engine = "CrankNicolson",
+ discreteDividends = c(0.5, 0.5),
+ discreteDividendsTimeUntil = c(2/12, 5/12)), digits=5)
Concise summary of valuation for AmericanOption
value delta gamma vega theta rho divRho
3.75022 0.57989 0.04613 NA NA NA NA
>
> ## barrier: down and out call == 9.0246
> print(BarrierOption("downout", barrier=95, rebate=3, type="call",
+ strike=90, underlying=100, div=0.04, riskF=0.08,
+ mat=0.5, vol=0.25), digits=4)
Concise summary of valuation for BarrierOption
value delta gamma vega theta rho divRho
9.0246 NA NA NA NA NA NA
> ## barrier: down and in call == 7.7627
> print(BarrierOption("downin", barrier=95, rebate=3, type="call",
+ strike=90, underlying=100, div=0.04, riskF=0.08,
+ mat=0.5, vol=0.25), digits=4)
Concise summary of valuation for BarrierOption
value delta gamma vega theta rho divRho
7.7627 NA NA NA NA NA NA
>
>
> ## binary aka digital: put == 2.6710
> print(BinaryOption(binType="cash", type="put", excType="european",
+ strike=80, underl=100, div=0.06, r=0.06,
+ mat=0.75, vol=0.35, cash=10), digits=4)
Concise summary of valuation for BinaryOption
value delta gamma vega theta rho divRho
2.6710 -0.1061 0.0031 8.1539 -1.7423 -9.9577 7.9545
>
> ## asianoption.cpp: put == 4.6922 (from testAnalyticContinuousGeometricAveragePrice())
> print( AsianOption("geometric", "put", underlying=80, strike=85, div=-0.03, riskFree=0.05, maturity=0.25, vol=0.2))
Concise summary of valuation for AsianOption
value delta gamma vega theta rho divRho
4.6922 -0.8031 0.0594 6.8662 0.0580 -9.2039 8.0309
>
> # simple call with unnamed parameters
>
> bond <- list(faceAmount=100,issueDate=as.Date("2004-11-30"),
+ maturityDate=as.Date("2008-11-30"), redemption=100 )
>
> dateparams <-list(settlementDays=1, calendar="UnitedStates/GovernmentBond", businessDayConvention='Unadjusted')
>
>
> discountCurve.param <- list(tradeDate=as.Date('2002-2-15'),
+ settleDate=as.Date('2002-2-15'),
+ dt=0.25,
+ interpWhat='discount', interpHow='loglinear')
> discountCurve <- DiscountCurve(discountCurve.param, list(flat=0.05))
>
> ZeroCouponBond(bond, discountCurve, dateparams)
Concise summary of valuation for ZeroCouponBond
Net present value : 71.21519
clean price : 81.884
dirty price : 81.884
accrued coupon : 0
yield : 0.050516
cash flows :
Date Amount
2008-11-30 100
>
>
> ## bond.cpp: examples from Fixed Income page of Matlab
> ZeroYield(95, 100, as.Date("1993-6-24"), as.Date("1993-11-1"))
[1] 0.1477733
attr(,"class")
[1] "ZeroYield"
>
> ## bond.cpp: test theoretical price of bond by its yield
> ZeroPriceByYield(0.1478, 100, as.Date("1993-6-24"), as.Date("1993-11-1"))
[1] 94.99914
attr(,"class")
[1] "ZeroPriceByYield"
>
> ## bond.cpp: test theoretical yield of a fixed rate bond, = 0.0307
> FixedRateBondYield(,99.282, 100000, as.Date("2004-11-30"), as.Date("2008-11-30"), 3, , c(0.02875), , , , ,as.Date("2004-11-30"))
[1] 0.03066526
attr(,"class")
[1] "FixedRateBondYield"
>
> ## bond.cpp: test theoretical price of a fixed rate bond = 99.2708
> FixedRateBondPriceByYield(,0.0307, 100000, as.Date("2004-11-30"), as.Date("2008-11-30"), 3, , c(0.02875), , , , ,as.Date("2004-11-30"))
[1] 99.26903
attr(,"class")
[1] "FixedRateBondPriceByYield"
>
> ## bond.cpp
>
> #Simple call with a flat curve
> bond <- list(settlementDays=1,
+ issueDate=as.Date("2004-11-30"),
+ faceAmount=100,
+ dayCounter='Thirty360',
+ paymentConvention='Unadjusted')
> schedule <- list(effectiveDate=as.Date("2004-11-30"),
+ maturityDate=as.Date("2008-11-30"),
+ period='Semiannual',
+ calendar='UnitedStates/GovernmentBond',
+ businessDayConvention='Unadjusted',
+ terminationDateConvention='Unadjusted',
+ dateGeneration='Forward',
+ endOfMonth=1)
> calc=list(dayCounter='Actual360',
+ compounding='Compounded',
+ freq='Annual',
+ durationType='Modified')
> coupon.rate <- c(0.02875)
> params <- list(tradeDate=as.Date('2002-2-15'),
+ settleDate=as.Date('2002-2-19'),
+ dt=.25,
+ interpWhat="discount",
+ interpHow="loglinear")
> discountCurve.flat <- DiscountCurve(params, list(flat=0.05))
> FixedRateBond(bond,
+ coupon.rate,
+ schedule,
+ calc,
+ discountCurve=discountCurve.flat)
Concise summary of valuation for FixedRateBond
Net present value : 80.21276
clean price : 92.167
dirty price : 92.167
accrued coupon : 0
yield : 0.050517
duration : 3.6672
settlement date : 2004-11-30
cash flows :
Date Amount
2005-05-31 1.4375
2005-11-30 1.4375
2006-05-31 1.4375
2006-11-30 1.4375
2007-05-31 1.4375
2007-11-30 1.4375
2008-05-31 1.4375
2008-11-30 1.4375
2008-11-30 100.0000
>
> #Same bond calculated from yield rather than from the discount curve
> yield <- 0.02
> FixedRateBond(bond,
+ coupon.rate,
+ schedule,
+ calc,
+ yield=yield)
Concise summary of valuation for FixedRateBond
Net present value : NaN
clean price : 103.27
dirty price : 103.27
accrued coupon : 0
yield : 0.02
duration : 3.7905
settlement date : 2004-11-30
cash flows :
Date Amount
2005-05-31 1.4375
2005-11-30 1.4375
2006-05-31 1.4375
2006-11-30 1.4375
2007-05-31 1.4375
2007-11-30 1.4375
2008-05-31 1.4375
2008-11-30 1.4375
2008-11-30 100.0000
>
>
> #same example with clean price
> price <- 103.31
> FixedRateBond(bond,
+ coupon.rate,
+ schedule,
+ calc,
+ price = price)
Concise summary of valuation for FixedRateBond
Net present value : NaN
clean price : 103.31
dirty price : 103.31
accrued coupon : 0
yield : 0.019905
duration : 3.7909
settlement date : 2004-11-30
cash flows :
Date Amount
2005-05-31 1.4375
2005-11-30 1.4375
2006-05-31 1.4375
2006-11-30 1.4375
2007-05-31 1.4375
2007-11-30 1.4375
2008-05-31 1.4375
2008-11-30 1.4375
2008-11-30 100.0000
>
> ## bond.cpp FloatingRateBond, following test-suite/bonds.cpp
>
> bond <- list(faceAmount=100, issueDate=as.Date("2004-11-30"),
+ maturityDate=as.Date("2008-11-30"), redemption=100,
+ effectiveDate=as.Date("2004-11-30"))
> dateparams <- list(settlementDays=1, calendar="UnitedStates/GovernmentBond",
+ dayCounter = 'ActualActual', period=2,
+ businessDayConvention = 1, terminationDateConvention=1,
+ dateGeneration=0, endOfMonth=0, fixingDays = 1)
>
> gearings <- spreads <- caps <- floors <- vector()
>
> params <- list(tradeDate=as.Date('2002-2-15'),
+ settleDate=as.Date('2002-2-19'),
+ dt=.25,
+ interpWhat="discount",
+ interpHow="loglinear")
>
> tsQuotes <- list(d1w =0.0382,
+ d1m =0.0372,
+ fut1=96.2875,
+ fut2=96.7875,
+ fut3=96.9875,
+ fut4=96.6875,
+ fut5=96.4875,
+ fut6=96.3875,
+ fut7=96.2875,
+ fut8=96.0875,
+ s3y =0.0398,
+ s5y =0.0443,
+ s10y =0.05165,
+ s15y =0.055175)
>
>
> ## when both discount and libor curves are flat.
>
> discountCurve.flat <- DiscountCurve(params, list(flat=0.05))
> termstructure <- DiscountCurve(params, list(flat=0.03))
> iborIndex.params <- list(type="USDLibor", length=6,
+ inTermOf="Month", term=termstructure)
> FloatingRateBond(bond, gearings, spreads, caps, floors,
+ iborIndex.params, discountCurve.flat, dateparams)
Concise summary of valuation for FloatingRateBond
Net present value : 80.54557
clean price : 92.549
dirty price : 92.549
accrued coupon : 0
yield : 0.050517
cash flows :
Date Amount
2005-05-31 1.4851
2005-11-30 1.4947
2006-05-30 1.4783
2006-11-30 1.5029
2007-05-30 1.4783
2007-11-30 1.5031
2008-05-30 1.4797
2008-11-28 1.4783
2008-11-28 100.0000
>
>
> proc.time()
user system elapsed
0.356 0.020 0.383