You are on page 1of 36

Fixed Income Mathematics in Quantlib

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 .

1. Option Free Bond + Flat Yield.


Page 74 75 in Fabozzi Book.
Bond1:
Maturity: 20 years
Coupon: 9 % (Simuannual)
Redemption:1000
Requiered Yield: 12 %
CleanPrice: 774.3

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 ) ;

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 ;


R e l i n k a b l e H a n d l e <FixedRateBond>BondInstrument ;
33
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Bond S c h e d u l e ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
35 S c h e d u l e s c h ( I s s u e , Maturity , P e r i o d ( Semiannual ) , c a l e n d a r , Unadjusted ,
Unadjusted , DateGeneration : : Backward , f a l s e ) ;

37 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Bond 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 <FixedRateBond> Bond1 ( new FixedRateBond ( SettlementDays ,
FaceValue , sch , Coupons ) ) ;
39 BondInstrument . l i n k T o ( Bond1 ) ;
Rate r a t e = 0 . 1 2 ;
41 Handle<Quote> Rate1 ( new SimpleQuote ( r a t e ) ) ;

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” ;

11 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗ For Bond2 j u s t o n l y change t h e Rate ∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/


rate = 0.07;
13 Handle<Quote> Rate2 ( new SimpleQuote ( r a t e ) ) ;
b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > TS1 ( new FlatForward ( I s s u e , Rate2 ,
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 ) ) ;
15 F l a t R a t e . l i n k T o ( TS1 ) ;
c o u t << ”Bond2 R e s u l t s \n” ;
17 c o u t << ”Book P r i c e : ” << 1 2 1 3 . 5 5 << e n d l ;
c o u t << ” QuantLib : ” << BondInstrument−>NPV( ) << e n d l ;
19 }

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 ) ;

32 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 ;


R e l i n k a b l e H a n d l e <FixedRateBond>BondInstrument ;
34
/∗ ∗∗∗∗∗∗∗∗∗ Bond S c h e d u l e ∗∗∗∗∗∗∗∗∗ ∗/
36 S c h e d u l e s c h ( I s s u e , Maturity , P e r i o d ( Semiannual ) , c a l e n d a r , Unadjusted ,
Unadjusted , DateGeneration : : Backward , f a l s e ) ;

38 /∗ ∗∗∗∗∗∗∗∗∗ Bond 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 <FixedRateBond> Bond1 ( new FixedRateBond ( SettlementDays ,
FaceValue , sch , Coupons ) ) ;
40 BondInstrument . l i n k T o ( Bond1 ) ;
Rate r a t e = 0 . 1 0 ;
42 Handle<Quote> Rate1 ( new SimpleQuote ( r a t e ) ) ;

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 ;

35 /∗ ∗∗∗∗∗∗∗∗∗ Bond S c h e d u l e ∗∗∗∗∗∗∗∗∗ ∗/


S c h e d u l e s c h ( I s s u e , Maturity , P e r i o d ( Semiannual ) , c a l e n d a r , Unadjusted ,
Unadjusted , DateGeneration : : Backward , f a l s e ) ;
37
/∗ ∗∗∗∗∗∗∗∗∗ Bond C o n s t r u c t o r ∗∗∗∗∗∗∗∗∗ ∗/
39 b o o s t : : s h a r e d p t r <FixedRateBond> Bond1 ( new FixedRateBond ( SettlementDays ,
FaceValue , sch , Coupons ) ) ;
BondInstrument . l i n k T o ( Bond1 ) ;
41 Rate r a t e = 0 . 1 0 ;
Handle<Quote> Rate1 ( new SimpleQuote ( r a t e ) ) ;

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 ) ;

24 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗ P r i n t P r i c e a f t e r s p r e a d ( Small ) ∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/

26 Real Approx BondPrice 1 = ( 1 − ( M o d i f i e d D u r a t i o n ∗ s m a l l s p r e a d ) ) ∗


BondPrice 0 ;
Real B o n d P r i c e S m a l l S p r e a d = BondInstrument−>NPV( ) ;
28
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 : ” << s m a l l s p r e a d << e n d l ;
30 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 S m a l l 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 << ” ( Good ) ”<< e n d l ;
32 c o u t << ” \n \n” ;

34 /∗ ∗∗∗∗∗∗∗∗∗∗ New f l a t c u r v e r a t e ( l a r g e s p r e a d ) ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗


