Quaderni SEMeQ

Daniele Marazzina

Interest Rate Modelling: A MATLAB Implementation
Aprile 2007

Quaderno n. 13/2007

Stampato in proprio presso la Segreteria del Dipartimento di Scienze Economiche e Metodi Quantitativi, Università degli Studi del Piemonte Orientale “A. Avogadro”

I Lavori pubblicati nella collana riflettono esclusivamente le opinioni degli autori e non impegnano la responsabilità del Dipartimento SEMeQ.

Dipartimento di Scienze Economiche e Metodi Quantitativi Via E. Perrone 18 – 28100 Novara Tel. 39 (0) 321- 375310 Fax 39 (0) 321 - 375305 e-mail: segreteria.dsemeq@unipmn.it

Interest Rate Modeling: A M ATLAB Implementation
Daniele Marazzina SEMeQ Department, Università del Piemonte Orientale Via Perrone 18, 28100 Novara, Italy e-mail: daniele.marazzina@eco.unipmn.it Avanade Italy Corso Venezia 46, 20121 Milano, Italy e-mail: danielema@avanade.com

Abstract The aim of this work is to present a M ATLAB implementation of different methods for estimating the term structure of interest rate. More precisely, we implement the exponential functional form of NelsonSiegel and polynomial spline methods (with or without penalty term), considering both coupon bonds, like Italian Btp, and Libor and Swap interest rates. Furthermore, we compare the models’performances, considering both computational costs and approximation results.

1

Introduction

An important tool in the development and testing of financial theory is the term structure of interest rate as it provides a characterization of interest rates as a function of maturity. The yield curve, or term structure of interest rate, is the set of interest rates for different investment periods or maturities. Yield curve can display a wide variety of shapes. Typically, a yield curve will slope upwards, with longer term rates being higher, although there are plenty of examples of inverted term structures where long rates are less then short rates. In the bond markets the yield curve is found from bond data, while in the money market the yield curve is derived from the prices of a variety of different types of instrument. Cash rates provide information about the short term rates, from one week to about 18 months. These are investments or borrowings where the entire sum of interest plus principal is due at the end of the period, with no interim payments. After about two-year point, Swaps become the dominant instrument: from there it is possible to derive the yield curve until 15-year point. After that, bond can be used. See [5] for further details. Information about the yield curve becomes steadily more sparse as the maturity increases. A good interest rate curve is the most basic requirement for pricing interest rate derivatives: if the curve is bad, the prices it returns will be bad (see [1]). The objective in empirical fitting of the term structure is to find a curve that fits the data sufficiently well and it is a sufficiently smooth function: the second requirement is at least as important as the first, even if it is less quantifiable (see, for example, [12]). In this paper we consider the methodology of fitting the discount function d (or related function, as, for example, the instantaneous forward rate function) by the exponential model of Nelson-Siegel (see [9] and [11]) and polynomial spline methods (see [6] and [7]) with or without penalty term (see [2] and [4]). Moreover, we consider a M ATLAB [13] code written for both Italian Btp data and Libor and Swap data in order to obtain the curves. The outline of the paper is the following: in Section 2 we describe how we can extract the necessary data from coupon bonds and Libor and Swap rates; in Section 3 we introduce the data-fitting techniques: more precisely, we describe the Nelson-Siegel model and some methods based on cubic spline. In Section 4 we explain how our M ATLAB code works using the theoretical results introduced in the previous 1

sections. Finally, Section 5 deals with the models’performances, considering both computational cost and approximation results.

2

The Discount Function

The term structure of interest rate can be identified with any of a number of related concepts. In this section we consider the discount function d(t), which describes the present value of 1 Euro repayable in t years. The aim of the algorithm described in this paper is to extract the term structure from a set of coupon bonds (such as the Italian Btp considered), whose prices are largely determined by the present value of their stated payments (see [2]), or a set of Libor and Swap rates. More precisely, in this section we show a way to obtain the values of the discount function at the payments’dates for both kind of data. For further details, see the examples reported in Appendix A.

2.1

Coupon Bonds

Following [2] and [8], let P (i) be the price of a coupon bond i at a fixed date t (value date) and A(i, j ) be its j th payment made at time BP (i, j ); moreover, let mi be the number of remaining payments for the bond i and n be the number of bonds. Then, if we set for i = 1, · · · , n and j = 1, · · · , mi T (i, j ) := α0 [t, BP (i, j )], where α0 [t1 , t2 ] is the distance between the two dates t1 and t2 , computed according to the actual/365 day-count convention (i.e., the day-count convention for bonds), we have
mi

P (i) =
j =1

A(i, j )d(T (i, j ))

∀ i = 1, · · · , n.

(1)

Since the coupon bond i is characterized by a periodic coupon Ci and a facial value Fi , we set A(i, j ) = Ci if j < mi A(i, j ) = Ci + Fi if j = mi , for i = 1, · · · , n. For simplicity, in this paper we consider coupons paid every six months (semi-annual coupons) and we define Ci = Ci /2, where Ci is the annual payment for the bond i, i = 1, · · · , n. If we now define T T as the vector containing all the elements of T , sorted in ascending order, and we define the matrix AA such that AA(i, j ) is the payment of the bond i at time T T (j ) (thus it could be equal to 0 if T T (j ) is not a time to payment for the considered bond), then (1) becomes AA D = P , (2)

where D (i) = d(T T (i)), i = 0, · · · , m, and m is the number of elements of T T (i.e., the number of the global payment times). Considering the bond i, i = 1, · · · , n, its price P (i) (i.e., the price that purchaser pays when buying the bond) is equal to the quoted price (clean price) plus the accrued interest AI (i.e., the interest accumulated between the most recent interest payment and the present time). In order to compute AI , let t−1 be the date of the previous coupon payment, t be the value date and t1 be the date of the next coupon payment, then the accrued interest is AI = α0 [t−1 , t] Ci α0 [t−1 , t] = Ci . 2 α0 [t−1 , t1 ] α0 [t−1 , t1 ]

2

