R Under development (unstable) (2024-07-23 r86915 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 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.76638 0.58704 0.04706 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 > > #.onWindows <- .Platform$OS.type == "windows" > > ## 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.19654 clean price : 81.873 dirty price : 81.873 accrued coupon : 0 yield : 0.050551 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.19268 clean price : 92.155 dirty price : 92.155 accrued coupon : 0 yield : 0.050551 duration : 3.667 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.53149 clean price : 92.544 dirty price : 92.544 accrued coupon : 0 yield : 0.050551 cash flows : Date Amount 2005-05-31 1.4858 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.4831 2008-11-28 1.4824 2008-11-28 100.0000 > > proc.time() user system elapsed 0.39 0.04 0.37