For a l a r g e 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
36 d o e s n t work w e l l .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
38 Spread l a r g e s p r e a d = 0 . 0 3 ;
Handle<Quote> R a t e 1 L a r g e S p r e a d ( new SimpleQuote ( r a t e + l a r g e s p r e a d ) ) ;
40 b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > T S l a r g e s p r e a d ( new FlatForward (
I s s u e , Rate1 LargeSpread , 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 ) ) ;
FlatRate . linkTo ( TS largespread ) ;

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

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 ;


27 R e l i n k a b l e H a n d l e <FixedRateBond>BondInstrument ;

29 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Bond S c h e d u l e ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/


S c h e d u l e s c h ( I s s u e , Maturity , P e r i o d ( Semiannual ) , c a l e n d a r , Unadjusted ,
Unadjusted , DateGeneration : : Backward , f a l s e ) ;
31
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ Bond C o n s t r u c t o r ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
33 b o o s t : : s h a r e d p t r <FixedRateBond> Bond1 ( new FixedRateBond ( SettlementDays ,
FaceValue , sch , Coupons ) ) ;
BondInstrument . l i n k T o ( Bond1 ) ;
35 Rate r a t e = 0 . 1 0 ;
Handle<Quote> Rate1 ( new SimpleQuote ( r a t e ) ) ;
37
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗ TermStructure C o n s t r u c t o r ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
39 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) ;
41 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 ) ;

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 ) ;

20 /∗ ∗∗∗∗∗∗∗∗∗∗ New f l a t c u r v e r a t e ( l a r g e s p r e a d ) ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗


For an l a r g e 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
22 d o e s n t work w e l l .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
24 Spread l a r g e s p r e a d = 0 . 0 3 ;
Handle<Quote> R a t e 1 L a r g e S p r e a d ( new SimpleQuote ( r a t e + l a r g e s p r e a d ) ) ;
26 b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > T S l a r g e s p r e a d ( new FlatForward (
I s s u e , Rate1 LargeSpread , 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 ) ) ;
FlatRate . linkTo ( TS largespread ) ;
28
/∗ ∗∗∗∗∗∗∗∗∗∗∗∗ P r i n t P r i c e a f t e r s p r e a d ( Large ) ∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
30 Real BondPrice 1 = BondInstrument−>NPV( ) ;
Real PriceChange = ( BondPrice 1 / BondPrice 0 ) − 1 ;
32

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 }

4. Valuation of Option Free Bonds (page 324 - 325)


In this case, we have an hypothetical yield curve integrated with on the run issues:

Maturity (Years) YTM Market Price


1 3.5 % 100.0
2 4% 100.0
3 4.5 % 100.0

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 ;

11 /∗ ∗∗∗∗ R e li n k H an l d e Quotes ∗∗∗∗ ∗/


R e l i n k a b l e H a n d l e <Quote> q u o t e H a n d l e s [ numberOfBonds ] ;
13
f o r ( S i z e i = 0 ; i != numberOfBonds ; i ++)
15 {
b o o s t : : s h a r e d p t r <SimpleQuote> Q( new SimpleQuote ( M ark etPr ice [ i ] ) ) ;
17 Quotes += Q;
q u o t e H a n d l e s [ i ] . l i n k T o ( Quotes [ i ] ) ;
19 }

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
);

39 /∗ ∗∗∗∗ Bond1 C o n s t r u c t o r ( t r y t o do i t i n a l o o p ) ∗∗∗∗ ∗/


Date MaturityBond1 = c a l e n d a r . advance ( BondSettlementDate , l e n g t h [ 0 ] ∗
Years ) ;
41 /∗ ∗∗∗∗ S c h e d u l e ∗/
S c h e d u l e SchBond1 ( BondSettlementDate , MaturityBond1 , 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 ) ;

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

4 /∗ ∗∗∗∗ Bond2 C o n s t r u c t o r ∗∗∗∗ ∗/


