Example.jl

Paul.Soderlind@unisg.ch, April 2002, to Julia Nov 2015


In [1]:
using Dates, Optim

include("jlFiles/BondPricePs.jl")
include("jlFiles/BondNSxPs.jl")
include("jlFiles/printmat.jl")


ytmLoss  = 0           #0: mimimize squared price errors: 1: minimize squared ytm errors
  weightLoss = 1       #1: 1/mat as weights for (fitted price-price)^2 in loss fn (only if ytmLoss==0)


Out[1]:
1

In [2]:
#Swedish bond data for 29 Dec 1993

#coupons in %/yr
c  = [ 0;        0;         0;         0;         11.5;      10.75;
       11;       13;        10.25;     6;         9 ]

#time to maturity in year
tm = [ 0.00274;  0.21096;   0.46027;   0.88219;   1.67397;   3.06849;
       5.06301;  7.46027;   9.34795;   11.11507;  15.30685 ]

#interest rates.  Bills: simple rates in %/yr; bonds: yield to maturity in %/yr
y  = [ 7.75;     6.835;    6.655;     6.41;      6.215;     6.195;
       6.41;     6.755;    7.01;      7.21;      7.325 ]

n = length(y)       #number of bonds


Out[2]:
11

In [3]:
#transform the data

c  = c/100 # -> column vector, coupons and yields as 0.05 rather than 5
y  = y/100

vvc    = c .== 0                #if bill, change from simple to effective rate
y[vvc] = (1 .+ tm[vvc].*y[vvc]).^(1.0./tm[vvc]) .- 1

P = fill(NaN,length(y))         #calculate bond prices
for i = 1:n
  local ti
  ti   = mod(tm[i],1):tm[i]
  P[i] = BondPricePs(y[i],c[i],ti)
end

In [4]:
#estimating parameters in extended Nelson&Siegel model, restricted so b0 + b1 = log(1+y[1])

parX0 = [0.1045;-0.03;-0.0562;1.2;0;0.5]       #starting guess

if ytmLoss == 1                                #loss(ytm)
  NSXbR = BondNSxEstPs(parX0,y,tm,c,log(1+y[1]),1)
else
  if weightLoss == 1                           #loss(P/maturity)
    NSXbR = BondNSxEstPs(parX0,P,tm,c,log(1+y[1]),0,1.0./tm)
  else                                         #loss(P)
    NSXbR = BondNSxEstPs(parX0,P,tm,c,log(1+y[1]))
  end
end

println("\nParameter estimates: ")
printTable(NSXbR,[""],["b0","b1","b2","tau","b3","tau2"])


Parameter estimates: 
              
b0       0.081
b1      -0.003
b2      -0.062
tau      1.634
b3      -0.020
tau2     0.175


In [5]:
ytmx = fill(NaN,n)            #model implied ytm
for i = 1:n
  local ti,d,Qx
  ti      = mod(tm[i],1):tm[i]
  d       = BondNSxPs(ti,NSXbR...)[3]    #... expands into NSXbR[1],NSXbR[2],...
  Qx      = sum(d.*c[i]) + d[end]     #model implied bond price
  #println(ti)
  ytmx[i] = BondYieldToMatPs(Qx,c[i],ti,1,1,0.05,1e-7)[1]
end

println("\nActual and model ytm, %: ")
printTable([y ytmx]*100,["ytm actual","ytm model"],string.(tm),width=15,cell00="maturity")


Actual and model ytm, %: 
maturity     ytm actual      ytm model
0.00274           8.057          8.035
0.21096           7.022          7.073
0.46027           6.775          6.718
0.88219           6.434          6.468
1.67397           6.215          6.216
3.06849           6.195          6.167
5.06301           6.410          6.439
7.46027           6.755          6.765
9.34795           7.010          6.985
11.11507          7.210          7.187
15.30685          7.325          7.346


In [6]:
#calculate model implied rates (spot, forward, yield to maturity) to plot

tmFig      = [1e-8;0.1:0.1:16]           #maturities to plot
(shx,fhx,) = BondNSxPs(tmFig,NSXbR...)
shx        = exp.(shx) .- 1     #effective interest rate
fhx        = exp.(fhx) .- 1

println("\nmodel spot and forward rates, %: ")
printTable([shx fhx]*100,["spot","forward"],string.(tmFig),width=15,cell00="maturity")


