Bond Examples Fabozzi

download Bond Examples Fabozzi

of 36

Transcript of Bond Examples Fabozzi

  • Fixed Income Mathematics in QuantlibPart I

    Mauricio Bedoya

    [email protected]

    February 2014

    In the previous blog, we implement some code to price option free bonds. However, due tothe different template parameters and available constructors, most of the people get lost.In this blog, Im going to implement some examples that you can find in: Fixed IncomeMathematics (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 yearsCoupon: 9 % (Simuannual)Redemption:1000Requiered Yield: 12 %CleanPrice: 774.3

    Bond2:Maturity: 20 yearsCoupon: 9 % (Simuannual)Redemption:1000Requiered Yield: 7 %CleanPrice: 1213.55

    Both bonds will be implemented with FixedRateBond class. The second one, will use Relinka-bleHandle class.

    1Code implemente in Xcode 4.5.2, with QuantLib version 1.4 and boost version 1.52.0.

    1

  • 1 / Fixed Income Mathematics Four Edit ion

    3 pag (74 , 75)Time path o f the Pr i ce o f a Discount Bond :

    5 20Years , 9% coupon , 12% Required Yie ld /

    7

    #inc lude 9 #inc lude #inc lude

    11

    us ing namespace QuantLib ;13 us ing namespace std ;

    15 i n t main ( ){

    17 t ry {/ Bond Informat ion /

    19 Calendar ca l endar = NullCalendar ( ) ;DayCounter DC = ActualActual ( ActualActual : : Bond) ;

    21 Natural SettlementDays = 3 ;Date Today = Date : : todaysDate ( ) ;

    23 Se t t i n g s : : i n s t ance ( ) . eva luat ionDate ( ) = Today ;Date I s su e = Today ;

    25 Date Maturity = I s su e+ 20Years ;Rate Coupon = 0 . 0 9 ;

    27 In t e r e s tRate CouponRate (Coupon , ActualActual ( ActualActual : : Bond) ,Compounded , Semiannual ) ;

    Real FaceValue = 1000 . 0 ;29 vector Coupons (40 , CouponRate ) ;

    31 Rel inkableHandleFlatRate ;Rel inkableHandleBondInstrument ;

    33

    / Bond Schedule /35 Schedule sch ( I s sue , Maturity , Period ( Semiannual ) , ca lendar , Unadjusted ,

    Unadjusted , DateGeneration : : Backward , f a l s e ) ;

    37 / Bond Constructor /boost : : shared ptr Bond1(new FixedRateBond ( SettlementDays ,

    FaceValue , sch , Coupons ) ) ;39 BondInstrument . l inkTo (Bond1) ;

    Rate ra t e = 0 . 1 2 ;41 Handle Rate1 (new SimpleQuote ( ra t e ) ) ;

    2

  • 1 / TermStructure Constructor /boost : : shared ptr TS(new FlatForward ( I s sue , Rate1 ,

    ActualActual ( ActualActual : : Bond) ,Compounded , Semiannual ) ) ;3 FlatRate . l inkTo (TS) ;

    boost : : shared ptr BondEngine (new DiscountingBondEngine (FlatRate ) ) ;

    5 BondInstrument>s e tPr i c ingEng ine (BondEngine ) ;cout

  • / 2 Fixed Income Mathematics Four Edit ion

    pag (215)4 Bond Informat ion :

    5Years , 10% coupon , 10% Required Yie ld6 /#inc lude

    8 #inc lude #inc lude

    10

    us ing namespace QuantLib ;12 us ing namespace std ;

    14 i n t main ( ){

    16 t ry {/ Bond Informat ion /

    18 Calendar ca l endar = NullCalendar ( ) ;DayCounter DC = ActualActual ( ActualActual : : Bond) ;

    20 Natural SettlementDays = 3 ;Date Today = Date : : todaysDate ( ) ;

    22 Se t t i n g s : : i n s t ance ( ) . eva luat ionDate ( ) = Today ;Date I s su e = Today ;

    24 Date Maturity = I s su e+ 5Years ;Rate Coupon = 0 . 1 0 ;

    26 Compounding compounding = Compounded ;Frequency f requency = Semiannual ;

    28 In t e r e s tRate CouponRate (Coupon , ActualActual ( ActualActual : : Bond) ,compounding , f requency ) ;

    Real FaceValue = 100 . 0 ;30 std : : vector Coupons (10 , CouponRate ) ;

    32 Rel inkableHandleFlatRate ;Rel inkableHandleBondInstrument ;

    34

    / Bond Schedule /36 Schedule sch ( I s sue , Maturity , Period ( Semiannual ) , ca lendar , Unadjusted ,

    Unadjusted , DateGeneration : : Backward , f a l s e ) ;

    38 / Bond Constructor /boost : : shared ptr Bond1(new FixedRateBond ( SettlementDays ,

    FaceValue , sch , Coupons ) ) ;40 BondInstrument . l inkTo (Bond1) ;

    Rate ra t e = 0 . 1 0 ;42 Handle Rate1 (new SimpleQuote ( ra t e ) ) ;

    4

  • / TermStructure Constructor /2 boost : : shared ptr TS(new FlatForward ( I s sue , Rate1 ,

    ActualActual ( ActualActual : : Bond) , compounding , f requency ) ) ;FlatRate . l inkTo (TS) ;

    4 boost : : shared ptr BondEngine (new DiscountingBondEngine (FlatRate ) ) ;

    BondInstrument>s e tPr i c ingEng ine (BondEngine ) ;6

    / Print /8 cout

  • 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 (bondprice volatility).

    1 / Fixed Income Mathematics Four Edit ion

    3 pag (219 I l u s t r a t i o n 133)Link between durat ion and pr ive v o l a t i l i t y

    5 Bond Informat ion :15Years , 8% coupon , 10% Required Yie ld

    7 /#inc lude

    9 #inc lude #inc lude

    11

    us ing namespace QuantLib ;13 us ing namespace std ;

    15 i n t main ( ){

    17 t ry {/ Bond Informat ion /Calendar ca l endar = NullCalendar ( ) ;

    19 DayCounter DC = ActualActual ( ActualActual : : Bond) ;Natural SettlementDays = 3 ;

    21 Date Today = Date : : todaysDate ( ) ;S e t t i n g s : : i n s t ance ( ) . eva luat ionDate ( ) = Today ;

    23 Date I s su e = Today ;Date Maturity = I s su e+ 15Years ;

    25 Rate Coupon = 0 . 0 8 ;Compounding compounding = Compounded ;

    27 Frequency f requency = Semiannual ;In t e r e s tRate CouponRate (Coupon , ActualActual ( ActualActual : : Bond) ,

    compounding , f requency ) ;29 Real FaceValue = 100 . 0 ;

    std : : vector Coupons (30 , CouponRate ) ;31

    Rel inkableHandleFlatRate ;33 Rel inkableHandleBondInstrument ;

    35 / Bond Schedule /Schedule sch ( I s sue , Maturity , Period ( Semiannual ) , ca lendar , Unadjusted ,

    Unadjusted , DateGeneration : : Backward , f a l s e ) ;37

    / Bond Constructor /39 boost : : shared ptr Bond1(new FixedRateBond ( SettlementDays ,

    FaceValue , sch , Coupons ) ) ;BondInstrument . l inkTo (Bond1) ;

    41 Rate ra t e = 0 . 1 0 ;Handle Rate1 (new SimpleQuote ( ra t e ) ) ;

    6

  • / TermStructure Constructor /2 boost : : shared ptr TS(new FlatForward ( I s sue , Rate1 ,

    ActualActual ( ActualActual : : Bond) , compounding , f requency ) ) ;FlatRate . l inkTo (TS) ;

    4 boost : : shared ptr BondEngine (new DiscountingBondEngine (FlatRate ) ) ;

    BondInstrument>s e tPr i c ingEng ine (BondEngine ) ;6

    / Print I n i t i a l Result /8 Real BondPrice 0 = BondInstrument>NPV() ;

    Real Modif iedDurat ion = BondFunctions : : durat ion (BondInstrument ,In t e r e s tRate ( rate ,DC, compounding , f requency ) ) ;

    10 cout

  • 1 / Print Pr i ce a f t e r spread ( Large ) /

    3 Approx BondPrice 1 = (1 ( Modif iedDurat ion l a r g e sp r ead ) ) BondPrice 0 ;

    Real BondPrice LargeSpread = BondInstrument>NPV() ;5

    cout

  • 1 void l i n e ( ){

    3 f o r ( i n t i = 1 ; i < 50 ; i++)cout

  • / Print I n i t i a l Result /2 Real BondPrice 0 = BondInstrument>NPV() ;

    Real Modif iedDurat ion = BondFunctions : : durat ion (BondInstrument ,In t e r e s tRate ( rate ,DC, compounding , f requency ) ) ;

    4 Real Convexity = BondFunctions : : convex i ty (BondInstrument , In t e r e s tRate( rate , DC, compounding , f requency ) ) ;

    cout

  • cout
  • 1 i n t main ( ){

    3 t ry {/ Real data /

    5 const S i z e numberOfBonds (3 ) ;Real MarketPrice [ numberOfBonds ]={100 ,100 ,100} ;

    7

    / Quote data /9 vector Quotes ;

    11 / RelinkHanlde Quotes /Rel inkableHandle quoteHandles [ numberOfBonds ] ;

    13

    f o r ( S i z e i = 0 ; i != numberOfBonds ; i++)15 {

    boost : : shared ptr Q(new SimpleQuote ( MarketPrice [ i ] ) ) ;17 Quotes += Q;

    quoteHandles [ i ] . l inkTo (Quotes [ i ] ) ;19 }

    21

    / Bonds Informat ion /23 I n t eg e r l ength [ ] = {1 , 2 , 3} ;

    Real coupons [ ] = {0 .035 , 0 . 04 , 0 . 0 45} ;25 Real y i e l d s [ ] = {0 .035 , 0 . 04 , 0 . 0 45} ;

    Frequency f requency = Annual ;27 DayCounter dc = SimpleDayCounter ( ) ;

    BusinessDayConvention accrualConvent ion = Modi f i edFol lowing ;29 BusinessDayConvention Convention = Modi f i edFol lowing ;

    Real Redemption = 100 ;31

    Calendar ca l endar = NullCalendar ( ) ;33 Date today = ca lendar . ad jus t (Date : : todaysDate ( ) ) ;

    S e t t i n g s : : i n s t ance ( ) . eva luat ionDate ( ) = today ;35

    Natural bondSettlementDays = 0 ;37 Date BondSettlementDate = ca lendar . advance ( today , bondSettlementDaysDays

    ) ;

    39 / Bond1 Constructor ( t ry to do i t in a loop ) /Date MaturityBond1 = ca lendar . advance ( BondSettlementDate , l ength [ 0 ]

    Years ) ;41 / Schedule /

    Schedule SchBond1 ( BondSettlementDate , MaturityBond1 , Period ( f requency ) ,ca lendar , accrualConvention , accrualConvention , DateGeneration : : Backward ,f a l s e ) ;

    12

  • boost : : shared ptr B1(new FixedRateBondHelper ( quoteHandles[ 0 ] , bondSettlementDays , 1 0 0 . 0 , SchBond1 , vector(1 , coupons [ 0 ] ) , dc ,Convention , Redemption ) ) ;

    2

    4 / Bond2 Constructor /Date MaturityBond2 = ca lendar . advance ( BondSettlementDate , l ength [ 1 ]

    Years ) ;6 / Schedule /

    Schedule SchBond2 ( BondSettlementDate , MaturityBond2 , Period ( f requency ) ,ca lendar , accrualConvention , accrualConvention ,

    8 DateGeneration : : Backward , f a l s e ) ;boost : : shared ptr B2( newFixedRateBondHelper ( quoteHandles [ 1 ] ,

    bondSettlementDays , 1 0 0 . 0 , SchBond2 , vector(2 , coupons [ 1 ] ) , dc , Convention ,Redemption ) ) ;

    10

    / Bond3 Constructor /12 Date MaturityBond3 = ca lendar . advance ( BondSettlementDate , l ength [ 2 ]

    Years ) ;/ Schedule /

    14 Schedule SchBond3 ( BondSettlementDate , MaturityBond3 , Period ( f requency ) ,ca lendar , accrualConvention , accrualConvention , DateGeneration : : Backward ,f a l s e ) ;

    boost : : shared ptr B3(new FixedRateBondHelper ( quoteHandles[ 2 ] , bondSettlementDays , 1 0 0 . 0 , SchBond3 , vector(3 , coupons [ 2 ] ) , dc ,Convention , Redemption ) ) ;

    16 / The Curve Be con s i s t e n t . I f you use he lper s ,

    18 cont inue us ing them un t i l curve bu i l d i ng . /

    20 DayCounter termStructureDayCounter = ActualActual ( ActualActual : : ISDA) ;Real t o l e r an c e = 1 .0 e12;

    22 vector BondPrices ;BondPrices += B1 ,B2 ,B3 ;

    24

    boost : : shared ptr TS(new PiecewiseYie ldCurve(BondSettlementDate , BondPrices , termStructureDayCounter ,t o l e r an c e ) ) ;

    26 / Get Spot Rates ( page 324 in book ) /Rate zero1Year = TS>zeroRate (MaturityBond1 , termStructureDayCounter ,

    Compounded) ;28 Rate zero2Year = TS>zeroRate (MaturityBond2 , termStructureDayCounter ,

    Compounded) ;Rate zero3Year = TS>zeroRate (MaturityBond3 , termStructureDayCounter ,

    Compounded) ;30

    / Print Spot /32 s t r i n g ru l e (20 , ) ;

    cout

  • cout
  • Normal Models and when = 1, we have LogNormal models.

    In QuantLib ShortRateModel class, you can identify that LogNormal models arent developedyet2. Thats way, I will implement: Black-Karasinski, and Hull-White.

    1 / Fixed Income Mathematics Four Edit ion

    3 pag (324 325)Estimate Ca l l Bond Pr i ce and

    5 Cal l opt ion value . /

    7 #inc lude #inc lude

    9 #inc lude #inc lude

    11 #inc lude #inc lude

    13

    us ing namespace QuantLib ;15 us ing namespace std ;

    us ing namespace boost : : a s s i gn ;17

    boost : : format FORMATER( %7s % 8.6d % 11.6d % 10.4d % 12.4d % 10 .4d ) ;19

    i n t main ( )21 {

    t ry {23 / Real data /

    const S i z e numberOfBonds (3 ) ;25 Real MarketPrice [ numberOfBonds ]={100 ,100 ,100} ;

    27 / Quote data /vector Quotes ;

    29

    / RelinkHanlde Quotes /31 Rel inkableHandle quoteHandles [ numberOfBonds ] ;

    33 f o r ( S i z e i = 0 ; i != numberOfBonds ; i++){

    35 boost : : shared ptr Q(new SimpleQuote ( MarketPrice [ i ] ) ) ;Quotes += Q;

    37 quoteHandles [ i ] . l inkTo (Quotes [ i ] ) ;}

    2However, VarianceGamma and other process are already implemented.

    15

  • / Bonds Informat ion /2 I n t eg e r l ength [ ] = {1 , 2 , 3} ;

    Real coupons [ ] = {0 .035 , 0 . 04 , 0 . 0 45} ;4 Real y i e l d s [ ] = {0 .035 , 0 . 04 , 0 . 0 45} ;

    Frequency f requency = Annual ;6 DayCounter dc = SimpleDayCounter ( ) ;

    BusinessDayConvention accrualConvent ion = Modi f i edFol lowing ;8 BusinessDayConvention Convention = Modi f i edFol lowing ;

    Real Redemption = 100 ;10

    Calendar ca l endar = NullCalendar ( ) ;12 Date today = ca lendar . ad jus t (Date : : todaysDate ( ) ) ;

    S e t t i n g s : : i n s t ance ( ) . eva luat ionDate ( ) = today ;14

    Natural bondSettlementDays = 0 ;16 Date BondSettlementDate = ca lendar . advance ( today , bondSettlementDaysDays

    ) ;

    18 / Bond1 Constructor ( t ry to do i t in a loop ) /Date MaturityBond1 = ca lendar . advance ( BondSettlementDate , l ength [ 0 ]

    Years ) ;20 / Schedule /

    Schedule SchBond1 ( BondSettlementDate , MaturityBond1 , Period ( f requency ) ,ca lendar , accrualConvention , accrualConvention , DateGeneration : : Backward ,f a l s e ) ;

    22

    boost : : shared ptr B1(new FixedRateBondHelper ( quoteHandles[ 0 ] , bondSettlementDays , 1 0 0 . 0 , SchBond1 ,

    24 vector(1 ,coupons [ 0 ] ) , dc , Convention , Redemption ) ) ;

    26 / Bond2 Constructor /Date MaturityBond2 = ca lendar . advance ( BondSettlementDate , l ength [ 1 ]

    Years ) ;28 / Schedule /

    Schedule SchBond2 ( BondSettlementDate , MaturityBond2 , Period ( f requency ) ,ca lendar , accrualConvention , accrualConvention ,

    30 DateGeneration : : Backward , f a l s e ) ;boost : : shared ptr B2(new FixedRateBondHelper ( quoteHandles

    [ 1 ] , bondSettlementDays , 1 0 0 . 0 , SchBond2 , vector(2 , coupons [ 1 ] ) , dc ,Convention , Redemption ) ) ;

    16

  • 1 / Bond3 Constructor /Date MaturityBond3 = ca lendar . advance ( BondSettlementDate , l ength [ 2 ]

    Years ) ;3 / Schedule /

    Schedule SchBond3 ( BondSettlementDate , MaturityBond3 , Period ( f requency ) ,ca lendar , accrualConvention , accrualConvention , DateGeneration : : Backward ,f a l s e ) ;

    5 boost : : shared ptr B3(new FixedRateBondHelper ( quoteHandles[ 2 ] , bondSettlementDays , 1 0 0 . 0 , SchBond3 , vector(3 , coupons [ 2 ] ) , dc ,Convention , Redemption ) ) ;

    / The Curve 7 Be con s i s t e n t . I f you use he lper s ,

    cont inue us ing them un t i l curve bu i l d i ng .9 /

    DayCounter termStructureDayCounter = ActualActual ( ActualActual : : ISDA) ;11 Real t o l e r an c e = 1 .0 e12;

    vector BondPrices ;13 BondPrices += B1 ,B2 ,B3 ;

    15 boost : : shared ptr TS(new PiecewiseYie ldCurve(BondSettlementDate , BondPrices , termStructureDayCounter ,t o l e r an c e ) ) ;

    17 Rel inkableHandle discountTermStructure ;discountTermStructure . l inkTo (TS) ;

    19

    / Valuing Ca l l ab l e Bond ( page 337 in book ) 21 Coupon : 5.25% Cal l p r i c e : 100

    Maturity : 3 years Ca l l Date : 1 year23 /

    25 / Step 1 : Set Bond Cha r a c t e r i s t i c s /Date i s s u e = today ;

    27 Date Maturity = ca lendar . advance ( today , 3 Years ) ;Natural SettlementDays = 0 ;

    29 Calendar bondCalendar = ca lendar ;Real Coupon = 0 . 0525 ;

    31 Frequency CouponFrequency = Annual ;BusinessDayConvention BondConvention = Convention ;

    33 BusinessDayConvention BondAccrueConvention = Convention ;DayCounter BondDC = dc ;

    35

    Schedule SchBond ( today , Maturity , Period (CouponFrequency ) , bondCalendar ,BondConvention , BondConvention , DateGeneration : : Backward , f a l s e ) ;

    37

    / Cal l Schedule /39 Ca l l a b i l i t y S ch edu l e Cal lSch ;

    Real Pr i ce = 100 . 0 ;41 Ca l l a b i l i t y : : Pr i ce Ca l lP r i c e ( Price , C a l l a b i l i t y : : Pr i ce : : Clean ) ; // Set

    Ca l l Pr i ce

    17

  • 1 Date CallDate = ca lendar . advance ( today , 1 Years ) ; // SetCa l l Date

    boost : : shared ptr c a l l a b i l i t y (new Ca l l a b i l i t y ( Ca l lPr i ce ,C a l l a b i l i t y : : Cal l , Cal lDate ) ) ;

    3 CallSch +=c a l l a b i l i t y ;

    5 S i z e maxIte rat ions = 1000 ;Real accuracy = 1e8;

    7 I n t eg e r g r i d I n t e r v a l s = 10 ;Real revers ionParameter = . 1 ;

    9

    Real sigma = 0 . 0 1 ; //QL EPSILON; // core dumps i f ze ro on Cygwin11

    boost : : shared ptr hw0(new HullWhite (discountTermStructure , revers ionParameter , sigma ) ) ;

    13 boost : : shared ptr Black Karas in sk i (new BlackKaras insk i (discountTermStructure ) ) ;

    15 boost : : shared ptr engine0 (newTreeCallableFixedRateBondEngine (hw0 , g r i d I n t e r v a l s ) ) ;

    boost : : shared ptr engine1 (newTreeCallableFixedRateBondEngine ( Black Karas insk i , g r i d I n t e r v a l s ) ) ;

    17 boost : : shared ptr BondEngine (new DiscountingBondEngine (discountTermStructure ) ) ;

    19 FixedRateBond Bond( SettlementDays , 100 , SchBond , vector(3 ,Coupon) ,BondDC) ;

    CallableFixedRateBond ca l lab l eBond ( SettlementDays , 100 , SchBond , vector(3 , Coupon) , BondDC, BondAccrueConvention ,

    21 100 , today , Cal lSch ) ;

    23 / Resu l t s /Bond . s e tPr i c ingEng ine (BondEngine ) ;

    25 Real Bond Price = Bond . c l e anPr i c e ( ) ; // Option f r e e bond valueRate Bond Yield = Bond . y i e l d (BondDC, Compounded , CouponFrequency ,

    accuracy , maxIte rat ions ) ;27

    ca l lab leBond . s e tPr i c ingEng ine ( engine0 ) ;29 Real CallPrice HW = cal lab leBond . c l e anPr i c e ( ) ;

    Rate Cal lBond Yield1 = ca l lab leBond . y i e l d (BondDC, Compounded ,CouponFrequency , accuracy , maxIte rat ions ) ;

    31 Real OptPrice1 = Bond Price CallPrice HW ;

    33 ca l lab leBond . s e tPr i c ingEng ine ( engine1 ) ;Real Cal lPrice BK = cal lab leBond . c l e anPr i c e ( ) ;

    18

  • Rate Cal lBond Yield2 = ca l lab leBond . y i e l d (BondDC, Compounded ,CouponFrequency , accuracy , maxIte rat ions ) ;

    2 Real OptPrice2 = Bond Price CallPrice BK ;

    4 Real BondPrice = 101 . 2654 ;Spread z Spread = 10000 BondFunctions : : zSpread (Bond , BondPrice , TS, dc

    , Compounded , f requency ) ;6 Spread HW oas = z Spread OptPrice1 ;

    Spread BK oas = z Spread OptPrice2 ;8

    / Print /10 s t r i n g ru l e (70 , ) ;

    cout

  • 1 / mode : c++; tabwidth : 4 ; indenttabsmode : n i l ; cbas ico f f s e t : 4 /

    3 /Copyright (C) 2014 Mauric io bedoya

    5

    This f i l e i s part o f QuantLib , a f r e eso f tware /opensource l i b r a r y7 f o r f i n a n c i a l quan t i t a t i v e ana l y s t s and deve l ope r s http :// quant l ib . org /

    9 QuantLib i s f r e e so f tware : you can r e d i s t r i b u t e i t and/ or modify i tunder the terms o f the QuantLib l i c e n s e . You should have r e c e i v ed a

    11 copy o f the l i c e n s e along with t h i s program ; i f not , p l e a s e emai l. The l i c e n s e i s a l s o a v a i l a b l e on l i n e at

    13 .

    15 This program i s d i s t r i b u t e d in the hope that i t w i l l be u se fu l , but WITHOUTANY WARRANTY; without even the impl i ed warranty o f 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 n d e f quant l ib l oan hpp21 #de f i n e quant l ib l oan hpp

    23 #inc lude #inc lude

    25

    namespace QuantLib{27

    // ! Base c l a s s f o r l oans29 / ! This c l a s s i s pure ly v i r t u a l and ac t s as a base c l a s s f o r the

    ac tua l l oans implementat ions .31 /

    c l a s s Loan : pub l i c Event{33 pub l i c :

    v i r t u a l Leg BeginingBalance ( ) const = 0 ;35 v i r t u a l Leg Payment ( ) const = 0 ;

    v i r t u a l Leg Net In t e r e s t ( ) const = 0 ;37 v i r t u a l Leg Fee ( ) const = 0 ;

    v i r t u a l Leg PrincipalRepayment ( ) const = 0 ;39 v i r t u a l Leg EndingBalance ( ) const = 0 ;

    v i r t u a l void accept ( Acy c l i cV i s i t o r& v) ;41 / from Event /

    v i r t u a l Date date ( ) const = 0 ;43 } ;

    45 }#end i f

    20

  • / mode : c++; tabwidth : 4 ; indenttabsmode : n i l ; cbas ico f f s e t : 4 /2

    /4 Copyright (C) 2014 Mauric io bedoya

    6 This f i l e i s part o f QuantLib , a f r e eso f tware /opensource l i b r a r yf o r f i n a n c i a l quan t i t a t i v e ana l y s t s and deve l ope r s http :// quant l ib . org /

    8

    QuantLib i s f r e e so f tware : you can r e d i s t r i b u t e i t and/ or modify i t10 under the terms o f the QuantLib l i c e n s e . You should have r e c e i v ed a

    copy o f the l i c e n s e along with t h i s program ; i f not , p l e a s e emai l12 . The l i c e n s e i s a l s o a v a i l a b l e on l i n e at

    .14

    This program i s d i s t r i b u t e d in the hope that i t w i l l be u se fu l , but WITHOUT16 ANY WARRANTY; without even the impl i ed warranty o f 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 #inc lude loan . hpp#inc lude

    22

    namespace QuantLib {24 void Loan : : accept ( Acy c l i cV i s i t o r& v)

    {26 Vis i t o r v1 = dynamic cast(&v ) ;

    i f ( v1 !=0)28 v1>v i s i t ( t h i s ) ;

    e l s e30 Event : : accept ( v ) ;

    }32 }

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

    21

  • / mode : c++; tabwidth : 4 ; indenttabsmode : n i l ; cbas ico f f s e t : 4 /2

    /4 Copyright (C) 2014 Mauric io bedoya

    6 This f i l e i s part o f QuantLib , a f r e eso f tware /opensource l i b r a r yf o r f i n a n c i a l quan t i t a t i v e ana l y s t s and deve l ope r s http :// quant l ib . org /

    8

    QuantLib i s f r e e so f tware : you can r e d i s t r i b u t e i t and/ or modify i t10 under the terms o f the QuantLib l i c e n s e . You should have r e c e i v ed a

    copy o f the l i c e n s e along with t h i s program ; i f not , p l e a s e emai l12 . The l i c e n s e i s a l s o a v a i l a b l e on l i n e at

    .14

    This program i s d i s t r i b u t e d in the hope that i t w i l l be u se fu l , but WITHOUT16 ANY WARRANTY; without even the impl i ed warranty o f 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 n d e f quant l ib mortgage hpp#de f i n e quant l ib mortgage hpp

    22

    #inc lude loan . hpp24 #inc lude

    #inc lude 26 #inc lude

    #inc lude 28 #inc lude

    30 namespace QuantLib {c l a s s Mortgage : pub l i c Loan{

    32 pub l i c :Mortgage ( const Date& issueDate , const Calendar& calendar , const Real&

    amount , const In t e r e s tRate& rate , const Rate& fee , const In t eg e r& term , constIn t eg e r& yearPayments ) ;

    34 v i r t u a l Mortgage ( ) ;//@{

    36 / In spec to r /Date i s sueDate ( ) ;

    38 Real amount ( ) ;In t e r e s tRate ra t e ( ) ;

    40 Rate f e e ( ) ;

    42 / Se t t e r s /void s e t i s s u eDa t e ( const Date& new Date ) ;

    44 void set amount ( const Real& new amount ) ;void s e t r a t e ( const In t e r e s tRat e& new rate ) ;

    46 void s e t f e e ( const Rate& new fee ) ;

    22

  • / Pure Vi r tua l /2 Leg BeginingBalance ( ) const ;

    Leg Payment ( ) const ;4 Leg Net In t e r e s t ( ) const ;

    Leg Fee ( ) const ;6 Leg PrincipalRepayment ( ) const ;

    Leg EndingBalance ( ) const ;8 Date date ( ) const ; // From Event c l a s s

    10 / Vi s i t o r /v i r t u a l void accept ( Acy c l i cV i s i t o r& v) ;

    12 //@}

    14 pr i va t e :Date i s sueDate ;

    16 Calendar ca l enda r ;Real amount ;

    18 I n t eg e r term , yearPayments ;Compounding compound ;

    20 In t e r e s tRate r a t e ;Rate f e e ;

    22

    / Helpers /24 Time t = (1 . 0/ yearPayments ) ;

    I n t eg e r numberOfPayments = ( term yearPayments ) ;26 Real compoundFactor = r a t e . compoundFactor ( t ) ;

    } ;28 }

    30 #end i f / de f ined ( hh mor tgage ) /

    23

  • / mode : c++; tabwidth : 4 ; indenttabsmode : n i l ; cbas ico f f s e t : 4 /2

    /4 Copyright (C) 2014 Mauric io bedoya

    6 This f i l e i s part o f QuantLib , a f r e eso f tware /opensource l i b r a r yf o r f i n a n c i a l quan t i t a t i v e ana l y s t s and deve l ope r s http :// quant l ib . org /

    8

    QuantLib i s f r e e so f tware : you can r e d i s t r i b u t e i t and/ or modify i t10 under the terms o f the QuantLib l i c e n s e . You should have r e c e i v ed a

    copy o f the l i c e n s e along with t h i s program ; i f not , p l e a s e emai l12 . The l i c e n s e i s a l s o a v a i l a b l e on l i n e at

    .14

    This program i s d i s t r i b u t e d in the hope that i t w i l l be u se fu l , but WITHOUT16 ANY WARRANTY; without even the impl i ed warranty o f 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

    #inc lude mortgage . hpp22 #inc lude

    #inc lude 24 #inc lude

    #inc lude 26

    us ing namespace boost : : a s s i gn ;28

    namespace QuantLib {30

    Mortgage : : Mortgage ( const Date& issueDate , const Calendar& calendar , constReal& amount , const In t e r e s tRate& rate , const Rate& fee , const In t eg e r& term ,const In t eg e r& yearPayments ) : i s sueDate ( i s sueDate ) , c a l enda r ( ca l endar ) ,

    amount ( amount ) , r a t e ( ra t e ) , f e e ( f e e ) , term ( term ) , yearPayments (yearPayments ) {}

    32

    Mortgage : : Mortgage ( ) {}34

    //@{36 / In spec to r /

    Date Mortgage : : i s sueDate ( ) { re turn i s sueDate ;}38 Real Mortgage : : amount ( ) { re turn amount ;}

    In t e r e s tRate Mortgage : : r a t e ( ) { re turn r a t e ;}40 Rate Mortgage : : f e e ( ) { re turn f e e ;}

    42 / Se t t e r s /void Mortgage : : s e t i s s u eDa t e ( const Date& new Date ) { i s sueDate = new Date ;}

    44 void Mortgage : : set amount ( const Real& new amount ) {amount = new amount ;}void Mortgage : : s e t r a t e ( const In t e r e s tRat e& new rate ) { r a t e = new rate ;}

    46 void Mortgage : : s e t f e e ( const Rate& new fee ) { f e e = new fee ;}

    24

  • / Pure Vi r tua l /2 Leg Mortgage : : BeginingBalance ( ) const

    {4 Real denominator = pow( compoundFactor , numberOfPayments ) 1 ;

    Leg beg in ingba lance ;6

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

    Date next = i s sueDate + Months ( ( i +1) 12 / yearPayments ) ;10 boost : : shared ptr BMB(new SimpleCashFlow ( amount (pow(

    compoundFactor , numberOfPayments ) pow( compoundFactor , i ) ) / denominator ,next ) ) ;

    beg in ingba lance += BMB;12 }

    14 re turn beg in ingba lance ;}

    16

    Leg Mortgage : : Payment ( ) const18 {

    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 sueDate + Months ( ( i +1) 12 / yearPayments ) ;boost : : shared ptr MP(new SimpleCashFlow (M, next ) ) ;

    28 payment += MP;}

    30

    re turn payment ;32 }

    34 Leg Mortgage : : Fee ( ) const{

    36 Leg beg in ingba lance = BeginingBalance ( ) ;Leg f e e ;

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

    40 Date next = i s sueDate + Months ( ( i +1) 12 / yearPayments ) ;boost : : shared ptr F(new SimpleCashFlow ( beg in ingba lance [ i

    ]>amount ( ) ( f e e / yearPayments ) , next ) ) ;42 f e e += F;

    }44 re turn f e e ;

    }

    25

  • 1 Leg Mortgage : : Ne t In t e r e s t ( ) const{

    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 sueDate + Months ( ( i +1) 12 / yearPayments ) ;

    9 boost : : shared ptr 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 re turn n e t i n t e r e s t ;}

    15

    Leg Mortgage : : PrincipalRepayment ( ) const17 {

    Real denominator = pow( compoundFactor , numberOfPayments ) 1 ;19 Leg pr inc ipa l repayment ;

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

    Date next = i s sueDate + Months ( ( i +1) 12 / yearPayments ) ;23 boost : : shared ptr PR(new SimpleCashFlow ( amount ( (

    compoundFactor 1) pow( compoundFactor , i ) ) / denominator , next ) ) ;pr inc ipa l repayment += PR;

    25 }

    27 re turn pr inc ipa l repayment ;}

    29

    Leg Mortgage : : EndingBalance ( ) const31 {

    Leg beg in ingba lance = BeginingBalance ( ) ;33 Leg pr inc ipa l repayment = PrincipalRepayment ( ) ;

    Leg endingbalance ;35 f o r ( i n t i = 0 ; i < numberOfPayments ; i++)

    {37 Date next = i s sueDate + Months ( ( i +1) 12 / yearPayments ) ;

    boost : : shared ptr EMB(new SimpleCashFlow ( beg in ingba lance [ i]>amount ( ) pr inc ipa l repayment [ i ]>amount ( ) , next ) ) ;

    39

    endingbalance += EMB;41 }

    43 re turn endingbalance ;}

    26

  • Date Mortgage : : date ( ) const { re turn i s sueDate ;}2

    / Vi s i t o r /4 void Mortgage : : accept (QuantLib : : A cy c l i cV i s i t o r &v)

    {6 Vis i t o r v1 = dynamic cast(&v ) ;

    i f ( v1 !=0)8 v1>v i s i t ( t h i s ) ;

    e l s e10 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. Theimplementation in main is:

    1 / Fixed Income Mathematics Four Edit ion

    3 pag (409)CashFlow o f mortgage with s e r v i c i n g f e e .

    5 /#inc lude

    7 #inc lude #inc lude

    9 #inc lude #inc lude

    11 #inc lude mortgage . hpp

    13 us ing namespace QuantLib ;us ing namespace std ;

    15 us ing namespace boost : : a s s i gn ;

    17 boost : : format FORMATER ( %20s %12s %12s %15s %12s %12s %12s ) ;

    19 i n t main ( ){

    21 t ry {Rate r f = 0 . 0 9 5 ;

    23 DayCounter DC= ActualActual ( ActualActual : : ISDA) ;Calendar ca l endar = NullCalendar ( ) ;

    25 Compounding compounded = Simple ;Frequency f requency = Annual ;

    27

    In t e r e s tRate r f Rate ( r f , DC, compounded , f requency ) ;29 Date i s s u e (1 , Jan ,2000 ) ;

    d = ca lendar . ad jus t (d) ;

    27

  • I n t eg e r YearPayments = 12 ;2 Rate f e e = 0 . 0 0 5 ;

    Mortgage M(d , ca lendar , 100000 , r f Rate , f ee , 30 , YearPayments ) ;4

    cout amount ( ) %M. Fee ( ) [i ]>amount ( ) %M. PrincipalRepayment ( ) [ i ]>amount ( ) %M. EndingBalance ( ) [ i ]>amount ( )

  • / mode : c++; tabwidth : 4 ; indenttabsmode : n i l ; cbas ico f f s e t : 4 /2

    /4 Copyright (C) 2014 Mauric io Bedoya

    6 This f i l e i s part o f QuantLib , a f r e eso f tware /opensource l i b r a r yf o r f i n a n c i a l quan t i t a t i v e ana l y s t s and deve l ope r s http :// quant l ib . org /

    8

    QuantLib i s f r e e so f tware : you can r e d i s t r i b u t e i t and/ or modify i t10 under the terms o f the QuantLib l i c e n s e . You should have r e c e i v ed a

    copy o f the l i c e n s e along with t h i s program ; i f not , p l e a s e emai l12 . The l i c e n s e i s a l s o a v a i l a b l e on l i n e at

    .14

    This program i s d i s t r i b u t e d in the hope that i t w i l l be u se fu l , but WITHOUT16 ANY WARRANTY; without even the impl i ed warranty o f 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 n d e f quant l ib f ixedratemortgagePSA hpp#de f i n e quant l ib f ixedratemortgagePSA hpp

    22

    #inc lude f ixedratemortgage . hpp24 #inc lude

    #inc lude 26 #inc lude

    #inc lude 28 #inc lude

    30 namespace QuantLib {c l a s s FixedRateMortgagePSA : pub l i c FixedRateMortgage {

    32 pub l i c :FixedRateMortgagePSA ( const Date& issueDate , const Calendar& calendar ,

    const Real& amount , const In t e r e s tRat e& rate , const Rate& fee , const In t eg e r&term , const In t eg e r& yearPayments , const Real& PSA) ;

    34 v i r t u a l FixedRateMortgagePSA ( ) ;//@

    36 / In spec to r /Real amount ( ) ;

    38 In t e r e s tRate ra t e ( ) ;Rate f e e ( ) ;

    40

    / Se t t e r s /42 void s e t i s s u eDa t e ( const Date& new Date ) ;

    void set amount ( const Real& new amount ) ;44 void s e t r a t e ( const In t e r e s tRat e& new rate ) ;

    void s e t f e e ( const Rate& new fee ) ;

    29

  • 1 / Pure v i r t u a l /Leg BeginingBalance ( ) const ;

    3 Leg Payment ( ) const ;Leg Ne t In t e r e s t ( ) const ;

    5 Leg Fee ( ) const ;Leg PrincipalRepayment ( ) const ;

    7 Leg EndingBalance ( ) const ;Date date ( ) const ; // from Event

    9

    / Vi s i t o r /11 v i r t u a l void accept ( Acy c l i cV i s i t o r& v) ;

    13 / Others /Leg SMM() const ;

    15 Leg b ( ) const ;Leg I n t e r e s t ( ) const ;

    17 Leg ProyectedPrincipalPayment ( ) const ;Leg CashFlowToInvestors ( ) const ;

    19

    //@21 pr i va t e :

    Date i s sueDate ;23 Calendar ca l enda r ;

    Real amount , PSA ;25 I n t eg e r term , yearPayments ;

    Compounding compound ;27 In t e r e s tRate r a t e ;

    Rate f e e ;29 Leg BeginingBalance , Payment , Ne t In t e r e s t , Fee , PrincipalRepayment ,

    I n t e r e s t , ProyectedPrincipalPayment , EndingBalance , CashFlowToInvestors ;

    31 / Helpers /Time t = (1 . 0/ yearPayments ) ;

    33 I n t eg e r numberOfPayments = ( term yearPayments ) ;Real compoundFactor = r a t e . compoundFactor ( t ) ;

    35 void GenerateCashFlows ( ) ;} ;

    37 }

    39 #end i f / de f ined ( hh f ixedratemortgagePSA ) /

    30

  • 1 / mode : c++; tabwidth : 4 ; indenttabsmode : n i l ; cbas ico f f s e t : 4 /

    3 /Copyright (C) 2014 Mauric io Bedoya

    5

    This f i l e i s part o f QuantLib , a f r e eso f tware /opensource l i b r a r y7 f o r f i n a n c i a l quan t i t a t i v e ana l y s t s and deve l ope r s http :// quant l ib . org /

    9 QuantLib i s f r e e so f tware : you can r e d i s t r i b u t e i t and/ or modify i tunder the terms o f the QuantLib l i c e n s e . You should have r e c e i v ed a

    11 copy o f the l i c e n s e along with t h i s program ; i f not , p l e a s e emai l. The l i c e n s e i s a l s o a v a i l a b l e on l i n e at

    13 .

    15 This program i s d i s t r i b u t e d in the hope that i t w i l l be u se fu l , but WITHOUTANY WARRANTY; without even the impl i ed warranty o f 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

    #inc lude fixedratemortgagePSA . hpp21 #inc lude

    #inc lude 23 #inc lude

    #inc lude 25

    us ing namespace boost : : a s s i gn ;27

    namespace QuantLib {29 FixedRateMortgagePSA : : FixedRateMortgagePSA ( const Date& issueDate , const

    Calendar& calendar , const Real& amount , const In t e r e s tRate& rate , const Rate&fee , const In t eg e r& term , const In t eg e r& yearPayments , const Real& PSA) :

    FixedRateMortgage ( issueDate , ca lendar , amount , rate , f ee , term , yearPayments ), i s sueDate ( i s sueDate ) , c a l enda r ( ca l endar ) , amount ( amount ) , r a t e ( ra t e ) ,f e e ( f e e ) , term ( term ) , yearPayments ( yearPayments ) , PSA (PSA) {GenerateCashFlows ( ) ;}FixedRateMortgagePSA : : FixedRateMortgagePSA ( ) {}

    31 //@/ In spec to r /

    33 Real FixedRateMortgagePSA : : amount ( ) { re turn amount ;}In t e r e s tRate FixedRateMortgagePSA : : r a t e ( ) { re turn r a t e ;}

    35 Rate FixedRateMortgagePSA : : f e e ( ) { re turn f e e ;}

    37 / Se t t e r s /void FixedRateMortgagePSA : : s e t i s s u eDa t e ( const Date& new Date ) { i s sueDate =new Date ;}

    39 void FixedRateMortgagePSA : : set amount ( const Real& new amount ) {amount =new amount ;}void FixedRateMortgagePSA : : s e t r a t e ( const In t e r e s tRat e& new rate ) { r a t e =new rate ;}

    41 void FixedRateMortgagePSA : : s e t f e e ( const Rate& new fee ) { f e e = new fee ;}

    31

  • 1 void 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 sueDate + ((1 + i ) 12 / yearPayments ) Months ;i f ( i amount ( ) , next ) ) ;Beg in ingBalance += MB;

    19 }

    21 boost : : shared ptr MP(new SimpleCashFlow ( bt [ i ]>amount ( ) PMT, next ) ) ;

    Payment += MP;23

    boost : : shared ptr I (new SimpleCashFlow ( Begin ingBalance [ i]>amount ( ) ( compoundFactor 1) , next ) ) ;

    25 I n t e r e s t += I ;

    27 boost : : shared ptr F(new SimpleCashFlow ( Begin ingBalance [ i]>amount ( ) ( f e e / yearPayments ) , next ) ) ;

    Fee += F;29

    boost : : shared ptr NI(new SimpleCashFlow ( I n t e r e s t [ i ]>amount ( ) Fee [ i ]>amount ( ) , next ) ) ;

    31 Net In t e r e s t += NI ;

    33 boost : : shared ptr SP(new SimpleCashFlow ( bt [ i ]>amount ( ) FixedRateMortgage : : PrincipalRepayment ( ) [ i ]>amount ( ) , next ) ) ;

    ProyectedPrincipalPayment += SP ;35

    boost : : shared ptr PR(new SimpleCashFlow (smm[ i ]>amount ( ) ( Beg in ingBalance [ i ]>amount ( ) ProyectedPrincipalPayment [ i ]>amount ( ) ) ,

    next ) ) ;37 PrincipalRepayment += PR;

    32

  • 1 boost : : shared ptr MBt(new SimpleCashFlow ( Begin ingBalance [i ]>amount ( ) ProyectedPrincipalPayment [ i ]>amount ( ) PrincipalRepayment [i ]>amount ( ) , next ) ) ;

    EndingBalance += MBt;3

    boost : : shared ptr CF(new SimpleCashFlow ( Ne t I n t e r e s t [ i ]>amount ( ) + ProyectedPrincipalPayment [ i ]>amount ( ) + PrincipalRepayment [ i ]>amount ( ) , next ) ) ;

    5 CashFlowToInvestors += CF;

    7 }

    9 }/ Pure v i r t u a l /

    11 Leg FixedRateMortgagePSA : : BeginingBalance ( ) const { re turn Begin ingBalance ;}

    13 Leg FixedRateMortgagePSA : : Payment ( ) const { re turn Payment ;}

    15 Leg FixedRateMortgagePSA : : Ne t In t e r e s t ( ) const { re turn Ne t I n t e r e s t ;}

    17 Leg FixedRateMortgagePSA : : Fee ( ) const { re turn Fee ;}

    19 Leg FixedRateMortgagePSA : : PrincipalRepayment ( ) const { re turnPrincipalRepayment ;}

    21 Leg FixedRateMortgagePSA : : EndingBalance ( ) const { re turn EndingBalance ;}

    23 Date FixedRateMortgagePSA : : date ( ) const { re turn i s sueDate ;}

    25 / Vi s i t o r /void FixedRateMortgagePSA : : accept ( Acy c l i cV i s i t o r& v)

    27 {Vis i t o r v1 = dynamic cast >(&v ) ;29 i f ( v1 !=0)

    v1>v i s i t ( t h i s ) ;31 e l s e

    FixedRateMortgage : : accept ( v ) ;33 }

    33

  • 1 / Others /Leg FixedRateMortgagePSA : :SMM() const

    3 {Leg smm;

    5 Real CRP;i n t N = 30 yearPayments / 12 ;

    7

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

    i f ( i < N)11 CRP = (0 . 06 ( i + 1) / N) PSA ;

    e l s e13 CRP = (0 . 0 6 ) PSA ;

    Date next = i s sueDate + ((1 + i ) 12 / yearPayments ) ;15 boost : : shared ptr S(new SimpleCashFlow (1 pow((1 CRP)

    , ( 1 . 0 / yearPayments ) ) , next ) ) ;smm += S ;

    17 }

    19 re turn smm;}

    21

    Leg FixedRateMortgagePSA : : b ( ) const23 {

    Leg b ;25 Leg smm = SMM() ;

    Real data = 1 ;27 Date next = i s sueDate + (1 12 / yearPayments ) ;

    boost : : shared ptr bt (new SimpleCashFlow ( data , next ) ) ;29 b += bt ;

    f o r ( i n t i = 1 ; i < numberOfPayments ; i++)31 {

    next = i s sueDate + ((1 + i ) 12 / yearPayments ) ;33 data =(1SMM() [ i1]>amount ( ) ) ;

    boost : : shared ptr bt (new SimpleCashFlow ( data , next ) ) ;35 b += bt ;

    }37

    re turn b ;39 }

    34

  • 1 Leg FixedRateMortgagePSA : : I n t e r e s t ( ) const{

    3 Leg beg in ingba lance = BeginingBalance ( ) ;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 sueDate + ((1 + i ) 12 / yearPayments ) Months ;boost : : shared ptr I (new SimpleCashFlow ( beg in ingba lance [ i

    ]>amount ( ) ( compoundFactor 1) , next ) ) ;9 i n t e r e s t += I ;

    }11

    re turn i n t e r e s t ;13 }

    15 Leg FixedRateMortgagePSA : : ProyectedPrincipalPayment ( ) const { re turnProyectedPrincipalPayment ;}

    17 Leg FixedRateMortgagePSA : : CashFlowToInvestors ( ) const { re turnCashFlowToInvestors ;}

    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 Edit ion

    pag (409)4 CashFlow o f mortgage with s e r v i c i n g f e e . /

    6 #inc lude #inc lude

    8 #inc lude #inc lude

    10 #inc lude #inc lude f ixedratemortgage . hpp

    12 #inc lude fixedratemortgagePSA . hpp

    14 us ing namespace QuantLib ;us ing namespace std ;

    16 us ing namespace boost : : a s s i gn ;

    18 boost : : format FORMATER1 ( %20s %12s %12s %12s %12s %12s %12s %12s%12s %12s ) ;

    35

  • i n t main ( )2 {

    t ry {4 Rate r f = 0 . 0 9 5 ;

    DayCounter DC= ActualActual ( ActualActual : : ISDA) ;6 Calendar ca l endar = NullCalendar ( ) ;

    Compounding compounded = Simple ;8 Frequency f requency = Annual ;

    10 In t e r e s tRate r f Rate ( r f , DC, compounded , f requency ) ;Date i s s u e (1 , Jan ,2000 ) ;

    12 i s s u e = ca lendar . ad jus t ( i s s u e ) ;I n t eg e r YearPayments = 12 ;

    14 Rate f e e = 0 . 0 0 5 ;Real PSA = 1 . 0 ;

    16 cout 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 ( )