Skip to content

Commit

Permalink
working zero rates
Browse files Browse the repository at this point in the history
  • Loading branch information
alecloudenback committed Nov 13, 2023
1 parent 683a4c9 commit 2d35be2
Showing 1 changed file with 86 additions and 14 deletions.
100 changes: 86 additions & 14 deletions src/model/Yield/MonotoneConvex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,54 @@ function g(x, f⁻, f, fᵈ)
end
end

function forward(t, rates, times)
# the array indexing in the paper and psuedo-VBA is messy
t = min(t, last(times))
N = length(times)
times = collect(times)
rates = collect(rates)
i_time = findfirst(x -> x > t, times)
if i_time == nothing
i_time = N
end
if !iszero(first(times))
pushfirst!(times, zero(eltype(times)))
pushfirst!(rates, first(rates))
function g_rate(x, f⁻, f, fᵈ)
@show x, f⁻, f, fᵈ
@show g0 = f⁻ - fᵈ
@show g1 = f - fᵈ
A = -2 * g0
B = -2 * g1
if sign(g0) == sign(g1)
@show "(iv)"

Check warning on line 67 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L60-L67

Added lines #L60 - L67 were not covered by tests
# sector (iv)
η = g1 / (g1 + g0)
α = -g0 * g1 / (g1 + g0)

Check warning on line 70 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L69-L70

Added lines #L69 - L70 were not covered by tests

if x < η
return α * x - (g0 - α) * ((η - x)^3 / η^2 - η) / 3

Check warning on line 73 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L72-L73

Added lines #L72 - L73 were not covered by tests
else
return (2 * α + g0) / 3 * η + α * (x - η) + (g1 - α) / 3 * (x - η)^3 / (1 - η)^2

Check warning on line 75 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L75

Added line #L75 was not covered by tests
end


elseif __issector1(g0, g1)
@show "(i)"

Check warning on line 80 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L79-L80

Added lines #L79 - L80 were not covered by tests
# sector (i)
g0 * (x - 2 * x^2 + x^3) + g1 * (-x^2 + x^3)
elseif __issector2(g0, g1)
@show "(ii)"

Check warning on line 84 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L82-L84

Added lines #L82 - L84 were not covered by tests
# sector (ii)
η = (g1 + 2 * g0) / (g1 - g0)
if x < η
return g0 * x

Check warning on line 88 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L86-L88

Added lines #L86 - L88 were not covered by tests
else
return g0 * x + ((g1 - g0) * (x - η)^3 / (1 - η)^2) / 3

Check warning on line 90 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L90

Added line #L90 was not covered by tests
end
else
@show "(iii)"

Check warning on line 93 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L93

Added line #L93 was not covered by tests
# sector (iii)
η = 3 * g1 / (g1 - g0)
if x > η
return (2 * g1 + g0) / 3 * η + g1 * (x - η)

Check warning on line 97 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L95-L97

Added lines #L95 - L97 were not covered by tests
else
return g1 * x - (g0 - g1) * ((η - x)^3 / η^2 - η) / 3

Check warning on line 99 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L99

Added line #L99 was not covered by tests
end

end
end

function forward(t, rates, times)

Check warning on line 105 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L105

Added line #L105 was not covered by tests

t, i_time, rates, times = __monotone_convex_init(t, rates, times)
f, fᵈ = __monotone_convex_fs(rates, times)

Check warning on line 108 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L107-L108

Added lines #L107 - L108 were not covered by tests

@show x = (t - times[i_time]) / (times[i_time+1] - times[i_time])
Expand All @@ -85,6 +117,26 @@ function forward(t, rates, times)

end

"""
returns the index associated with the time t, an initial rate vector, and a time vector
"""
function __monotone_convex_init(t, rates, times)

Check warning on line 123 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L123

Added line #L123 was not covered by tests
# the array indexing in the paper and psuedo-VBA is messy
t = min(t, last(times))
times = collect(times)
rates = collect(rates)
i_time = findfirst(x -> x > t, times)
if i_time == nothing
i_time = lastindex(times)

Check warning on line 130 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L125-L130

Added lines #L125 - L130 were not covered by tests
end
if !iszero(first(times))
pushfirst!(times, zero(eltype(times)))
pushfirst!(rates, first(rates))

Check warning on line 134 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L132-L134

Added lines #L132 - L134 were not covered by tests

end

return t, i_time, rates, times

Check warning on line 138 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L138

Added line #L138 was not covered by tests
end
"""
returns a pair of vectors (f and fᵈ) used in Monotone Convext Yield Curve fitting
"""
Expand Down Expand Up @@ -114,14 +166,28 @@ function __monotone_convex_fs(rates, times)
return f, fᵈ

Check warning on line 166 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L166

Added line #L166 was not covered by tests
end
function myzero(t, rates, times)
#TODO
lt = last(times)

Check warning on line 169 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L168-L169

Added lines #L168 - L169 were not covered by tests
# if the time is greater than the last input time then extrapolate using the forwards
if t > lt
r = myzero(lt, rates, times)
return r * lt / t + forward(lt, rates, times) * (1 - lt / t)

Check warning on line 173 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L171-L173

Added lines #L171 - L173 were not covered by tests
end

t, i_time, rates, times = __monotone_convex_init(t, rates, times)
f, fᵈ = __monotone_convex_fs(rates, times)
x = (t - times[i_time]) / (times[i_time+1] - times[i_time])
@show G = g_rate(x, f[i_time], f[i_time+1], fᵈ[i_time+1])
return 1 / t * (times[i_time] * rates[i_time] + (t - times[i_time]) * fᵈ[i_time+1] + (times[i_time+1] - times[i_time]) * G)

Check warning on line 180 in src/model/Yield/MonotoneConvex.jl

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L176-L180

Added lines #L176 - L180 were not covered by tests




end

times = 1:5
rates = [0.03, 0.04, 0.047, 0.06, 0.06]
forward(5.19, rates, times)
myzero(1, rates, times)


using Test
Expand All @@ -132,3 +198,9 @@ using Test
@test forward(5, rates, times) 0.05025
@test forward(5.2, rates, times) 0.05025

@test myzero(0.5, rates, times) 0.02625
@test myzero(1, rates, times) 0.03
@test myzero(2, rates, times) 0.04
@test myzero(2.5, rates, times) 0.0431375956535047
@test myzero(5, rates, times) 0.06
@test myzero(5.2, rates, times) 0.059625

0 comments on commit 2d35be2

Please sign in to comment.