model spot and forward rates, %: 
maturity           spot        forward
1.0e-8            8.057          8.057
0.1               7.443          6.994
0.2               7.100          6.590
0.3               6.900          6.437
0.4               6.774          6.363
0.5               6.686          6.306
0.6               6.618          6.245
0.7               6.560          6.178
0.8               6.508          6.110
0.9               6.460          6.045
1.0               6.415          5.987
1.1               6.374          5.938
1.2               6.336          5.900
1.3               6.301          5.871
1.4               6.270          5.854
1.5               6.241          5.846
1.6               6.217          5.848
1.7               6.195          5.858
1.8               6.177          5.875
1.9               6.162          5.899
2.0               6.149          5.929
2.1               6.139          5.964
2.2               6.132          6.003
2.3               6.128          6.047
2.4               6.125          6.093
2.5               6.125          6.143
2.6               6.127          6.194
2.7               6.130          6.247
2.8               6.135          6.302
2.9               6.142          6.358
3.0               6.150          6.415
3.1               6.160          6.472
3.2               6.170          6.529
3.3               6.182          6.586
3.4               6.195          6.643
3.5               6.208          6.700
3.6               6.223          6.756
3.7               6.238          6.811
3.8               6.253          6.866
3.9               6.270          6.920
4.0               6.287          6.973
4.1               6.304          7.025
4.2               6.322          7.075
4.3               6.340          7.125
4.4               6.358          7.173
4.5               6.377          7.220
4.6               6.395          7.266
4.7               6.414          7.311
4.8               6.433          7.354
4.9               6.453          7.396
5.0               6.472          7.437
5.1               6.491          7.476
5.2               6.510          7.515
5.3               6.529          7.552
5.4               6.549          7.587
5.5               6.568          7.622
5.6               6.587          7.655
5.7               6.606          7.687
5.8               6.624          7.718
5.9               6.643          7.748
6.0               6.662          7.777
6.1               6.680          7.804
6.2               6.698          7.831
6.3               6.716          7.857
6.4               6.734          7.881
6.5               6.752          7.905
6.6               6.770          7.927
6.7               6.787          7.949
6.8               6.804          7.970
6.9               6.821          7.990
7.0               6.838          8.009
7.1               6.854          8.027
7.2               6.871          8.045
7.3               6.887          8.062
7.4               6.903          8.078
7.5               6.918          8.093
7.6               6.934          8.108
7.7               6.949          8.122
7.8               6.964          8.136
7.9               6.979          8.148
8.0               6.994          8.161
8.1               7.008          8.173
8.2               7.022          8.184
8.3               7.036          8.194
8.4               7.050          8.205
8.5               7.064          8.215
8.6               7.077          8.224
8.7               7.090          8.233
8.8               7.103          8.241
8.9               7.116          8.249
9.0               7.128          8.257
9.1               7.141          8.265
9.2               7.153          8.272
9.3               7.165          8.278
9.4               7.177          8.285
9.5               7.188          8.291
9.6               7.200          8.296
9.7               7.211          8.302
9.8               7.222          8.307
9.9               7.233          8.312
10.0              7.244          8.317
10.1              7.255          8.322
10.2              7.265          8.326
10.3              7.275          8.330
10.4              7.285          8.334
10.5              7.295          8.338
10.6              7.305          8.341
10.7              7.315          8.345
10.8              7.324          8.348
10.9              7.334          8.351
11.0              7.343          8.354
11.1              7.352          8.356
11.2              7.361          8.359
11.3              7.370          8.361
11.4              7.378          8.364
11.5              7.387          8.366
11.6              7.395          8.368
11.7              7.404          8.370
11.8              7.412          8.372
11.9              7.420          8.374
12.0              7.428          8.376
12.1              7.436          8.377
12.2              7.443          8.379
12.3              7.451          8.380
12.4              7.458          8.382
12.5              7.466          8.383
12.6              7.473          8.384
12.7              7.480          8.386
12.8              7.487          8.387
12.9              7.494          8.388
13.0              7.501          8.389
13.1              7.508          8.390
13.2              7.514          8.391
13.3              7.521          8.392
13.4              7.527          8.393
13.5              7.534          8.393
13.6              7.540          8.394
13.7              7.546          8.395
13.8              7.552          8.395
13.9              7.558          8.396
14.0              7.564          8.397
14.1              7.570          8.397
14.2              7.576          8.398
14.3              7.582          8.398
14.4              7.587          8.399
14.5              7.593          8.399
14.6              7.599          8.400
14.7              7.604          8.400
14.8              7.609          8.401
14.9              7.615          8.401
15.0              7.620          8.401
15.1              7.625          8.402
15.2              7.630          8.402
15.3              7.635          8.402
15.4              7.640          8.403
15.5              7.645          8.403
15.6              7.650          8.403
15.7              7.655          8.403
15.8              7.659          8.404
15.9              7.664          8.404
16.0              7.669          8.404


In [7]:
#Comment out this if you do not have PyPlot installed.

using PyPlot
PyPlot.svg(true)           #for ipynb notebooks
close("all")
                                       #plotting
figure()
  plot(tm,c,"+",tm,y,"s",tmFig,shx,"b--",tmFig,fhx,"r-",tm,ytmx,".")
  title("Swedish Interest Rates 29 Dec 1993")
  xlabel("Years to Maturity")
  legend(["Coupon rate","Yield to maturity","Estimated spot rate",
         "Estimated forward rate","Estimated yield to maturity"],loc=1)
  ylim(0.04,0.14)
  #display(gcf())            #uncomment in Atom/Juno


Out[7]:
(0.04, 0.14)

This notebook was generated using Literate.jl.