Skip to content

Commit

Permalink
basic passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alecloudenback committed Nov 12, 2023
1 parent 86a3ba5 commit 5201bba
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/model/Yield/MonotoneConvex.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@

function __issector1(g0, g1)
a = (g0 > 0) && (g1 >= -2 * g0) && (-0.5 * g0 >= g1)
b = (g0 < 0) && (g1 <= -2 * g0) && (-0.5 * g0 <= g1)
a || b

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L2-L5

Added lines #L2 - L5 were not covered by tests
end

function __issector2(g0, g1)
a = (g0 > 0) && (g1 < -2 * g0)
b = (g0 < 0) && (g1 > -2 * g0)
a || b

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L8-L11

Added lines #L8 - L11 were not covered by tests
end

# Hagan West - WILMOTT magazine pgs 75-81
function g(x, f⁻, f, fᵈ)
@show x, f⁻, f, fᵈ
Expand All @@ -7,6 +19,7 @@ function g(x, f⁻, f, fᵈ)
A = -2 * g0
B = -2 * g1
if sign(g0) == sign(g1)
@show "(iv)"

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L15-L22

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

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L24-L25

Added lines #L24 - L25 were not covered by tests
Expand All @@ -18,10 +31,12 @@ function g(x, f⁻, f, fᵈ)
end


elseif g1 < A && g0 > B
elseif __issector1(g0, g1)
@show "(i)"

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L34-L35

Added lines #L34 - L35 were not covered by tests
# sector (i)
g0 * (1 - 4 * x + 3 * x^2) + g1 * (-2 * x + 3 * x^2)
elseif g1 > A
elseif __issector2(g0, g1)
@show "(ii)"

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L37-L39

Added lines #L37 - L39 were not covered by tests
# sector (ii)
η = (g1 + 2 * g0) / (g1 - g0)
if x < η
Expand All @@ -30,6 +45,7 @@ function g(x, f⁻, f, fᵈ)
return g0 + (g1 - g0) * ((x - η) / (1 - η))^2

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L45

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

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L48

Added line #L48 was not covered by tests
# sector (iii)
η = 3 * g1 / (g1 - g0)
if x > η
Expand All @@ -43,10 +59,14 @@ end

function forward(t, rates, times)

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L60

Added line #L60 was not covered by tests
# 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

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L62-L68

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L70 - L72 were not covered by tests
Expand Down Expand Up @@ -85,14 +105,14 @@ end

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


using Test
@test forward(0.5, rates, times) 0.02875
@test forward(1, rates, times) 0.04
@test forward(2, rates, times) 0.0555
@test forward(2.5, rates, times) 0.0571254591368226
@test forward(5, rates, times) 0.050252925
@test forward(5.2, rates, times) 0.050252925
@test forward(5, rates, times) 0.05025
@test forward(5.2, rates, times) 0.05025

0 comments on commit 5201bba

Please sign in to comment.