Professional Documents
Culture Documents
Bond Examples Fabozzi
Bond Examples Fabozzi
Part I
Mauricio Bedoya
javierma36@gmail.com
February 2014
In the previous blog, we implement some code to price option free bonds. However, due to
the different template parameters and available constructors, most of the people get lost.
In this blog, I’m going to implement some examples that you can find in: Fixed Income
Mathematics (Fabozzi), fourth edition. For more information about QuantLib, go to:http:
//quantlib.org/reference/index.html. I will also recommend to read Luigi Ballabio blog
(http://implementingquantlib.blogspot.com)1 .
Bond2:
Maturity: 20 years
Coupon: 9 % (Simuannual)
Redemption:1000
Requiered Yield: 7 %
CleanPrice: 1213.55
Both bonds will be implemented with FixedRateBond class. The second one, will use Relinka-
bleHandle class.
1
Code implemente in Xcode 4.5.2, with QuantLib version 1.4 and boost version 1.52.0.
1
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
Fixed Income Mathematics Four E d i t i o n
3 pag ( 7 4 , 7 5 )
Time path o f t h e P r i c e o f a D i s c o u n t Bond :
5 20−Years , 9 % coupon , 12 % Required Y i e l d
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
7
#i n c l u d e <i o s t r e a m >
9 #i n c l u d e <q l / q u a n t l i b . hpp>
#i n c l u d e <a l g o r i t h m >
11
u s i n g namespace QuantLib ;
13 u s i n g namespace s t d ;
15 i n t main ( )
{
17 try {
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Bond I n f o r m a t i o n ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
19 Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
DayCounter DC = A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ;
21 N a t u r a l Se tt le me nt Day s = 3 ;
Date Today = Date : : todaysDate ( ) ;
23 S e t t i n g s : : i n s t a n c e ( ) . e v a l u a t i o n D a t e ( ) = Today ;
Date I s s u e = Today ;
25 Date Maturity = I s s u e+ 20∗ Years ;
Rate Coupon = 0 . 0 9 ;
27 I n t e r e s t R a t e CouponRate ( Coupon , A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ,
Compounded , Semiannual ) ;
Real FaceValue = 1 0 0 0 . 0 ;
29 v e c t o r <I n t e r e s t R a t e > Coupons ( 4 0 , CouponRate ) ;
2
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ TermStructure C o n s t r u c t o r ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > TS( new FlatForward ( I s s u e , Rate1 ,
A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) , Compounded , Semiannual ) ) ;
3 F l a t R a t e . l i n k T o (TS) ;
b o o s t : : s h a r e d p t r <P r i c i n g E n g i n e > BondEngine ( new DiscountingBondEngine (
FlatRate ) ) ;
5 BondInstrument−>s e t P r i c i n g E n g i n e ( BondEngine ) ;
c o u t << ”Bond1 R e s u l t s \n” ;
7 c o u t << ”Book P r i c e : ” << 7 7 4 . 3 << e n d l ;
c o u t << ” QuantLib : ” << BondInstrument−>NPV( ) << e n d l ;
9 c o u t << ”\n” ;
21 c a t c h ( s t d : : e x c e p t i o n& e ) {
c e r r << e . what ( ) << e n d l ;
23 }
}
2. Duration
Now lets estimate the duration of a bond (Simple, Modified, Macaulay). Check pag 215 in
Fabozzi book.
Bond3:
Maturity: 5 years
Coupon: 10 % (Simuannual)
Redemption:100.0
Requiered Yield: 10 %
CleanPrice: 100.0
3
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 Fixed Income Mathematics Four E d i t i o n
pag ( 2 1 5 )
4 Bond I n f o r m a t i o n :
5−Years , 10 % coupon , 10 % Required Y i e l d
6 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
#i n c l u d e <i o s t r e a m >
8 #i n c l u d e <q l / q u a n t l i b . hpp>
#i n c l u d e <a l g o r i t h m >
10
u s i n g namespace QuantLib ;
12 u s i n g namespace s t d ;
14 i n t main ( )
{
16 try {
/∗ ∗∗∗∗∗∗∗∗∗ Bond I n f o r m a t i o n ∗∗∗∗∗∗∗∗∗ ∗/
18 Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
DayCounter DC = A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ;
20 N a t u r a l Se tt le me nt Day s = 3 ;
Date Today = Date : : todaysDate ( ) ;
22 S e t t i n g s : : i n s t a n c e ( ) . e v a l u a t i o n D a t e ( ) = Today ;
Date I s s u e = Today ;
24 Date Maturity = I s s u e+ 5∗ Years ;
Rate Coupon = 0 . 1 0 ;
26 Compounding compounding = Compounded ;
Frequency f r e q u e n c y = Semiannual ;
28 I n t e r e s t R a t e CouponRate ( Coupon , A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ,
compounding , f r e q u e n c y ) ;
Real FaceValue = 1 0 0 . 0 ;
30 s t d : : v e c t o r <I n t e r e s t R a t e > Coupons ( 1 0 , CouponRate ) ;
4
/∗ ∗∗∗∗∗∗∗∗∗ TermStructure C o n s t r u c t o r ∗∗∗∗∗∗∗∗∗ ∗/
2 b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > TS( new FlatForward ( I s s u e , Rate1 ,
A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) , compounding , f r e q u e n c y ) ) ;
F l a t R a t e . l i n k T o (TS) ;
4 b o o s t : : s h a r e d p t r <P r i c i n g E n g i n e > BondEngine ( new DiscountingBondEngine (
FlatRate ) ) ;
BondInstrument−>s e t P r i c i n g E n g i n e ( BondEngine ) ;
6
/∗ ∗∗∗∗∗∗∗∗∗ P r i n t ∗∗∗∗∗∗∗∗∗ ∗/
8 c o u t << ”Bond1 R e s u l t s \n” ;
c o u t << ”Book P r i c e : ” << 1 0 0 . 0 << e n d l ;
10 c o u t << ” QuantLib : ” << BondInstrument−>NPV( ) << ” (Ok , i s Par ) ” << e n d l ;
c o u t << ” Duration ( Simple ) : ” << BondFunctions : : d u r a t i o n ( ∗ ∗ BondInstrument
, I n t e r e s t R a t e ( r a t e , DC, compounding , f r e q u e n c y ) , Duration : : Simple ) << ” vs
Book : 4 . 0 5 (Ok) ” << e n d l ;
12 c o u t << ” Duration ( Macaulay ) : ” << BondFunctions : : d u r a t i o n ( ∗ ∗
BondInstrument , I n t e r e s t R a t e ( r a t e , DC, compounding , f r e q u e n c y ) , Duration : :
Macaulay ) << ” vs Book : 4 . 0 5 (Ok) ”<< e n d l ;
c o u t << ” Duration ( M o d i f i e d ) : ” << BondFunctions : : d u r a t i o n ( ∗ ∗
BondInstrument , I n t e r e s t R a t e ( r a t e , DC, compounding , f r e q u e n c y ) , Duration : :
M o d i f i e d ) << ” vs Book : 3 . 8 6 (Ok) ” << e n d l ;
14 }
16 c a t c h ( s t d : : e x c e p t i o n& e ) {
c e r r << e . what ( ) << e n d l ;
18 }
}
Now in the reference manual , search for other Public Memeber Functions (Owned and Inheri-
ted). You have a lot of stuff there: date, cashflow, coupon inspector; and many others. These
inspectors allow you to calculate other measures found in the book and not in QuantLib 1.4,
like: dollar Duration, dollar Convexity, total price volatility (duration + convexity), etc.
5
3. Price Volatility for Option Free Bonds
Now lets demonstrate the relation between yield / price volatility (duration). In page 219,
Fabozzi says that we can use the modified duration to estimate the new price change (bond
price volatility).
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
Fixed Income Mathematics Four E d i t i o n
3 pag ( 2 1 9 − I l u s t r a t i o n 13−3)
Link between d u r a t i o n and p r i v e v o l a t i l i t y
5 Bond I n f o r m a t i o n :
15−Years , 8 % coupon , 10 % Required Y i e l d
7 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
#i n c l u d e <i o s t r e a m >
9 #i n c l u d e <q l / q u a n t l i b . hpp>
#i n c l u d e <a l g o r i t h m >
11
u s i n g namespace QuantLib ;
13 u s i n g namespace s t d ;
15 i n t main ( )
{
17 t r y { /∗ ∗∗∗∗∗∗∗∗∗ Bond I n f o r m a t i o n ∗∗∗∗∗∗∗∗∗ ∗/
Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
19 DayCounter DC = A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ;
N a t u r a l Se tt le me nt Day s = 3 ;
21 Date Today = Date : : todaysDate ( ) ;
S e t t i n g s : : i n s t a n c e ( ) . e v a l u a t i o n D a t e ( ) = Today ;
23 Date I s s u e = Today ;
Date Maturity = I s s u e+ 15∗ Years ;
25 Rate Coupon = 0 . 0 8 ;
Compounding compounding = Compounded ;
27 Frequency f r e q u e n c y = Semiannual ;
I n t e r e s t R a t e CouponRate ( Coupon , A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ,
compounding , f r e q u e n c y ) ;
29 Real FaceValue = 1 0 0 . 0 ;
s t d : : v e c t o r <I n t e r e s t R a t e > Coupons ( 3 0 , CouponRate ) ;
31
R e l i n k a b l e H a n d l e <Yi eldT erm Str uct ure >F l a t R a t e ;
33 R e l i n k a b l e H a n d l e <FixedRateBond>BondInstrument ;
6
/∗ ∗∗∗∗∗∗∗∗∗ TermStructure C o n s t r u c t o r ∗∗∗∗∗∗∗∗∗ ∗/
2 b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > TS( new FlatForward ( I s s u e , Rate1 ,
A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) , compounding , f r e q u e n c y ) ) ;
F l a t R a t e . l i n k T o (TS) ;
4 b o o s t : : s h a r e d p t r <P r i c i n g E n g i n e > BondEngine ( new DiscountingBondEngine (
FlatRate ) ) ;
BondInstrument−>s e t P r i c i n g E n g i n e ( BondEngine ) ;
6
/∗ ∗∗∗∗∗∗∗∗∗ P r i n t I n i t i a l R e s u l t ∗∗∗∗∗∗∗∗∗ ∗/
8 Real BondPrice 0 = BondInstrument−>NPV( ) ;
Real M o d i f i e d D u r a t i o n = BondFunctions : : d u r a t i o n ( ∗ ∗ BondInstrument ,
I n t e r e s t R a t e ( r a t e ,DC, compounding , f r e q u e n c y ) ) ;
10 c o u t << ”Bond1 R e s u l t s \n” ;
c o u t << ” QuantLib : ” << BondPrice 0 << e n d l ;
12 c o u t << ” Duration ( M o d i f i e d ) : ” << M o d i f i e d D u r a t i o n << e n d l ;
c o u t << ”\n \n” ;
14
/∗ ∗∗∗∗∗∗∗∗∗ New f l a t c u r v e r a t e ( s m a l l s p r e a d ) ∗∗∗∗∗∗∗∗∗∗
16 For a s m a l l spread , t h e d u r a t i o n p r i c e a p p r o x i m a t i o n o f p r i c e
works r e l a t i v e l y w e l l .
18 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
Spread s m a l l s p r e a d = 0 . 0 0 1 ;
20 Handle<Quote> R a t e 1 S m a l l S p r e a d ( new SimpleQuote ( r a t e + s m a l l s p r e a d ) ) ;
b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > T S s m a l l s p r e a d ( new FlatForward (
I s s u e , Rate1 SmallSpread , A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) , compounding ,
frequency ) ) ;
22 FlatRate . linkTo ( TS smallspread ) ;
7
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗ P r i n t P r i c e a f t e r s p r e a d ( Large ) ∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
3 Approx BondPrice 1 = ( 1 − ( M o d i f i e d D u r a t i o n ∗ l a r g e s p r e a d ) ) ∗
BondPrice 0 ;
Real B o n d P r i c e L a r g e S p re a d = BondInstrument−>NPV( ) ;
5
c o u t << ”Bond R e s u l t a f t e r s p r e a d i n y i e l d o f : ” << l a r g e s p r e a d << e n d l ;
7 c o u t << setw ( 1 5 ) << ”New Bond P r i c e : ” << setw ( 6 ) <<
B o n d P r i c e L a r g e S p r e a d << e n d l ;
c o u t << setw ( 1 5 ) << ” Duration BondPrice Aprox : ” << setw ( 6 ) <<
Approx BondPrice 1 << ” ( Not Good ) ” << e n d l ;
9 }
11 c a t c h ( s t d : : e x c e p t i o n& e ) {
c e r r << e . what ( ) << e n d l ;
13 }
}
Duration consider linear price change effect (first order Taylor change in bond price), but bond
price is not lineal. Fabozzi recommends to make an additional adjustment to the price, when
spread is large (convexity adjustment done latter). At the same time, Fabozzi explore the effect
of yield change in total return( bond price change + coupon reinvestment) when developing
immunization strategies: Exhibit 13-6 in book. However, immunization will work only in case
of parallel curve shift and small yield change.
Now lest estimate the price change based on convexity and duration for an option free bond:
Ilustration 14.2 in book (page 253).
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 Fixed Income Mathematics Four E d i t i o n
pag ( 2 5 3 − I l u s t r a t i o n 14−2)
4 Link between d u r a t i o n and p r i v e v o l a t i l i t y
Bond I n f o r m a t i o n :
6 15−Years , 8 % coupon ,
Y i e l d 0 = 10 %, Y i e l d 1 = 13 %
8 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
#i n c l u d e <i o s t r e a m >
10 #i n c l u d e <q l / q u a n t l i b . hpp>
#i n c l u d e <a l g o r i t h m >
12 #i n c l u d e <b o o s t / format . hpp>
14 u s i n g namespace QuantLib ;
u s i n g namespace s t d ;
8
1 void l i n e ( )
{
3 f o r ( i n t i = 1 ; i < 50 ; i ++)
c o u t << ’− ’ ;
5 c o u t << e n d l ;
}
7
i n t main ( )
9 {
try {
11 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Bond I n f o r m a t i o n ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
13 DayCounter DC = A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ;
N a t u r a l Se tt le me nt Day s = 3 ;
15 Date Today = Date : : todaysDate ( ) ;
S e t t i n g s : : i n s t a n c e ( ) . e v a l u a t i o n D a t e ( ) = Today ;
17 Date I s s u e = Today ;
Date Maturity = I s s u e+ 15∗ Years ;
19 Rate Coupon = 0 . 0 8 ;
Compounding compounding = Compounded ;
21 Frequency f r e q u e n c y = Semiannual ;
I n t e r e s t R a t e CouponRate ( Coupon , A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ,
compounding , f r e q u e n c y ) ;
23 Real FaceValue = 1 0 0 . 0 ;
s t d : : v e c t o r <I n t e r e s t R a t e > Coupons ( 3 0 , CouponRate ) ;
25
9
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ P r i n t I n i t i a l R e s u l t ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
2 Real BondPrice 0 = BondInstrument−>NPV( ) ;
Real M o d i f i e d D u r a t i o n = BondFunctions : : d u r a t i o n ( ∗ ∗ BondInstrument ,
I n t e r e s t R a t e ( r a t e ,DC, compounding , f r e q u e n c y ) ) ;
4 Real Convexity = BondFunctions : : c o n v e x i t y ( ∗ ∗ BondInstrument , I n t e r e s t R a t e
( r a t e , DC, compounding , f r e q u e n c y ) ) ;
c o u t << ”Bond1 R e s u l t s \n” ;
6 c o u t << ” QuantLib : ” << BondPrice 0 << e n d l ;
c o u t << ” Duration ( M o d i f i e d ) : ” << M o d i f i e d D u r a t i o n << e n d l ;
8 c o u t << ” Convexity : ” << Convexity << e n d l ;
c o u t << ”\n \n” ;
10
/∗ ∗∗∗∗∗∗∗∗∗∗ New f l a t c u r v e r a t e ( s m a l l s p r e a d ) ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
12 For an s m a l l spread , t h e d u r a t i o n p r i c e a p p r o x i m a t i o n o f p r i c e
works r e l a t i v e l l y w e l l .
14 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
Spread s m a l l s p r e a d = 0 . 0 0 1 ;
16 Handle<Quote> R a t e 1 S m a l l S p r e a d ( new SimpleQuote ( r a t e + s m a l l s p r e a d ) ) ;
b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > T S s m a l l s p r e a d ( new FlatForward (
I s s u e , Rate1 SmallSpread , A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) , compounding ,
frequency ) ) ;
18 FlatRate . linkTo ( TS smallspread ) ;
Real C o n v e x i t y E f f e c t = 0 . 5 ∗ Convexity ∗ l a r g e s p r e a d ∗ l a r g e s p r e a d ;
34 Real D u r a t i o n E f f e c t = −M o d i f i e d D u r a t i o n ∗ l a r g e s p r e a d ;
b o o s t : : format FORMATER( ” % −25 s % −20 s ” ) ;
36 c o u t << ” Y i e l d change from 0 . 1 t o 0 . 1 3 ” << e n d l ;
line () ;
38 c o u t << FORMATER % ” P r i c e Change Based on” % ”Aproxx % P r i c e Change \n” ;
line () ;
40 c o u t << FORMATER % ” Duration ” % D u r a t i o n E f f e c t << e n d l ;
c o u t << FORMATER % ” Convexity ” % C o n v e x i t y E f f e c t << e n d l ;
42 line () ;
c o u t << FORMATER % ”TOTAL” % ( D u r a t i o n E f f e c t + C o n v e x i t y E f f e c t ) << e n d l ;
44 c o u t << ”\n \n” ;
10
c o u t << ”The a c t u a l % change i n p r i c e would be : ” << PriceChange ;
2
}
4
c a t c h ( s t d : : e x c e p t i o n& e ) {
6 c e r r << e . what ( ) << e n d l ;
}
8 }
from this yield curve, we have to identify: spot rates, forward rates, and price a bond. For more
information, check the book. In QuantLib proceed as follow:
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 Fixed Income Mathematics Four E d i t i o n
pag ( 3 2 4 − 3 2 5 )
4 S e t Y i e l d curve , and g e t : z e r o r a t e s ,
f o r w a r d r a t e s , and p r i c e an o p t i o n f r e e
6 bond .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
8 #i n c l u d e <i o s t r e a m >
#i n c l u d e <q l / q u a n t l i b . hpp>
10 #i n c l u d e <v e c t o r >
#i n c l u d e <s t r i n g >
12 #i n c l u d e <b o o s t / format . hpp>
#i n c l u d e <b o o s t / a s s i g n / s t d / v e c t o r . hpp>
14
u s i n g namespace QuantLib ;
16 u s i n g namespace s t d ;
u s i n g namespace b o o s t : : a s s i g n ;
18
b o o s t : : format FORMATER( ” % −8s % −8.4d” ) ;
11
1 i n t main ( )
{
3 try {
/∗ ∗∗∗∗ Real data ∗∗∗∗ ∗/
5 c o n s t S i z e numberOfBonds ( 3 ) ;
Real Mar ket Pric e [ numberOfBonds ] = { 1 0 0 , 1 0 0 , 1 0 0 } ;
7
/∗ ∗∗∗∗ Quote data ∗∗∗∗ ∗/
9 v e c t o r <b o o s t : : s h a r e d p t r <SimpleQuote> > Quotes ;
21
/∗ ∗∗∗∗ Bonds I n f o r m a t i o n ∗∗∗∗ ∗/
23 I n t e g e r length [ ] = {1 , 2 , 3};
Real coupons [ ] = { 0 . 0 3 5 , 0 . 0 4 , 0 . 0 4 5 } ;
25 Real y i e l d s [ ] = { 0 . 0 3 5 , 0 . 0 4 , 0 . 0 4 5 } ;
Frequency f r e q u e n c y = Annual ;
27 DayCounter dc = SimpleDayCounter ( ) ;
BusinessDayConvention a c c r u a l C o n v e n t i o n = M o d i f i e d F o l l o w i n g ;
29 BusinessDayConvention Convention = M o d i f i e d F o l l o w i n g ;
Real Redemption = 1 0 0 ;
31
Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
33 Date today = c a l e n d a r . a d j u s t ( Date : : todaysDate ( ) ) ;
S e t t i n g s : : i n s t a n c e ( ) . e v a l u a t i o n D a t e ( ) = today ;
35
N a t u r a l bondSettlementDays = 0 ;
37 Date BondSettlementDate = c a l e n d a r . advance ( today , bondSettlementDays ∗Days
);
12
b o o s t : : s h a r e d p t r <BondHelper> B1 ( new FixedRateBondHelper ( q u o t e H a n d l e s
[ 0 ] , bondSettlementDays , 1 0 0 . 0 , SchBond1 , v e c t o r <Rate >(1 , coupons [ 0 ] ) , dc ,
Convention , Redemption ) ) ;
2
13
c o u t << FORMATER % 1 % z e r o 1 Y e a r << e n d l ;
2 c o u t << FORMATER % 2 % z e r o 2 Y e a r << e n d l ;
c o u t << FORMATER % 3 % z e r o 3 Y e a r << ”\n \n” ;
4
/∗ ∗∗∗∗ Get Forward Rates ( page 325 i n book ) ∗∗∗∗ ∗/
6 Rate F0x1 = TS−>forwardRate ( today , MaturityBond1 ,
termStructureDayCounter , Simple ) ;
Rate F1x2 = TS−>forwardRate ( MaturityBond1 , MaturityBond2 , dc , Compounded
);
8 Rate F2x3 = TS−>forwardRate ( MaturityBond2 , MaturityBond3 , dc , Compounded
);
34 }
36 c a t c h ( s t d : : e x c e p t i o n& e ) {
c e r r << e . what ( ) << e n d l ;
38 }
}
14
Normal Models and when γ = 1, we have LogNormal models.
In QuantLib ShortRateModel class, you can identify that LogNormal models aren’t developed
yet2 . That’s way, I will implement: Black-Karasinski, and Hull-White.
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
Fixed Income Mathematics Four E d i t i o n
3 pag ( 3 2 4 − 3 2 5 )
Estimat e C a l l Bond P r i c e and
5 Call option value .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
7 #i n c l u d e <i o s t r e a m >
#i n c l u d e <q l / q u a n t l i b . hpp>
9 #i n c l u d e <v e c t o r >
#i n c l u d e <s t r i n g >
11 #i n c l u d e <b o o s t / format . hpp>
#i n c l u d e <b o o s t / a s s i g n / s t d / v e c t o r . hpp>
13
u s i n g namespace QuantLib ;
15 u s i n g namespace s t d ;
u s i n g namespace b o o s t : : a s s i g n ;
17
b o o s t : : format FORMATER( ” % −7s % −8.6d % −11.6d % −10.4d % −12.4d % 1 0 . 4 d” ) ;
19
i n t main ( )
21 {
try {
23 /∗ ∗∗∗∗ Real data ∗∗∗∗ ∗/
c o n s t S i z e numberOfBonds ( 3 ) ;
25 Real Mar ket Pric e [ numberOfBonds ] = { 1 0 0 , 1 0 0 , 1 0 0 } ;
33 f o r ( S i z e i = 0 ; i != numberOfBonds ; i ++)
{
35 b o o s t : : s h a r e d p t r <SimpleQuote> Q( new SimpleQuote ( M ark etPr ice [ i ] ) ) ;
Quotes += Q;
37 q u o t e H a n d l e s [ i ] . l i n k T o ( Quotes [ i ] ) ;
}
2
However, VarianceGamma and other process are already implemented.
15
/∗ ∗∗∗∗ Bonds I n f o r m a t i o n ∗∗∗∗ ∗/
2 I n t e g e r length [ ] = {1 , 2 , 3};
Real coupons [ ] = { 0 . 0 3 5 , 0 . 0 4 , 0 . 0 4 5 } ;
4 Real y i e l d s [ ] = { 0 . 0 3 5 , 0 . 0 4 , 0 . 0 4 5 } ;
Frequency f r e q u e n c y = Annual ;
6 DayCounter dc = SimpleDayCounter ( ) ;
BusinessDayConvention a c c r u a l C o n v e n t i o n = M o d i f i e d F o l l o w i n g ;
8 BusinessDayConvention Convention = M o d i f i e d F o l l o w i n g ;
Real Redemption = 1 0 0 ;
10
Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
12 Date today = c a l e n d a r . a d j u s t ( Date : : todaysDate ( ) ) ;
S e t t i n g s : : i n s t a n c e ( ) . e v a l u a t i o n D a t e ( ) = today ;
14
N a t u r a l bondSettlementDays = 0 ;
16 Date BondSettlementDate = c a l e n d a r . advance ( today , bondSettlementDays ∗Days
);
16
1 /∗ ∗∗∗∗ Bond3 C o n s t r u c t o r ∗∗∗∗ ∗/
Date MaturityBond3 = c a l e n d a r . advance ( BondSettlementDate , l e n g t h [ 2 ] ∗
Years ) ;
3 /∗ ∗∗∗∗∗ S c h e d u l e ∗/
S c h e d u l e SchBond3 ( BondSettlementDate , MaturityBond3 , P e r i o d ( f r e q u e n c y ) ,
c a l e n d a r , a c c r u a l C o n v e n t i o n , a c c r u a l C o n v e n t i o n , DateGeneration : : Backward ,
false ) ;
5 b o o s t : : s h a r e d p t r <BondHelper> B3 ( new FixedRateBondHelper ( q u o t e H a n d l e s
[ 2 ] , bondSettlementDays , 1 0 0 . 0 , SchBond3 , v e c t o r <Rate >(3 , coupons [ 2 ] ) , dc ,
Convention , Redemption ) ) ;
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗ The Curve ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
7 Be c o n s i s t e n t . I f you u s e h e l p e r s ,
c o n t i n u e u s i n g them u n t i l c u r v e b u i l d i n g .
9 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
DayCounter termStructureDayCounter = A c t u a l A c t u a l ( A c t u a l A c t u a l : : ISDA) ;
11 Real t o l e r a n c e = 1 . 0 e −12;
v e c t o r <b o o s t : : s h a r e d p t r <RateHelper> > BondPrices ;
13 BondPrices += B1 , B2 , B3 ;
15 b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > TS( new P i e c e w i s e Y i e l d C u r v e <
Discount , LogLinear >(BondSettlementDate , BondPrices , termStructureDayCounter ,
tolerance ) ) ;
17
1 Date C a l l D a t e = c a l e n d a r . advance ( today , 1 ∗ Years ) ; // S e t
C a l l Date
b o o s t : : s h a r e d p t r <C a l l a b i l i t y > c a l l a b i l i t y ( new C a l l a b i l i t y ( C a l l P r i c e ,
C a l l a b i l i t y : : Call , CallDate ) ) ;
3 C a l l S c h +=c a l l a b i l i t y ;
23 /∗ ∗∗∗∗ R e s u l t s ∗∗∗∗ ∗/
Bond . s e t P r i c i n g E n g i n e ( BondEngine ) ;
25 Real Bond Price = Bond . c l e a n P r i c e ( ) ; // Option f r e e bond v a l u e
Rate Bond Yield = Bond . y i e l d (BondDC , Compounded , CouponFrequency ,
acc uracy , m a x I t e r a t i o n s ) ;
27
callableBond . setPricingEngine ( engine0 ) ;
29 Real CallPrice HW = c a l l a b l e B o n d . c l e a n P r i c e ( ) ;
Rate C a l l B o n d Y i e l d 1 = c a l l a b l e B o n d . y i e l d (BondDC , Compounded ,
CouponFrequency , ac curacy , m a x I t e r a t i o n s ) ;
31 Real OptPrice1 = Bond Price − CallPrice HW ;
18
Rate C a l l B o n d Y i e l d 2 = c a l l a b l e B o n d . y i e l d (BondDC , Compounded ,
CouponFrequency , ac curacy , m a x I t e r a t i o n s ) ;
2 Real OptPrice2 = Bond Price − C a l l P r i c e B K ;
4 Real BondPrice = 1 0 1 . 2 6 5 4 ;
Spread z S p r e a d = 10000 ∗ BondFunctions : : zSpread ( Bond , BondPrice , TS , dc
, Compounded , f r e q u e n c y ) ;
6 Spread HW oas = z S p r e a d − OptPrice1 ;
Spread BK oas = z S p r e a d − OptPrice2 ;
8
/∗ ∗∗∗∗ P r i n t ∗∗∗∗ ∗/
10 s t r i n g r u l e ( 7 0 , ’− ’ ) ;
c o u t << r u l e << e n d l ;
12 c o u t << FORMATER % ”MODEL” % ”Bond” % ” C a l l Bond” % ”Opt . Value ” % ”Z−
s p r e a d ( bsp ) ” % ”OAS( bsp ) ” << e n d l ;
c o u t << r u l e << e n d l ;
14 c o u t << FORMATER % ”HW: ” % Bond Price % CallPrice HW % OptPrice1 %
z S p r e a d % HW oas << e n d l ;
c o u t << FORMATER % ”BK: ” % Bond Price % C a l l P r i c e B K % OptPrice2 %
z S p r e a d % BK oas << e n d l ;
16 }
18 c a t c h ( s t d : : e x c e p t i o n& e ) {
c e r r << e . what ( ) << e n d l ;
20 }
}
19
1 /∗ −∗− mode : c++; tab−width : 4 ; i n d e n t −tabs−mode : n i l ; c−b a s i c −o f f s e t : 4 −∗− ∗/
3 /∗
Copyright (C) 2014 M a u r i c i o bedoya
5
This f i l e i s p a r t o f QuantLib , a f r e e −s o f t w a r e / open−s o u r c e l i b r a r y
7 f o r f i n a n c i a l q u a n t i t a t i v e a n a l y s t s and d e v e l o p e r s − h t t p : / / q u a n t l i b . o r g /
23 #i n c l u d e <q l / e v e n t . hpp>
#i n c l u d e <q l / c a s h f l o w s / s i m p l e c a s h f l o w . hpp>
25
namespace QuantLib {
27
// ! Base c l a s s f o r l o a n s
29 /∗ ! This c l a s s i s p u r e l y v i r t u a l and a c t s a s a b a s e c l a s s f o r t h e
actual loans implementations .
31 ∗/
c l a s s Loan : p u b l i c Event {
33 public :
v i r t u a l Leg B e g i n i n g B a l a n c e ( ) c o n s t = 0 ;
35 v i r t u a l Leg Payment ( ) c o n s t = 0 ;
v i r t u a l Leg N e t I n t e r e s t ( ) c o n s t = 0 ;
37 v i r t u a l Leg Fee ( ) c o n s t = 0 ;
v i r t u a l Leg PrincipalRepayment ( ) c o n s t = 0 ;
39 v i r t u a l Leg EndingBalance ( ) c o n s t = 0 ;
v i r t u a l v o i d a c c e p t ( A c y c l i c V i s i t o r& v ) ;
41 /∗ ∗∗ from Event ∗∗ ∗/
v i r t u a l Date d a t e ( ) c o n s t = 0 ;
43 };
45 }
#e n d i f
20
/∗ −∗− mode : c++; tab−width : 4 ; i n d e n t −tabs−mode : n i l ; c−b a s i c −o f f s e t : 4 −∗− ∗/
2
/∗
4 Copyright (C) 2014 M a u r i c i o bedoya
20 #i n c l u d e ” l o a n . hpp”
#i n c l u d e <q l / p a t t e r n s / v i s i t o r . hpp>
22
namespace QuantLib {
24 v o i d Loan : : a c c e p t ( A c y c l i c V i s i t o r& v )
{
26 V i s i t o r <Loan> ∗ v1 = d y n a m i c c a s t <V i s i t o r <Loan> ∗>(&v ) ;
i f ( v1 !=0)
28 v1−>v i s i t ( ∗ t h i s ) ;
else
30 Event : : a c c e p t ( v ) ;
}
32 }
21
/∗ −∗− mode : c++; tab−width : 4 ; i n d e n t −tabs−mode : n i l ; c−b a s i c −o f f s e t : 4 −∗− ∗/
2
/∗
4 Copyright (C) 2014 M a u r i c i o bedoya
20 #i f n d e f q u a n t l i b m o r t g a g e h p p
#d e f i n e q u a n t l i b m o r t g a g e h p p
22
#i n c l u d e ” l o a n . hpp”
24 #i n c l u d e <q l / i n t e r e s t r a t e . hpp>
#i n c l u d e <q l / time / c a l e n d a r . hpp>
26 #i n c l u d e <q l / time / d a y c o u n t e r . hpp>
#i n c l u d e <q l / time / d a t e g e n e r a t i o n r u l e . hpp>
28 #i n c l u d e <q l / c a s h f l o w s / s i m p l e c a s h f l o w . hpp>
30 namespace QuantLib {
c l a s s Mortgage : p u b l i c Loan{
32 public :
Mortgage ( c o n s t Date& i s s u e D a t e , c o n s t Calendar& c a l e n d a r , c o n s t Real&
amount , c o n s t I n t e r e s t R a t e& r a t e , c o n s t Rate& f e e , c o n s t I n t e g e r& term , c o n s t
I n t e g e r& yearPayments ) ;
34 v i r t u a l ˜ Mortgage ( ) ;
//@{
36 /∗ ∗∗ I n s p e c t o r ∗∗ ∗/
Date i s s u e D a t e ( ) ;
38 Real amount ( ) ;
InterestRate rate () ;
40 Rate f e e ( ) ;
42 /∗ ∗∗ S e t t e r s ∗∗ ∗/
void s e t i s s u e D a t e ( c o n s t Date& new Date ) ;
44 void set amount ( c o n s t Real& new amount ) ;
void s e t r a t e ( c o n s t I n t e r e s t R a t e& n e w r a t e ) ;
46 void s e t f e e ( c o n s t Rate& n e w f e e ) ;
22
/∗ ∗∗ Pure V i r t u a l ∗∗ ∗/
2 Leg B e g i n i n g B a l a n c e ( ) c o n s t ;
Leg Payment ( ) c o n s t ;
4 Leg N e t I n t e r e s t ( ) c o n s t ;
Leg Fee ( ) c o n s t ;
6 Leg PrincipalRepayment ( ) c o n s t ;
Leg EndingBalance ( ) c o n s t ;
8 Date d a t e ( ) c o n s t ; // From Event c l a s s
10 /∗ ∗∗ V i s i t o r ∗∗ ∗/
v i r t u a l v o i d a c c e p t ( A c y c l i c V i s i t o r& v ) ;
12 //@}
14 private :
Date i s s u e D a t e ;
16 Calendar c a l e n d a r ;
Real amount ;
18 I n t e g e r term , yearPayments ;
Compounding compound ;
20 InterestRate rate ;
Rate f e e ;
22
/∗ ∗∗ H e l p e r s ∗∗ ∗/
24 Time t = ( 1 . 0 / yearPayments ) ;
I n t e g e r numberOfPayments = ( term ∗ yearPayments ) ;
26 Real compoundFactor = r a t e . compoundFactor ( t ) ;
};
28 }
30 #e n d i f /∗ d e f i n e d ( hh mortgage ) ∗/
23
/∗ −∗− mode : c++; tab−width : 4 ; i n d e n t −tabs−mode : n i l ; c−b a s i c −o f f s e t : 4 −∗− ∗/
2
/∗
4 Copyright (C) 2014 M a u r i c i o bedoya
20
#i n c l u d e ” mortgage . hpp”
22 #i n c l u d e <q l / i n t e r e s t r a t e . hpp>
#i n c l u d e <q l / time / d a y c o u n t e r . hpp>
24 #i n c l u d e <q l / time / d a t e g e n e r a t i o n r u l e . hpp>
#i n c l u d e <b o o s t / a s s i g n / s t d / v e c t o r . hpp>
26
u s i n g namespace b o o s t : : a s s i g n ;
28
namespace QuantLib {
30
Mortgage : : Mortgage ( c o n s t Date& i s s u e D a t e , c o n s t Calendar& c a l e n d a r , c o n s t
Real& amount , c o n s t I n t e r e s t R a t e& r a t e , c o n s t Rate& f e e , c o n s t I n t e g e r& term ,
c o n s t I n t e g e r& yearPayments ) : i s s u e D a t e ( i s s u e D a t e ) , c a l e n d a r ( c a l e n d a r ) ,
amount ( amount ) , r a t e ( r a t e ) , f e e ( f e e ) , term ( term ) , yearPayments (
yearPayments ) {}
32
Mortgage : : ˜ Mortgage ( ) {}
34
//@{
36 /∗ ∗∗ I n s p e c t o r ∗∗ ∗/
Date Mortgage : : i s s u e D a t e ( ) { r e t u r n i s s u e D a t e ; }
38 Real Mortgage : : amount ( ) { r e t u r n amount ; }
I n t e r e s t R a t e Mortgage : : r a t e ( ) { r e t u r n r a t e ; }
40 Rate Mortgage : : f e e ( ) { r e t u r n f e e ; }
42 /∗ ∗∗ S e t t e r s ∗∗ ∗/
void Mortgage : : s e t i s s u e D a t e ( c o n s t Date& new Date ) { i s s u e D a t e = new Date ; }
44 void Mortgage : : set amount ( c o n s t Real& new amount ) { amount = new amount ; }
void Mortgage : : s e t r a t e ( c o n s t I n t e r e s t R a t e& n e w r a t e ) { r a t e = n e w r a t e ; }
46 void Mortgage : : s e t f e e ( c o n s t Rate& n e w f e e ) { f e e = n e w f e e ; }
24
/∗ ∗∗ Pure V i r t u a l ∗∗ ∗/
2 Leg Mortgage : : B e g i n i n g B a l a n c e ( ) c o n s t
{
4 Real denominator = pow ( compoundFactor , numberOfPayments ) − 1 ;
Leg b e g i n i n g b a l a n c e ;
6
f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)
8 {
Date next = i s s u e D a t e + Months ∗ ( ( i +1) ∗ 12 / yearPayments ) ;
10 b o o s t : : s h a r e d p t r <CashFlow> BMB( new SimpleCashFlow ( amount ∗ ( pow (
compoundFactor , numberOfPayments ) − pow ( compoundFactor , i ) ) / denominator ,
next ) ) ;
b e g i n i n g b a l a n c e += BMB;
12 }
14 return beginingbalance ;
}
16
Leg Mortgage : : Payment ( ) c o n s t
18 {
Real numerator = amount ∗ ( ( compoundFactor − 1 ) ∗ pow ( compoundFactor ,
numberOfPayments ) ) ;
20 Real denominator = pow ( compoundFactor , numberOfPayments ) − 1 ;
Real M = numerator / denominator ;
22 Leg payment ;
25
1 Leg Mortgage : : N e t I n t e r e s t ( ) c o n s t
{
3 Real denominator = pow ( compoundFactor , numberOfPayments ) − 1 ;
Leg f e e s = Fee ( ) ;
5 Leg n e t i n t e r e s t ;
f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)
7 {
Date next = i s s u e D a t e + Months ∗ ( ( i +1) ∗ 12 / yearPayments ) ;
9 b o o s t : : s h a r e d p t r <CashFlow> I ( new SimpleCashFlow ( amount ∗ ( ( (
compoundFactor − 1 ) ∗ ( pow ( compoundFactor , numberOfPayments ) − pow (
compoundFactor , i ) ) ) / denominator ) − f e e s [ i ]−>amount ( ) , next ) ) ;
n e t i n t e r e s t += I ;
11 }
13 return netinterest ;
}
15
Leg Mortgage : : PrincipalRepayment ( ) c o n s t
17 {
Real denominator = pow ( compoundFactor , numberOfPayments ) − 1 ;
19 Leg p r i n c i p a l r e p a y m e n t ;
f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)
21 {
Date next = i s s u e D a t e + Months ∗ ( ( i +1) ∗ 12 / yearPayments ) ;
23 b o o s t : : s h a r e d p t r <CashFlow> PR( new SimpleCashFlow ( amount ∗ ( (
compoundFactor − 1 ) ∗ pow ( compoundFactor , i ) ) / denominator , next ) ) ;
p r i n c i p a l r e p a y m e n t += PR;
25 }
27 return principalrepayment ;
}
29
43 return endingbalance ;
}
26
Date Mortgage : : d a t e ( ) c o n s t { r e t u r n i s s u e D a t e ; }
2
/∗ ∗∗ V i s i t o r ∗∗ ∗/
4 v o i d Mortgage : : a c c e p t ( QuantLib : : A c y c l i c V i s i t o r &v )
{
6 V i s i t o r <Mortgage> ∗ v1 = d y n a m i c c a s t <V i s i t o r <Mortgage> ∗>(&v ) ;
i f ( v1 !=0)
8 v1−>v i s i t ( ∗ t h i s ) ;
else
10 Loan : : a c c e p t ( v ) ;
}
12 //@}
}
To replicate exhibit 21.1, set fee equal to 0.0, and set fee to 0.005 to replicate exhibit 21.3. The
implementation in main is:
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
Fixed Income Mathematics Four E d i t i o n
3 pag ( 4 0 9 )
CashFlow o f mortgage with s e r v i c i n g f e e .
5 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
#i n c l u d e <i o s t r e a m >
7 #i n c l u d e <q l / q u a n t l i b . hpp>
#i n c l u d e <v e c t o r >
9 #i n c l u d e <b o o s t / a s s i g n / s t d / v e c t o r . hpp>
#i n c l u d e <b o o s t / format . hpp>
11 #i n c l u d e ” mortgage . hpp”
13 u s i n g namespace QuantLib ;
u s i n g namespace s t d ;
15 u s i n g namespace b o o s t : : a s s i g n ;
19 i n t main ( )
{
21 try {
Rate r f = 0 . 0 9 5 ;
23 DayCounter DC= A c t u a l A c t u a l ( A c t u a l A c t u a l : : ISDA ) ;
Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
25 Compounding compounded = Simple ;
Frequency f r e q u e n c y = Annual ;
27
I n t e r e s t R a t e r f R a t e ( r f , DC, compounded , f r e q u e n c y ) ;
29 Date i s s u e ( 1 , Jan , 2 0 0 0 ) ;
d = calendar . adjust (d) ;
27
I n t e g e r YearPayments = 1 2 ;
2 Rate f e e = 0 . 0 0 5 ;
Mortgage M( d , c a l e n d a r , 1 0 0 0 0 0 , r f R a t e , f e e , 3 0 , YearPayments ) ;
4
c o u t << FORMATER % ” Date ” % ”Beg . Balance ” % ”Payment” % ” Net . I n t e r e s t ” %
” Fees ” % ”Repayment” % ”End . Balance ” << e n d l ;
6 f o r ( i n t i= 0 ; i <12; i ++)
c o u t << FORMATER %M. Fee ( ) [ i ]−>d a t e ( ) %M. B e g i n i n g B a l a n c e ( ) [ i ]−>
amount ( ) %M. Payment ( ) [ i ]−>amount ( ) %M. N e t I n t e r e s t ( ) [ i ]−>amount ( ) %M. Fee ( ) [
i ]−>amount ( ) %M. PrincipalRepayment ( ) [ i ]−>amount ( ) %M. EndingBalance ( ) [ i ]−>
amount ( )<< e n d l ;
8
Date d1 ( 1 , Jan , 1 9 9 9 ) ;
10 c o u t << M. hasOccurred ( d1 ) ;
}
12
c a t c h ( s t d : : e x c e p t i o n& e ) {
14 c e r r << e . what ( ) << e n d l ;
}
16 }
28
/∗ −∗− mode : c++; tab−width : 4 ; i n d e n t −tabs−mode : n i l ; c−b a s i c −o f f s e t : 4 −∗− ∗/
2
/∗
4 Copyright (C) 2014 M a u r i c i o Bedoya
20 #i f n d e f q u a n t l i b f i x e d r a t e m o r t g a g e P S A h p p
#d e f i n e q u a n t l i b f i x e d r a t e m o r t g a g e P S A h p p
22
#i n c l u d e ” f i x e d r a t e m o r t g a g e . hpp”
24 #i n c l u d e <q l / i n t e r e s t r a t e . hpp>
#i n c l u d e <q l / time / c a l e n d a r . hpp>
26 #i n c l u d e <q l / time / d a y c o u n t e r . hpp>
#i n c l u d e <q l / time / d a t e g e n e r a t i o n r u l e . hpp>
28 #i n c l u d e <q l / c a s h f l o w s / s i m p l e c a s h f l o w . hpp>
30 namespace QuantLib {
c l a s s FixedRateMortgagePSA : p u b l i c FixedRateMortgage {
32 public :
FixedRateMortgagePSA ( c o n s t Date& i s s u e D a t e , c o n s t Calendar& c a l e n d a r ,
c o n s t Real& amount , c o n s t I n t e r e s t R a t e& r a t e , c o n s t Rate& f e e , c o n s t I n t e g e r&
term , c o n s t I n t e g e r& yearPayments , c o n s t Real& PSA) ;
34 v i r t u a l ˜ FixedRateMortgagePSA ( ) ;
//@
36 /∗ ∗∗ I n s p e c t o r ∗∗ ∗/
Real amount ( ) ;
38 InterestRate rate () ;
Rate f e e ( ) ;
40
/∗ ∗∗ S e t t e r s ∗∗ ∗/
42 void s e t i s s u e D a t e ( c o n s t Date& new Date ) ;
void set amount ( c o n s t Real& new amount ) ;
44 void s e t r a t e ( c o n s t I n t e r e s t R a t e& n e w r a t e ) ;
void s e t f e e ( c o n s t Rate& n e w f e e ) ;
29
1 /∗ ∗∗ Pure v i r t u a l ∗∗ ∗/
Leg B e g i n i n g B a l a n c e ( ) c o n s t ;
3 Leg Payment ( ) c o n s t ;
Leg N e t I n t e r e s t ( ) c o n s t ;
5 Leg Fee ( ) c o n s t ;
Leg PrincipalRepayment ( ) c o n s t ;
7 Leg EndingBalance ( ) c o n s t ;
Date d a t e ( ) c o n s t ; // from Event
9
/∗ ∗∗ V i s i t o r ∗∗ ∗/
11 v i r t u a l v o i d a c c e p t ( A c y c l i c V i s i t o r& v ) ;
13 /∗ ∗∗ Others ∗∗ ∗/
Leg SMM( ) c o n s t ;
15 Leg b ( ) c o n s t ;
Leg I n t e r e s t ( ) c o n s t ;
17 Leg P r o y e c t e d P r i n c i p a l P a y m e n t ( ) c o n s t ;
Leg CashFlowToInvestors ( ) c o n s t ;
19
//@
21 private :
Date i s s u e D a t e ;
23 Calendar c a l e n d a r ;
Real amount , PSA ;
25 I n t e g e r term , yearPayments ;
Compounding compound ;
27 InterestRate rate ;
Rate f e e ;
29 Leg B e g i n i n g B a l a n c e , Payment , N e t I n t e r e s t , Fee , Pr i nc i pa l Re p ay m en t ,
I n t e r e s t , P r o y e c t e d P r i n c i p a l P a y m e n t , EndingBalance , C a s h F l o w T o I n v e s t o r s ;
31 /∗ ∗∗ H e l p e r s ∗∗ ∗/
Time t = ( 1 . 0 / yearPayments ) ;
33 I n t e g e r numberOfPayments = ( term ∗ yearPayments ) ;
Real compoundFactor = r a t e . compoundFactor ( t ) ;
35 v o i d GenerateCashFlows ( ) ;
};
37 }
39 #e n d i f /∗ d e f i n e d ( hh fixedratemortgagePSA ) ∗/
30
1 /∗ −∗− mode : c++; tab−width : 4 ; i n d e n t −tabs−mode : n i l ; c−b a s i c −o f f s e t : 4 −∗− ∗/
3 /∗
Copyright (C) 2014 M a u r i c i o Bedoya
5
This f i l e i s p a r t o f QuantLib , a f r e e −s o f t w a r e / open−s o u r c e l i b r a r y
7 f o r f i n a n c i a l q u a n t i t a t i v e a n a l y s t s and d e v e l o p e r s − h t t p : / / q u a n t l i b . o r g /
37 /∗ ∗∗ S e t t e r s ∗∗ ∗/
v o i d FixedRateMortgagePSA : : s e t i s s u e D a t e ( c o n s t Date& new Date ) { i s s u e D a t e =
new Date ; }
39 v o i d FixedRateMortgagePSA : : set amount ( c o n s t Real& new amount ) { amount =
new amount ; }
v o i d FixedRateMortgagePSA : : s e t r a t e ( c o n s t I n t e r e s t R a t e& n e w r a t e ) { r a t e =
new rate ;}
41 v o i d FixedRateMortgagePSA : : s e t f e e ( c o n s t Rate& n e w f e e ) { f e e = new fee ;}
31
1 v o i d FixedRateMortgagePSA : : GenerateCashFlows ( )
{
3 Leg smm = SMM( ) ;
Leg bt = b ( ) ;
5 Real PMT = FixedRateMortgage : : Payment ( ) [0]−>amount ( ) ;
32
1 b o o s t : : s h a r e d p t r <CashFlow> MBt( new SimpleCashFlow ( B e g i n i n g B a l a n c e [
i ]−>amount ( ) − P r o y e c t e d P r i n c i p a l P a y m e n t [ i ]−>amount ( ) − P r i n c i p a l R e p a y m e n t [
i ]−>amount ( ) , next ) ) ;
EndingBalance += MBt ;
3
b o o s t : : s h a r e d p t r <CashFlow> CF( new SimpleCashFlow ( N e t I n t e r e s t [ i ]−>
amount ( ) + P r o y e c t e d P r i n c i p a l P a y m e n t [ i ]−>amount ( ) + P r i n c i p a l R e p a y m e n t [ i ]−>
amount ( ) , next ) ) ;
5 C a s h F l o w T o I n v e s t o r s += CF ;
7 }
9 }
/∗ ∗∗ Pure v i r t u a l ∗∗ ∗/
11 Leg FixedRateMortgagePSA : : B e g i n i n g B a l a n c e ( ) c o n s t { r e t u r n B e g i n i n g B a l a n c e ; }
15 Leg FixedRateMortgagePSA : : N e t I n t e r e s t ( ) c o n s t { r e t u r n N e t I n t e r e s t ; }
23 Date FixedRateMortgagePSA : : d a t e ( ) c o n s t { r e t u r n i s s u e D a t e ; }
25 /∗ ∗∗ V i s i t o r ∗∗ ∗/
v o i d FixedRateMortgagePSA : : a c c e p t ( A c y c l i c V i s i t o r& v )
27 {
V i s i t o r <FixedRateMortgagePSA>∗ v1 = d y n a m i c c a s t <V i s i t o r <
FixedRateMortgagePSA>∗ >(&v ) ;
29 i f ( v1 !=0)
v1−>v i s i t ( ∗ t h i s ) ;
31 else
FixedRateMortgage : : a c c e p t ( v ) ;
33 }
33
1 /∗ ∗∗ Others ∗∗ ∗/
Leg FixedRateMortgagePSA : :SMM( ) c o n s t
3 {
Leg smm;
5 Real CRP;
i n t N = 30 ∗ yearPayments / 1 2 ;
7
f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)
9 {
i f ( i < N)
11 CRP = ( 0 . 0 6 ∗ ( i + 1 ) / N) ∗ PSA ;
else
13 CRP = ( 0 . 0 6 ) ∗ PSA ;
Date next = i s s u e D a t e + ( ( 1 + i ) ∗ 12 / yearPayments ) ;
15 b o o s t : : s h a r e d p t r <CashFlow> S ( new SimpleCashFlow ( 1 − pow ( ( 1 − CRP)
, ( 1 . 0 / yearPayments ) ) , next ) ) ;
smm += S ;
17 }
19 r e t u r n smm;
}
21
Leg FixedRateMortgagePSA : : b ( ) c o n s t
23 {
Leg b ;
25 Leg smm = SMM( ) ;
Real data = 1 ;
27 Date next = i s s u e D a t e + ( 1 ∗ 12 / yearPayments ) ;
b o o s t : : s h a r e d p t r <CashFlow> bt ( new SimpleCashFlow ( data , next ) ) ;
29 b += bt ;
f o r ( i n t i = 1 ; i < numberOfPayments ; i ++)
31 {
next = i s s u e D a t e + ( ( 1 + i ) ∗ 12 / yearPayments ) ;
33 data ∗=(1−SMM( ) [ i −1]−>amount ( ) ) ;
b o o s t : : s h a r e d p t r <CashFlow> bt ( new SimpleCashFlow ( data , next ) ) ;
35 b += bt ;
}
37
return b ;
39 }
34
1 Leg FixedRateMortgagePSA : : I n t e r e s t ( ) c o n s t
{
3 Leg b e g i n i n g b a l a n c e = B e g i n i n g B a l a n c e ( ) ;
Leg i n t e r e s t ;
5 f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)
{
7 Date next = i s s u e D a t e + ( ( 1 + i ) ∗ 12 / yearPayments ) ∗ Months ;
b o o s t : : s h a r e d p t r <CashFlow> I ( new SimpleCashFlow ( b e g i n i n g b a l a n c e [ i
]−>amount ( ) ∗ ( compoundFactor − 1 ) , next ) ) ;
9 i n t e r e s t += I ;
}
11
return i n t e r e s t ;
13 }
15 Leg FixedRateMortgagePSA : : P r o y e c t e d P r i n c i p a l P a y m e n t ( ) c o n s t { r e t u r n
ProyectedPrincipalPayment ;}
19 //@
}
To generate Exhibit 22.8, 22.9 and 22.10 (book), just change the PSA value. The implementa-
tion in main is:
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 Fixed Income Mathematics Four E d i t i o n
pag ( 4 0 9 )
4 CashFlow o f mortgage with s e r v i c i n g f e e .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
6 #i n c l u d e <i o s t r e a m >
#i n c l u d e <q l / q u a n t l i b . hpp>
8 #i n c l u d e <v e c t o r >
#i n c l u d e <b o o s t / a s s i g n / s t d / v e c t o r . hpp>
10 #i n c l u d e <b o o s t / format . hpp>
#i n c l u d e ” f i x e d r a t e m o r t g a g e . hpp”
12 #i n c l u d e ” f i x e d r a t e m o r t g a g e P S A . hpp”
14 u s i n g namespace QuantLib ;
u s i n g namespace s t d ;
16 u s i n g namespace b o o s t : : a s s i g n ;
18 b o o s t : : format FORMATER1 ( ” %−20s % −12 s % −12 s % −12 s % −12 s % −12 s % −12 s % −12 s
% −12 s % −12 s ” ) ;
35
i n t main ( )
2 {
try {
4 Rate r f = 0 . 0 9 5 ;
DayCounter DC= A c t u a l A c t u a l ( A c t u a l A c t u a l : : ISDA ) ;
6 Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ;
Compounding compounded = Simple ;
8 Frequency f r e q u e n c y = Annual ;
10 I n t e r e s t R a t e r f R a t e ( r f , DC, compounded , f r e q u e n c y ) ;
Date i s s u e ( 1 , Jan , 2 0 0 0 ) ;
12 issue = calendar . adjust ( issue ) ;
I n t e g e r YearPayments = 1 2 ;
14 Rate f e e = 0 . 0 0 5 ;
Real PSA = 1 . 0 ;
16 c o u t << e n d l ;
18 FixedRateMortgagePSA M( i s s u e , c a l e n d a r , 1 0 0 0 0 0 , r f R a t e , f e e , 3 0 ,
YearPayments , PSA) ;
c o u t << FORMATER1 % ” Date ” % ”BMB” % ”SMM” % ”MP” % ”SP” % ” I ” % ”PR” %
”S” % ”CF” %”EMB” << e n d l ;
20 f o r ( i n t i= 0 ; i <12; i ++) {
c o u t << FORMATER1 %M. B e g i n i n g B a l a n c e ( ) [ i ]−>d a t e ( ) %M.
B e g i n i n g B a l a n c e ( ) [ i ]−>amount ( )
22 %M.SMM( ) [ i ]−>amount ( ) %M. Payment ( ) [ i ]−>amount ( ) %
M. P r o y e c t e d P r i n c i p a l P a y m e n t ( ) [ i ]−>amount ( )
%M. I n t e r e s t ( ) [ i ]−>amount ( ) %M. PrincipalRepayment
( ) [ i ]−>amount ( ) %M. Fee ( ) [ i ]−>amount ( )
24 %M. CashFlowToInvestors ( ) [ i ]−>amount ( ) %M.
EndingBalance ( ) [ i ]−>amount ( ) << e n d l ;
}
26
v e c t o r <Real> PSA1 ;
28 PSA1 += 1 . 0 , 1 . 5 , 0 . 5 ;
30 // Vector A n a l i s i s
FixedRateMortgagePSA M1( i s s u e , c a l e n d a r , 1 0 0 0 0 0 , r f R a t e , f e e , 3 0 ,
YearPayments , PSA1 [ 0 ] ) ;
32 FixedRateMortgagePSA M2( i s s u e , c a l e n d a r , 1 0 0 0 0 0 , r f R a t e , f e e , 3 0 ,
YearPayments , PSA1 [ 1 ] ) ;
FixedRateMortgagePSA M3( i s s u e , c a l e n d a r , 1 0 0 0 0 0 , r f R a t e , f e e , 3 0 ,
YearPayments , PSA1 [ 2 ] ) ;
34 }
36 c a t c h ( s t d : : e x c e p t i o n& e ) {
c e r r << e . what ( ) << e n d l ;
38 }
}
At the end you will find three different FixedRateMortgagePSA object, each one with a diffe-
rent PSA value. This is the introduction code to implement vector analysis (page 446 in book).
Implement the code that generate the corresponding cash flows.
36