Date MaturityBond2 = c a l e n d a r . advance ( BondSettlementDate , l e n g t h [ 1 ] ∗
Years ) ;
6 /∗ ∗∗∗∗∗ S c h e d u l e ∗/
S c h e d u l e SchBond2 ( BondSettlementDate , MaturityBond2 , P e r i o d ( f r e q u e n c y ) ,
calendar , accrualConvention , accrualConvention ,
8 DateGeneration : : Backward , f a l s e ) ;
b o o s t : : s h a r e d p t r <BondHelper> B2 ( newFixedRateBondHelper ( q u o t e H a n d l e s [ 1 ] ,
bondSettlementDays , 1 0 0 . 0 , SchBond2 , v e c t o r <Rate >(2 , coupons [ 1 ] ) , dc , Convention ,
Redemption ) ) ;
10
/∗ ∗∗∗∗ Bond3 C o n s t r u c t o r ∗∗∗∗ ∗/
12 Date MaturityBond3 = c a l e n d a r . advance ( BondSettlementDate , l e n g t h [ 2 ] ∗
Years ) ;
/∗ ∗∗∗∗∗ S c h e d u l e ∗/
14 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 ) ;
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 ) ) ;
16 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗ The Curve ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
Be c o n s i s t e n t . I f you u s e h e l p e r s ,
18 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 .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
20 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) ;
Real t o l e r a n c e = 1 . 0 e −12;
22 v e c t o r <b o o s t : : s h a r e d p t r <RateHelper> > BondPrices ;
BondPrices += B1 , B2 , B3 ;
24
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 ) ) ;
26 /∗ ∗∗∗∗ Get Spot Rates ( page 324 i n book ) ∗∗∗∗ ∗/
Rate z e r o 1 Y e a r = TS−>z e r o R a t e ( MaturityBond1 , termStructureDayCounter ,
Compounded ) ;
28 Rate z e r o 2 Y e a r = TS−>z e r o R a t e ( MaturityBond2 , termStructureDayCounter ,
Compounded ) ;
Rate z e r o 3 Y e a r = TS−>z e r o R a t e ( MaturityBond3 , termStructureDayCounter ,
Compounded ) ;
30
/∗ ∗∗∗∗ P r i n t Spot ∗∗∗∗ ∗/
32 s t r i n g r u l e ( 2 0 , ’− ’ ) ;
c o u t << FORMATER % ” Year ” % ” Spot Rate \n” ;
34 c o u t << r u l e << e n d l ;

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
);

10 /∗ ∗∗∗∗ P r i n t Forward ∗∗∗∗ ∗/


c o u t << FORMATER % ” Year ” % ” Forward Rate \n” ;
12 c o u t << r u l e << e n d l ;
c o u t << FORMATER % ”F0x1” % F0x1 << e n d l ;
14 c o u t << FORMATER % ”F1x2” % F1x2 << e n d l ;
c o u t << FORMATER % ”F2x3” % F2x3 << ”\n \n” ;
16
/∗ ∗∗∗∗ P r i c e t h e Bond ( page 325 i n book ) ∗∗∗∗∗
18 Bond I n f o r m a t i o n
3−Years , 5.25 % coupon ( annual )
20 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
Date Maturity = c a l e n d a r . advance ( BondSettlementDate , 3 ∗ Years ) ;
22
S c h e d u l e SchBond ( BondSettlementDate , Maturity , 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 ) ;
24
FixedRateBond Bond ( bondSettlementDays , 1 0 0 . 0 , SchBond , v e c t o r <Rate
> ( 3 , 0 . 0 5 2 5 ) , dc ) ;
26

R e l i n k a b l e H a n d l e <Yi eldT erm Str uct ure > d i s c o u n t T e r m S t r u c t u r e ;


28 d i s c o u n t T e r m S t r u c t u r 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 (
discountTermStructure ) ) ;
30 Bond . s e t P r i c i n g E n g i n e ( BondEngine ) ;

32 c o u t << ”Bond P r i c e i s : ” << Bond . c l e a n P r i c e ( ) ;

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 }
}

5. Callable Bond (page 337)


In page 293, Fabozzi differentiate between one factor Normal models, and one factor Lognormal
models. The difference is due to constant elasticity of variance value (γ). When γ = 0, we have

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 } ;

27 /∗ ∗∗∗∗ Quote data ∗∗∗∗ ∗/


v e c t o r <b o o s t : : s h a r e d p t r <SimpleQuote> > Quotes ;
29
/∗ ∗∗∗∗ R e li n k H an l d e Quotes ∗∗∗∗ ∗/
31 R e l i n k a b l e H a n d l e <Quote> q u o t e H a n d l e s [ numberOfBonds ] ;

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
);

18 /∗ ∗∗∗∗ Bond1 C o n s t r u c t o r ( t r y t o do i t i n a l o o p ) ∗∗∗∗ ∗/


