Skip to content

Commit

Permalink
WIP Monotone Convex
Browse files Browse the repository at this point in the history
  • Loading branch information
alecloudenback committed Nov 11, 2023
1 parent 078e19e commit 86a3ba5
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/model/Yield/MonotoneConvex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

# Hagan West - WILMOTT magazine pgs 75-81
function g(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)

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L3-L9

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L11 - L12 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L14 - L15 were not covered by tests
else
return α + (g1 - α) * ((x - η) / (1 - η))^2

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L17

Added line #L17 was not covered by tests
end


elseif g1 < A && g0 > B

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L21

Added line #L21 was not covered by tests
# sector (i)
g0 * (1 - 4 * x + 3 * x^2) + g1 * (-2 * x + 3 * x^2)
elseif g1 > A

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L26-L28

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

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L30

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L34 - L36 were not covered by tests
else
return g1 + (g0 - g1) * ((η - x) / η)^2

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L38

Added line #L38 was not covered by tests
end

end
end

function forward(t, rates, times)

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L44

Added line #L44 was not covered by tests
# the array indexing in the paper and psuedo-VBA is messy
N = length(times)
times = collect(times)
rates = collect(rates)
i_time = findfirst(x -> x > t, times)
if !iszero(first(times))
pushfirst!(times, zero(eltype(times)))
pushfirst!(rates, first(rates))

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L46-L52

Added lines #L46 - L52 were not covered by tests

end
# step 1
fᵈ = map(2:length(times)) do i
(times[i] * rates[i] - times[i-1] * rates[i-1]) / (times[i] - times[i-1])

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L56-L57

Added lines #L56 - L57 were not covered by tests
end
pushfirst!(fᵈ, 0)

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

View check run for this annotation

Codecov / codecov/patch

src/model/Yield/MonotoneConvex.jl#L59

Added line #L59 was not covered by tests
# step 2
f = map(2:length(times)-1) do i
(times[i] - times[i-1]) / (times[i+1] - times[i-1]) * fᵈ[i+1] +

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L61 - L62 were not covered by tests
(times[i+1] - times[i]) / (times[i+1] - times[i-1]) * fᵈ[i]
end
# step 3
# collar(a,b,c) = clamp(b, a, c)
pushfirst!(f, fᵈ[2] - 0.5 * (f[1] - fᵈ[2]))
fᵈ[end], f[end-1], fᵈ[end]
push!(f, fᵈ[end] - 0.5 * (f[end] - fᵈ[end]))
f[1] = clamp(f[1], 0, 2 * fᵈ[2])
f[end] = clamp(f[end], 0, 2 * fᵈ[end])

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L67 - L71 were not covered by tests

for j in 2:(length(times)-1)
f[j] = clamp(f[j], 0, 2 * min(fᵈ[j], fᵈ[j+1]))
end
@show x = (t - times[i_time]) / (times[i_time+1] - times[i_time])
@show fᵈ, f
@show fᵈ[i_time+1], g(x, f[i_time], f[i_time+1], fᵈ[i_time+1])
return fᵈ[i_time+1] + g(x, f[i_time], f[i_time+1], fᵈ[i_time+1])

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L73 - L79 were not covered by tests




end

times = 1:5
rates = [0.03, 0.04, 0.047, 0.06, 0.06]
forward(1, 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

0 comments on commit 86a3ba5

Please sign in to comment.