Sie sind auf Seite 1von 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 dierent template parameters and available constructors, most of the people get lost.
In this blog, Im going to implement some examples that you can nd 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 /
Fi xed Income Mathematics Four Edi t i on
3 pag ( 74 , 75)
Time path of the Pr i ce of a Di scount Bond :
5 20Years , 9 % coupon , 12 % Requi red Yi el d
/
7
#i nc l ude <i ostream>
9 #i nc l ude <ql / quant l i b . hpp>
#i nc l ude <al gori thm>
11
us i ng namespace QuantLib ;
13 us i ng namespace std ;
15 i nt main ( )
{
17 t r y {
/ Bond I nf or mat i on /
19 Cal endar cal endar = Nul l Cal endar ( ) ;
DayCounter DC = Actual Actual ( Actual Actual : : Bond) ;
21 Natural Settl ementDays = 3;
Date Today = Date : : todaysDate ( ) ;
23 Se t t i ngs : : i ns t anc e ( ) . eval uat i onDat e ( ) = Today ;
Date I s s ue = Today ;
25 Date Maturi ty = I s s ue+ 20 Years ;
Rate Coupon = 0 . 0 9 ;
27 I nt e r e s t Rat e CouponRate ( Coupon , Actual Actual ( Actual Actual : : Bond) ,
Compounded , Semi annual ) ;
Real FaceVal ue = 1000. 0;
29 vector <I nt er es t Rat e > Coupons ( 40 , CouponRate ) ;
31 Rel i nkabl eHandl e<Yi el dTermStructure>Fl atRate ;
Rel i nkabl eHandl e<FixedRateBond>BondInstrument ;
33
/ Bond Schedul e /
35 Schedul e sch ( I s s ue , Maturi ty , Peri od ( Semi annual ) , cal endar , Unadjusted ,
Unadjusted , DateGenerati on : : Backward , f a l s e ) ;
37 / Bond Constructor /
boost : : s har ed pt r <FixedRateBond> Bond1( new FixedRateBond ( Settl ementDays ,
FaceValue , sch , Coupons ) ) ;
39 BondInstrument . l i nkTo ( Bond1) ;
Rate r at e = 0 . 1 2 ;
41 Handle<Quote> Rate1 ( new SimpleQuote ( r at e ) ) ;
2
1 / TermStructure Constructor /
boost : : s har ed pt r <Yi el dTermStructure> TS( new Fl atForward ( I s s ue , Rate1 ,
Actual Actual ( Actual Actual : : Bond) , Compounded , Semi annual ) ) ;
3 Fl atRate . l i nkTo (TS) ;
boost : : s har ed pt r <Pri ci ngEngi ne> BondEngine ( new Di scounti ngBondEngi ne (
Fl atRate ) ) ;
5 BondInstrument>s et Pr i ci ngEngi ne ( BondEngine ) ;
cout << Bond1 Res ul t s \n ;
7 cout << Book Pr i ce : << 774. 3 << endl ;
cout << QuantLib : << BondInstrument>NPV( ) << endl ;
9 cout << \n ;
11 / For Bond2 j us t onl y change the Rate /
r at e = 0 . 0 7 ;
13 Handle<Quote> Rate2 ( new SimpleQuote ( r at e ) ) ;
boost : : s har ed pt r <Yi el dTermStructure> TS1( new Fl atForward ( I s s ue , Rate2 ,
Actual Actual ( Actual Actual : : Bond) , Compounded , Semi annual ) ) ;
15 Fl atRate . l i nkTo (TS1) ;
cout << Bond2 Res ul t s \n ;
17 cout << Book Pr i ce : << 1213. 55 << endl ;
cout << QuantLib : << BondInstrument>NPV( ) << endl ;
19 }
21 catch ( st d : : except i on& e ) {
c e r r << e . what ( ) << endl ;
23 }
}
2. Duration
Now lets estimate the duration of a bond (Simple, Modied, 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 Fi xed Income Mathematics Four Edi t i on
pag ( 215)
4 Bond I nf or mat i on :
5Years , 10 % coupon , 10 % Requi red Yi el d
6 /
#i nc l ude <i ostream>
8 #i nc l ude <ql / quant l i b . hpp>
#i nc l ude <al gori thm>
10
us i ng namespace QuantLib ;
12 us i ng namespace std ;
14 i nt main ( )
{
16 t r y {
/ Bond I nf or mat i on /
18 Cal endar cal endar = Nul l Cal endar ( ) ;
DayCounter DC = Actual Actual ( Actual Actual : : Bond) ;
20 Natural Settl ementDays = 3;
Date Today = Date : : todaysDate ( ) ;
22 Se t t i ngs : : i ns t anc e ( ) . eval uat i onDat e ( ) = Today ;
Date I s s ue = Today ;
24 Date Maturi ty = I s s ue+ 5 Years ;
Rate Coupon = 0 . 1 0 ;
26 Compounding compounding = Compounded ;
Frequency f r equency = Semi annual ;
28 I nt e r e s t Rat e CouponRate ( Coupon , Actual Actual ( Actual Actual : : Bond) ,
compounding , f r equency ) ;
Real FaceVal ue = 100. 0;
30 st d : : vector <I nt er es t Rat e > Coupons ( 10 , CouponRate ) ;
32 Rel i nkabl eHandl e<Yi el dTermStructure>Fl atRate ;
Rel i nkabl eHandl e<FixedRateBond>BondInstrument ;
34
/ Bond Schedul e /
36 Schedul e sch ( I s s ue , Maturi ty , Peri od ( Semi annual ) , cal endar , Unadjusted ,
Unadjusted , DateGenerati on : : Backward , f a l s e ) ;
38 / Bond Constructor /
boost : : s har ed pt r <FixedRateBond> Bond1( new FixedRateBond ( Settl ementDays ,
FaceValue , sch , Coupons ) ) ;
40 BondInstrument . l i nkTo ( Bond1) ;
Rate r at e = 0 . 1 0 ;
42 Handle<Quote> Rate1 ( new SimpleQuote ( r at e ) ) ;
4
/ TermStructure Constructor /
2 boost : : s har ed pt r <Yi el dTermStructure> TS( new Fl atForward ( I s s ue , Rate1 ,
Actual Actual ( Actual Actual : : Bond) , compounding , f r equency ) ) ;
Fl atRate . l i nkTo (TS) ;
4 boost : : s har ed pt r <Pri ci ngEngi ne> BondEngine ( new Di scounti ngBondEngi ne (
Fl atRate ) ) ;
BondInstrument>s et Pr i ci ngEngi ne ( BondEngine ) ;
6
/ Pr i nt /
8 cout << Bond1 Res ul t s \n ;
cout << Book Pr i ce : << 100. 0 << endl ;
10 cout << QuantLib : << BondInstrument>NPV( ) << (Ok, i s Par ) << endl ;
cout << Durati on ( Si mpl e ) : << BondFunctions : : dur at i on ( BondInstrument
, I nt e r e s t Rat e ( rate , DC, compounding , f r equency ) , Durati on : : Si mpl e ) << vs
Book : 4. 05 (Ok) << endl ;
12 cout << Durati on ( Macaulay ) : << BondFunctions : : dur at i on (
BondInstrument , I nt e r e s t Rat e ( rate , DC, compounding , f r equency ) , Durati on : :
Macaulay ) << vs Book : 4. 05 (Ok) << endl ;
cout << Durati on ( Modi f i ed ) : << BondFunctions : : dur at i on (
BondInstrument , I nt e r e s t Rat e ( rate , DC, compounding , f r equency ) , Durati on : :
Modi f i ed ) << vs Book : 3. 86 (Ok) << endl ;
14 }
16 catch ( st d : : except i on& e ) {
c e r r << e . what ( ) << endl ;
18 }
}
Now in the reference manual , search for other Public Memeber Functions (Owned and Inheri-
ted). You have a lot of stu there: date, cashow, 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 modied duration to estimate the new price change (bond
price volatility).
1 /
Fi xed Income Mathematics Four Edi t i on
3 pag (219 I l u s t r a t i o n 133)
Link between dur at i on and pr i ve v o l a t i l i t y
5 Bond I nf or mat i on :
15Years , 8 % coupon , 10 % Requi red Yi el d
7 /
#i nc l ude <i ostream>
9 #i nc l ude <ql / quant l i b . hpp>
#i nc l ude <al gori thm>
11
us i ng namespace QuantLib ;
13 us i ng namespace std ;
15 i nt main ( )
{
17 t r y {/ Bond I nf or mat i on /
Cal endar cal endar = Nul l Cal endar ( ) ;
19 DayCounter DC = Actual Actual ( Actual Actual : : Bond) ;
Natural Settl ementDays = 3;
21 Date Today = Date : : todaysDate ( ) ;
Se t t i ngs : : i ns t anc e ( ) . eval uat i onDat e ( ) = Today ;
23 Date I s s ue = Today ;
Date Maturi ty = I s s ue+ 15 Years ;
25 Rate Coupon = 0 . 0 8 ;
Compounding compounding = Compounded ;
27 Frequency f r equency = Semi annual ;
I nt e r e s t Rat e CouponRate ( Coupon , Actual Actual ( Actual Actual : : Bond) ,
compounding , f r equency ) ;
29 Real FaceVal ue = 100. 0;
st d : : vector <I nt er es t Rat e > Coupons ( 30 , CouponRate ) ;
31
Rel i nkabl eHandl e<Yi el dTermStructure>Fl atRate ;
33 Rel i nkabl eHandl e<FixedRateBond>BondInstrument ;
35 / Bond Schedul e /
Schedul e sch ( I s s ue , Maturi ty , Peri od ( Semi annual ) , cal endar , Unadjusted ,
Unadjusted , DateGenerati on : : Backward , f a l s e ) ;
37
/ Bond Constructor /
39 boost : : s har ed pt r <FixedRateBond> Bond1( new FixedRateBond ( Settl ementDays ,
FaceValue , sch , Coupons ) ) ;
BondInstrument . l i nkTo ( Bond1) ;
41 Rate r at e = 0 . 1 0 ;
Handle<Quote> Rate1 ( new SimpleQuote ( r at e ) ) ;
6
/ TermStructure Constructor /
2 boost : : s har ed pt r <Yi el dTermStructure> TS( new Fl atForward ( I s s ue , Rate1 ,
Actual Actual ( Actual Actual : : Bond) , compounding , f r equency ) ) ;
Fl atRate . l i nkTo (TS) ;
4 boost : : s har ed pt r <Pri ci ngEngi ne> BondEngine ( new Di scounti ngBondEngi ne (
Fl atRate ) ) ;
BondInstrument>s et Pr i ci ngEngi ne ( BondEngine ) ;
6
/ Pr i nt I n i t i a l Resul t /
8 Real BondPri ce 0 = BondInstrument>NPV( ) ;
Real Modi f i edDurati on = BondFuncti ons : : dur at i on ( BondInstrument ,
I nt e r e s t Rat e ( rate , DC, compounding , f r equency ) ) ;
10 cout << Bond1 Res ul t s \n ;
cout << QuantLib : << BondPri ce 0 << endl ;
12 cout << Durati on ( Modi f i ed ) : << Modi f i edDurati on << endl ;
cout << \n \n ;
14
/ New f l a t curve r at e ( s mal l spread )
16 For a s mal l spread , the dur at i on pr i c e approxi mati on of pr i c e
works r e l a t i v e l y we l l .
18 /
Spread s mal l s pr ead = 0 . 001;
20 Handle<Quote> Rate1 Smal l Spread ( new Simpl eQuote ( r at e + s mal l s pr ead ) ) ;
boost : : s har ed pt r <Yi el dTermStructure> TS smal l spread ( new Fl atForward (
I s s ue , Rate1 Smal l Spread , Actual Actual ( Actual Actual : : Bond) , compounding ,
f r equency ) ) ;
22 Fl atRate . l i nkTo ( TS smal l spread ) ;
24 / Pr i nt Pr i ce a f t e r spread ( Smal l ) /
26 Real Approx BondPri ce 1 = (1 ( Modi f i edDurati on s mal l s pr ead ) )
BondPri ce 0 ;
Real BondPri ce Smal l Spread = BondInstrument>NPV( ) ;
28
cout << Bond Resul t a f t e r spread i n yi e l d of : << s mal l s pr ead << endl ;
30 cout << setw ( 15) << New Bond Pr i ce : << setw ( 6) <<
BondPri ce Smal l Spread << endl ;
cout << setw ( 15) << Durati on BondPri ce Aprox : << setw ( 6) <<
Approx BondPri ce 1 << ( Good) << endl ;
32 cout << \n \n ;
34 / New f l a t curve r at e ( l ar ge spread )
For a l ar ge spread , the dur at i on pr i c e approxi mati on of pr i c e
36 doesnt work we l l .
/
38 Spread l ar ge s pr e ad = 0 . 0 3 ;
Handle<Quote> Rate1 LargeSpread ( new SimpleQuote ( r at e + l ar ge s pr e ad ) ) ;
40 boost : : s har ed pt r <Yi el dTermStructure> TS l ar ges pr ead ( new Fl atForward (
I s s ue , Rate1 LargeSpread , Actual Actual ( Actual Actual : : Bond) , compounding ,
f r equency ) ) ;
Fl atRate . l i nkTo ( TS l ar ges pr ead ) ;
7
1 / Pr i nt Pr i ce a f t e r spread ( Large ) /
3 Approx BondPri ce 1 = (1 ( Modi f i edDurati on l ar ge s pr e ad ) )
BondPri ce 0 ;
Real BondPri ce LargeSpread = BondInstrument>NPV( ) ;
5
cout << Bond Resul t a f t e r spread i n yi e l d of : << l ar ge s pr e ad << endl ;
7 cout << setw ( 15) << New Bond Pr i ce : << setw ( 6) <<
BondPri ce LargeSpread << endl ;
cout << setw ( 15) << Durati on BondPri ce Aprox : << setw ( 6) <<
Approx BondPri ce 1 << ( Not Good) << endl ;
9 }
11 catch ( st d : : except i on& e ) {
c e r r << e . what ( ) << endl ;
13 }
}
Duration consider linear price change eect (rst 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 eect
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 Fi xed Income Mathematics Four Edi t i on
pag (253 I l u s t r a t i o n 142)
4 Link between dur at i on and pr i ve v o l a t i l i t y
Bond I nf or mat i on :
6 15Years , 8 % coupon ,
Yi el d 0 = 10 %, Yi el d 1 = 13 %
8 /
#i nc l ude <i ostream>
10 #i nc l ude <ql / quant l i b . hpp>
#i nc l ude <al gori thm>
12 #i nc l ude <boost /f ormat . hpp>
14 us i ng namespace QuantLib ;
us i ng namespace std ;
8
1 voi d l i n e ( )
{
3 f or ( i nt i = 1 ; i < 50 ; i ++)
cout << ;
5 cout << endl ;
}
7
i nt main ( )
9 {
t r y {
11 / Bond I nf or mat i on /
Cal endar cal endar = Nul l Cal endar ( ) ;
13 DayCounter DC = Actual Actual ( Actual Actual : : Bond) ;
Natural Settl ementDays = 3;
15 Date Today = Date : : todaysDate ( ) ;
Se t t i ngs : : i ns t anc e ( ) . eval uat i onDat e ( ) = Today ;
17 Date I s s ue = Today ;
Date Maturi ty = I s s ue+ 15 Years ;
19 Rate Coupon = 0 . 0 8 ;
Compounding compounding = Compounded ;
21 Frequency f r equency = Semi annual ;
I nt e r e s t Rat e CouponRate ( Coupon , Actual Actual ( Actual Actual : : Bond) ,
compounding , f r equency ) ;
23 Real FaceVal ue = 100. 0;
st d : : vector <I nt er es t Rat e > Coupons ( 30 , CouponRate ) ;
25
Rel i nkabl eHandl e<Yi el dTermStructure>Fl atRate ;
27 Rel i nkabl eHandl e<FixedRateBond>BondInstrument ;
29 / Bond Schedul e /
Schedul e sch ( I s s ue , Maturi ty , Peri od ( Semi annual ) , cal endar , Unadjusted ,
Unadjusted , DateGenerati on : : Backward , f a l s e ) ;
31
/ Bond Constructor /
33 boost : : s har ed pt r <FixedRateBond> Bond1( new FixedRateBond ( Settl ementDays ,
FaceValue , sch , Coupons ) ) ;
BondInstrument . l i nkTo ( Bond1) ;
35 Rate r at e = 0 . 1 0 ;
Handle<Quote> Rate1 ( new SimpleQuote ( r at e ) ) ;
37
/ TermStructure Constructor /
39 boost : : s har ed pt r <Yi el dTermStructure> TS( new Fl atForward ( I s s ue , Rate1 ,
Actual Actual ( Actual Actual : : Bond) , compounding , f r equency ) ) ;
Fl atRate . l i nkTo (TS) ;
41 boost : : s har ed pt r <Pri ci ngEngi ne> BondEngine ( new Di scounti ngBondEngi ne (
Fl atRate ) ) ;
BondInstrument>s et Pr i ci ngEngi ne ( BondEngine ) ;
9
/ Pr i nt I n i t i a l Resul t /
2 Real BondPri ce 0 = BondInstrument>NPV( ) ;
Real Modi f i edDurati on = BondFuncti ons : : dur at i on ( BondInstrument ,
I nt e r e s t Rat e ( rate , DC, compounding , f r equency ) ) ;
4 Real Convexi ty = BondFunctions : : convexi t y ( BondInstrument , I nt e r e s t Rat e
( rate , DC, compounding , f r equency ) ) ;
cout << Bond1 Res ul t s \n ;
6 cout << QuantLib : << BondPri ce 0 << endl ;
cout << Durati on ( Modi f i ed ) : << Modi f i edDurati on << endl ;
8 cout << Convexi ty : << Convexi ty << endl ;
cout << \n \n ;
10
/ New f l a t curve r at e ( s mal l spread )
12 For an s mal l spread , the dur at i on pr i c e approxi mati on of pr i c e
works r e l a t i v e l l y we l l .
14 /
Spread s mal l s pr ead = 0 . 001;
16 Handle<Quote> Rate1 Smal l Spread ( new Simpl eQuote ( r at e + s mal l s pr ead ) ) ;
boost : : s har ed pt r <Yi el dTermStructure> TS smal l spread ( new Fl atForward (
I s s ue , Rate1 Smal l Spread , Actual Actual ( Actual Actual : : Bond) , compounding ,
f r equency ) ) ;
18 Fl atRate . l i nkTo ( TS smal l spread ) ;
20 / New f l a t curve r at e ( l ar ge spread )
For an l ar ge spread , the dur at i on pr i c e approxi mati on of pr i c e
22 doesnt work we l l .
/
24 Spread l ar ge s pr e ad = 0 . 0 3 ;
Handle<Quote> Rate1 LargeSpread ( new SimpleQuote ( r at e + l ar ge s pr e ad ) ) ;
26 boost : : s har ed pt r <Yi el dTermStructure> TS l ar ges pr ead ( new Fl atForward (
I s s ue , Rate1 LargeSpread , Actual Actual ( Actual Actual : : Bond) , compounding ,
f r equency ) ) ;
Fl atRate . l i nkTo ( TS l ar ges pr ead ) ;
28
/ Pr i nt Pr i ce a f t e r spread ( Large ) /
30 Real BondPri ce 1 = BondInstrument>NPV( ) ;
Real Pri ceChange = ( BondPri ce 1/BondPri ce 0 ) 1;
32
Real Convexi t yEf f ect = 0. 5 Convexi ty l ar ge s pr e ad l ar ge s pr e ad ;
34 Real Dur at i onEf f ect = Modi f i edDurati on l ar ge s pr e ad ;
boost : : f ormat FORMATER( %25s %20s ) ;
36 cout << Yi el d change from 0. 1 to 0. 13 << endl ;
l i n e ( ) ;
38 cout << FORMATER % Pr i ce Change Based on % Aproxx % Pr i ce Change \n ;
l i n e ( ) ;
40 cout << FORMATER % Durati on % Dur at i onEf f ect << endl ;
cout << FORMATER % Convexi ty % Convexi t yEf f ect << endl ;
42 l i n e ( ) ;
cout << FORMATER % TOTAL % ( Dur at i onEf f ect + Convexi t yEf f ect ) << endl ;
44 cout << \n \n ;
10
cout << The ac t ual % change i n pr i c e would be : << Pri ceChange ;
2
}
4
catch ( st d : : except i on& e ) {
6 c e r r << e . what ( ) << endl ;
}
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 Fi xed Income Mathematics Four Edi t i on
pag (324 325)
4 Set Yi el d curve , and get : zer o r at es ,
f orward r at es , and pr i c e an opt i on f r e e
6 bond .
/
8 #i nc l ude <i ostream>
#i nc l ude <ql / quant l i b . hpp>
10 #i nc l ude <vector >
#i nc l ude <s t r i ng >
12 #i nc l ude <boost /f ormat . hpp>
#i nc l ude <boost / as s i gn / st d/ vect or . hpp>
14
us i ng namespace QuantLib ;
16 us i ng namespace std ;
us i ng namespace boost : : as s i gn ;
18
boost : : f ormat FORMATER( %8s % 8.4d ) ;
11
1 i nt main ( )
{
3 t r y {
/ Real data /
5 cons t Si z e numberOfBonds ( 3) ;
Real MarketPri ce [ numberOfBonds ] ={100 , 100 , 100};
7
/ Quote data /
9 vector <boost : : s har ed pt r <SimpleQuote> > Quotes ;
11 / Rel i nkHanl de Quotes /
Rel i nkabl eHandl e<Quote> quoteHandl es [ numberOfBonds ] ;
13
f o r ( Si z e i = 0; i != numberOfBonds ; i ++)
15 {
boost : : s har ed pt r <SimpleQuote> Q( new SimpleQuote ( MarketPri ce [ i ] ) ) ;
17 Quotes += Q;
quoteHandl es [ i ] . l i nkTo ( Quotes [ i ] ) ;
19 }
21
/ Bonds I nf or mat i on /
23 I nt e ge r l engt h [ ] = {1 , 2 , 3};
Real coupons [ ] = {0. 035 , 0. 04 , 0. 045};
25 Real y i e l ds [ ] = {0. 035 , 0. 04 , 0. 045};
Frequency f r equency = Annual ;
27 DayCounter dc = SimpleDayCounter ( ) ;
Busi nessDayConventi on accrual Conventi on = Modi f i edFol l owi ng ;
29 Busi nessDayConventi on Conventi on = Modi f i edFol l owi ng ;
Real Redemption = 100;
31
Cal endar cal endar = Nul l Cal endar ( ) ;
33 Date today = cal endar . adj us t ( Date : : todaysDate ( ) ) ;
Se t t i ngs : : i ns t anc e ( ) . eval uat i onDat e ( ) = today ;
35
Natural bondSettl ementDays = 0;
37 Date BondSettl ementDate = cal endar . advance ( today , bondSettl ementDays Days
) ;
39 / Bond1 Constructor ( t r y to do i t i n a l oop ) /
Date MaturityBond1 = cal endar . advance ( BondSettl ementDate , l engt h [ 0 ]
Years ) ;
41 / Schedul e /
Schedul e SchBond1 ( BondSettl ementDate , MaturityBond1 , Peri od ( f r equency ) ,
cal endar , accrual Conventi on , accrual Conventi on , DateGenerati on : : Backward ,
f a l s e ) ;
12
boost : : s har ed pt r <BondHelper> B1( new Fi xedRateBondHel per ( quoteHandl es
[ 0 ] , bondSettl ementDays , 1 0 0 . 0 , SchBond1 , vector <Rate >(1 , coupons [ 0 ] ) , dc ,
Convention , Redemption ) ) ;
2
4 / Bond2 Constructor /
Date MaturityBond2 = cal endar . advance ( BondSettl ementDate , l engt h [ 1 ]
Years ) ;
6 / Schedul e /
Schedul e SchBond2 ( BondSettl ementDate , MaturityBond2 , Peri od ( f r equency ) ,
cal endar , accrual Conventi on , accrual Conventi on ,
8 DateGenerati on : : Backward , f a l s e ) ;
boost : : s har ed pt r <BondHelper> B2( newFixedRateBondHelper ( quoteHandl es [ 1 ] ,
bondSettl ementDays , 1 0 0 . 0 , SchBond2 , vector <Rate >(2 , coupons [ 1 ] ) , dc , Convention ,
Redemption ) ) ;
10
/ Bond3 Constructor /
12 Date MaturityBond3 = cal endar . advance ( BondSettl ementDate , l engt h [ 2 ]
Years ) ;
/ Schedul e /
14 Schedul e SchBond3 ( BondSettl ementDate , MaturityBond3 , Peri od ( f r equency ) ,
cal endar , accrual Conventi on , accrual Conventi on , DateGenerati on : : Backward ,
f a l s e ) ;
boost : : s har ed pt r <BondHelper> B3( new Fi xedRateBondHel per ( quoteHandl es
[ 2 ] , bondSettl ementDays , 1 0 0 . 0 , SchBond3 , vector <Rate >(3 , coupons [ 2 ] ) , dc ,
Convention , Redemption ) ) ;
16 / The Curve
Be c o ns i s t e nt . I f you use hel per s ,
18 cont i nue us i ng them unt i l curve bui l di ng .
/
20 DayCounter termStructureDayCounter = Actual Actual ( Actual Actual : : ISDA) ;
Real t ol e r anc e = 1. 0 e 12;
22 vector <boost : : s har ed pt r <RateHel per> > BondPri ces ;
BondPri ces += B1 , B2 , B3 ;
24
boost : : s har ed pt r <Yi el dTermStructure> TS( new Pi ecewi seYi el dCurve<
Di scount , LogLi near >(BondSettl ementDate , BondPri ces , termStructureDayCounter ,
t ol e r anc e ) ) ;
26 / Get Spot Rates ( page 324 i n book ) /
Rate zero1Year = TS>zeroRate ( MaturityBond1 , termStructureDayCounter ,
Compounded) ;
28 Rate zero2Year = TS>zeroRate ( MaturityBond2 , termStructureDayCounter ,
Compounded) ;
Rate zero3Year = TS>zeroRate ( MaturityBond3 , termStructureDayCounter ,
Compounded) ;
30
/ Pr i nt Spot /
32 s t r i ng r ul e ( 20 , ) ;
cout << FORMATER % Year % Spot Rate \n ;
34 cout << r ul e << endl ;
13
cout << FORMATER % 1 % zero1Year << endl ;
2 cout << FORMATER % 2 % zero2Year << endl ;
cout << FORMATER % 3 % zero3Year << \n \n ;
4
/ Get Forward Rates ( page 325 i n book ) /
6 Rate F0x1 = TS>f orwardRate ( today , MaturityBond1 ,
termStructureDayCounter , Si mpl e ) ;
Rate F1x2 = TS>f orwardRate ( MaturityBond1 , MaturityBond2 , dc , Compounded
) ;
8 Rate F2x3 = TS>f orwardRate ( MaturityBond2 , MaturityBond3 , dc , Compounded
) ;
10 / Pr i nt Forward /
cout << FORMATER % Year % Forward Rate \n ;
12 cout << r ul e << endl ;
cout << FORMATER % F0x1 % F0x1 << endl ;
14 cout << FORMATER % F1x2 % F1x2 << endl ;
cout << FORMATER % F2x3 % F2x3 << \n \n ;
16
/ Pr i ce the Bond ( page 325 i n book )
18 Bond I nf or mat i on
3Years , 5.25 % coupon ( annual )
20 /
Date Maturi ty = cal endar . advance ( BondSettl ementDate , 3 Years ) ;
22
Schedul e SchBond( BondSettl ementDate , Maturi ty , Peri od ( f r equency ) ,
cal endar , accrual Conventi on , accrual Conventi on , DateGenerati on : : Backward ,
f a l s e ) ;
24
FixedRateBond Bond( bondSettl ementDays , 100. 0 , SchBond , vector <Rate
>(3 , 0. 0525) , dc ) ;
26
Rel i nkabl eHandl e<Yi el dTermStructure> di scountTermStructure ;
28 di scountTermStructure . l i nkTo (TS) ;
boost : : s har ed pt r <Pri ci ngEngi ne> BondEngine ( new Di scounti ngBondEngi ne (
di scountTermStructure ) ) ;
30 Bond . s et Pr i ci ngEngi ne ( BondEngine ) ;
32 cout << Bond Pr i ce i s : << Bond . c l e anPr i c e ( ) ;
34 }
36 catch ( st d : : except i on& e ) {
c e r r << e . what ( ) << endl ;
38 }
}
5. Callable Bond (page 337)
In page 293, Fabozzi dierentiate between one factor Normal models, and one factor Lognormal
models. The dierence 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 arent developed
yet
2
. Thats way, I will implement: Black-Karasinski, and Hull-White.
1 /
Fi xed Income Mathematics Four Edi t i on
3 pag (324 325)
Esti mate Cal l Bond Pr i ce and
5 Cal l opt i on val ue .
/
7 #i nc l ude <i ostream>
#i nc l ude <ql / quant l i b . hpp>
9 #i nc l ude <vector >
#i nc l ude <s t r i ng >
11 #i nc l ude <boost /f ormat . hpp>
#i nc l ude <boost / as s i gn / st d/ vect or . hpp>
13
us i ng namespace QuantLib ;
15 us i ng namespace std ;
us i ng namespace boost : : as s i gn ;
17
boost : : f ormat FORMATER( %7s % 8.6d % 11.6d % 10.4d % 12.4d % 10. 4d ) ;
19
i nt main ( )
21 {
t r y {
23 / Real data /
cons t Si z e numberOfBonds ( 3) ;
25 Real MarketPri ce [ numberOfBonds ] ={100 , 100 , 100};
27 / Quote data /
vector <boost : : s har ed pt r <SimpleQuote> > Quotes ;
29
/ Rel i nkHanl de Quotes /
31 Rel i nkabl eHandl e<Quote> quoteHandl es [ numberOfBonds ] ;
33 f o r ( Si z e i = 0; i != numberOfBonds ; i ++)
{
35 boost : : s har ed pt r <SimpleQuote> Q( new SimpleQuote ( MarketPri ce [ i ] ) ) ;
Quotes += Q;
37 quoteHandl es [ i ] . l i nkTo ( Quotes [ i ] ) ;
}
2
However, VarianceGamma and other process are already implemented.
15
/ Bonds I nf or mat i on /
2 I nt e ge r l engt h [ ] = {1 , 2 , 3};
Real coupons [ ] = {0. 035 , 0. 04 , 0. 045};
4 Real y i e l ds [ ] = {0. 035 , 0. 04 , 0. 045};
Frequency f r equency = Annual ;
6 DayCounter dc = SimpleDayCounter ( ) ;
Busi nessDayConventi on accrual Conventi on = Modi f i edFol l owi ng ;
8 Busi nessDayConventi on Conventi on = Modi f i edFol l owi ng ;
Real Redemption = 100;
10
Cal endar cal endar = Nul l Cal endar ( ) ;
12 Date today = cal endar . adj us t ( Date : : todaysDate ( ) ) ;
Se t t i ngs : : i ns t anc e ( ) . eval uat i onDat e ( ) = today ;
14
Natural bondSettl ementDays = 0;
16 Date BondSettl ementDate = cal endar . advance ( today , bondSettl ementDays Days
) ;
18 / Bond1 Constructor ( t r y to do i t i n a l oop ) /
Date MaturityBond1 = cal endar . advance ( BondSettl ementDate , l engt h [ 0 ]
Years ) ;
20 / Schedul e /
Schedul e SchBond1 ( BondSettl ementDate , MaturityBond1 , Peri od ( f r equency ) ,
cal endar , accrual Conventi on , accrual Conventi on , DateGenerati on : : Backward ,
f a l s e ) ;
22
boost : : s har ed pt r <BondHelper> B1( new Fi xedRateBondHel per ( quoteHandl es
[ 0 ] , bondSettl ementDays , 1 0 0 . 0 , SchBond1 ,
24 vector <Rate >(1 ,
coupons [ 0 ] ) , dc , Convention , Redemption ) ) ;
26 / Bond2 Constructor /
Date MaturityBond2 = cal endar . advance ( BondSettl ementDate , l engt h [ 1 ]
Years ) ;
28 / Schedul e /
Schedul e SchBond2 ( BondSettl ementDate , MaturityBond2 , Peri od ( f r equency ) ,
cal endar , accrual Conventi on , accrual Conventi on ,
30 DateGenerati on : : Backward , f a l s e ) ;
boost : : s har ed pt r <BondHelper> B2( new Fi xedRateBondHel per ( quoteHandl es
[ 1 ] , bondSettl ementDays , 1 0 0 . 0 , SchBond2 , vector <Rate >(2 , coupons [ 1 ] ) , dc ,
Convention , Redemption ) ) ;
16
1 / Bond3 Constructor /
Date MaturityBond3 = cal endar . advance ( BondSettl ementDate , l engt h [ 2 ]
Years ) ;
3 / Schedul e /
Schedul e SchBond3 ( BondSettl ementDate , MaturityBond3 , Peri od ( f r equency ) ,
cal endar , accrual Conventi on , accrual Conventi on , DateGenerati on : : Backward ,
f a l s e ) ;
5 boost : : s har ed pt r <BondHelper> B3( new Fi xedRateBondHel per ( quoteHandl es
[ 2 ] , bondSettl ementDays , 1 0 0 . 0 , SchBond3 , vector <Rate >(3 , coupons [ 2 ] ) , dc ,
Convention , Redemption ) ) ;
/ The Curve
7 Be c o ns i s t e nt . I f you use hel per s ,
cont i nue us i ng them unt i l curve bui l di ng .
9 /
DayCounter termStructureDayCounter = Actual Actual ( Actual Actual : : ISDA) ;
11 Real t ol e r anc e = 1. 0 e 12;
vector <boost : : s har ed pt r <RateHel per> > BondPri ces ;
13 BondPri ces += B1 , B2 , B3 ;
15 boost : : s har ed pt r <Yi el dTermStructure> TS( new Pi ecewi seYi el dCurve<
Di scount , LogLi near >(BondSettl ementDate , BondPri ces , termStructureDayCounter ,
t ol e r anc e ) ) ;
17 Rel i nkabl eHandl e<Yi el dTermStructure> di scountTermStructure ;
di scountTermStructure . l i nkTo (TS) ;
19
/ Val ui ng Cal l abl e Bond ( page 337 i n book )
21 Coupon : 5.25 % Cal l pr i c e : 100
Maturi ty : 3 year s Cal l Date : 1 year
23 /
25 / Step 1: Set Bond Cha r a c t e r i s t i c s /
Date i s s ue = today ;
27 Date Maturi ty = cal endar . advance ( today , 3 Years ) ;
Natural Settl ementDays = 0;
29 Cal endar bondCal endar = cal endar ;
Real Coupon = 0. 0525;
31 Frequency CouponFrequency = Annual ;
Busi nessDayConventi on BondConvention = Conventi on ;
33 Busi nessDayConventi on BondAccrueConvention = Conventi on ;
DayCounter BondDC = dc ;
35
Schedul e SchBond( today , Maturi ty , Peri od ( CouponFrequency ) , bondCalendar ,
BondConvention , BondConvention , DateGenerati on : : Backward , f a l s e ) ;
37
/ Cal l Schedul e /
39 Cal l abi l i t ySc he dul e Cal l Sch ;
Real Pr i ce = 100 . 0;
41 Ca l l a b i l i t y : : Pr i ce Cal l Pr i c e ( Pri ce , Ca l l a b i l i t y : : Pr i ce : : Cl ean ) ; // Set
Cal l Pr i ce
17
1 Date Cal l Date = cal endar . advance ( today , 1 Years ) ; // Set
Cal l Date
boost : : s har ed pt r <Ca l l a bi l i t y > c a l l a b i l i t y ( new Ca l l a b i l i t y ( Cal l Pr i ce ,
Ca l l a b i l i t y : : Cal l , Cal l Date ) ) ;
3 Cal l Sch +=c a l l a b i l i t y ;
5 Si z e maxI t er at i ons = 1000;
Real accuracy = 1e 8;
7 I nt e ge r g r i dI nt e r v a l s = 10;
Real r ever s i onPar amet er = . 1 ;
9
Real si gma = 0 . 0 1 ; //QL EPSILON; // cor e dumps i f zer o on Cygwin
11
boost : : s har ed pt r <ShortRateModel> hw0( new Hul l Whi te (
di scountTermStructure , reversi onParameter , sigma ) ) ;
13 boost : : s har ed pt r <ShortRateModel> Bl ack Kar as i ns ki ( new Bl ackKar as i ns ki (
di scountTermStructure ) ) ;
15 boost : : s har ed pt r <Pri ci ngEngi ne> engi ne0 ( new
TreeCal l abl eFi xedRateBondEngi ne (hw0 , g r i dI nt e r v a l s ) ) ;
boost : : s har ed pt r <Pri ci ngEngi ne> engi ne1 ( new
TreeCal l abl eFi xedRateBondEngi ne ( Bl ack Kar as i ns ki , g r i dI nt e r v a l s ) ) ;
17 boost : : s har ed pt r <Pri ci ngEngi ne> BondEngine ( new Di scounti ngBondEngi ne (
di scountTermStructure ) ) ;
19 FixedRateBond Bond( Settl ementDays , 100 , SchBond , vector <Rate >(3 , Coupon) ,
BondDC) ;
Cal l abl eFi xedRateBond cal l abl eBond ( Settl ementDays , 100 , SchBond , vector <
Rate >(3 , Coupon) , BondDC, BondAccrueConvention ,
21 100 , today , Cal l Sch ) ;
23 / Res ul t s /
Bond . s et Pr i ci ngEngi ne ( BondEngine ) ;
25 Real Bond Pri ce = Bond . c l e anPr i c e ( ) ; // Option f r e e bond val ue
Rate Bond Yi el d = Bond . yi e l d (BondDC, Compounded , CouponFrequency ,
accuracy , maxI t er at i ons ) ;
27
cal l abl eBond . s et Pr i ci ngEngi ne ( engi ne0 ) ;
29 Real CallPrice HW = cal l abl eBond . c l e anPr i c e ( ) ;
Rate Cal l Bond Yi el d1 = cal l abl eBond . yi e l d (BondDC, Compounded ,
CouponFrequency , accuracy , maxI t er at i ons ) ;
31 Real OptPri ce1 = Bond Pri ce CallPrice HW ;
33 cal l abl eBond . s et Pr i ci ngEngi ne ( engi ne1 ) ;
Real Cal l Pri ce BK = cal l abl eBond . c l e anPr i c e ( ) ;
18
Rate Cal l Bond Yi el d2 = cal l abl eBond . yi e l d (BondDC, Compounded ,
CouponFrequency , accuracy , maxI t er at i ons ) ;
2 Real OptPri ce2 = Bond Pri ce Cal l Pri ce BK ;
4 Real BondPri ce = 101. 2654;
Spread z Spread = 10000 BondFunctions : : zSpread ( Bond , BondPrice , TS, dc
, Compounded , f r equency ) ;
6 Spread HW oas = z Spread OptPri ce1 ;
Spread BK oas = z Spread OptPri ce2 ;
8
/ Pr i nt /
10 s t r i ng r ul e ( 70 , ) ;
cout << r ul e << endl ;
12 cout << FORMATER % MODEL % Bond % Cal l Bond % Opt . Value % Z
spread ( bsp ) % OAS( bsp ) << endl ;
cout << r ul e << endl ;
14 cout << FORMATER % HW: % Bond Pri ce % CallPrice HW % OptPri ce1 %
z Spread %HW oas << endl ;
cout << FORMATER % BK: % Bond Pri ce % Cal l Pri ce BK % OptPri ce2 %
z Spread % BK oas << endl ;
16 }
18 catch ( st d : : except i on& e ) {
c e r r << e . what ( ) << endl ;
20 }
}
6. Analizing Securitized Products (page 397)
QuantLib (1.4) doesnt have any class named loan or mortgage. However, implementing this is
not too hard. Because a mortgage is a loan, Im going to implement a base class named Loan
and inherited from this other classes like: xed 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 les are:
19
1 / mode : c++; tabwi dth : 4; i ndent tabsmode : n i l ; cbas i c o f f s e t : 4 /
3 /
Copyri ght (C) 2014 Mauri ci o bedoya
5
Thi s f i l e i s part of QuantLib , a f r e e s of t war e /opens our ce l i br a r y
7 f or f i n a n c i a l quant i t at i ve anal ys t s and de ve l ope r s http : // quant l i b . org /
9 QuantLib i s f r e e s of t war e : you can r e di s t r i but e i t and/ or modi fy i t
under the terms of the QuantLib l i c e n s e . You shoul d have r e c e i ve d a
11 copy of the l i c e n s e al ong wi th t hi s program ; i f not , pl e as e emai l
<quant l i bde v@l i s t s . s f . net >. The l i c e n s e i s al s o a va i l a bl e onl i ne at
13 <http : // quant l i b . org / l i c e n s e . shtml >.
15 Thi s program i s di s t r i but e d i n the hope that i t wi l l be us e f ul , but WITHOUT
ANY WARRANTY; wi thout even the i mpl i ed warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the l i c e n s e f o r more d e t a i l s .
/
19
#i f nde f quant l i b l oan hpp
21 #de f i ne quant l i b l oan hpp
23 #i nc l ude <ql / event . hpp>
#i nc l ude <ql / cas hf l ows / s i mpl ecas hf l ow . hpp>
25
namespace QuantLib{
27
// ! Base c l a s s f o r l oans
29 / ! Thi s c l a s s i s pur el y v i r t ua l and ac t s as a base c l a s s f o r the
ac t ual l oans i mpl ementati ons .
31 /
c l a s s Loan : publ i c Event {
33 publ i c :
v i r t ua l Leg Begi ni ngBal ance ( ) cons t = 0;
35 v i r t ua l Leg Payment ( ) cons t = 0;
v i r t ua l Leg Ne t I nt e r e s t ( ) cons t = 0;
37 v i r t ua l Leg Fee ( ) cons t = 0;
v i r t ua l Leg Pri nci pal Repayment ( ) cons t = 0;
39 v i r t ua l Leg Endi ngBal ance ( ) cons t = 0;
v i r t ua l voi d accept ( Ac yc l i c Vi s i t or& v) ;
41 / from Event /
v i r t ua l Date date ( ) cons t = 0;
43 };
45 }
#e ndi f
20
/ mode : c++; tabwi dth : 4; i ndent tabsmode : n i l ; cbas i c o f f s e t : 4 /
2
/
4 Copyri ght (C) 2014 Mauri ci o bedoya
6 Thi s f i l e i s part of QuantLib , a f r e e s of t war e /opens our ce l i br a r y
f or f i n a n c i a l quant i t at i ve anal ys t s and de ve l ope r s http : // quant l i b . org /
8
QuantLib i s f r e e s of t war e : you can r e di s t r i but e i t and/ or modi fy i t
10 under the terms of the QuantLib l i c e n s e . You shoul d have r e c e i ve d a
copy of the l i c e n s e al ong wi th t hi s program ; i f not , pl e as e emai l
12 <quant l i bde v@l i s t s . s f . net >. The l i c e n s e i s al s o a va i l a bl e onl i ne at
<http : // quant l i b . org / l i c e n s e . shtml >.
14
Thi s program i s di s t r i but e d i n the hope that i t wi l l be us e f ul , but WITHOUT
16 ANY WARRANTY; wi thout even the i mpl i ed warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the l i c e n s e f o r more d e t a i l s .
18 /
20 #i nc l ude l oan . hpp
#i nc l ude <ql / pat t er ns / v i s i t o r . hpp>
22
namespace QuantLib {
24 voi d Loan : : accept ( Ac yc l i c Vi s i t or& v)
{
26 Vi s i t or <Loan> v1 = dynami c cast<Vi s i t or <Loan> >(&v) ;
i f ( v1! =0)
28 v1>v i s i t ( t hi s ) ;
e l s e
30 Event : : accept ( v) ;
}
32 }
For the mortgage.hpp and mortgage.cpp, we have:
21
/ mode : c++; tabwi dth : 4; i ndent tabsmode : n i l ; cbas i c o f f s e t : 4 /
2
/
4 Copyri ght (C) 2014 Mauri ci o bedoya
6 Thi s f i l e i s part of QuantLib , a f r e e s of t war e /opens our ce l i br a r y
f or f i n a n c i a l quant i t at i ve anal ys t s and de ve l ope r s http : // quant l i b . org /
8
QuantLib i s f r e e s of t war e : you can r e di s t r i but e i t and/ or modi fy i t
10 under the terms of the QuantLib l i c e n s e . You shoul d have r e c e i ve d a
copy of the l i c e n s e al ong wi th t hi s program ; i f not , pl e as e emai l
12 <quant l i bde v@l i s t s . s f . net >. The l i c e n s e i s al s o a va i l a bl e onl i ne at
<http : // quant l i b . org / l i c e n s e . shtml >.
14
Thi s program i s di s t r i but e d i n the hope that i t wi l l be us e f ul , but WITHOUT
16 ANY WARRANTY; wi thout even the i mpl i ed warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the l i c e n s e f o r more d e t a i l s .
18 /
20 #i f nde f quantl i b mortgage hpp
#de f i ne quantl i b mortgage hpp
22
#i nc l ude l oan . hpp
24 #i nc l ude <ql / i n t e r e s t r a t e . hpp>
#i nc l ude <ql /ti me / cal endar . hpp>
26 #i nc l ude <ql /ti me / daycounter . hpp>
#i nc l ude <ql /ti me / dat e ge ne r at i onr ul e . hpp>
28 #i nc l ude <ql / cas hf l ows / s i mpl ecas hf l ow . hpp>
30 namespace QuantLib {
c l a s s Mortgage : publ i c Loan{
32 publ i c :
Mortgage ( cons t Date& i ssueDate , cons t Cal endar& cal endar , cons t Real&
amount , cons t I nt e r e s t Rat e& rate , cons t Rate& f ee , cons t I nt e ge r& term , cons t
I nt e ge r& yearPayments ) ;
34 v i r t ua l Mortgage ( ) ;
//@{
36 / I ns pe c t or /
Date i s s ueDat e ( ) ;
38 Real amount ( ) ;
I nt e r e s t Rat e r at e ( ) ;
40 Rate f e e ( ) ;
42 / Se t t e r s /
voi d s e t i s s ue Dat e ( cons t Date& new Date ) ;
44 voi d set amount ( cons t Real& new amount ) ;
voi d s e t r a t e ( cons t I nt e r e s t Rat e& new rate ) ;
46 voi d s e t f e e ( cons t Rate& new f ee ) ;
22
/ Pure Vi r t ual /
2 Leg Begi ni ngBal ance ( ) cons t ;
Leg Payment ( ) cons t ;
4 Leg Ne t I nt e r e s t ( ) cons t ;
Leg Fee ( ) cons t ;
6 Leg Pri nci pal Repayment ( ) cons t ;
Leg Endi ngBal ance ( ) cons t ;
8 Date date ( ) cons t ; // From Event c l a s s
10 / Vi s i t o r /
v i r t ua l voi d accept ( Ac yc l i c Vi s i t or& v) ;
12 //@}
14 pr i vat e :
Date i s s ue Dat e ;
16 Cal endar c al e ndar ;
Real amount ;
18 I nt e ge r term , yearPayments ;
Compounding compound ;
20 I nt e r e s t Rat e r a t e ;
Rate f e e ;
22
/ Hel per s /
24 Time t = ( 1. 0/ yearPayments ) ;
I nt e ge r numberOfPayments = ( term yearPayments ) ;
26 Real compoundFactor = r a t e . compoundFactor ( t ) ;
};
28 }
30 #e ndi f / def i ne d ( hh mor t gage ) /
23
/ mode : c++; tabwi dth : 4; i ndent tabsmode : n i l ; cbas i c o f f s e t : 4 /
2
/
4 Copyri ght (C) 2014 Mauri ci o bedoya
6 Thi s f i l e i s part of QuantLib , a f r e e s of t war e /opens our ce l i br a r y
f or f i n a n c i a l quant i t at i ve anal ys t s and de ve l ope r s http : // quant l i b . org /
8
QuantLib i s f r e e s of t war e : you can r e di s t r i but e i t and/ or modi fy i t
10 under the terms of the QuantLib l i c e n s e . You shoul d have r e c e i ve d a
copy of the l i c e n s e al ong wi th t hi s program ; i f not , pl e as e emai l
12 <quant l i bde v@l i s t s . s f . net >. The l i c e n s e i s al s o a va i l a bl e onl i ne at
<http : // quant l i b . org / l i c e n s e . shtml >.
14
Thi s program i s di s t r i but e d i n the hope that i t wi l l be us e f ul , but WITHOUT
16 ANY WARRANTY; wi thout even the i mpl i ed warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the l i c e n s e f o r more d e t a i l s .
18 /
20
#i nc l ude mortgage . hpp
22 #i nc l ude <ql / i n t e r e s t r a t e . hpp>
#i nc l ude <ql /ti me / daycounter . hpp>
24 #i nc l ude <ql /ti me / dat e ge ne r at i onr ul e . hpp>
#i nc l ude <boost / as s i gn / st d/ vect or . hpp>
26
us i ng namespace boost : : as s i gn ;
28
namespace QuantLib {
30
Mortgage : : Mortgage ( cons t Date& i ssueDate , cons t Cal endar& cal endar , cons t
Real& amount , cons t I nt e r e s t Rat e& rate , cons t Rate& f ee , cons t I nt e ge r& term ,
cons t I nt e ge r& yearPayments ) : i s s ue Dat e ( i s s ueDat e ) , c al e ndar ( cal endar ) ,
amount ( amount ) , r a t e ( r at e ) , f e e ( f e e ) , term ( term) , yearPayments (
yearPayments ) {}
32
Mortgage : : Mortgage ( ) {}
34
//@{
36 / I ns pe c t or /
Date Mortgage : : i s s ueDat e ( ) { r et ur n i s s ue Dat e ; }
38 Real Mortgage : : amount ( ) { r et ur n amount ; }
I nt e r e s t Rat e Mortgage : : r at e ( ) { r et ur n r a t e ; }
40 Rate Mortgage : : f e e ( ) { r et ur n f e e ; }
42 / Se t t e r s /
voi d Mortgage : : s e t i s s ue Dat e ( cons t Date& new Date ) { i s s ue Dat e = new Date ; }
44 voi d Mortgage : : set amount ( cons t Real& new amount ) {amount = new amount ; }
voi d Mortgage : : s e t r a t e ( cons t I nt e r e s t Rat e& new rate ) { r a t e = new rate ; }
46 voi d Mortgage : : s e t f e e ( cons t Rate& new f ee ) { f e e = new f ee ; }
24
/ Pure Vi r t ual /
2 Leg Mortgage : : Begi ni ngBal ance ( ) cons t
{
4 Real denomi nator = pow( compoundFactor , numberOfPayments ) 1;
Leg begi ni ngbal ance ;
6
f o r ( i nt i = 0; i < numberOfPayments ; i ++)
8 {
Date next = i s s ue Dat e + Months ( ( i +1) 12 / yearPayments ) ;
10 boost : : s har ed pt r <CashFlow> BMB( new SimpleCashFlow( amount (pow(
compoundFactor , numberOfPayments ) pow( compoundFactor , i ) ) / denomi nator ,
next ) ) ;
begi ni ngbal ance += BMB;
12 }
14 r et ur n begi ni ngbal ance ;
}
16
Leg Mortgage : : Payment ( ) cons t
18 {
Real numerator = amount ( ( compoundFactor 1) pow( compoundFactor ,
numberOfPayments ) ) ;
20 Real denomi nator = pow( compoundFactor , numberOfPayments ) 1;
Real M = numerator / denomi nator ;
22 Leg payment ;
24 f o r ( i nt i = 0; i < numberOfPayments ; i ++)
{
26 Date next = i s s ue Dat e + Months ( ( i +1) 12 / yearPayments ) ;
boost : : s har ed pt r <CashFlow> MP( new SimpleCashFlow(M, next ) ) ;
28 payment += MP;
}
30
r et ur n payment ;
32 }
34 Leg Mortgage : : Fee ( ) cons t
{
36 Leg begi ni ngbal ance = Begi ni ngBal ance ( ) ;
Leg f e e ;
38 f o r ( i nt i = 0; i < numberOfPayments ; i ++)
{
40 Date next = i s s ue Dat e + Months ( ( i +1) 12 / yearPayments ) ;
boost : : s har ed pt r <CashFlow> F( new SimpleCashFlow( begi ni ngbal ance [ i
]>amount ( ) ( f e e / yearPayments ) , next ) ) ;
42 f e e += F;
}
44 r et ur n f e e ;
}
25
1 Leg Mortgage : : Ne t I nt e r e s t ( ) cons t
{
3 Real denomi nator = pow( compoundFactor , numberOfPayments ) 1;
Leg f e e s = Fee ( ) ;
5 Leg ne t i nt e r e s t ;
f o r ( i nt i = 0; i < numberOfPayments ; i ++)
7 {
Date next = i s s ue Dat e + Months ( ( i +1) 12 / yearPayments ) ;
9 boost : : s har ed pt r <CashFlow> I ( new SimpleCashFlow( amount ( ( (
compoundFactor 1) (pow( compoundFactor , numberOfPayments ) pow(
compoundFactor , i ) ) ) / denomi nator ) f e e s [ i ]>amount ( ) , next ) ) ;
ne t i nt e r e s t += I ;
11 }
13 r et ur n ne t i nt e r e s t ;
}
15
Leg Mortgage : : Pri nci pal Repayment ( ) cons t
17 {
Real denomi nator = pow( compoundFactor , numberOfPayments ) 1;
19 Leg pr i nci pal r epayment ;
f o r ( i nt i = 0; i < numberOfPayments ; i ++)
21 {
Date next = i s s ue Dat e + Months ( ( i +1) 12 / yearPayments ) ;
23 boost : : s har ed pt r <CashFlow> PR( new SimpleCashFlow( amount ( (
compoundFactor 1) pow( compoundFactor , i ) ) / denomi nator , next ) ) ;
pr i nci pal r epayment += PR;
25 }
27 r et ur n pr i nci pal r epayment ;
}
29
Leg Mortgage : : Endi ngBal ance ( ) cons t
31 {
Leg begi ni ngbal ance = Begi ni ngBal ance ( ) ;
33 Leg pr i nci pal r epayment = Pri nci pal Repayment ( ) ;
Leg endi ngbal ance ;
35 f o r ( i nt i = 0; i < numberOfPayments ; i ++)
{
37 Date next = i s s ue Dat e + Months ( ( i +1) 12 / yearPayments ) ;
boost : : s har ed pt r <CashFlow> EMB( new SimpleCashFlow( begi ni ngbal ance [ i
]>amount ( ) pr i nci pal r epayment [ i ]>amount ( ) , next ) ) ;
39
endi ngbal ance += EMB;
41 }
43 r et ur n endi ngbal ance ;
}
26
Date Mortgage : : date ( ) cons t { r et ur n i s s ue Dat e ; }
2
/ Vi s i t o r /
4 voi d Mortgage : : accept ( QuantLib : : Ac yc l i c Vi s i t or &v)
{
6 Vi s i t or <Mortgage> v1 = dynami c cast<Vi s i t or <Mortgage> >(&v) ;
i f ( v1! =0)
8 v1>v i s i t ( t hi s ) ;
e l s e
10 Loan : : accept ( 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 /
Fi xed Income Mathematics Four Edi t i on
3 pag ( 409)
CashFlow of mortgage wi th s e r v i c i ng f e e .
5 /
#i nc l ude <i ostream>
7 #i nc l ude <ql / quant l i b . hpp>
#i nc l ude <vector >
9 #i nc l ude <boost / as s i gn / st d/ vect or . hpp>
#i nc l ude <boost /f ormat . hpp>
11 #i nc l ude mortgage . hpp
13 us i ng namespace QuantLib ;
us i ng namespace std ;
15 us i ng namespace boost : : as s i gn ;
17 boost : : f ormat FORMATER ( %20s %12s %12s %15s %12s %12s %12s ) ;
19 i nt main ( )
{
21 t r y {
Rate r f = 0. 09 5;
23 DayCounter DC= Actual Actual ( Actual Actual : : ISDA) ;
Cal endar cal endar = Nul l Cal endar ( ) ;
25 Compounding compounded = Si mpl e ;
Frequency f r equency = Annual ;
27
I nt e r e s t Rat e r f Rat e ( r f , DC, compounded , f r equency ) ;
29 Date i s s ue ( 1 , Jan , 2000) ;
d = cal endar . adj us t ( d) ;
27
I nt e ge r YearPayments = 12;
2 Rate f e e = 0. 005 ;
Mortgage M( d , cal endar , 100000 , r f Rat e , f ee , 30 , YearPayments ) ;
4
cout << FORMATER % Date % Beg . Bal ance % Payment % Net . I nt e r e s t %
Fees % Repayment % End. Bal ance << endl ;
6 f o r ( i nt i= 0; i <12; i ++)
cout << FORMATER %M. Fee ( ) [ i ]>date ( ) %M. Begi ni ngBal ance ( ) [ i ]>
amount ( ) %M. Payment ( ) [ i ]>amount ( ) %M. Ne t I nt e r e s t ( ) [ i ]>amount ( ) %M. Fee ( ) [
i ]>amount ( ) %M. Pri nci pal Repayment ( ) [ i ]>amount ( ) %M. Endi ngBal ance ( ) [ i ]>
amount ( )<< endl ;
8
Date d1 ( 1 , Jan , 1999) ;
10 cout << M. hasOccurred ( d1 ) ;
}
12
catch ( st d : : except i on& e ) {
14 c e r r << e . what ( ) << endl ;
}
16 }
7. MBS (page 437 - 447)
In the previous section, no prepayment model was considered. Now, lets assume the PSA
prepayment convention to generate mortgage cash ows. 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++; tabwi dth : 4; i ndent tabsmode : n i l ; cbas i c o f f s e t : 4 /
2
/
4 Copyri ght (C) 2014 Mauri ci o Bedoya
6 Thi s f i l e i s part of QuantLib , a f r e e s of t war e /opens our ce l i br a r y
f or f i n a n c i a l quant i t at i ve anal ys t s and de ve l ope r s http : // quant l i b . org /
8
QuantLib i s f r e e s of t war e : you can r e di s t r i but e i t and/ or modi fy i t
10 under the terms of the QuantLib l i c e n s e . You shoul d have r e c e i ve d a
copy of the l i c e n s e al ong wi th t hi s program ; i f not , pl e as e emai l
12 <quant l i bde v@l i s t s . s f . net >. The l i c e n s e i s al s o a va i l a bl e onl i ne at
<http : // quant l i b . org / l i c e n s e . shtml >.
14
Thi s program i s di s t r i but e d i n the hope that i t wi l l be us e f ul , but WITHOUT
16 ANY WARRANTY; wi thout even the i mpl i ed warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the l i c e n s e f o r more d e t a i l s .
18 /
20 #i f nde f quantl i b f i xedratemortgagePSA hpp
#de f i ne quantl i b f i xedratemortgagePSA hpp
22
#i nc l ude f i xedr at emor t gage . hpp
24 #i nc l ude <ql / i n t e r e s t r a t e . hpp>
#i nc l ude <ql /ti me / cal endar . hpp>
26 #i nc l ude <ql /ti me / daycounter . hpp>
#i nc l ude <ql /ti me / dat e ge ne r at i onr ul e . hpp>
28 #i nc l ude <ql / cas hf l ows / s i mpl ecas hf l ow . hpp>
30 namespace QuantLib {
c l a s s FixedRateMortgagePSA : publ i c Fi xedRateMortgage {
32 publ i c :
FixedRateMortgagePSA( cons t Date& i ssueDate , cons t Cal endar& cal endar ,
cons t Real& amount , cons t I nt e r e s t Rat e& rate , cons t Rate& f ee , cons t I nt e ge r&
term , cons t I nt e ge r& yearPayments , cons t Real& PSA) ;
34 v i r t ua l FixedRateMortgagePSA( ) ;
//@
36 / I ns pe c t or /
Real amount ( ) ;
38 I nt e r e s t Rat e r at e ( ) ;
Rate f e e ( ) ;
40
/ Se t t e r s /
42 voi d s e t i s s ue Dat e ( cons t Date& new Date ) ;
voi d set amount ( cons t Real& new amount ) ;
44 voi d s e t r a t e ( cons t I nt e r e s t Rat e& new rate ) ;
voi d s e t f e e ( cons t Rate& new f ee ) ;
29
1 / Pure v i r t ua l /
Leg Begi ni ngBal ance ( ) cons t ;
3 Leg Payment ( ) cons t ;
Leg Ne t I nt e r e s t ( ) cons t ;
5 Leg Fee ( ) cons t ;
Leg Pri nci pal Repayment ( ) cons t ;
7 Leg Endi ngBal ance ( ) cons t ;
Date date ( ) cons t ; // from Event
9
/ Vi s i t o r /
11 v i r t ua l voi d accept ( Ac yc l i c Vi s i t or& v) ;
13 / Others /
Leg SMM( ) cons t ;
15 Leg b( ) cons t ;
Leg I nt e r e s t ( ) cons t ;
17 Leg ProyectedPri nci pal Payment ( ) cons t ;
Leg CashFl owToInvestors ( ) cons t ;
19
//@
21 pr i vat e :
Date i s s ue Dat e ;
23 Cal endar c al e ndar ;
Real amount , PSA ;
25 I nt e ge r term , yearPayments ;
Compounding compound ;
27 I nt e r e s t Rat e r a t e ;
Rate f e e ;
29 Leg Begi ni ngBal ance , Payment , Ne t I nt e r e s t , Fee , Pri nci pal Repayment ,
I nt e r e s t , ProyectedPri nci pal Payment , Endi ngBal ance , CashFl owToInvestors ;
31 / Hel per s /
Time t = ( 1. 0/ yearPayments ) ;
33 I nt e ge r numberOfPayments = ( term yearPayments ) ;
Real compoundFactor = r a t e . compoundFactor ( t ) ;
35 voi d GenerateCashFl ows ( ) ;
};
37 }
39 #e ndi f / def i ne d ( hh f i xedr at emor t gagePSA ) /
30
1 / mode : c++; tabwi dth : 4; i ndent tabsmode : n i l ; cbas i c o f f s e t : 4 /
3 /
Copyri ght (C) 2014 Mauri ci o Bedoya
5
Thi s f i l e i s part of QuantLib , a f r e e s of t war e /opens our ce l i br a r y
7 f or f i n a n c i a l quant i t at i ve anal ys t s and de ve l ope r s http : // quant l i b . org /
9 QuantLib i s f r e e s of t war e : you can r e di s t r i but e i t and/ or modi fy i t
under the terms of the QuantLib l i c e n s e . You shoul d have r e c e i ve d a
11 copy of the l i c e n s e al ong wi th t hi s program ; i f not , pl e as e emai l
<quant l i bde v@l i s t s . s f . net >. The l i c e n s e i s al s o a va i l a bl e onl i ne at
13 <http : // quant l i b . org / l i c e n s e . shtml >.
15 Thi s program i s di s t r i but e d i n the hope that i t wi l l be us e f ul , but WITHOUT
ANY WARRANTY; wi thout even the i mpl i ed warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the l i c e n s e f o r more d e t a i l s .
/
19
#i nc l ude f i xedratemortgagePSA . hpp
21 #i nc l ude <ql / i n t e r e s t r a t e . hpp>
#i nc l ude <ql /ti me / daycounter . hpp>
23 #i nc l ude <ql /ti me / dat e ge ne r at i onr ul e . hpp>
#i nc l ude <boost / as s i gn / st d/ vect or . hpp>
25
us i ng namespace boost : : as s i gn ;
27
namespace QuantLib {
29 FixedRateMortgagePSA : : FixedRateMortgagePSA( cons t Date& i ssueDate , cons t
Cal endar& cal endar , cons t Real& amount , cons t I nt e r e s t Rat e& rate , cons t Rate&
f ee , cons t I nt e ge r& term , cons t I nt e ge r& yearPayments , cons t Real& PSA) :
Fi xedRateMortgage ( i ssueDate , cal endar , amount , rate , f ee , term , yearPayments )
, i s s ue Dat e ( i s s ueDat e ) , c al e ndar ( cal endar ) , amount ( amount ) , r a t e ( r at e ) ,
f e e ( f e e ) , term ( term) , yearPayments ( yearPayments ) , PSA (PSA) {
GenerateCashFl ows ( ) ; }
FixedRateMortgagePSA : : FixedRateMortgagePSA( ) {}
31 //@
/ I ns pe c t or /
33 Real FixedRateMortgagePSA : : amount ( ) { r et ur n amount ; }
I nt e r e s t Rat e FixedRateMortgagePSA : : r at e ( ) { r et ur n r a t e ; }
35 Rate FixedRateMortgagePSA : : f e e ( ) { r et ur n f e e ; }
37 / Se t t e r s /
voi d FixedRateMortgagePSA : : s e t i s s ue Dat e ( cons t Date& new Date ) { i s s ue Dat e =
new Date ; }
39 voi d FixedRateMortgagePSA : : set amount ( cons t Real& new amount ) {amount =
new amount ; }
voi d FixedRateMortgagePSA : : s e t r a t e ( cons t I nt e r e s t Rat e& new rate ) { r a t e =
new rate ; }
41 voi d FixedRateMortgagePSA : : s e t f e e ( cons t Rate& new f ee ) { f e e = new f ee ; }
31
1 voi d FixedRateMortgagePSA : : GenerateCashFl ows ( )
{
3 Leg smm = SMM( ) ;
Leg bt = b( ) ;
5 Real PMT = Fi xedRateMortgage : : Payment ( ) [0] >amount ( ) ;
7 f o r ( i nt i = 0; i < numberOfPayments ; i ++)
{
9 Date next = i s s ue Dat e + ( ( 1 + i ) 12 / yearPayments ) Months ;
i f ( i <1)
11 {
boost : : s har ed pt r <CashFlow> MB( new SimpleCashFlow( amount , next ) )
;
13 Begi ni ngBal ance += MB;
}
15 e l s e
{
17 boost : : s har ed pt r <CashFlow> MB( new SimpleCashFlow( Endi ngBal ance
( ) [ i 1]>amount ( ) , next ) ) ;
Begi ni ngBal ance += MB;
19 }
21 boost : : s har ed pt r <CashFlow> MP( new SimpleCashFlow( bt [ i ]>amount ( )
PMT, next ) ) ;
Payment += MP;
23
boost : : s har ed pt r <CashFlow> I ( new SimpleCashFlow( Begi ni ngBal ance [ i
]>amount ( ) ( compoundFactor 1) , next ) ) ;
25 I n t e r e s t += I ;
27 boost : : s har ed pt r <CashFlow> F( new SimpleCashFlow( Begi ni ngBal ance [ i
]>amount ( ) ( f e e / yearPayments ) , next ) ) ;
Fee += F;
29
boost : : s har ed pt r <CashFlow> NI ( new SimpleCashFlow( I n t e r e s t [ i ]>
amount ( ) Fee [ i ]>amount ( ) , next ) ) ;
31 Ne t I nt e r e s t += NI ;
33 boost : : s har ed pt r <CashFlow> SP( new SimpleCashFlow( bt [ i ]>amount ( )
Fi xedRateMortgage : : Pri nci pal Repayment ( ) [ i ]>amount ( ) , next ) ) ;
ProyectedPri nci pal Payment += SP;
35
boost : : s har ed pt r <CashFlow> PR( new SimpleCashFlow(smm[ i ]>amount ( )
( Begi ni ngBal ance [ i ]>amount ( ) ProyectedPri nci pal Payment [ i ]>amount ( ) ) ,
next ) ) ;
37 Pri nci pal Repayment += PR;
32
1 boost : : s har ed pt r <CashFlow> MBt( new SimpleCashFlow( Begi ni ngBal ance [
i ]>amount ( ) ProyectedPri nci pal Payment [ i ]>amount ( ) Pri nci pal Repayment [
i ]>amount ( ) , next ) ) ;
Endi ngBal ance += MBt;
3
boost : : s har ed pt r <CashFlow> CF( new SimpleCashFlow( Ne t I nt e r e s t [ i ]>
amount ( ) + ProyectedPri nci pal Payment [ i ]>amount ( ) + Pri nci pal Repayment [ i ]>
amount ( ) , next ) ) ;
5 CashFl owToInvestors += CF;
7 }
9 }
/ Pure v i r t ua l /
11 Leg FixedRateMortgagePSA : : Begi ni ngBal ance ( ) cons t { r et ur n Begi ni ngBal ance ; }
13 Leg FixedRateMortgagePSA : : Payment ( ) cons t { r et ur n Payment ; }
15 Leg FixedRateMortgagePSA : : Ne t I nt e r e s t ( ) cons t { r et ur n Ne t I nt e r e s t ; }
17 Leg FixedRateMortgagePSA : : Fee ( ) cons t { r et ur n Fee ; }
19 Leg FixedRateMortgagePSA : : Pri nci pal Repayment ( ) cons t { r et ur n
Pri nci pal Repayment ; }
21 Leg FixedRateMortgagePSA : : Endi ngBal ance ( ) cons t { r et ur n Endi ngBal ance ; }
23 Date FixedRateMortgagePSA : : date ( ) cons t { r et ur n i s s ue Dat e ; }
25 / Vi s i t o r /
voi d FixedRateMortgagePSA : : accept ( Ac yc l i c Vi s i t or& v)
27 {
Vi s i t or <FixedRateMortgagePSA> v1 = dynami c cast<Vi s i t or <
FixedRateMortgagePSA> >(&v) ;
29 i f ( v1! =0)
v1>v i s i t ( t hi s ) ;
31 e l s e
Fi xedRateMortgage : : accept ( v) ;
33 }
33
1 / Others /
Leg FixedRateMortgagePSA : :SMM( ) cons t
3 {
Leg smm;
5 Real CRP;
i nt N = 30 yearPayments / 12;
7
f o r ( i nt i = 0; i < numberOfPayments ; i ++)
9 {
i f ( i < N)
11 CRP = ( 0. 06 ( i + 1) / N) PSA ;
e l s e
13 CRP = ( 0. 06) PSA ;
Date next = i s s ue Dat e + ( ( 1 + i ) 12 / yearPayments ) ;
15 boost : : s har ed pt r <CashFlow> S( new SimpleCashFlow(1 pow( ( 1 CRP)
, ( 1. 0/ yearPayments ) ) , next ) ) ;
smm += S ;
17 }
19 r et ur n smm;
}
21
Leg FixedRateMortgagePSA : : b( ) cons t
23 {
Leg b ;
25 Leg smm = SMM( ) ;
Real data = 1;
27 Date next = i s s ue Dat e + (1 12 / yearPayments ) ;
boost : : s har ed pt r <CashFlow> bt ( new SimpleCashFlow( data , next ) ) ;
29 b += bt ;
f o r ( i nt i = 1; i < numberOfPayments ; i ++)
31 {
next = i s s ue Dat e + ( ( 1 + i ) 12 / yearPayments ) ;
33 data =(1SMM( ) [ i 1]>amount ( ) ) ;
boost : : s har ed pt r <CashFlow> bt ( new SimpleCashFlow( data , next ) ) ;
35 b += bt ;
}
37
r et ur n b ;
39 }
34
1 Leg FixedRateMortgagePSA : : I nt e r e s t ( ) cons t
{
3 Leg begi ni ngbal ance = Begi ni ngBal ance ( ) ;
Leg i n t e r e s t ;
5 f o r ( i nt i = 0; i < numberOfPayments ; i ++)
{
7 Date next = i s s ue Dat e + ( ( 1 + i ) 12 / yearPayments ) Months ;
boost : : s har ed pt r <CashFlow> I ( new SimpleCashFlow( begi ni ngbal ance [ i
]>amount ( ) ( compoundFactor 1) , next ) ) ;
9 i n t e r e s t += I ;
}
11
r et ur n i n t e r e s t ;
13 }
15 Leg FixedRateMortgagePSA : : ProyectedPri nci pal Payment ( ) cons t { r et ur n
ProyectedPri nci pal Payment ; }
17 Leg FixedRateMortgagePSA : : CashFl owToInvestors ( ) cons t { r et ur n
CashFl owToInvestors ; }
19 //@
}
To generate Exhibit 22.8, 22.9 and 22.10 (book), just change the PSA value. The implementa-
tion in main is:
/
2 Fi xed Income Mathematics Four Edi t i on
pag ( 409)
4 CashFlow of mortgage wi th s e r v i c i ng f e e .
/
6 #i nc l ude <i ostream>
#i nc l ude <ql / quant l i b . hpp>
8 #i nc l ude <vector >
#i nc l ude <boost / as s i gn / st d/ vect or . hpp>
10 #i nc l ude <boost /f ormat . hpp>
#i nc l ude f i xedr at emor t gage . hpp
12 #i nc l ude f i xedratemortgagePSA . hpp
14 us i ng namespace QuantLib ;
us i ng namespace std ;
16 us i ng namespace boost : : as s i gn ;
18 boost : : f ormat FORMATER1 ( %20s %12s %12s %12s %12s %12s %12s %12s
%12s %12s ) ;
35
i nt main ( )
2 {
t r y {
4 Rate r f = 0. 09 5;
DayCounter DC= Actual Actual ( Actual Actual : : ISDA) ;
6 Cal endar cal endar = Nul l Cal endar ( ) ;
Compounding compounded = Si mpl e ;
8 Frequency f r equency = Annual ;
10 I nt e r e s t Rat e r f Rat e ( r f , DC, compounded , f r equency ) ;
Date i s s ue ( 1 , Jan , 2000) ;
12 i s s ue = cal endar . adj us t ( i s s ue ) ;
I nt e ge r YearPayments = 12;
14 Rate f e e = 0. 005 ;
Real PSA = 1 . 0 ;
16 cout << endl ;
18 FixedRateMortgagePSA M( i s s ue , cal endar , 100000 , r f Rat e , f ee , 30 ,
YearPayments , PSA) ;
cout << FORMATER1 % Date % BMB % SMM % MP % SP % I % PR %
S % CF %EMB << endl ;
20 f o r ( i nt i= 0; i <12; i ++) {
cout << FORMATER1 %M. Begi ni ngBal ance ( ) [ i ]>date ( ) %M.
Begi ni ngBal ance ( ) [ i ]>amount ( )
22 %M.SMM( ) [ i ]>amount ( ) %M. Payment ( ) [ i ]>amount ( ) %
M. ProyectedPri nci pal Payment ( ) [ i ]>amount ( )
%M. I nt e r e s t ( ) [ i ]>amount ( ) %M. Pri nci pal Repayment
( ) [ i ]>amount ( ) %M. Fee ( ) [ i ]>amount ( )
24 %M. CashFl owToInvestors ( ) [ i ]>amount ( ) %M.
Endi ngBal ance ( ) [ i ]>amount ( ) << endl ;
}
26
vector <Real> PSA1;
28 PSA1 += 1. 0 , 1. 5 , 0 . 5 ;
30 // Vector Ana l i s i s
FixedRateMortgagePSA M1( i s s ue , cal endar , 100000 , r f Rat e , f ee , 30 ,
YearPayments , PSA1 [ 0 ] ) ;
32 FixedRateMortgagePSA M2( i s s ue , cal endar , 100000 , r f Rat e , f ee , 30 ,
YearPayments , PSA1 [ 1 ] ) ;
FixedRateMortgagePSA M3( i s s ue , cal endar , 100000 , r f Rat e , f ee , 30 ,
YearPayments , PSA1 [ 2 ] ) ;
34 }
36 catch ( st d : : except i on& e ) {
c e r r << e . what ( ) << endl ;
38 }
}
At the end you will nd three dierent FixedRateMortgagePSA object, each one with a die-
rent PSA value. This is the introduction code to implement vector analysis (page 446 in book).
Implement the code that generate the corresponding cash ows.
36

Das könnte Ihnen auch gefallen