Date MaturityBond1 = c a l e n d a r . advance ( BondSettlementDate , l e n g t h [ 0 ] ∗
Years ) ;
20 /∗ ∗∗∗∗ S c h e d u l e ∗/
S c h e d u l e SchBond1 ( BondSettlementDate , MaturityBond1 , 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 ) ;
22

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 ,
24 v e c t o r <Rate >(1 ,
coupons [ 0 ] ) , dc , Convention , Redemption ) ) ;

26 /∗ ∗∗∗∗ Bond2 C o n s t r u c t o r ∗∗∗∗ ∗/


Date MaturityBond2 = c a l e n d a r . advance ( BondSettlementDate , l e n g t h [ 1 ] ∗
Years ) ;
28 /∗ ∗∗∗∗∗ S c h e d u l e ∗/
S c h e d u l e SchBond2 ( BondSettlementDate , MaturityBond2 , P e r i o d ( f r e q u e n c y ) ,
calendar , accrualConvention , accrualConvention ,
30 DateGeneration : : Backward , f a l s e ) ;
b o o s t : : s h a r e d p t r <BondHelper> B2 ( new FixedRateBondHelper ( q u o t e H a n d l e s
[ 1 ] , bondSettlementDays , 1 0 0 . 0 , SchBond2 , v e c t o r <Rate >(2 , coupons [ 1 ] ) , dc ,
Convention , Redemption ) ) ;

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 R e l i n k a b l e H a n d l e <Yi eldT erm Str uct ure > d i s c o u n t T e r m S t r u c t u r e ;


d i s c o u n t T e r m S t r u c t u r e . l i n k T o (TS) ;
19

/∗ ∗∗∗∗ Valuing C a l l a b l e Bond ( page 337 i n book ) ∗∗∗∗∗


21 Coupon : 5.25 % C a l l p r i c e : 100
Maturity : 3 y e a r s C a l l Date : 1 y e a r
23 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/

25 /∗ ∗∗∗∗∗ Step 1 : S e t Bond C h a r a c t e r i s t i c s ∗∗∗∗ ∗/


Date i s s u e = today ;
27 Date Maturity = c a l e n d a r . advance ( today , 3 ∗ Years ) ;
N a t u r a l Se tt le me nt Day s = 0 ;
29 Calendar bondCalendar = c a l e n d a r ;
Real Coupon = 0 . 0 5 2 5 ;
31 Frequency CouponFrequency = Annual ;
BusinessDayConvention BondConvention = Convention ;
33 BusinessDayConvention BondAccrueConvention = Convention ;
DayCounter BondDC = dc ;
35
S c h e d u l e SchBond ( today , Maturity , P e r i o d ( CouponFrequency ) , bondCalendar ,
BondConvention , BondConvention , DateGeneration : : Backward , f a l s e ) ;
37
/∗ ∗∗∗∗ C a l l S c h e d u l e ∗∗∗∗ ∗/
39 C a l l a b i l i t y S c h e d u l e CallSch ;
Real P r i c e = 1 0 0 . 0 ;
41 C a l l a b i l i t y : : P r i c e C a l l P r i c e ( P r i c e , C a l l a b i l i t y : : P r i c e : : Clean ) ; // S e t
Call Price

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 ;

5 Size maxIterations = 1000;


Real a c c u r a c y = 1 e −8;
7 Integer gridIntervals = 10;
Real r e v e r s i o n P a r a m e t e r = . 1 ;
9
Real sigma = 0 . 0 1 ; //QL EPSILON ; // c o r e dumps i f z e r o on Cygwin
11
b o o s t : : s h a r e d p t r <ShortRateModel> hw0 ( new HullWhite (
d i s c o u n t T e r m S t r u c t u r e , r e v e r s i o n P a r a m e t e r , sigma ) ) ;
13 b o o s t : : s h a r e d p t r <ShortRateModel> B l a c k K a r a s i n s k i ( new B l a c k K a r a s i n s k i (
discountTermStructure ) ) ;

15 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 > e n g i n e 0 ( new


Tr ee Cal la ble Fi xed Ra teB on dEn gin e ( hw0 , g r i d I n t e r v a l s ) ) ;
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 > e n g i n e 1 ( new
Tr ee Cal la ble Fi xed Ra teB on dEn gin e ( B l a c k K a r a s i n s k i , g r i d I n t e r v a l s ) ) ;
17 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 (
discountTermStructure ) ) ;

