[Rquantlib-commits] r177 - papers/rinfinance2010
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Feb 21 05:51:18 CET 2010
Author: knguyen
Date: 2010-02-21 05:51:16 +0100 (Sun, 21 Feb 2010)
New Revision: 177
Added:
papers/rinfinance2010/BondsR2.cpp
papers/rinfinance2010/QLBondExample.R
Modified:
papers/rinfinance2010/rquantlib_slides.tex
Log:
rfinance10 slides and codes
Added: papers/rinfinance2010/BondsR2.cpp
===================================================================
--- papers/rinfinance2010/BondsR2.cpp (rev 0)
+++ papers/rinfinance2010/BondsR2.cpp 2010-02-21 04:51:16 UTC (rev 177)
@@ -0,0 +1,461 @@
+/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+/*!
+ Copyright (C) 2008 Florent Grenier
+
+ This file is part of QuantLib, a free-software/open-source library
+ for financial quantitative analysts and developers - http://quantlib.org/
+
+ QuantLib is free software: you can redistribute it and/or modify it
+ under the terms of the QuantLib license. You should have received a
+ copy of the license along with this program; if not, please email
+ <quantlib-dev at lists.sf.net>. The license is also available online at
+ <http://quantlib.org/license.shtml>.
+
+ This program 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 license for more details.
+ */
+
+/* This example shows how to set up a term structure and then price
+ some simple bonds. The last part is dedicated to peripherical
+ computations such as "Yield to Price" or "Price to Yield"
+ */
+
+// the only header you need to use QuantLib
+#include <ql/quantlib.hpp>
+
+#include <boost/timer.hpp>
+#include <iostream>
+#include <iomanip>
+
+using namespace QuantLib;
+
+#if defined(QL_ENABLE_SESSIONS)
+namespace QuantLib {
+
+Integer sessionId() { return 0; }
+
+}
+#endif
+
+
+int main(int, char* []) {
+
+ try {
+
+ boost::timer timer;
+ std::cout << std::endl;
+
+ /*********************
+ *** MARKET DATA ***
+ *********************/
+
+ Calendar calendar = TARGET();
+
+ Date settlementDate(18, September, 2008);
+ // must be a business day
+ settlementDate = calendar.adjust(settlementDate);
+
+ Integer fixingDays = 3;
+ Natural settlementDays = 3;
+
+ Date todaysDate = calendar.advance(settlementDate, -fixingDays, Days);
+ // nothing to do with Date::todaysDate
+ Settings::instance().evaluationDate() = todaysDate;
+
+ std::cout << "Today: " << todaysDate.weekday()
+ << ", " << todaysDate << std::endl;
+
+ std::cout << "Settlement date: " << settlementDate.weekday()
+ << ", " << settlementDate << std::endl;
+
+
+ // Building of the bonds discounting yield curve
+
+ /*********************
+ *** RATE HELPERS ***
+ *********************/
+
+ // RateHelpers are built from the above quotes together with
+ // other instrument dependant infos. Quotes are passed in
+ // relinkable handles which could be relinked to some other
+ // data source later.
+
+ // Common data
+
+ // ZC rates for the short end
+ Rate zc3mQuote=0.0096;
+ Rate zc6mQuote=0.0145;
+ Rate zc1yQuote=0.0194;
+
+ boost::shared_ptr<Quote> zc3mRate(new SimpleQuote(zc3mQuote));
+ boost::shared_ptr<Quote> zc6mRate(new SimpleQuote(zc6mQuote));
+ boost::shared_ptr<Quote> zc1yRate(new SimpleQuote(zc1yQuote));
+
+ DayCounter zcBondsDayCounter = Actual365Fixed();
+
+ boost::shared_ptr<RateHelper> zc3m(new DepositRateHelper(
+ Handle<Quote>(zc3mRate),
+ 3*Months, fixingDays,
+ calendar, ModifiedFollowing,
+ true, zcBondsDayCounter));
+ boost::shared_ptr<RateHelper> zc6m(new DepositRateHelper(
+ Handle<Quote>(zc6mRate),
+ 6*Months, fixingDays,
+ calendar, ModifiedFollowing,
+ true, zcBondsDayCounter));
+ boost::shared_ptr<RateHelper> zc1y(new DepositRateHelper(
+ Handle<Quote>(zc1yRate),
+ 1*Years, fixingDays,
+ calendar, ModifiedFollowing,
+ true, zcBondsDayCounter));
+
+ // setup bonds
+ Real redemption = 100.0;
+
+ const Size numberOfBonds = 5;
+
+ Date issueDates[] = {
+ todaysDate + 3,
+ todaysDate + 3,
+ todaysDate + 3,
+ todaysDate + 3,
+ todaysDate + 3
+ };
+
+ Date maturities[] = {
+ calendar.advance(todaysDate + 3, 5, Years),
+ calendar.advance(todaysDate + 3, 6, Years),
+ calendar.advance(todaysDate + 3, 7, Years),
+ calendar.advance(todaysDate + 3, 16, Years),
+ calendar.advance(todaysDate + 3, 48, Years)
+ };
+
+ Real couponRates[] = {
+ 0.02375,
+ 0.04625,
+ 0.03125,
+ 0.04000,
+ 0.04500
+ };
+
+ Real marketQuotes[] = {
+ 100.390625,
+ 106.21875,
+ 100.59375,
+ 101.6875,
+ 102.140625
+ };
+
+ std::vector< boost::shared_ptr<SimpleQuote> > quote;
+ for (Size i=0; i<numberOfBonds; i++) {
+ boost::shared_ptr<SimpleQuote> cp(new SimpleQuote(marketQuotes[i]));
+ quote.push_back(cp);
+ }
+
+ RelinkableHandle<Quote> quoteHandle[numberOfBonds];
+ for (Size i=0; i<numberOfBonds; i++) {
+ quoteHandle[i].linkTo(quote[i]);
+ }
+
+ // Definition of the rate helpers
+ std::vector<boost::shared_ptr<FixedRateBondHelper> > bondsHelpers;
+
+ for (Size i=0; i<numberOfBonds; i++) {
+
+ Schedule schedule(issueDates[i], maturities[i], Period(Semiannual), UnitedStates(UnitedStates::GovernmentBond),
+ Unadjusted, Unadjusted, DateGeneration::Backward, false);
+
+ boost::shared_ptr<FixedRateBondHelper> bondHelper(new FixedRateBondHelper(
+ quoteHandle[i],
+ settlementDays,
+ 100.0,
+ schedule,
+ std::vector<Rate>(1,couponRates[i]),
+ ActualActual(ActualActual::Bond),
+ Unadjusted,
+ redemption,
+ issueDates[i]));
+
+ bondsHelpers.push_back(bondHelper);
+ }
+
+ /*********************
+ ** CURVE BUILDING **
+ *********************/
+
+ // Any DayCounter would be fine.
+ // ActualActual::ISDA ensures that 30 years is 30.0
+ DayCounter termStructureDayCounter =
+ ActualActual(ActualActual::ISDA);
+
+ double tolerance = 1.0e-15;
+
+ // A depo-bond curve
+ std::vector<boost::shared_ptr<RateHelper> > bondInstruments;
+
+ // Adding the ZC bonds to the curve for the short end
+// bondInstruments.push_back(zc3m);
+ // bondInstruments.push_back(zc6m);
+ // bondInstruments.push_back(zc1y);
+
+ // Adding the Fixed rate bonds to the curve for the long end
+ for (Size i=0; i<numberOfBonds; i++) {
+ bondInstruments.push_back(bondsHelpers[i]);
+ }
+
+ ExponentialSplinesFitting exponentialSplines(true);
+ boost::shared_ptr<YieldTermStructure> bondDiscountingTermStructure(
+ new FittedBondDiscountCurve(settlementDays,
+ calendar,
+ bondsHelpers,
+ termStructureDayCounter,
+ exponentialSplines,
+ tolerance,
+ 5000));
+
+// boost::shared_ptr<YieldTermStructure> bondDiscountingTermStructure(
+ // new PiecewiseYieldCurve<Discount,LogLinear>(
+ // settlementDate, bondInstruments,
+ // termStructureDayCounter,
+ // tolerance));
+
+ // Building of the Libor forecasting curve
+ // deposits
+ Rate d1wQuote=0.043375;
+ Rate d1mQuote=0.031875;
+ Rate d3mQuote=0.0320375;
+ Rate d6mQuote=0.03385;
+ Rate d9mQuote=0.0338125;
+ Rate d1yQuote=0.0335125;
+ // swaps
+ Rate s2yQuote=0.0295;
+ Rate s3yQuote=0.0323;
+ Rate s5yQuote=0.0359;
+ Rate s10yQuote=0.0412;
+ Rate s15yQuote=0.0433;
+
+
+ /********************
+ *** QUOTES ***
+ ********************/
+
+ // SimpleQuote stores a value which can be manually changed;
+ // other Quote subclasses could read the value from a database
+ // or some kind of data feed.
+
+ // deposits
+ boost::shared_ptr<Quote> d1wRate(new SimpleQuote(d1wQuote));
+ boost::shared_ptr<Quote> d1mRate(new SimpleQuote(d1mQuote));
+ boost::shared_ptr<Quote> d3mRate(new SimpleQuote(d3mQuote));
+ boost::shared_ptr<Quote> d6mRate(new SimpleQuote(d6mQuote));
+ boost::shared_ptr<Quote> d9mRate(new SimpleQuote(d9mQuote));
+ boost::shared_ptr<Quote> d1yRate(new SimpleQuote(d1yQuote));
+ // swaps
+ boost::shared_ptr<Quote> s2yRate(new SimpleQuote(s2yQuote));
+ boost::shared_ptr<Quote> s3yRate(new SimpleQuote(s3yQuote));
+ boost::shared_ptr<Quote> s5yRate(new SimpleQuote(s5yQuote));
+ boost::shared_ptr<Quote> s10yRate(new SimpleQuote(s10yQuote));
+ boost::shared_ptr<Quote> s15yRate(new SimpleQuote(s15yQuote));
+
+ /*********************
+ *** RATE HELPERS ***
+ *********************/
+
+ // RateHelpers are built from the above quotes together with
+ // other instrument dependant infos. Quotes are passed in
+ // relinkable handles which could be relinked to some other
+ // data source later.
+
+ // deposits
+ DayCounter depositDayCounter = Actual360();
+
+ boost::shared_ptr<RateHelper> d1w(new DepositRateHelper(
+ Handle<Quote>(d1wRate),
+ 1*Weeks, fixingDays,
+ calendar, ModifiedFollowing,
+ true, depositDayCounter));
+ boost::shared_ptr<RateHelper> d1m(new DepositRateHelper(
+ Handle<Quote>(d1mRate),
+ 1*Months, fixingDays,
+ calendar, ModifiedFollowing,
+ true, depositDayCounter));
+ boost::shared_ptr<RateHelper> d3m(new DepositRateHelper(
+ Handle<Quote>(d3mRate),
+ 3*Months, fixingDays,
+ calendar, ModifiedFollowing,
+ true, depositDayCounter));
+ boost::shared_ptr<RateHelper> d6m(new DepositRateHelper(
+ Handle<Quote>(d6mRate),
+ 6*Months, fixingDays,
+ calendar, ModifiedFollowing,
+ true, depositDayCounter));
+ boost::shared_ptr<RateHelper> d9m(new DepositRateHelper(
+ Handle<Quote>(d9mRate),
+ 9*Months, fixingDays,
+ calendar, ModifiedFollowing,
+ true, depositDayCounter));
+ boost::shared_ptr<RateHelper> d1y(new DepositRateHelper(
+ Handle<Quote>(d1yRate),
+ 1*Years, fixingDays,
+ calendar, ModifiedFollowing,
+ true, depositDayCounter));
+
+ // setup swaps
+ Frequency swFixedLegFrequency = Annual;
+ BusinessDayConvention swFixedLegConvention = Unadjusted;
+ DayCounter swFixedLegDayCounter = Thirty360(Thirty360::European);
+ boost::shared_ptr<IborIndex> swFloatingLegIndex(new Euribor6M);
+
+ const Period forwardStart(1*Days);
+
+ boost::shared_ptr<RateHelper> s2y(new SwapRateHelper(
+ Handle<Quote>(s2yRate), 2*Years,
+ calendar, swFixedLegFrequency,
+ swFixedLegConvention, swFixedLegDayCounter,
+ swFloatingLegIndex, Handle<Quote>(),forwardStart));
+ boost::shared_ptr<RateHelper> s3y(new SwapRateHelper(
+ Handle<Quote>(s3yRate), 3*Years,
+ calendar, swFixedLegFrequency,
+ swFixedLegConvention, swFixedLegDayCounter,
+ swFloatingLegIndex, Handle<Quote>(),forwardStart));
+ boost::shared_ptr<RateHelper> s5y(new SwapRateHelper(
+ Handle<Quote>(s5yRate), 5*Years,
+ calendar, swFixedLegFrequency,
+ swFixedLegConvention, swFixedLegDayCounter,
+ swFloatingLegIndex, Handle<Quote>(),forwardStart));
+ boost::shared_ptr<RateHelper> s10y(new SwapRateHelper(
+ Handle<Quote>(s10yRate), 10*Years,
+ calendar, swFixedLegFrequency,
+ swFixedLegConvention, swFixedLegDayCounter,
+ swFloatingLegIndex, Handle<Quote>(),forwardStart));
+ boost::shared_ptr<RateHelper> s15y(new SwapRateHelper(
+ Handle<Quote>(s15yRate), 15*Years,
+ calendar, swFixedLegFrequency,
+ swFixedLegConvention, swFixedLegDayCounter,
+ swFloatingLegIndex, Handle<Quote>(),forwardStart));
+
+
+ /*********************
+ ** CURVE BUILDING **
+ *********************/
+
+ // Any DayCounter would be fine.
+ // ActualActual::ISDA ensures that 30 years is 30.0
+
+ // A depo-swap curve
+ std::vector<boost::shared_ptr<RateHelper> > depoSwapInstruments;
+ depoSwapInstruments.push_back(d1w);
+ depoSwapInstruments.push_back(d1m);
+ depoSwapInstruments.push_back(d3m);
+ depoSwapInstruments.push_back(d6m);
+ depoSwapInstruments.push_back(d9m);
+ depoSwapInstruments.push_back(d1y);
+ depoSwapInstruments.push_back(s2y);
+ depoSwapInstruments.push_back(s3y);
+ depoSwapInstruments.push_back(s5y);
+ depoSwapInstruments.push_back(s10y);
+ depoSwapInstruments.push_back(s15y);
+ boost::shared_ptr<YieldTermStructure> depoSwapTermStructure(
+ new PiecewiseYieldCurve<Discount,LogLinear>(
+ settlementDate, depoSwapInstruments,
+ termStructureDayCounter,
+ tolerance));
+
+ // Term structures that will be used for pricing:
+ // the one used for discounting cash flows
+ RelinkableHandle<YieldTermStructure> discountingTermStructure;
+ // the one used for forward rate forecasting
+ RelinkableHandle<YieldTermStructure> forecastingTermStructure;
+
+ /*********************
+ * BONDS TO BE PRICED *
+ **********************/
+
+ // Common data
+ Real faceAmount = 100;
+
+ // Pricing engine
+ boost::shared_ptr<PricingEngine> bondEngine(
+ new DiscountingBondEngine(discountingTermStructure));
+
+ // Zero coupon bond
+ ZeroCouponBond zeroCouponBond(
+ settlementDays,
+ UnitedStates(UnitedStates::GovernmentBond),
+ faceAmount,
+ Date(15,August,2013),
+ Following,
+ Real(116.92),
+ Date(15,August,2003));
+
+ zeroCouponBond.setPricingEngine(bondEngine);
+
+ // Fixed 4.5% US Treasury Note
+ Schedule fixedBondSchedule(Date(15, May, 2007),
+ Date(15,May,2017), Period(Semiannual),
+ UnitedStates(UnitedStates::GovernmentBond),
+ Unadjusted, Unadjusted, DateGeneration::Backward, false);
+
+ FixedRateBond fixedRateBond(
+ settlementDays,
+ faceAmount,
+ fixedBondSchedule,
+ std::vector<Rate>(1, 0.045),
+ ActualActual(ActualActual::Bond),
+ ModifiedFollowing,
+ 100.0, Date(15, May, 2007));
+
+ fixedRateBond.setPricingEngine(bondEngine);
+
+
+ // Yield curve bootstrapping
+ forecastingTermStructure.linkTo(depoSwapTermStructure);
+ discountingTermStructure.linkTo(bondDiscountingTermStructure);
+
+
+ /***************
+ * BOND PRICING *
+ ****************/
+
+ std::cout << std::endl;
+
+ // write column headings
+ Size widths[] = { 18, 10, 10, 10 };
+
+ std::cout << std::setw(widths[0]) << " "
+ << std::setw(widths[1]) << "ZC"
+ << std::setw(widths[2]) << "Fixed"
+ << std::endl;
+
+ std::string separator = " | ";
+ Size width = widths[0]
+ + widths[1]
+ + widths[2]
+ + widths[3];
+ std::string rule(width, '-'), dblrule(width, '=');
+ std::string tab(8, ' ');
+
+ std::cout << rule << std::endl;
+
+ std::cout << std::fixed;
+ std::cout << std::setprecision(2);
+
+ std::cout << std::setw(widths[0]) << "Net present value"
+ << std::setw(widths[1]) << zeroCouponBond.NPV()
+ << std::setw(widths[2]) << fixedRateBond.NPV()
+ << std::endl;
+
+
+ return 0;
+
+ } catch (std::exception& e) {
+ std::cout << e.what() << std::endl;
+ return 1;
+ } catch (...) {
+ std::cout << "unknown error" << std::endl;
+ return 1;
+ }
+}
+
Added: papers/rinfinance2010/QLBondExample.R
===================================================================
--- papers/rinfinance2010/QLBondExample.R (rev 0)
+++ papers/rinfinance2010/QLBondExample.R 2010-02-21 04:51:16 UTC (rev 177)
@@ -0,0 +1,69 @@
+library(RQuantLib)
+
+fixingDays <- 3
+settlementDays <- 3
+
+settlementDate <- as.Date('2008-09-18')
+todaysDate <- settlementDate - fixingDays
+
+#begin to set up bond discounting term structure
+lengths <- c(5, 6, 7, 16, 48)
+coupons <- c(0.02375, 0.04625, 0.03125, 0.04000, 0.04500)
+marketQuotes <- c(100.390625, 106.21875, 100.59375, 101.6875, 102.140625)
+dateparams <- list(settlementDays=settlementDays,
+ period=2,
+ dayCounter="ActualActual",
+ businessDayConvention ="Unadjusted")
+curveparams <- list(method="ExponentialSplinesFitting",
+ origDate=todaysDate)
+bondDsctTsr <- FittedBondCurve(curveparams,
+ lengths,
+ coupons,
+ marketQuotes,
+ dateparams)
+
+#begin to set up swap term structure
+swp.tsr.params <- list(tradeDate=todaysDate,
+ settleDate=todaysDate+2,
+ dt=0.25,
+ interpWhat="discount",
+ interpHow="loglinear")
+market.quotes <- list(d1w=0.043375,
+ d1m=0.031875,
+ d3m=0.0320375,
+ d6m=0.03385,
+ d9m=0.0338125,
+ d1y=0.0335125,
+ s2y=0.0295,
+ s3y=0.0323,
+ s5y=0.0359,
+ s10y=0.0412,
+ s15y=0.0433)
+depoSwpTsr <- DiscountCurve(swp.tsr.params, market.quotes)
+
+#Zero-Coupon Bond
+zc.bond.param <- list(maturityDate=as.Date('2013-08-15'),
+ issueDate=as.Date('2003-08-15'),
+ redemption=116.92)
+zc.bond.dateparam <- list(refDate=todaysDate,
+ settlementDays=settlementDays,
+ businessDayConvention='Following')
+ZeroCouponBond(zc.bond.param, bondDsctTsr, zc.bond.dateparam)
+
+#Fixed-Coupon Bond
+fixed.bond.param <- list(maturityDate=as.Date('2017-05-15'),
+ issueDate=as.Date('2007-05-15'),
+ redemption=100,
+ effectiveDate=as.Date('2007-05-15'))
+fixed.bond.dateparam <- list(settlementDays=settlementDays,
+ dayCounter='ActualActual',
+ period='Semiannual',
+ businessDayConvention='Unadjusted',
+ terminationDateConvention='Unadjusted',
+ dateGeneration='Backward',
+ endOfMonth=0)
+fixed.bond.coupon <- c(0.045)
+FixedRateBond(fixed.bond.param,
+ fixed.bond.coupon,
+ bondDsctTsr,
+ fixed.bond.dateparam)
Modified: papers/rinfinance2010/rquantlib_slides.tex
===================================================================
--- papers/rinfinance2010/rquantlib_slides.tex 2010-02-13 05:01:03 UTC (rev 176)
+++ papers/rinfinance2010/rquantlib_slides.tex 2010-02-21 04:51:16 UTC (rev 177)
@@ -1,4 +1,3 @@
-
%% add 'handout' option for handouts, and pgfpages for 2-on-1
\documentclass[smaller,compress]{beamer}
%\usepackage{pgfpages}
@@ -89,6 +88,210 @@
\end{frame}
+
+\begin{frame}
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Quick overview}
+ \begin{itemize}
+ \item Fixed Income functions are added during the summer of 2009 as part of the Google Summer of Code program.
+ \item The functions aim to support two primary tasks: pricing and curve fitting.
+ \end{itemize}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Primary tasks: Curve fitting}
+ \begin{itemize}
+ \item Curve fitting
+ \begin{itemize}
+ \item Curve fitting functions return a DiscountCurve object that contains a two column dates/zeroRates data frame.
+ \item The returned DiscountCurve object are used as inputs for pricing functions.
+ \item Currently, there are two curve fitting functions
+ \begin{itemize}
+ \item DiscountCurve - constructs the spot term structure of interest rates based on input market data including the settltment date, deposit rates, future prices, FRA rates or swap rates in various combination.
+ \item FittedBondCurve - fits a term structure to a set of bonds using three different fitting methods (ExponentialSplinesFitting, SimplePolynomialFitting, NelsonSiegelFitting).
+ \end{itemize}
+ \end{itemize}
+ \end{itemize}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Primary tasks: Pricing}
+ \begin{itemize}
+ \item Pricing
+ \begin{itemize}
+ \item Zero Coupon Bond
+ \item Fixed Rate Bond
+ \item Floating Rate Bond
+ \item Convertible Zero Coupon Bond
+ \item Convertible Fixed Rate Bond
+ \item Convertible Floating Rate Bond
+ \item Callable Bond
+ \end{itemize}
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Examples: Curve fitting}
+ \begin{itemize}
+ \item DiscountCurve example:
+
+ \lstset{language=R,basicstyle=\tiny}
+ \begin{lstlisting}
+params <- list(tradeDate=as.Date('2004-09-20'),
+ settleDate=as.Date('2004-09-22'),
+ interpWhat="discount",
+ interpHow="loglinear")
+tsQuotes <- list(d1w = 0.0382,
+ d1m = 0.0372,
+ d3m = 0.0363,
+ d6m = 0.0353,
+ d9m = 0.0348,
+ d1y = 0.0345,
+ fut2=96.7875,
+ fut3=96.9875,
+ fut4=96.6875,
+ fut5=96.4875,
+ fut7=96.2875,
+ s2y = 0.037125,
+ s3y = 0.0398,
+ s5y = 0.0443,
+ s10y = 0.05165,
+ s15y = 0.055175)
+curves <- DiscountCurve(params, tsQuotes)
+\end{lstlisting}
+\end{itemize}
+\end{frame}
+
+
+
+\begin{frame}[fragile]
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Examples: Curve fitting}
+ \begin{itemize}
+ \item FittedBondCurve example:
+ \lstset{language=R,basicstyle=\tiny}
+ \begin{lstlisting}
+lengths <- c(2,4,6,8,10,12,14,16,18,20,22,24,26,28,30)
+coupons <- c(0.0200, 0.0225, 0.0250, 0.0275, 0.0300,
+ 0.0325, 0.0350, 0.0375, 0.0400, 0.0425,
+ 0.0450, 0.0475, 0.0500, 0.0525, 0.0550 )
+marketQuotes <- rep(100, length(lengths))
+dateparams <- list(settlementDays=0, period="Annual",
+ dayCounter="ActualActual",
+ businessDayConvention ="Unadjusted")
+curveparams <- list(method="ExponentialSplinesFitting",
+ origDate = Sys.Date())
+curve <- FittedBondCurve(curveparams, lengths, coupons, marketQuotes, dateparams)
+library(zoo)
+z <- zoo(curve$table$zeroRates, order.by=curve$table$date)
+plot(z)
+ \end{lstlisting}
+
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Examples: Bond pricing}
+\begin{columns}[c] % the "c" option specifies center vertical alignment
+\column{.5\textwidth} % column designated by a command
+ \lstset{language=R,basicstyle=\tiny}
+ \begin{lstlisting}
+fixingDays <- 3
+settlementDays <- 3
+
+settlementDate <- as.Date('2008-09-18')
+todaysDate <- settlementDate - fixingDays
+
+#begin to set up bond discounting term structure
+lengths <- c(5, 6, 7, 16, 48)
+coupons <- c(0.02375, 0.04625, 0.03125, 0.04000, 0.04500)
+marketQuotes <- c(100.390625, 106.21875, 100.59375, 101.6875, 102.140625)
+dateparams <- list(settlementDays=settlementDays,
+ period=2,
+ dayCounter="ActualActual",
+ businessDayConvention ="Unadjusted")
+curveparams <- list(method="ExponentialSplinesFitting",
+ origDate=todaysDate)
+bondDsctTsr <- FittedBondCurve(curveparams,
+ lengths,
+ coupons,
+ marketQuotes,
+ dateparams)
+\end{lstlisting}
+\column{.5\textwidth}
+
+[I tried to line up the corresponding C++ codes in BondsR2.cpp
+here but it was too long compared to the R codes. Thoughts?]
+\lstset{language=C++,basicstyle=\tiny}
+ \begin{lstlisting}
+
+ \end{lstlisting}
+\end{columns}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Examples: Bond pricing}
+\begin{columns}[c] % the "c" option specifies center vertical alignment
+\column{.5\textwidth} % column designated by a command
+ \lstset{language=R,basicstyle=\tiny}
+ \begin{lstlisting}
+#begin to set up swap term structure
+swp.tsr.params <- list(tradeDate=todaysDate,
+ settleDate=todaysDate+2,
+ dt=0.25,
+ interpWhat="discount",
+ interpHow="loglinear")
+market.quotes <- list(d1w=0.043375,
+ d1m=0.031875,
+ d3m=0.0320375,
+ d6m=0.03385,
+ d9m=0.0338125,
+ d1y=0.0335125,
+ s2y=0.0295,
+ s3y=0.0323,
+ s5y=0.0359,
+ s10y=0.0412,
+ s15y=0.0433)
+depoSwpTsr <- DiscountCurve(swp.tsr.params, market.quotes)
+\end{lstlisting}
+\column{.5\textwidth}
+
+\end{columns}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Fixed Income in RQuantLib}
+ \framesubtitle{Examples: Bond pricing}
+\begin{columns}[c] % the "c" option specifies center vertical alignment
+\column{.5\textwidth} % column designated by a command
+ \lstset{language=R,basicstyle=\tiny}
+ \begin{lstlisting}
+#Zero-Coupon Bond
+zc.bond.param <- list(maturityDate=as.Date('2013-08-15'), issueDate=as.Date('2003-08-15'), redemption=116.92)
+zc.bond.dateparam <- list(refDate=todaysDate, settlementDays=settlementDays, businessDayConvention='Following')
+ZeroCouponBond(zc.bond.param, bondDsctTsr, zc.bond.dateparam)
+
+#Fixed-Coupon Bond
+fixed.bond.param <- list(maturityDate=as.Date('2017-05-15'), issueDate=as.Date('2007-05-15'),redemption=100, effectiveDate=as.Date('2007-05-15'))
+fixed.bond.dateparam <- list(settlementDays=settlementDays, dayCounter='ActualActual', period='Semiannual', businessDayConvention='Unadjusted', terminationDateConvention='Unadjusted', dateGeneration='Backward', endOfMonth=0)
+fixed.bond.coupon <- c(0.045)
+FixedRateBond(fixed.bond.param,
+ fixed.bond.coupon,
+ bondDsctTsr,
+ fixed.bond.dateparam)
+\end{lstlisting}
+\column{.5\textwidth}
+
+\end{columns}
+\end{frame}
+
+
+
\end{document}
%%% Local Variables:
More information about the Rquantlib-commits
mailing list