Skip to content

Commit

Permalink
Merge pull request #11 from lungben/linear_interpolation_constructor
Browse files Browse the repository at this point in the history
Added constructor for linear interpolator without arguments
  • Loading branch information
pazzo83 authored Jan 15, 2020
2 parents bebb119 + 990a3af commit 072c7cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/math/interpolation/linear_interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ mutable struct LinearInterpolation <: Interpolation
s::Vector{Float64}
end

"""
initialize without data, e.g. for usage in curve constructors
Example:
QuantLib.InterpolatedDiscountCurve(curve_dates, curve_values, daycount,
QuantLib.Math.LinearInterpolation())
"""
function LinearInterpolation()
x_vals = Vector{Float64}()
y_vals = Vector{Float64}()
s = Vector{Float64}()

QuantLib.Math.LinearInterpolation(x_vals, y_vals, s)
end

# Linear initialize
function initialize!(interp::LinearInterpolation, x_vals::Vector{Float64}, y_vals::Vector{Float64})
interp.x_vals = x_vals
Expand Down
26 changes: 26 additions & 0 deletions test/curves.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Dates

effective_date = Date(2020, 01, 15)

days = [369, 462, 553, 735, 1100, 1465]
df = [0.97678403, 0.97153083, 0.96655364, 0.95695995, 0.93829085, 0.91954718]
test_date = effective_date + Day(900)

const dc_act_360 = QuantLib.Time.Actual360()

# test log-linear interpolation
test_curve_loglinear = QuantLib.InterpolatedDiscountCurve(effective_date .+ Day.(days), df, dc_act_360, QuantLib.Math.LogLinear())
disc_factor = QuantLib.discount(test_curve_loglinear, test_date)
x1 = (test_date - test_curve_loglinear.dates[4]).value
dx = (test_curve_loglinear.dates[5]-test_curve_loglinear.dates[4]).value
dy = log(test_curve_loglinear.data[5]) - log(test_curve_loglinear.data[4])
df_recalc = exp(log(test_curve_loglinear.data[4]) + x1*dy/dx)
@test disc_factor == df_recalc

test_curve_linear = QuantLib.InterpolatedDiscountCurve(effective_date .+ Day.(days), df, dc_act_360, QuantLib.Math.LinearInterpolation())
disc_factor = QuantLib.discount(test_curve_linear, test_date)
x1 = (test_date - test_curve_linear.dates[4]).value
dx = (test_curve_linear.dates[5]-test_curve_linear.dates[4]).value
dy = (test_curve_linear.data[5]) - (test_curve_linear.data[4])
df_recalc = ((test_curve_linear.data[4]) + x1*dy/dx)
@test disc_factor == df_recalc
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ tests = ["cash_flows",
"options",
"swaps",
"swaptions",
"fd_mesher"]
"fd_mesher",
"curves"]

println("Running tests:")

Expand Down

0 comments on commit 072c7cf

Please sign in to comment.