19 FixedRateBond Bond ( SettlementDays , 1 0 0 , SchBond , v e c t o r <Rate >(3 , Coupon ) ,


BondDC) ;
C a l l ab l e F i x e dR a t e B o n d c a l l a b l e B o n d ( SettlementDays , 1 0 0 , SchBond , v e c t o r <
Rate >(3 , Coupon ) , BondDC , BondAccrueConvention ,
21 1 0 0 , today , C a l l S c h ) ;

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 ;

33 callableBond . setPricingEngine ( engine1 ) ;


Real C a l l P r i c e B K = c a l l a b l e B o n d . c l e a n P r i c e ( ) ;

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 }
}

6. Analizing Securitized Products (page 397)


QuantLib (1.4) doesn’t have any class named loan or mortgage. However, implementing this is
not too hard. Because a mortgage is a loan, I’m going to implement a base class named Loan
and inherited from this other classes like: fixed mortgage loan, variable mortgage loan, student
loan, car loan, etc. The purpose of this section is to replicate exhibit 21.1 and 21.3 of the book
using the formulas suplied in exhibit 21.2.

The loan.hpp and loan.cpp files are:

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 /

9 QuantLib i s f r e e s o f t w a r e : you can r e d i s t r i b u t e i t and/ o r modify i t


under t h e terms o f t h e QuantLib l i c e n s e . You s h o u l d have r e c e i v e d a
11 copy o f t h e l i c e n s e a l o n g with t h i s program ; i f not , p l e a s e e m a i l
<q u a n t l i b −d e v @ l i s t s . s f . net >. The l i c e n s e i s a l s o a v a i l a b l e o n l i n e a t
13 <h t t p : / / q u a n t l i b . o r g / l i c e n s e . shtml >.

15 This program i s d i s t r i b u t e d i n t h e hope t h a t i t w i l l be u s e f u l , but WITHOUT


ANY WARRANTY; w i t h o u t even t h e i m p l i e d warranty o f MERCHANTABILITY o r FITNESS
17 FOR A PARTICULAR PURPOSE. See t h e l i c e n s e f o r more d e t a i l s .
∗/
19
#i f n d e f q u a n t l i b l o a n h p p
21 #d e f i n e q u a n t l i b l o a n h p p

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

6 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


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 /
8

QuantLib i s f r e e s o f t w a r e : you can r e d i s t r i b u t e i t and/ o r modify i t


10 under t h e terms o f t h e QuantLib l i c e n s e . You s h o u l d have r e c e i v e d a
copy o f t h e l i c e n s e a l o n g with t h i s program ; i f not , p l e a s e e m a i l
12 <q u a n t l i b −d e v @ l i s t s . s f . net >. The l i c e n s e i s a l s o a v a i l a b l e o n l i n e a t
<h t t p : / / q u a n t l i b . o r g / l i c e n s e . shtml >.
14

This program i s d i s t r i b u t e d i n t h e hope t h a t i t w i l l be u s e f u l , but WITHOUT


16 ANY WARRANTY; w i t h o u t even t h e i m p l i e d warranty o f MERCHANTABILITY o r FITNESS
FOR A PARTICULAR PURPOSE. See t h e l i c e n s e f o r more d e t a i l s .
18 ∗/

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 }

For the mortgage.hpp and mortgage.cpp, we have:

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

6 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


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 /
8

QuantLib i s f r e e s o f t w a r e : you can r e d i s t r i b u t e i t and/ o r modify i t


10 under t h e terms o f t h e QuantLib l i c e n s e . You s h o u l d have r e c e i v e d a
copy o f t h e l i c e n s e a l o n g with t h i s program ; i f not , p l e a s e e m a i l
12 <q u a n t l i b −d e v @ l i s t s . s f . net >. The l i c e n s e i s a l s o a v a i l a b l e o n l i n e a t
<h t t p : / / q u a n t l i b . o r g / l i c e n s e . shtml >.
14

This program i s d i s t r i b u t e d i n t h e hope t h a t i t w i l l be u s e f u l , but WITHOUT


16 ANY WARRANTY; w i t h o u t even t h e i m p l i e d warranty o f MERCHANTABILITY o r FITNESS
FOR A PARTICULAR PURPOSE. See t h e l i c e n s e f o r more d e t a i l s .
18 ∗/

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

