Skip to content

Commit 6bfd48f

Browse files
Merge pull request #20 from JuliaActuary/treasury_fix
Treasury fix
2 parents 620720a + 58dabe7 commit 6bfd48f

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Yields.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,21 @@ function Forward(rate_vector, times)
178178
return Zero(1 ./ disc_v.^(1 ./ times) .- 1, times)
179179
end
180180

181+
"""
182+
USTreasury(rates,maturities)
183+
184+
Takes CMT yields (bond equivalent), and assumes that instruments <= one year maturity pay no coupons and that the rest pay semi-annual.
185+
"""
181186
function USTreasury(rates, maturities)
182187
z = zeros(length(rates))
183188

184189
# use the discount rate for T-Bills with maturities <= 1 year
185190
for (i, (rate, mat)) in enumerate(zip(rates, maturities))
186191

187192
if mat <= 1
188-
z[i] = rate
193+
z[i] = (1 + rate * mat) ^ (1/mat) -1
189194
else
190-
# uses spline b/c of common, but uneven maturities often present under 1 year.
195+
# uses interpolation b/c of common, but uneven maturities often present under 1 year.
191196
curve = linear_interp(maturities, z)
192197
pmts = [rate / 2 for t in 0.5:0.5:mat] # coupons only
193198
pmts[end] += 1 # plus principal
@@ -222,7 +227,7 @@ end
222227
"""
223228
rate(yield,time)
224229
225-
The spot rate at `time` for the given `yield`.
230+
The annual effective spot rate at `time` for the given `yield`.
226231
"""
227232
rate(yc,time) = yc.spline(time)
228233

test/runtests.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,25 @@ using Test
179179

180180
curve = Yields.USTreasury(YTM, maturity)
181181

182-
@test rate(curve, 0.5) 0.04
182+
@test rate(curve, 0.5) (1 + 0.04/2) ^ 2 - 1
183183
@test rate(curve, 1.0) 0.043
184184

185185
# need more future tests, but need validating source...
186186

187187
end
188188

189189
@testset "actual cmt treasury" begin
190-
191-
cmt = [0.12,0.15,0.14,0.17,0.17,0.17,0.19,0.30,0.49,0.64,1.15,1.37] ./ 100
192-
mats = [1 / 12,2 / 12,3 / 12,6 / 12,1,2,3,5,7,10,20,30]
193-
190+
# Hull 10th ed, 4.7
191+
cmt = [1.6064,2.0202,2.2495,2.2949,2.4238] ./ 100
192+
mats = [.25,.5,1.,1.5,2.]
193+
curve = Yields.USTreasury(cmt,mats)
194+
195+
# rates in book are continuous, but Yields focuses on annual
196+
@test log(rate(curve,0.25)+1) 0.01603 atol=0.001
197+
@test log(rate(curve,0.5 )+1) 0.02010 atol=0.001
198+
@test log(rate(curve,1.0 )+1) 0.02225 atol=0.001
199+
@test log(rate(curve,1.5 )+1) 0.02284 atol=0.001
200+
@test log(rate(curve,2.0 )+1) 0.02416 atol=0.001
194201
end
195202

196203

0 commit comments

Comments
 (0)