computed according to the actual/360 day-count convention. otherwise LP (i) is the first business day after t + LM (i). we obtain A1 (i) d(T1 (i)) = P1 (i) ∀ i = 1. P1 (i) = 1.e. n2 . (3) Now we consider Swaps. which are daily reference rates based on the interest rates at which banks offer to lend funds to other banks. if we define for each Libor rate i A1 (i) = 1 + LR(i) T1 (i). we define T2 (i.. t2 ] is the distance between the two dates t1 and t2 . SP (i. · · · . following [5]. j ).e. i = 1. where: • LP (i) is the payment date.e. · · · . t2 ] is the distance between the two dates t1 and t2 . (i.. LP (i)] 1 + LR(i) T1 (i) and so. SR(i) be the interest rate of the Swap with expiration at i years and SP (i. computed according to the 30/360 daycount convention (i. LP (i)].2 Libor and Swap Rates First of all we consider Libor (London Interbank Offered Rate) rates. n 1 . · · · . In order to derive the discount function from Swap rates. we suppose that they have cash flow at one-year intervals.. • α1 [t1 . 1 + LR(i) α1 [t. which are agreement to exchange one set of cash flow for another. the day-count convention for Swaps considering the Euro market). reasoning as in the previous subsection. i = 1. where α2 [t1 . The most commonly traded Swap requires one side to pay a fixed rate and the other to pay a floating rate. the day-count convention for Libors). j ) − T2 (i.e. Thus.2. be its j th payment date. the value date) gives 1 + LR(i) α1 [t. LP (i) = t + LM (i) if it is a business day. we have d(T2 (i)) = 1 − SR(i) i−1 j =1 [T2 (i. Let n1 be the number of Libors considered. if we set T1 (i) := α1 [t. j − 1)] d(T2 (i. Let t be the value date. LP (i)] Euros at time LP (i).. we have d(T1 (i)) = 1 1 = . n1 : this means that 1 Euro bought at time t (i. j ) := α2 [t. with 0 < j ≤ i. LM be the vector containing the times to maturity of the n1 Libors and LR(i) be the interest rate of the Libor with time to maturity LM (i). (4) 3 . i − 1)] . j )) 1 + SR(i) [T2 (i) − T2 (i. i. Thus. j )].

3 The Fitting Procedure The aim of this section is to introduce the data-fitting techniques: we start with the Nelson-Siegel model. all the elements T1 (l). · · · . n. i = 0. · · · ..e. the coupon bonds case) and equation (6) (i. n = n1 + n2 ). Notice that equation (2) (i. · · · . i = 1. j ) = SR(i) [T2 (i. then we present six models related with the cubic spline method. where m is the size of T T . A2 (i. • AA(i. j = 1. · · · . i.. its time to maturity. · · · . If it is not possible. l = 1. if we suppose that the values of SR(i − 1) and SR(i + 1) are known. j − 1)] if j < i. n2 . Thus we will consider their resolution through a fitting procedure without any distinction between the kind of data considered.e. then (4) is equivalent to i A2 (i. i. introduced in [9]. i). the Libor and Swap rates case) are of the same type. we obtain the missing Swap rates using linear interpolation.e. j = 1... Thus equations (3) and (5) become AA D = P . 2 See the M ATLAB routine MISSING _S WAP in Section 4. j ) = A1 (i) if i ≤ n1 and T T (j ) = T1 (i)... the number of payment dates. n2 . n2 . j )) = P2 (i) ∀ i = 1.e. n1 and T2 (i. we set SR(i) = SR(i − 1) + SR(i + 1) . i. i − 1)]. 4 . p).e. T T as the vector containing all the times to payment of both the Libor and the Swap rates (i. j =1 (5) It is easy to see that in order to use equations (4) and (5) it is necessary to know each Swap rates with expiration from 1 to n2 years. while SR(i) is unknown. p) if i > n1 and T T (j ) = T2 (i − n1 . If we define for i = 1. · · · . j ).where n2 is the maximum expiration time of the Swap considered and we have set T2 (i. (6) where D (i) = d(T T (i)). · · · . · · · . n. P2 (i) = 1. we can define • AA(i. j ) d(T2 (i. n2 A2 (i. for i = 0.1 for further details and [1] for other interpolation techniques. m.e. if we now define n as the total number of financial instruments considered (i. j ) − T2 (i. Reasoning as above. • P (i) = P2 (i − n1 ) if i > n1 . 0) = 0 and T2 (i) := T2 (i. j ) = A2 (i − n1 . i.. i) = 1 + SR(i) [T2 (i) − T2 (i. • P (i) = P1 (i) if i ≤ n1 .e. sorted in ascending order). · · · . and p = 1.

Cubic splines are piecewise polynomial.e. Following [2]. where E is the errors’vector (which depends on Θ) and we have to solve the following least square minimization problem: min E T E . 5 . α3 . first of all we consider the case in which we parametrize d as a cubic spline. β > 0. i.1 Nelson-Siegel Model The Nelson-Siegel model uses a single exponential functional form over the entire maturity range (see [8]). thus the estimation of the discount function requires searching the unknown parameters Θ := {α1 .2. α1 + α2 > 0. Therefore. 3. we have implemented the term structure fitting techniques below. β } which minimize the sum of the squared errors. Θ (9) 3. i. [10] for further details). we impose that D (i) := d(T T (i)) = e−α1 T T (i)−β (α2 +α3 )(1−e −T T (i)/β −t/β )+α3 te−t/β (7) (8) )+α3 T T (i)e−T T (i)/β . A B-spline (or bell spline) is a spline function that has minimal support with respect to a given degree and domain partition. s d(τ ) = i=1 βi φi (τ ). characterized by the fact that their first and second derivatives are continuous functions.e. each spline function can be represented as a linear combination of B-splines of the same degree and over the same partition. considering equations (2) and (6). for example. α3 and β which satisfies AA D = P .2 Cubic Spline Method The term spline is used to refer to a wide class of functions that are used in applications requiring data interpolation and/or smoothing (see.1 B-spline Let {φi }s i=1 be the set of the B-spline basis functions.3. α2 . we consider the system AA D = P + E . α2 .. In Figure 1 we show an example of B-spline basis function (see Appendix B and [2] for further details).. (3) and (4) is given as d(t) = e−α1 t−β (α2 +α3 )(1−e with the constraints α1 > 0. joined at so-called knot points. The discount function associated with (1). It is easy to understand that we are not sure that it is possible to find values of α1 .

i.6 0. where β = (β1 .5 1 1. we have D := −exp(G β ). · · · . Then we consider the function g (τ ) := −log (d(τ )) and we parametrize it as a cubic spline. s G(i. 2. · · · .2 0. s (10) g (τ ) = i=1 βi φi (τ ).9 0.3 0.4 0. As it holds d(τ ) = −eg(τ ) . βs )T .8 0.1 0 φ1 φ2 φ3 φ4 φ5 0 0.7 0.5 0. if we define for i = 1. dτ 6 (12) (11) .5 4 Therefore. where exp is the exponential function which operates element-wise on arrays. j ) = φj (T T (i)).e.5 2 2. · · · . Finally we consider the instantaneous forward rate function f (τ ) = − d log (d(τ )). we obtain D := G β .Figure 1: B−spline Basis Functions (Knot Points: 0. m and j = 1.5 3 3. 4) 1 0..

e. If the domain of the spline is [0. j ) = 0 φi (t)φj (t) dt. s Rτ 0 f (t) dt . In order to choose the appropriate λ. we have to solve the least square minimization problem min[(P − AA D )T (P − AA D )]. we consider the value of λ which minimizes the so-called Generalized Cross Validation (GCV) function γ (λ) := ((I − Q)Y )T ((I − Q)Y ) . the fitting procedure for the three cases above consists of solving the minimization problem associated to the following system AA D = P + E . The penalty parameter λ controls the penalty matrix H . since we have to search β which minimize E T E . reducing the effective number of parameters since the penalty forces implicit relationship between the spline basis functions. The minimization problem associated with the B-spline with penalty fitting technique is min[(P − AA D )T (P − AA D ) + λβ T Hβ ]. if we parametrize f as a cubic spline. The strategy proposed in [2] is to penalize excess variability in the estimated discount function.reasoning as above. depending on the function to be parametrized.. β (16) where λ is the penalty parameter (i. we define the penalty term H as the matrix of dimension s × s such that T H (i.2. T ]. and we define the matrix T T ( i) IG(i. a positive real number) and D is defined as in (10).2 B-spline with Penalty Term In a spline the number of basis functions s is determined by the number of knot points: too few or too many parameters can lead to poor estimates.e. it holds d(τ ) = e− Thus. i. (12) or (14). (14) Reasoning as for the Nelson-Siegel model. j ) = 0 φj (t) dt. (s − 2tr(Q))2 7 .. then we have D := −exp(IG β ). λ moves inversely to the effective number of parameters: the more λ becomes a large number. the more the penalty matrix H becomes important in (16). (13) f (τ ) = i=1 βi φi (τ ).e. β (15) 3. i. naturally..

below we report the main file. function main(flag1. such as FMINSEARCH or DAYSDIF. With few parameters in the estimation the residual will tend to be large. when we parametrize g or f ).e. while with an interpolant the perfect fit will tend to produce spurious movements and hence large out of sample residuals. tr is the trace operator and X and Y are defined in Table 1. Notice that in Table 1 β is the solution of (16) and thus it depends also on λ: therefore.flag2) % INPUT % flag1: kind of data % flag1 = 0 -> Btp % flag1 = 1 -> Swap + Libor % % flag2: kind of curve % flag2 = 0 -> Nelson-Siegel % flag2 = 1 -> B-spline (function: d) % flag2 = 2 -> B-spline + Penalty (function: d) % flag2 = 3 -> B-spline (function: g) % flag2 = 4 -> B-spline + Penalty (function: g) % flag2 = 5 -> B-spline (function: f) % flag2 = 6 -> B-spline + Penalty (function: f) %%%%%%%%%%%%%%%%%%%%%%%%%%%% % FIRST STEP: Getting Data % %%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 . in order to find the best λ we have to find β solving the minimization problem (16) at each step of the GCV-minimization procedure. For further information on the M ATLAB routines used in the following code. If data is not scarce.. A better method. which is intended to avoid the possible bias introduced by relying on any particular division into test and train components. I is the s × s identity matrix. cross validation starts by looking at a leave-one-out estimator for each data point. Table 1: Choice of X and Y (X i denotes the ith row of X ) Function to Parametrize W Xi Y d (AA G)i P g AA exp(−G β ) −W (i) (AA G)i P −W +Xβ f AA exp(−IG β ) −W (i) (AA IG)i P − W + X β Intuitively. Somewhere in between is the lowest value of the cross validation measure and thus the best estimate. The residual values from actual data point and the fitted data point from the “leaveone-out” estimation are averaged to construct the cross validation measure. see Appendix C. due to a poor overall fit. in the second and the third case (i. See [2] for further details. This is called “leave-one-out” cross-validation. then the set of available input-output measurements can be divided into two parts: one part for training and one part for testing.where Q := X (X T X + λH )−1 X T . is to partition the original set in several different ways and to compute an average score over the different partitions. 4 The M ATLAB Code The aim of this section is to present our code.

Swap.02.KPa).options.’MaxIter’.x].value_date]=Libor_Swap_data.A]=matrix_vector_Btp(value_date.AA.AA*G. Swap=missing_Swap(Swap).-0. s=max(3. elseif flag1==1 [Libor.TT).MAT]=matrix_vector(T.KP(end).1e-9.round(sqrt(K))).KP(1). %initial value x=fminsearch(’Bspline’.value_date]=Btp_data. elseif flag1==1 [P. elseif flag2==2 %B-SPLINE + PENALTY (function d) for i=1:length(TT) for j=1:s 9 .A]=matrix_vector_LS(value_date.P.%Augmented knot points if flag2==1 %B-SPLINE (function d) for i=1:length(TT) for j=1:s G(i. %number of B-spline basis function KPa=[KP(1).KP(end).options.04.KP(1). %number of knot points + 1 KP=knot_points(s. s=length(KP)+2.T.2].MAT). end [TT.Libor.4.0.T.’TolX’. %%%%%%%%%%%%%%%%%%%%%%%%%%%%% % THIRD STEP: Curve Fitting % %%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Find the vector x of the parameters of the curve % % Options for FMINSEARCH options=optimset(’TolFun’.P).x_0.Btp).x_0. %initial value x=fminsearch(’NelsonSiegel’.TT(i).Swap).KP(end)]. x=[1.AA.KP. else %B-SPLINE METHODS K=length(MAT). end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % SECOND STEP: Building Matrices and Vectors % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % TT-> vector of global payments (time) % AA-> matrix of global payments (value) % MAT->vector of maturity dates if flag1==0 [P.1).1e8).j)=BsplineBasis(j.1e8.if flag1==0 [Btp. % if flag2==0 %NELSON-SIEGEL x_0=[0.K. %B-spline matrix end end x_0=ones(s-1.’MaxFunEvals’.1e-9.A).001.

H).4.H.options.TT(i). % initial value lambda=fminsearch(’GCV_g’.P.x].x].AA.options.4.1).options.TT(i).KPa). %initial value lambda=fminsearch(’GCV_f’. % Find x x_0=ones(s-1.IG. elseif flag2==6 %B-SPLINE+PENALTY (function f) IG=intBsplineBasis(TT.options.G. % Penalty Term % Find the optimal Penalty Parameter lambda=10^10. end end H=penalty(s. % Find x x=fminsearch(’Bspline_penalty_g’. %initial value lambda=10^3.H.TT(i).lambda).4.AA*G.G.KP.options).1).AA.P.IG.P.options).options.s. % integral of the B-spline basis H=penalty(s. 10 .j)=BsplineBasis(j.AA.options. %initial value lambda=10^3.KPa). %initial value x=fminsearch(’Bspline_penalty’. x=[1.KPa).lambda).options.x_0. end end H=penalty(s.j)=BsplineBasis(j.H.s.s. elseif flag2==3 %B-SPLINE (function g) for i=1:length(TT) for j=1:s G(i.s.x_0. %initial value lambda=fminsearch(’GCV’. % integral of the B-spline basis x_0=ones(s.KPa). x=fminsearch(’Bspline_g’.P).G(i.1). x=[0.lambda. % Find x x=fminsearch(’Bspline_penalty_f’.KPa).lambda).G.KPa).P.lambda.KPa).AA*G.H. % Penalty Term % Find the optimal Penalty Parameter x_0=ones(s.H.AA. elseif flag2==4 %B-SPLINE+PENALTY (function g) for i=1:length(TT) for j=1:s G(i. end end x_0=ones(s-1.KPa).x_0. %initial value x=fminsearch(’Bspline_f’.x_0.AA.x_0.x]. elseif flag2==5 %B-SPLINE (function f) IG=intBsplineBasis(TT.AA.KP.P.x_0.P).j)=BsplineBasis(j.options.lambda.x_0.P.IG. x=[0.1).1). % Penalty Term % Find the optimal Penalty Parameter x_0=ones(s-1.

’01/15/2007’. function [Libor.2. which uses linear interpolation for the missing Swap rates (see Section 2.100. number of coupons (for year)] % value_date=’12/19/2006’.2) ]. %initialize % difference(i)=n means that between the duration of swap i and i+1 % there are n years-> if n=1 we do nothing. which provides all the necessary data.’mat’..rate Swap=[ 1.00.duration (in months) .’mat’. .. function [Btp.99. the program calls the B TP _ DATA routine.value_date]=Libor_Swap_data % value_date=’09/21/2006’..4. clean value. maturity.’rate’. otherwise we create the 11 . function Swap1=missing_Swap(Swap) % Compute missing Swap using linear interpolation % n=length(Swap). 3.2).1). .’coupons’.’coupons’. 0.’val’. 4.1 First Step: Getting Data The first step of the M ATLAB program is to get the Btp or the Libor and Swap data. 30.end end 4.96. If instead we consider Libor and Swap. 3. struct(’n_val’.. %number of Swaps difference=zeros(n.100. the program calls the L IBOR _S WAP _ DATA routine for the necessary data and then MISSING _S WAP.Swap.37025.3. %%%%%%%%%%%%%%%%%%%%%% % Libor -> duration (in days) .’rate’.value_date]=Btp_data % Btp -> Btp struct --> [nominal value. % Btp=[ struct(’n_val’.’02/01/2037’. 3. 3.’val’.808. % rate.rate Libor=[ 1. 0... If we consider Btp.94. ].75. %%%%%%%%%%%%%%%%%%%%%% % Swap -> duration (in years) .214.04188. ].2) .

2). s2=Swap(i+1.Date.Ncoupons(1).T.2 Second Step: Building Matrices and Vectors In the second step. (daysdif(Pcoupon. end % count=0. the main program calls the MATRIX _ VECTOR _B TP routine. for i=1:n count=count+1. t=Swap1(count.1).Date).1). for i=1:n % 1-compute Previous coupon and Next coupons [Pcoupon. Swap1(count.2). for j=1:difference(i)-1 count=count+1.1).1). function [P. T=[].1)+j.1). which computes matrices A and T and the vector P (see Section 2. we build the matrix AA and the vector P necessary for the third step: the curve fitting. % DAYSDIF->Days between dates with day-count basis 3(=actual/365) 12 . following Section 2.% missing Swap/Swaps using linear interpolation for i=1:n-1 difference(i)=Swap(i+1. % 2-compute Accrued Interest AI=Btp(i).rate/100)*(1/(Btp(i). If we consider Btp.3)). Swap1(count. Swap1(count.1).Ncoupons]=PNcoupons(Btp(i).j]=jth payment of the ith Btp % Date is taken as time 0 % % Initialize Matrices and Vector n=length(Btp). % number of Btp P=zeros(n.2)=(s1*(t2-t)+s2*(t-t1))/(t2-t1).coupons))*. t1=Swap(i.1)=Swap(i. s1=Swap(i.Btp) % Compute: P -> price vector % T -> matrix of coupons’payments (time) % T[i..:)=Swap(i. A=[]. end end 4.n_val*(Btp(i).:)..j]=time to the jth payment of the ith Btp % A -> matrix of coupons’payments (value) % A[i.1)-Swap(i.A]=matrix_vector_Btp(Date.3)/daysdif(Pcoupon. t2=Swap(i+1.

%month of the maturity date cyear=year(maturity). end A(i. %number of next coupons while( daysdif(Date.Ncoupons(j).3)>0 ) %DAYSDIF->days between dates with day-count basis 3(=actual/365) count=count+1.%number of coupons paid on each year monthcoup=12/numcoup.Ncoupons]=PNcoupons(Btp.n_val.%day of the maturity date cmonth=month(maturity).cmonth.j)=daysdif(Date. end coupon=datenum(cyear.m)=A(i. %months between two coupons % coupon=datenum(maturity). A(i. while( isbusday(temp)==0 ) % if it is not a business day 13 .mat. which computes the previous coupon date and the dates of the next coupons. %DATENUM converts date string into serial date number cday=day(maturity). %pay the nominal value at maturity end The above routine calls PN COUPON. % number of next coupons for j=1:m T(i. while( isbusday(Pcoupon)==0 ) % if it is not a business day Pcoupon=Pcoupon+1. end % for i=1:count temp=coup(i).Date). if temp<=0 cyear=cyear-1.cday). cmonth=12+temp. % Compute the Previous coupon and the Next coupons date % maturity=Btp.% 3-compute P P(i)=Btp(i).coupon. coup(count)=coupon.val+AI.coupons.j)=((Btp(i).n_val. else cmonth=temp.coupons)*Btp(i).rate/100)/Btp(i). numcoup=Btp. function [Pcoupon. end Pcoupon=coupon.%next coupons’date temp=cmonth-monthcoup.m)+Btp(i).%year of the maturity date count=0. % 4-compute A and T m=length(Ncoupons).3)/365.

1)=daysdif(Date. end T(i.Libor.cday). function [P. we compute matrix A. else % if the duration is in months temp=datenum(cyear.cmonth.2). % number of Swaps cday=day(Date). cyear=year(Date). A(i.temp.1). % ascending order end In the same way. % %%%%%%%%%%% % A and T % %%%%%%%%%%% % Libor % daycount -> actual/360 (basis 2 in daysdif) for i=1:n1 % temp is the maturity of the i-th Libor if Libor(i.2).cday+Libor(i.1)>0 % if the duration is in days temp=datenum(cyear. P1 and P2 .temp=temp+1. T and vector P using the Notice that A. % %%%%% % P % %%%%% P=ones(n1+n2. if we consider Libor and Swap.2)/360.3)/100)*T(i.cmonth+Libor(i. cmonth=month(Date).1). T and P contain the elements of A1 and A2 .T. T1 and T2 . respectively (see Section 2. end % % Swap % daycount -> 30/360 (basis 6 in daysdif) for i=1:n2 14 . end Ncoupons(count-i+1)=temp. MATRIX _ VECTOR _LS. end while( isbusday(temp)==0 ) %if it is not a business day temp=temp+1.Swap) % Compute: P -> price vector % T -> matrix of payments (time) % A -> matrix of payments (value) % Date is taken as time 0 % n1=length(Libor).1)).A]=matrix_vector_LS(Date.1)=1+(Libor(i. % number of Libors n2=length(Swap).

1))=A(n1+i.1))+1.2) for j=1:size(T.6)/360. if j==1 %if it is the first payment t=T(n1+i.Swap(i.j-1). function [TT. % add a to TT count=count+1.for j=1:Swap(i. using routine MATRIX _ VECTOR. end A(n1+i.MAT]=matrix_vector(T. TT(count)=a.A) % Compute: % TT-> vector of global payments (time) % AA-> matrix of global payments (value) % MAT->vector of times to maturity % %%%%%% % TT % %%%%%% count=1. if a==0 %all the remaining element of the column are 0 break % break the j-loop else flag=0. we compute matrix AA and vector T T (see Section 2) for both the Btp case or the Libor-Swap case.2)/100)*t. end A(n1+i. end end 15 .cmonth. else t=T(n1+i.j). % a is already present in T break % break the k-loop end end if flag==0 % if the date a is not present in TT.AA.1) a=T(end+1-j. end At the end. while( isbusday(temp)==0 ) %if it is not a business day temp=temp+1.temp.j)-T(n1+i..Swap(i. end T(n1+i. % the date a is not present in TT for k=1:count-1 if TT(k)==a flag=1.j)=daysdif(Date. for i=1:size(T..j)=(Swap(i.1) % temp is the j-th payment date of the i-th Swap temp=datenum(cyear+j.cday).i).

end end TT=sort(TT). %initial value x=fminsearch(’NelsonSiegel’.3.1) for j=1:size(T. 4.2]. break.j).j)==TT(k) %if the payment date are equal AA(i.1) for j=1:size(A.3 Third Step: Curve Fitting In this last step we compute the curve’s parameters. end end end MAT=sort(MAT).j-1). break.k)=A(i. break.2) MAT(i)=T(i. 4.AA.2) if T(i. where the N ELSON S IEGEL routine is the following 16 . Notice that both the Btp case and the Libor and Swap case are considered without any distinction. elseif j==size(T.TT).0.x_0.04.001.1 Nelson Siegel Model From the MAIN routine we have x_0=[0. end end end end %%%%%%% % MAT % %%%%%%% for i=1:size(T.j).j)==0 MAT(i)=T(i. %%%%%% % AA % %%%%%% for k=1:length(TT) for i=1:size(A.2) if T(i. %Sort in ascending order.options.02.-0. elseif T(i.P.j)>TT(k) break. For further details see the examples reported in Appendix A.

x(4):= β .KP(1). end temp=P-A*t.. for i=1:length(temp) f=f+temp(i)^2. x(4) is positive. %number of knot points + 1 KP=knot_points(s. f=10^15. in the MAIN program we have K=length(MAT). elseif x(4)<=0 % x(4) must be positive. we have x(1):= α1 . Notice that.. f=0. 4. else % if x(1). for i=2:s-2 h=fix(((i-1)*k/(s-2))).KP(end). f=10^15. x(3):= α3 .. %Augmented KP where the KNOT _ POINTS routine is as follows function T=knot_points(s... s=length(KP)+2. T(i)=MAT(h)+theta*(MAT(h+1)-MAT(h)). x(3)*T(i)*exp(-T(i)/x(4))).1)=exp(-x(1)*T(i)-x(4)*(x(2)+x(3))*(1-exp(-T(i)/x(4)))+.1.KP(end)].round(sqrt(K))).T) % f is the quantity to minimize % if x(1)<=0 % x(1) must be positive. we impose f=10^15 if (8) is not satisfied. x(2):= α2 ..k. for i=1:length(T) t(i. in order to solve the constrain minimization (9). % fix(X) rounds X to the nearest integers towards zero. elseif x(1)+x(2)<=0 % x(1)+x(2) must be positive. end T(s-1)=MAT(end).3. %number of B-spline basis functions KPa=[KP(1). in order to define the basis functions...KP(end).KP(1). f=10^15.P. 17 .MAT) % Compute the knot points following McCulloch (1971) % These points are used to compute the B-spline basis functions % T(1)=0. s=max(3. theta=((i-1)*k/(s-2))-h.MAT).K.A.function [f]=NelsonSiegel(x.. end end If we consider Section 3.KP. x(1)+x(2).2 B-spline methods For the B-spline methods..

end Notice that d(0) = 1. the minimization procedure involves only the parameters β2 . For this reason. • B-spline (parametrized function: d) From the MAIN routine we have x_0=ones(s-1. · · · s.x]. Following Sections 3. for i=1:length(temp) f=f+temp(i)^2. where function [f]=Bspline(x.As in [6] and [8].1).x].2.x_0. where we recall that s is the number of basis functions (see Figure 1 and [2]). βs . x=[1. In • B SPLINEBASIS returns the value of B-spline basis functions. • B-spline with Penalty (parametrized function: d) From the MAIN routine we have 18 . DD B SPLINEBASIS INT B SPLINEBASIS returns the integrals of the B-spline basis functions: this routine is used to compute matrix IG. %initial value x=fminsearch(’Bspline’. we have to consider the minimization problem below.2. f=0. returns the value of the second derivative of the B-spline basis functions. φi (0) = 0 if i = 2. thus we have to impose β1 = 1 since 0 is the first knot point and it holds φ1 (0) = 1. · · · .P). the number of knot points (and thus the number of basis functions) depends on Appendix B we report the following routines: √ K . this routine is used to compute matrix G. • • • D B SPLINEBASIS returns the value of the first derivative of the B-spline basis functions. in order to compute the set of parameters β .options.AA*G.2. %the first term of the vector must be equal to 1 temp=b-A*x. since the present value of 1 Euro repayable in 0 years is actually 1.b) % B-spline % f is the quantity to minimize % x=[1.A.1 and 3.

P.x]. The penalty term.KPa).j)=H(i.x(t).AA*G.4). is computed by the P ENALTY routine (see Section 3.H) % GENERAL CROSS VALIDATION % Q=X*inv(X’*X+lambda*H)*X’. which is unique for each B-spline with penalty method. % Penalty Term % Find the optimal Penalty Parameter lambda=10^10. %initial value lambda=fminsearch(’GCV’.w]=gauleg(KPa(k+3).lambda.X.KPa).options.2.KPa) % Compute the Penalty Term % K=length(KPa)-6. %identity matrix of size n T=(I-Q)*Y. H=sparse(s. end for i=1:s for j=1:s H(i. for k=1:K-1 [x.Y.H=penalty(s. and 19 .H).options.s).lambda). %initial value x=fminsearch(’Bspline_penalty’. I=eye(n).KPa(k+4). end end end end For the GAULEG routine. function H=penalty(s.j)+w(t)*f(i)*f(j). % Find x x_0=ones(s-1. n=length(Y).AA*G.4.x_0. for t=1:length(x) f=[]. x=[1.H.1). which computes the nodes and weights of the Gauss-Legendre quadrature formula. theta=2.2).5. see Appendix B. f=((T’)*T)/((n-theta*trace(Q))^2). for i=1:s f(i)=ddBsplineBasis(i. The minimization procedures call the following routines: function f=GCV(lambda.P.

Pi=C*exp(-F*x).lambda).AA.P.F.G.AA. Notice that we have imposed β1 = 0 since g (0) = 0 implies d(0) = 1 (see (11) ).b_0.lambda.lambda).b_0. As above.lambda) % f is the quantity to minimize % x=[1.x]. temp=AA*G. where function [f]=Bspline_g(x.P.H.x].H.P.G. % Penalty Term % Find the optimal Penalty Parameter x_0=ones(s-1. • B-spline (parametrized function: g ) From the MAIN routine we have x_0=ones(s-1.H.x].b) % f is the quantity to minimize % x=[0. · · · .x]. • B-spline with Penalty (parametrized function: g ) From the MAIN routine we have H=penalty(s.options) % GENERAL CROSS VALIDATION % if lambda>0 b=fminsearch(’Bspline_penalty_g’. W=AA*exp(-G*b).AA.options.s.x_0. the second minimization procedure involves only the parameters β2 .options.options).s. where function e=GCV_g(lambda.G.P. x=fminsearch(’Bspline_g’. x=[0.A.b].1).1).options.KPa).AA. % initial value lambda=fminsearch(’GCV_g’.%the first term of the vector must be equal to 1 f=((b-A*x)’)*(b-A*x)+lambda*((x’)*H*x). b=[0.H. %initial value lambda=10^3.x_0. 20 . x=[0.C. f=((b-Pi)’)*(b-Pi).AA.b. % Find x x=fminsearch(’Bspline_penalty_g’.G.x_0.H. βs .function [f]=usa_Bspline_penalty(x.options.G.P).

end and function [f]=Bspline_penalty_g(x.x_0.lambda) % f is the quantity to minimize % x=[0.lambda. e=GCV(lambda.1). Pi=C*exp(-F*x).SPLINE _ PENALTY _ G in a minimization procedure. %initial value lambda=10^3.IG.s. where function [f]=Bspline_f(x. Moreover. %initial value x=fminsearch(’Bspline_f’.options. Notice that GCV_ G call B.Xg. · · · .x].:)=(-W(i)).H.options.options).options.H. βs . • B-spline (parametrized function: f ) From the MAIN routine we have x_0=zeros(s.lambda).F.F.for i=1:length(W) Xg(i. We have H=penalty(s.P.H.x_0.1).H).IG.AA.b) % f is the quantity to minimize % Pi=C*exp(-F*x).C. f=((b-Pi)’)*(b-Pi).:)).IG. • B-spline with Penalty (parametrized function: f ) This case is similar to the B-spline with Penalty (parametrized function: g ) case. f=(((b-Pi)’)*(b-Pi))+lambda*((x’)*H*x). · · · .AA. thus we do not impose any restriction to β1 and the minimization procedure involves β1 . as above.AA. end Yg=P-W+Xg*b.C. % Penalty Term % Find the optimal Penalty Parameter x_0=ones(s.KPa). we have imposed β1 = 0 and the minimization procedures involve only parameters β2 . Notice that (13) implies d(0) = 1.P). %initial value lambda=fminsearch(’GCV_f’.x_0. 21 .b.P. else %constrain: lambda>0 e=10^12. % Find x x=fminsearch(’Bspline_penalty_f’.*(temp(i.Yg. βs .

while if we consider the residual. for i=1:length(W) Xf(i. the B-spline methods with penalty (g ) and (f ) have a great computational cost: this 22 . else %constrain: lambda>0 e=10^12. 5.F.b_0. for this last class of methods. and using M ATLAB 7. Numerical experiments have been carried out on a Personal Computer with a Pentium IV (2. temp=X*IG. which does not depend on the fitting model considered.lambda). 5 Numerical Results The aim of this section is to present the models’performances. Moreover. we also report the best λ computed with the GCV procedure.options) % GENERAL CROSS VALIDATION % if lambda>0 b=fminsearch(’Bspline_penalty_f’. which are Italian coupon bonds with semi-annual payments. First of all we consider the computational cost: the “Building Matrices and Vectors” step..H.:)).e. end and function [f]=Bspline_penalty_f(x. i.b_0.:)=(-W(i)).IG. considering both computational cost and approximation results.e.40 Ghz) processor and 768 MB of RAM.H).0 (R14).s.Xf.b.5 seconds.P.lambda) % f is the quantity to minimize % Pi=C*exp(-F*x). the best choice seems to be the B-spline method (f ). the “Curve Fitting” steps) for each method considered. e=GCV(lambda.. In the numerical experiments we have considered the Btp quotation with value date 19th December 2006 (see Table 2). it seems that the best choice from a computational point of view is the B-spline model with the parametrization of the discount function.H.options. end Yf=P-W+Xf*b. we parametrize the instantaneous forward rate function f using B-spline basis functions.IG. W=X*exp(-IG*b). in the same table we present the residuals |E| of the minimization problems (9) for the Nelson Siegel model.1 Btp Case In this section we deal with the Btp case. From this table. while in Table 3 we report the elapsed time of the third step of the MAIN routine (i.X. takes approximately 8.H.X.C. (15) for the B-spline methods. Moreover. f=(((b-Pi)’)*(b-Pi))+lambda*((x’)*H*x). and (16) for the B-spline with penalty methods.where function e=GCV_f(lambda.Yf.P.*(temp(i.

τ and of the instantaneous forward rate function f (Figure 4).27 98.19 110.6 98.2.44 101.75 100 5.25 100 3.89 99.57 105.50 100 3.22 102.00 100 6.25 4.69 98.471925 Bspline+Penalty (f ) 80.50 5.17 99. which call another minimization procedure at each step (see Section 3.75 9.062 seconds 1.50 100 Clean Price 105.437 seconds 0.75 5.00 100 2.25 6.75 5.234 seconds 0.91 101.25 3.50 100 3.75 100 4.25 97.00 Table 3: Btp Data: Timing and Residual Curve Fitting Residual Best λ (elapsed time) Nelson Siegel 0.00 100 4.00 100 2.50 100 3.599565 Bspline+Penalty(d) 0.560438 1273.50 3.24 96.75 3.59 106.94 96.3 Expiration 08/01/2011 09/15/2011 02/01/2012 02/01/2013 08/01/2013 08/01/2014 02/01/2015 08/01/2015 08/01/2016 08/01/2017 02/01/2019 02/01/2020 08/01/2021 11/01/2023 11/01/2026 11/01/2027 11/01/2029 05/01/2031 02/01/2033 08/01/2034 02/01/2037 Annual Rate (%) 5.78 101.00 5.25 4.73 Expiration 01/15/2007 02/01/2007 03/01/2007 06/01/2007 07/01/2007 10/15/2007 11/01/2007 01/15/2008 02/01/2008 05/01/2008 06/15/2008 09/15/2008 04/15/2009 02/01/2009 05/01/2009 06/15/2009 11/01/2009 01/15/2010 06/15/2010 11/01/2010 03/15/2011 Table 2: Btp Annual Rate Value (%) 2.406 seconds 1.50 100 2.75 4.18 127.641775 52148137.91 99.531 seconds 0. Then we show the plots of the discount function d (Figure 2).50 100 3.77 98.09 124.75 100 5.197764 Bspline (g ) 0. of the spot rate function h (Figure 3).31 104.27 112.858068 15442.81 102.92 101.36 100.16 158.75 100 6. which are important in order to understand the behavior of the different data-fitting techniques: 23 .00 7.7 101.25 3.00 4.03 98.2). where h(τ ) := − log (d(τ )) .42 131.96 104.25 4.63 98.313 seconds 0.7 115.058905 Bspline (d) 0.75 5.00 4.00 100 3.50 100 3.6 101 101.78 141.18 99.6 99.544416 Bspline+Penalty (g ) 28.Value 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 Clean Price 99.00 100 3.37 98.922 seconds 0.94 100.75 100 5.527466 Method is due to the GCV-minimization procedure.00 100 6.75 100 4.74 96.31 102.25 4.437583 Bspline (f ) 3.25 6. These figures suggest the following observations.

037 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 0.6 0.042 0.04 0.7 0.038 0.3 0.043 0.036 0 5 10 15 20 25 30 24 .039 0.Figure 2: Btp Case Discount Function 1 0.9 0.4 0.2 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 0 5 10 15 20 25 30 Figure 3: Btp Case Spot Rate 0.5 0.041 0.8 0.

038 0.Figure 4: Btp Case Instantaneous Forward Rate 0.2 0 −0.8 0 25 .046 0.034 0 5 10 15 20 25 30 Figure 5: Btp Case Comparison 0.2 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 5 10 15 20 25 30 35 40 −0.4 0.044 0.6 −0.6 0.04 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 0.4 −0.042 0.036 0.

947 30 Rate (%) 3. Notice that the elapsed time for the “Building Matrices and Vectors” step is approximately 3. 26 . respectively. 42.892 15 3.2 Libor and Swap Case In this section we present the same data as above.218 4. Finally we compare our models with the initial data.06275 2 months 3. Table 4: Libor Rate (%) Duration (months) 3. 7 and 8 we show the plots of the discount function.202 4. as in the Btp case. where 42 is the number of Btp considered. These figures suggest the following observations: • discount function in Figure 6 the plots are really similar. the best choices seem to be the Nelson-Siegel and the B-spline with penalty (f ) models.04188 1 month 3. Moreover.06563 3 months Duration (days) 1 day 7 days 15 days Rate (%) 3. this behavior is due to the large penalty parameter computed with the GCV procedure (see Table 3). • instantaneous forward rate function the plots presented in Figure 4 are quite different. More precisely.011 4.214 As above. considering Libor and Swap rates with value date 21th September 2006 (see Tables 4 and 5).19325 3. i = 1.973 4. of the spot rate function and of the instantaneous forward rate function. let P be the vector containing the prices of the considered Btp at the value date. • spot rate function from Figure 3 it seems that the Nelson Siegel and the B-spline with penalty (f ) models behave quite different from the other methods after the 15-years point.808 8 3. respectively. 5.88 9 3.066 4.883 10 3. · · · . since for the other methods the plotted curves decrease too rapidly after the 20-years point (see [3] for further details).37025 Duration (year) 1 2 3 4 5 6 7 Table 5: Swap Rate (%) Duration (year) 3.906 20 3. then in Figure 5 we plot the values P (i) − P (i).2935 3. in Figures 6.029 4.5 seconds. If we consider the last method. The results are in agreement with the Btp case. in Table 6 we compare the different methods considering both the computational cost and the residuals |E|. computed using the discount function obtained with a data-fitting technique.• discount function from Figure 2 it seems that the different methods for curve construction produce very similar plots.925 25 3.

043 0.6 0.04 0.7 0.036 0 27 .5 0.041 0.037 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 5 10 15 20 25 30 0.042 0.4 0.8 0.038 0.2 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 0 5 10 15 20 25 30 Figure 7: Libor and Swap Case Spot Rate 0.9 0.039 0.3 0.Figure 6: Libor and Swap Case Discount Function 1 0.

04 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 5 10 15 20 25 30 0.038 0.034 0 Figure 9: Libor and Swap Case x 10 4 −3 Comparison 2 0 −2 Nelson−Siegel Bspline (d) Bspline + Penalty (d) Bspline (g) Bspline + Penalty (g) Bspline (f) Bspline + Penalty (f) 5 10 15 20 25 30 35 40 −4 −6 −8 0 28 .048 0.046 0.044 0.036 0.042 0.Figure 8: Libor and Swap Case Instantaneous Forward Rate 0.

as in the Btp case) behaves like the Nelson Siegel model: this is due to the different penalty parameter value. Following [2]. in Figure 9 we compare our models with the initial data.875 seconds 0.205400 Bspline (f ) 0. if we consider the residual.012356 22. using few knot points. Notice that in [2] the authors use a great number of knot points and thus the penalty term is necessary in order to obtain well-behaved curves.006086 1. since the classical B-spline methods produce curves which increase or decrease too rapidly after the 20-years points. From the numerical experiments. the best choice is to use smoothing splines which parametrized the instantaneous forward rate function f : this result is in agreement with [2]. since in the Btp case the largest parameter is the one of the B-spline with penalty (f ) method. 6 Conclusion In this paper we have shown how to fit smoothing splines or exponential curves (like the Nelson Siegel model) to the term structure of interest rates. if we are interested in the instantaneous forward rate function.Table 6: Libor and Swap Data: Timing and Residual Method Curve Fitting Residual Best λ (elapsed time) Nelson Siegel 0. 29 . Finally.920 seconds 0. In fact.828 seconds 0. comparing the different models.011997 Bspline (d) 0.125 seconds 0.089915 Bspline (g ) 0.157 seconds 0.006932 Bspline+Penalty(d) 0. while in the Libor and Swap case it is the one computed for the B-spline with penalty (g ) model (see Table 6).011889 4. we have also presented methods with a penalty term: these methods behaves better than the classical B-spline methods if we consider the instantaneous forward rate function. it is clear that it is not possible to find the best model: the choice of which method to use is subjective and need to be decided on a case by case basis. • instantaneous forward rate function Figure 8 suggests that the best plots are the ones derived from the Nelson Siegel and the B-spline with penalty methods. We have described the M ATLAB code and we have presented numerical results. but they are characterized by high computational costs due to the GCV procedure in order to compute the best penalty parameter.005384 Bspline+Penalty (f ) 44. the best choice seems to be the Nelson Siegel model. as in the previous subsection.419115 • spot rate function from Figure 7 it seems that the B-spline with penalty (g ) method (and not the B-spline with penalty (f ). Moreover. while in this paper we have followed [6] and [8].007672 Bspline+Penalty (g ) 18.157 seconds 0.360 seconds 0.

94 01/15/2007 99. 75 1 . = 1. computed according to the actual/365 day-count convention.5 1. the facial value multiplied by the semi-annual interest rate.1.e. • C1 = 100 2. we have • m1 = 1.1 Btp Case In this section we consider the Btp quotation with value date 19th December 2006 reported in Table 7. 75     2.75 3.375 0 0    101..00 2.    01/16/07 06/01/07 01/16/07 05/01/07 06/15/07     01/15/08    05/01/08  06/16/08 07/16/07 11/01/07 12/17/07 where we recall that T contains the days between the value date and the payment dates of the Btp BP . i. there is only 1 payment date for the first bond.e. Now we can compute vector T T . A.375.7 06/01/2007 99.5 102..25 Moreover.18 06/15/2008 Facial Value 100 100 100 100 100 Annual Interest Rate (%) 2.e. i.63 05/01/2008 98.50 We now consider the first bond of Table 7: following Section 2. we have     BP =     and thus     T =    28 164 28 133 178 0 0 0 0 209 392 317 499 363 545     .25 1. Table 7: Btp Clean Price Expiration 99.A Building Matrices and Vectors: Examples The aim of this section is to explain Sections 2 and 4.00 3. i. the vector containing all the non-zero elements of T TT = 28 133 164 178 209 317 30 363 392 499 545 . i. 75 101 . A= 1 .e.77 01/15/2008 101.75% 2 Reasoning in the same way for all the elements of Table 7.25 101.50 5..5  0 0   .2 with two examples: one for the Btp case and the other for the Libor and Swap case. .5   2. we have   101. the facial value. • F1 = 100.

375 0 0 0 0 0 0 0 0 0   0 0 101.    102. which is based on the US calendar (that is why the 15th January 2007 is considered as holiday and thus the payment date of the first bond is 16th January 2007).88 3 years 3.041667 .5 0  0 0 0 1.25 0 0 1. 75 0 0 101 . following Section 2.25     . respectively.207472527473 A.and thus we define matrix AA as follows  101.    Notice that in the M ATLAB code (see Section 4) both the elements of T and T T are computed in years and not in days.04188 7 days 3.848351648352     P =  101. 75 0 0 0 1 . if we sum the clean prices and the accrued interests. 75 0 0   0 2.808 2 years 3.25 0 0 101. 0.0306563 .019444 31 .5 0 0 102.06275 15 days 3. 0.0306275 0.252240437158  .06563 Table 9: Swap Duration Interest Rate (%) 1 year 3. we have used the M ATLAB function ISBUSDAY. we consider the Libor case: from Table 8.002778 0. . Table 8: Libor Duration Interest Rate (%) 1 day 3.883 First of all. we have LP = T1 = 09/22/06 09/28/06 10/06/06 0. Finally.292983425414  98. in order to compute the payment dates (and thus BP ).104617486339    99.2. we obtain LR = LM = thus. we have to check if each date is a business day: in our code. we obtain   101.5 0 0 0 0 0 0 0  AA =  1 .5 0 0 0 2.0304188 1 7 15 0.2 Libor and Swap Now we consider the Libor and Swap interest rates with value date 21th September 2006 reported in Tables 8 and 9. Moreover.

038722      .and so A1 = 1. we have   1 0 0   T2 =  1 2.000084 1. Now we consider the Swap case: from Table 9 we obtain SR = and  09/21/07   SP =  09/21/07 09/22/08 .2) the elements of A1 and A2 are stored in a unique matrix A   1.038938 1. while the elements of T1 are computed in years. the vector containing all the non-zero elements of T1 and T2 TT = 0.002778 3 .0388  1. we have to check if each date is a business day.000084 0 0 0 0 0  0 1. otherwise we have to look for the next business day: this is the reason why T2 (2. Finally.019444 0. using the actual/360 day-count convention.000595     1..002778. 0.03883 If we now compute vector T T .e. i.0388 0.038908 0 .038938 1.002778 3 We recall that in order to compute the payment dates.002778 0.03808 0. notice that in this example the elements of LM are computed in days.2.002778 0  . we can define matrix AA as follows  1.000595 0 0 0 0    0 0 1 .038722 0. 09/21/07 09/22/08 09/21/09  Recalling that the value date is 09/21/06.03883 0.001278 .0388 1. A=  0 0  1. we calculate matrix A2   1.000595 1. using the 30/360 day-count convention.041667 1 2.03883 0. following Section 2.03883 0. 1 2.038908 0 0. 001277 0 0 0 AA =   0 0 0 1. 2) = 2.     Notice that in the MATRIX _ VECTOR _LS routine (see Section 4.03808 0 0   A2 =  0.03808 0 0    0 0 0 0.038938 1.0388 1.03808     0.001278  0 0  .038908 0 0 0 0 0.000084 0 0   0 0  1.038722 32 .

in the same way.x.1 B SPLINE BASIS function F=BsplineBasis(k.r. 2.002778 0  1 2.002778 0 0   0 0   0. B. 3.d) % B-spline Basis Function % k -> consider the kth basis function % r -> r-1 is the degree of the spline % x -> is the the point in which I evaluate the function % d -> augmented knot points % if (k+r-1<5 & k<5) if (x<d(5)) F=((d(5)-x)/(d(5)-d(1)))^(r-1). DD B SPLINEBASIS INT B SPLINEBASIS GAULEG computes the nodes and weights of the Gauss-Legendre quadrature formula. we present the following routines: 1. B SPLINEBASIS computes the value of B-spline basis functions. end elseif (k+1>length(d)-4 & k+r>length(d)-4) if (x>=d(end-4)) F=((x-d(end-4))/(d(end)-d(end-4)))^(r-1). else F=0. the elements of T1 and T2 are stored in T as follows   0. end else if r==1 if (x>=d(k) & x<d(k+1)) F=1.002778 3 B Bspline Routines In this section.019444    0.  T = 1 0 0       1 2. returns the integrals of the B-spline basis functions. else 33 . else F=0. 5. computes the value of the second derivative of the B-spline basis functions. 4.041667 0 0  . D B SPLINEBASIS computes the value of the first derivative of the B-spline basis functions.

r-1.x.x..r-1. F=((f1*(x-d(k)))/(d(k+r-1)-d(k)))+.r-1.r-1. else F=0.x.2 D B SPLINE BASIS function F=dBsplineBasis(k. F=((f1+(x-d(k))*d1)/((d(k+r-1)-d(k))))+..d). f2=BsplineBasis(k+1. ((f2*(d(k+r)-x))/(d(k+r)-d(k+1))).x.x. ((-f2+(d(k+r)-x)*d2)/(d(k+r)-d(k+1))). end end B. end else f1=BsplineBasis(k.d).d)..d).F=0. end elseif (k+1>length(d)-4 & k+r>length(d)-4) if (x>=d(end-4)) F=(r-1)*(((x-d(end-4))/(d(end)-d(end-4)))^(r-2))*.. (1/(d(end)-d(end-4))).d). d2=dBsplineBasis(k+1.r-1.r-1.x. end else f1=BsplineBasis(k.x. else F=0..d) % First Derivative of B-spline Basis Function % k -> consider the kth basis function % r -> r-1 is the degree of the spline % x -> is the vector of the points in which I evaluate the function % d -> augmented knot points (necessary to define the basis function) % if r<2 F=0.d). else if (k+r-1<5 & k<5) if (x<d(5)) F=(r-1)*(((d(5)-x)/(d(5)-d(1)))^(r-2))*(-1/(d(5)-d(1))).r. end end end 34 . d1=dBsplineBasis(k.. f2=BsplineBasis(k+1.

d2=dBsplineBasis(k+1.r-1. end elseif (k+1>length(d)-4 & k+r>length(d)-4) if (x>=d(end-4)) F=(r-1)*(r-2)*(((x-d(end-4))/(d(end)-d(end-4)))^(r-3))*.d)..s..d) % Second Derivative of B-spline Basis Function % k -> consider the kth basis function % r -> r-1 is the degree of the spline % x -> is the vector of the points in which I evaluate the function % d -> augmented knot points (necessary to define the basis function) % if r<3 F=0.3 DD B SPLINE BASIS function F=ddBsplineBasis(k. ((-1/(d(5)-d(1)))^2).j]=int_{0}^{T(i)} B(j) dx % where B(j) is the jth basis function % KP -> Knot Points % d -> Augmented Knot Points % IG=zeros(length(T). else F=0.d).r-1. else F=0.4 INT B SPLINE BASIS function IG=intBsplineBasis(T...r-1.r.KP.. dd2=ddBsplineBasis(k+1.x.x. dd1=ddBsplineBasis(k.B. F=((d1+(x-d(k))*dd1+d1)/((d(k+r-1)-d(k))))+. ((-d2+(d(k+r)-x)*dd2-d2)/(d(k+r)-d(k+1))).x.d).x.s).d) % Integral of the cubic B-spline basis function % IG[i.x.r-1. end else d1=dBsplineBasis(k.d). for i=1:length(T) 35 .. else if (k+r-1<5 & k<5) if (x<d(5)) F=(r-1)*(r-2)*(((d(5)-x)/(d(5)-d(1)))^(r-3))*. ((1/(d(end)-d(end-4)))^2). end end end B.

z=z1-p1/pp.n) % Gauss-Legendre quadrature formula % Computes the Gauss-Legendre nodes xx and weights ww % of order n for the interval (a. xl=0. 36 .25)/(n+0. else t=T(i).d).j)+temp*w(jj).0)*p3)/j. p1=((2.x(jj).b) % m=(n+1)/2.0.5*(b-a).4).t.w]=gauleg(KP(ii).5 GAULEG function [xx. if (abs(z-z1)<eps). for j=1:s for jj=1:length(x) temp=BsplineBasis(j.5*(b+a). for i=1:m z=cos(pi*(i-0. xm=0.0*j-1.j)=IG(i. while 1 % do untill break p1=1. end end xx(i)=xm-xl*z.0).0)*z*p2-(j-1.0. xx(n+1-i)=xm+xl*z. end end else %the integral between 0 and T(i) has already been computed break %stop the ii-loop and change the value of i end end end B. z1=z. end pp=n*(z*p1-p2)/(z*z-1.KP(ii+1)). break. p2=p1. end [x. p2=0.5)). for j=1:n p3=p2.4.b.for ii=1:length(KP) if T(i)>KP(ii) if ii<length(KP) t=min(T(i). IG(i.ww]=gauleg(a.

[Scalar or Vector] of dates. The year will default to the current year. Enter dates as serial date numbers or date strings. Date strings with 2 character years are interpreted to be within the 100 years centered around the current year.D]) return the serial date numbers for corresponding elements of the Y. Y. will default to January for the month. Optional Inputs: Basis . D2.Serial date number N = DATENUM(S) converts the string or date vector S into a serial date number.D (year.Days between dates for any day count basis DAYSDIF returns the number of days between D1 and D2 using the given day count basis.M. Examples: n = datenum(’19-May-2000’) returns n = 730625. C. Date numbers are serial days where 1 corresponds to 1-Jan-0000. N = DATENUM(Y. D2) D = daysdif(D1.D) and N = DATENUM([Y.day) arrays.0*xl/((1.M.act/365 (Japanese) C.ww(i)=2. D = daysdif(D1. M. In those cases.month. and D must be arrays of the same size (or any can be a scalar). ww(n+1-i)=ww(i). Basis) Inputs: D1 .2 DATENUM . end C M ATLAB Routines The following informations are taken from the M ATLAB [13] manual. minutes and seconds.0-z*z)*pp*pp).1 DAYSDIF .30/360 (ISDA compliant) 6 . Certain formats may not contain enough information to compute a date number.30/360 (PSA compliant) 5 .[Scalar or Vector] of dates.30/360 (European) 7 . 37 . D2 .[Scalar or Vector] of day-count basis. and 1 for the day of month.M. Valid Basis are: 0 = actual/actual (default) 1 = 30/360 2 = actual/360 3 = actual/365 4 . missing values will in general default to 0 for hours.

TolFun.0) returns n = 731204.4 ISBUSDAY .18.12.HOL. MaxFunEvals.FVAL.. Possible values of EXITFLAG and the corresponding exit conditions are: 1 . created with the OPTIMSET function.3 FMINSEARCH .EXITFLAG] = FMINSEARCH(.a user-defined vector of holidays. n = datenum(2001. Outputs: T = . MaxIter.. WEEKEND .Multidimensional unconstrained nonlinear minimization X = FMINSEARCH(FUN.5 OPTIMSET . Optional Inputs: HOL .. 0 . at X.Create/alter OPTIM OPTIONS structure OPTIONS = OPTIMSET(’PARAM1’.0. [X.) returns the value of the objective function.WEEKEND) Inputs: D . The first element of this vector corresponds to Sunday. C..X0) starts at X0 and attempts to find a local minimizer X of the function FUN. [X.a vector of dates in question. containing 0 and 1.Maximum number of function evaluations allowed [ positive integer ] 38 . vector or matrix. The default is Saturday and Sunday weekend. Thus.m).X0. FMINSEARCH uses the Nelder-Mead simplex (direct search) method.a vector of length 7.) returns an EXITFLAG that describes the exit condition of FMINSEARCH. The default is a predefined US holidays (in holidays. -1 .’PARAM2’. and OutputFcn.19) returns n = 731204.Algorithm terminated by the output function.True for dates that are business days T = ISBUSDAY(D.) creates an optimization options structure OPTIONS in which the named parameters have the specified values. OPTIMSET PARAMETERS for MATLAB: Display ..VALUE2. FUN accepts input X and returns a scalar function value F evaluated at X.12. C. TolX.Maximum number of function evaluations or iterations reached. X0 can be a scalar. C. FunValCheck. when Saturday and Sunday are weekend then WEEKDAY = [1 0 0 0 0 0 1]. with the value of 1 to indicate weekend day(s)..Level of display [ off | iter | notify | final ] MaxFunEvals .75. FMINSEARCH uses these options: Display.n = datenum(2001. described in FUN. X = FMINSEARCH(FUN.FVAL]= FMINSEARCH(..VALUE1.FMINSEARCH converged to a solution X. Any unspecified parameters are set to [] (parameters with value [] indicate to use the default value for that parameter when OPTIONS is passed to the optimization function).1 if D is business day and 0 if otherwise.19.OPTIONS) minimizes with the default optimization parameters replaced by values in the structure OPTIONS.

“Long-Term Behaviour of Yield Curves”. FINCAD News. [4] M. [2] M.M. References [1] Revisiting “The Art and Science of Curve Building”. Hagan. “A Comparison of Yield Curve Estimation Techniques Using UK Data”. Vol. R. G. 2000.K.G. “Measuring the Term Structure of Interest Rate”. Fong.H. Board of Governors of the Federal Reserve System (U. The Journal of Finance. Vol. Vol. Quarteroni. [10] A.K. 60-4. Interest Rate Risk Modeling: The Fixed Income Valuation Course. Beliaeva. [9] C. McCulloch. “Term Structure Modeling Using Exponential Splines”. [13] www. Sacco.F. 1971. [7] J. [5] J. West. Vol. Journal of Business. “Fitting the Term Structure of Interest Rates with Smoothing Splines”. D.com 39 . G. 2000. N. 1987.Name of installable output function [ function ]. Vol. Springer.F. XLIV. [8] S. Numerical Mathematics. This output function is called by the solver after each iteration. 1982.H. C. “The Tax-Adjusted Yield Curve”. Soto. H. 13-2. Finance and Economics Discussion Series. 2003. A. The Journal of Finance. Nawalkha. Vasicek.Maximum number of iterations allowed [ positive scalar ] TolFun . Saleri. Vol. “Parsimonious Modeling of Yield Curves”. [11] A. 23-1. F.R.mathworks. 1994. James. D. 2006.MaxIter . Interest Rate Modelling. December 2005 Issue. Nelson. Webber. Ioannides. XXXVII-2. Vol.). Journal of Business. [6] J.Check for invalid values. John Wiley & Sons. XXX-3. from user-supplied functions [ off | on ] OutputFcn . 1975.R. Nelson. Siegel. such as NaN or complex. [3] P. 2005. Nychka. 95-1. Vol. Zervos. “Interpolation Methods for Curve Construction”. John Wiley & Sons.Termination tolerance on X [ positive scalar ] FunValCheck . McCulloch. Applied Mathematical Finance. Siegel.A. [12] O.Termination tolerance on the function value [ positive scalar ] TolX . Journal of Financial and Quantitative Analysis. 1988. Journal of Banking & Finance. N.S. Fisher. 27.

Sign up to vote on this title
UsefulNot useful