Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LinearZeroCurve #52

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/serialisation/RebuildTermstructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ function _get_labels_and_values(ts::ZeroCurve)
end


"""
_get_labels_and_values(ts::LinearZeroCurve)

Extract labels and values from LinearZeroCurve.
"""
function _get_labels_and_values(ts::LinearZeroCurve)
param_labels = [
alias(ts) * _split_alias_identifyer *
string(typeof(ts)) * _split_alias_identifyer *
(@sprintf("%.2f", t))
for t in ts.times
]
return ( param_labels, ts.values )
end


"""
_get_labels_and_values(ts::PiecewiseFlatParameter)

Expand Down Expand Up @@ -99,9 +115,14 @@ function termstructure_dictionary!(
continue
end
if isa(ts_dict[ts_alias], ZeroCurve)
@warn "ZeroCurve rebuild does not work with Zygote. Consider using LinearZeroCurve."
ts_dict[ts_alias] = zero_curve(ts_dict[ts_alias].alias, ts_dict[ts_alias].times, values)
continue
end
if isa(ts_dict[ts_alias], LinearZeroCurve)
ts_dict[ts_alias] = linear_zero_curve(ts_dict[ts_alias].alias, ts_dict[ts_alias].times, values)
continue
end
if isa(ts_dict[ts_alias], BackwardFlatParameter)
ts_dict[ts_alias] = backward_flat_parameter(ts_dict[ts_alias].alias, ts_dict[ts_alias].times, values)
continue
Expand Down
7 changes: 7 additions & 0 deletions src/serialisation/Termstructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ function serialise(o::ZeroCurve)
return d
end

"""
serialise(o::LinearZeroCurve)

Serialise LinearZeroCurve.
"""
serialise(o::LinearZeroCurve) = serialise_struct(o)

"""
serialise(o::BackwardFlatVolatility)

Expand Down
85 changes: 85 additions & 0 deletions src/termstructures/rates/YieldTermstructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,88 @@ function discount(ts::ZeroCurve, t::ModelTime)
z = ts.interpolation(t)
return exp(-z * t)
end


"""
struct LinearZeroCurve <: YieldTermstructure
alias::String
times::AbstractVector
values::AbstractVector
end

A yield term structure based on continuous compounded zero rates
with linear interpolation and flat extrapolation.

This curve aims at mitigating limitations of Zygote and ZeroCurve.
"""
struct LinearZeroCurve <: YieldTermstructure
alias::String
times::AbstractVector
values::AbstractVector
end


"""
linear_zero_curve(
alias::String,
times::AbstractVector,
values::AbstractVector,
)

Create a LinearZeroCurve.
"""
function linear_zero_curve(
alias::String,
times::AbstractVector,
values::AbstractVector,
)
return LinearZeroCurve(alias, times, values)
end


"""
linear_zero_curve(
times::AbstractVector,
values::AbstractVector,
)

Create a LinearZeroCurve with empty alias.
"""
function linear_zero_curve(
times::AbstractVector,
values::AbstractVector,
)
return linear_zero_curve("", times, values)
end


"""
_interpole(ts::LinearZeroCurve, t::ModelTime)

Linear interpolation with flat exrapolation.
"""
function _interpole(ts::LinearZeroCurve, t::ModelTime)
idx = searchsortedfirst(ts.times, t)
# left flat extrapolation
if idx == 1
return ts.values[idx]
end
# right flat extrapolation
if idx > length(ts.times)
return ts.values[idx-1]
end
# linear interpolation
rho = (t - ts.times[idx-1]) / ((ts.times[idx] - ts.times[idx-1]))
return rho * ts.values[idx] + (1.0 - rho) * ts.values[idx-1]
end


"""
discount(ts::LinearZeroCurve, t::ModelTime)

Calculate discount factor.
"""
function discount(ts::LinearZeroCurve, t::ModelTime)
z = _interpole(ts, t)
return exp(-z * t)
end
10 changes: 7 additions & 3 deletions test/unittests/serialisation/rebuild_termstructures.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DiffFusion
using Test
using OrderedCollections

@testset "Extract term structures and rebuild term structure dictionary." begin