6 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


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 /
8

QuantLib i s f r e e s o f t w a r e : you can r e d i s t r i b u t e i t and/ o r modify i t


10 under t h e terms o f t h e QuantLib l i c e n s e . You s h o u l d have r e c e i v e d a
copy o f t h e l i c e n s e a l o n g with t h i s program ; i f not , p l e a s e e m a i l
12 <q u a n t l i b −d e v @ l i s t s . s f . net >. The l i c e n s e i s a l s o a v a i l a b l e o n l i n e a t
<h t t p : / / q u a n t l i b . o r g / l i c e n s e . shtml >.
14

This program i s d i s t r i b u t e d i n t h e hope t h a t i t w i l l be u s e f u l , but WITHOUT


16 ANY WARRANTY; w i t h o u t even t h e i m p l i e d warranty o f MERCHANTABILITY o r FITNESS
FOR A PARTICULAR PURPOSE. See t h e l i c e n s e f o r more d e t a i l s .
18 ∗/

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 ;

24 f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)


{
26 Date next = i s s u e D a t e + Months ∗ ( ( i +1) ∗ 12 / yearPayments ) ;
b o o s t : : s h a r e d p t r <CashFlow> MP( new SimpleCashFlow (M, next ) ) ;
28 payment += MP;
}
30
r e t u r n payment ;
32 }

34 Leg Mortgage : : Fee ( ) c o n s t


{
36 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 f e e ;
38 f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)
{
40 Date next = i s s u e D a t e + Months ∗ ( ( i +1) ∗ 12 / yearPayments ) ;
b o o s t : : s h a r e d p t r <CashFlow> F( new SimpleCashFlow ( b e g i n i n g b a l a n c e [ i
]−>amount ( ) ∗ ( f e e / yearPayments ) , next ) ) ;
42 f e e += F ;
}
44 return fee ;
}

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

Leg Mortgage : : EndingBalance ( ) c o n s t


31 {
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 ( ) ;
33 Leg p r i n c i p a l r e p a y m e n t = PrincipalRepayment ( ) ;
Leg e n d i n g b a l a n c e ;
35 f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)
{
37 Date next = i s s u e D a t e + Months ∗ ( ( i +1) ∗ 12 / yearPayments ) ;
b o o s t : : s h a r e d p t r <CashFlow> EMB( new SimpleCashFlow ( b e g i n i n g b a l a n c e [ i
]−>amount ( ) − p r i n c i p a l r e p a y m e n t [ i ]−>amount ( ) , next ) ) ;
39
e n d i n g b a l a n c e += EMB;
41 }

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 ;

17 b o o s t : : format FORMATER ( ” %−20s % −12 s % −12 s % −15 s % −12 s % −12 s % −12 s ” ) ;

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 }

7. MBS (page 437 - 447)


In the previous section, no prepayment model was considered. Now, let’s assume the PSA
prepayment convention to generate mortgage cash flows. Because this is not implemented in
QuantLib 1.4, I propose the following FixedRateMortgagePSA class. FixedRateMortgagePSA
will inherited from FixedRateMortgage (FixedRateMortgage = Mortgage previous class).

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

6 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


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 /
8

QuantLib i s f r e e s o f t w a r e : you can r e d i s t r i b u t e i t and/ o r modify i t


10 under t h e terms o f t h e QuantLib l i c e n s e . You s h o u l d have r e c e i v e d a
copy o f t h e l i c e n s e a l o n g with t h i s program ; i f not , p l e a s e e m a i l
12 <q u a n t l i b −d e v @ l i s t s . s f . net >. The l i c e n s e i s a l s o a v a i l a b l e o n l i n e a t
<h t t p : / / q u a n t l i b . o r g / l i c e n s e . shtml >.
14

This program i s d i s t r i b u t e d i n t h e hope t h a t i t w i l l be u s e f u l , but WITHOUT


16 ANY WARRANTY; w i t h o u t even t h e i m p l i e d warranty o f MERCHANTABILITY o r FITNESS
FOR A PARTICULAR PURPOSE. See t h e l i c e n s e f o r more d e t a i l s .
18 ∗/

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 /

9 QuantLib i s f r e e s o f t w a r e : you can r e d i s t r i b u t e i t and/ o r modify i t


