Skip to content

Commit

Permalink
add missing zero method for Constant (#69)
Browse files Browse the repository at this point in the history
* add missing `zero` method for `Constant`

* fix argument order
  • Loading branch information
alecloudenback authored Dec 1, 2021
1 parent b225751 commit b17070e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Yields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ end


"""
Constant(rate)
Constant(rate::Real, cf::CompoundingFrequency=Periodic(1))
Constant(r::Rate)
Construct a yield object where the spot rate is constant for all maturities. If `rate` is not a `Rate` type, will assume `Periodic(1)` for the compounding frequency
Expand All @@ -277,10 +278,12 @@ struct Constant{T} <: AbstractYield
rate::T
end

function Constant(rate::T) where {T<:Real}
return Constant(Rate(rate, Periodic(1)))
function Constant(rate::T, cf::C = Periodic(1)) where {T<:Real,C<:CompoundingFrequency}
return Constant(Rate(rate, cf))
end

zero(c::Constant, time) = c.rate
zero(c::Constant, time, cf::C) where {C<:CompoundingFrequency} = convert(cf, c.rate)
rate(c::Constant) = c.rate
rate(c::Constant, time) = c.rate
discount(c::T, time) where {T<:Real} = discount(Constant(c), time)
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ using Test
yield = Yields.Constant(0.05)
rate = Yields.Rate(0.05, Yields.Periodic(1))

@test Yields.zero(yield, 1) == Rate(0.05, Yields.Periodic(1))
@test Yields.zero(Yields.Constant(0.05, Yields.Periodic(2)), 10) == Rate(0.05, Yields.Periodic(2))
@test Yields.zero(yield, 5, Yields.Periodic(2)) == convert(Yields.Periodic(2), Rate(0.05, Yields.Periodic(1)))

@testset "constant discount time: $time" for time in [0, 0.5, 1, 10]
@test discount(yield, time) 1 / (1.05)^time
@test discount(rate, time) 1 / (1.05)^time
Expand Down

0 comments on commit b17070e

Please sign in to comment.