Expand All @@ -8,21 +9,24 @@ using Test
ts_vector = [
DiffFusion.flat_forward("yc/flat_forward", 0.01),
DiffFusion.zero_curve("yc/zero_curve", [ 1.0, 10.0 ], [ 0.02, 0.03 ]),
DiffFusion.linear_zero_curve("yc/linear_zero_curve", [ 2.0, 5.0 ], [ 0.02, 0.03 ]),
DiffFusion.backward_flat_parameter("pa/backward_flat_parameter", [ 0.0 ], [ 1.0 ]),
DiffFusion.forward_flat_parameter("pa/forward_flat_parameter", [0.0, 2.0], [ 0.10, 0.15] ),
]
ts_dict = Dict{String,DiffFusion.Termstructure}(((DiffFusion.alias(ts_), ts_) for ts_ in ts_vector))
ts_dict = OrderedDict{String,DiffFusion.Termstructure}(((DiffFusion.alias(ts_), ts_) for ts_ in ts_vector))
#
(l, v) = DiffFusion.termstructure_values(ts_dict)
labels = [
"yc/flat_forward" * delim * "DiffFusion.FlatForward" * delim * "rate",
"yc/zero_curve" * delim * "DiffFusion.ZeroCurve" * delim * "1.00",
"yc/zero_curve" * delim * "DiffFusion.ZeroCurve" * delim * "10.00",
"yc/linear_zero_curve" * delim * "DiffFusion.LinearZeroCurve" * delim * "2.00",
"yc/linear_zero_curve" * delim * "DiffFusion.LinearZeroCurve" * delim * "5.00",
"pa/backward_flat_parameter" * delim * "DiffFusion.BackwardFlatParameter" * delim * "0.00",
"pa/forward_flat_parameter" * delim * "DiffFusion.ForwardFlatParameter" * delim * "0.00",
"pa/forward_flat_parameter" * delim * "DiffFusion.ForwardFlatParameter" * delim * "2.00"
]
values = [0.01, 0.02, 0.03, 1.0, 0.1, 0.15]
values = [0.01, 0.02, 0.03, 0.02, 0.03, 1.0, 0.1, 0.15]
@test l == labels
@test v == values
#
Expand All @@ -41,7 +45,7 @@ using Test
DiffFusion.backward_flat_parameter("pa/backward_flat_parameter", [ 0.0 ], [ 1.0 ]),
DiffFusion.forward_flat_parameter("pa/forward_flat_parameter", [0.0, 2.0], [ 0.10, 0.15] ),
]
ts_dict = Dict{String,DiffFusion.Termstructure}(((DiffFusion.alias(ts_), ts_) for ts_ in ts_vector))
ts_dict = OrderedDict{String,DiffFusion.Termstructure}(((DiffFusion.alias(ts_), ts_) for ts_ in ts_vector))
#
(l, v) = DiffFusion.termstructure_values(ts_dict)
labels = [
Expand Down
9 changes: 9 additions & 0 deletions test/unittests/serialisation/termstructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ using Test
"values" => [0.02, 0.03]
)
#
d = DiffFusion.serialise(DiffFusion.linear_zero_curve("USD", [0.0, 10.0], [0.02, 0.03]))
@test d == OrderedDict{String, Any}(
"typename" => "DiffFusion.LinearZeroCurve",
"constructor" => "LinearZeroCurve",
"alias" => "USD",
"times" => [0.0, 10.0],
"values" => [0.02, 0.03]
)
#
times = [ 1., 2., 5., 10. ]
values = [ 50. 60. 70. 80. ;
50. 50. 50. 50. ;
Expand Down
30 changes: 29 additions & 1 deletion test/unittests/termstructures/rates/rates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using Interpolations
@test ts(2.0) == exp(-0.06)
end

@testset "Linear ZeroCurve" begin
@testset "ZeroCurve with linear interpolation." begin
times = [1.0, 3.0, 6.0, 10.0]
values = [1.0, 1.0, 2.0, 3.0] .* 1e-2
ts = DiffFusion.zero_curve("EUR", times, values)
Expand Down Expand Up @@ -49,6 +49,34 @@ using Interpolations
@test DiffFusion.discount(ts2, 10.0) == exp(-10.0*0.03)
end

@testset "LinearZeroCurve." begin
#
times = [1.0, 3.0, 6.0, 10.0]
values = [1.0, 1.0, 2.0, 3.0] .* 1e-2
#
@test DiffFusion.alias(DiffFusion.linear_zero_curve("Std", times, values)) == "Std"
#
ts_tst = DiffFusion.linear_zero_curve(times, values)
# test correct interpolation
ts_ref = DiffFusion.zero_curve(times, values)
_times = 1.0:0.5:10.0
for t in _times
@test DiffFusion.discount(ts_tst, t) == DiffFusion.discount(ts_ref, t)
end
# test left flat extrapolation
ts_ref = DiffFusion.flat_forward(0.01)
_times = 0.0:0.5:1.0
for t in _times
@test DiffFusion.discount(ts_tst, t) == DiffFusion.discount(ts_ref, t)
end
# test right flat extrapolation
ts_ref = DiffFusion.flat_forward(0.03)
_times = 10.0:0.5:11.0
for t in _times
@test DiffFusion.discount(ts_tst, t) == DiffFusion.discount(ts_ref, t)
end
end

@testset "Test alias-based interpolated zero curves." begin
times = [ 1.0, 2.0, 3.0, 5.0, 7.0, 10.0, 15.0, 20.0, 30.0 ]
values = [ 3.61, 3.62, 3.43, 3.17, 3.07, 3.04, 3.06, 2.92, 2.59 ] * 1.0e-2
Expand Down