under t h e terms o f t h e QuantLib l i c e n s e . You s h o u l d have r e c e i v e d a
11 copy o f t h e l i c e n s e a l o n g with t h i s program ; i f not , p l e a s e e m a i l
<q u a n t l i b −d e v @ l i s t s . s f . net >. The l i c e n s e i s a l s o a v a i l a b l e o n l i n e a t
13 <h t t p : / / q u a n t l i b . o r g / l i c e n s e . shtml >.

15 This program i s d i s t r i b u t e d i n t h e hope t h a t i t w i l l be u s e f u l , but WITHOUT


ANY WARRANTY; w i t h o u t even t h e i m p l i e d warranty o f MERCHANTABILITY o r FITNESS
17 FOR A PARTICULAR PURPOSE. See t h e l i c e n s e f o r more d e t a i l s .
∗/
19
#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”
21 #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>
23 #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>
25
u s i n g namespace b o o s t : : a s s i g n ;
27
namespace QuantLib {
29 FixedRateMortgagePSA : : 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) :
FixedRateMortgage ( i s s u e D a t e , c a l e n d a r , amount , r a t e , f e e , term , 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 ) , PSA (PSA) {
GenerateCashFlows ( ) ; }
FixedRateMortgagePSA : : ˜ FixedRateMortgagePSA ( ) {}
31 //@
/∗ ∗∗ I n s p e c t o r ∗∗ ∗/
33 Real FixedRateMortgagePSA : : amount ( ) { r e t u r n amount ; }
I n t e r e s t R a t e FixedRateMortgagePSA : : r a t e ( ) { r e t u r n r a t e ; }
35 Rate FixedRateMortgagePSA : : f e e ( ) { r e t u r n f e e ; }

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 ( ) ;

7 f o r ( i n t i = 0 ; i < numberOfPayments ; i ++)


{
9 Date next = i s s u e D a t e + ( ( 1 + i ) ∗ 12 / yearPayments ) ∗ Months ;
i f ( i <1)
11 {
b o o s t : : s h a r e d p t r <CashFlow> MB( new SimpleCashFlow ( amount , next ) )
;
13 BeginingBalance += MB;
}
15 else
{
17 b o o s t : : s h a r e d p t r <CashFlow> MB( new SimpleCashFlow ( EndingBalance
( ) [ i −1]−>amount ( ) , next ) ) ;
B e g i n i n g B a l a n c e += MB;
19 }

21 b o o s t : : s h a r e d p t r <CashFlow> MP( new SimpleCashFlow ( bt [ i ]−>amount ( ) ∗


PMT, next ) ) ;
Payment += MP;
23

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 ) ) ;
25 I n t e r e s t += I ;

27 b o o s t : : s h a r e d p t r <CashFlow> F( new SimpleCashFlow ( B e g i n i n g B a l a n c e [ i


]−>amount ( ) ∗ ( f e e / yearPayments ) , next ) ) ;
F ee += F ;
29
b o o s t : : s h a r e d p t r <CashFlow> NI ( new SimpleCashFlow ( I n t e r e s t [ i ]−>
amount ( ) − F ee [ i ]−>amount ( ) , next ) ) ;
31 N e t I n t e r e s t += NI ;

33 b o o s t : : s h a r e d p t r <CashFlow> SP( new SimpleCashFlow ( bt [ i ]−>amount ( ) ∗


FixedRateMortgage : : PrincipalRepayment ( ) [ i ]−>amount ( ) , next ) ) ;
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 += SP ;
35
b o o s t : : s h a r e d p t r <CashFlow> PR( new SimpleCashFlow (smm[ i ]−>amount ( ) ∗
( 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 ( ) ) ,
next ) ) ;
37 P r i n c i p a l R e p a y m e n t += PR;

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 ; }

13 Leg FixedRateMortgagePSA : : Payment ( ) c o n s t { r e t u r n Payment ; }

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 ; }

17 Leg FixedRateMortgagePSA : : Fee ( ) c o n s t { r e t u r n F e e ; }

19 Leg FixedRateMortgagePSA : : PrincipalRepayment ( ) c o n s t { r e t u r n


PrincipalRepayment ; }

21 Leg FixedRateMortgagePSA : : EndingBalance ( ) c o n s t { r e t u r n EndingBalance ; }

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 ;}

17 Leg FixedRateMortgagePSA : : CashFlowToInvestors ( ) c o n s t { r e t u r n


CashFlowToInvestors ; }

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

You might also like