Skip to content

Commit 62e4467

Browse files
Improve type declaration for Rate to improve package type inference (#55)
* use parametric Rate to improve inference * bump version
1 parent a93d80e commit 62e4467

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Yields"
22
uuid = "d7e99b2f-e7f3-4d9e-9f01-2338fc023ad3"
33
authors = ["Alec Loudenback <[email protected]> and contributors"]
4-
version = "0.7.0"
4+
version = "0.7.1"
55

66
[deps]
77
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

src/Yields.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ See also: [`Continuous`](@ref)
7878
"""
7979
Periodic(x,frequency) = Rate(x, Periodic(frequency))
8080

81-
struct Rate
82-
value
83-
compounding::CompoundingFrequency
81+
struct Rate{N<:Real,T<:CompoundingFrequency}
82+
value::N
83+
compounding::T
8484
end
8585

8686
# Base.:==(r1::Rate,r2::Rate) = (r1.value == r2.value) && (r1.compounding == r2.compounding)
@@ -141,7 +141,9 @@ julia> convert(Continuous(),r)
141141
Rate(0.009995835646701251, Continuous())
142142
```
143143
"""
144-
Base.convert(T::CompoundingFrequency,r::Rate) = convert(T, r, r.compounding)
144+
function Base.convert(T::CompoundingFrequency,r::Rate{<:Real,<:CompoundingFrequency})
145+
convert(T, r, r.compounding)
146+
end
145147
function Base.convert(to::Continuous, r, from::Continuous)
146148
return r
147149
end
@@ -159,8 +161,9 @@ function Base.convert(to::Periodic, r, from::Periodic)
159161
return convert(to, c, Continuous())
160162
end
161163

162-
rate(r::Rate) = r.value
163-
164+
function rate(r::Rate{<:Real,<:CompoundingFrequency})
165+
r.value
166+
end
164167

165168
"""
166169
An AbstractYield is an object which can be used as an argument to:
@@ -221,8 +224,8 @@ julia> discount(y,2)
221224
0.9070294784580498 # 1 / (1.05) ^ 2
222225
```
223226
"""
224-
struct Constant <: AbstractYield
225-
rate
227+
struct Constant{T} <: AbstractYield
228+
rate::T
226229
end
227230

228231
function Constant(rate::T) where {T <: Real}
@@ -338,7 +341,7 @@ Rate(0.06000000000000005, Periodic(1))
338341
339342
```
340343
"""
341-
function Par(rates::Vector{Rate}, maturities)
344+
function Par(rates::Vector{<:Rate}, maturities)
342345
# bump to a constant yield if only given one rate
343346
if length(rates) == 1
344347
return Constant(rate[1])
@@ -394,7 +397,7 @@ function CMT(rates::Vector{T}, maturities) where {T <: Real}
394397
CMT(rs, maturities)
395398
end
396399

397-
function CMT(rates::Vector{Rate}, maturities)
400+
function CMT(rates::Vector{<:Rate}, maturities)
398401
return YieldCurve(
399402
rates,
400403
maturities,
@@ -421,7 +424,7 @@ function OIS(rates::Vector{T}, maturities) where {T <: Real}
421424

422425
return OIS(rs, maturities)
423426
end
424-
function OIS(rates::Vector{Rate}, maturities)
427+
function OIS(rates::Vector{<:Rate}, maturities)
425428
return YieldCurve(
426429
rates,
427430
maturities,
@@ -708,8 +711,8 @@ end
708711
The discount factor for the `rate` for times `from` through `to`. If rate is a `Real` number, will assume a `Constant` interest rate.
709712
"""
710713
discount(yc,time) = yc.discount(time)
711-
discount(rate::Rate,from,to) = discount(Constant(rate), from, to)
712-
discount(rate::Rate,to) = discount(Constant(rate), to)
714+
discount(rate::Rate{<:Real,<:CompoundingFrequency},from,to) = discount(Constant(rate), from, to)
715+
discount(rate::Rate{<:Real,<:CompoundingFrequency},to) = discount(Constant(rate), to)
713716

714717

715718

@@ -731,12 +734,12 @@ The accumulation factor for the `rate` for times `from` through `to`. If rate is
731734
function accumulation(y::T, time) where {T <: AbstractYield}
732735
return 1 ./ discount(y, time)
733736
end
734-
accumulation(rate::Rate,to) = accumulation(Constant(rate), to)
737+
accumulation(rate::Rate{<:Real,<:CompoundingFrequency},to) = accumulation(Constant(rate), to)
735738

736739
function accumulation(y::T, from, to) where {T <: AbstractYield}
737740
return 1 ./ discount(y, from, to)
738741
end
739-
accumulation(rate::Rate,from,to) = accumulation(Constant(rate), from, to)
742+
accumulation(rate::Rate{<:Real,<:CompoundingFrequency},from,to) = accumulation(Constant(rate), from, to)
740743

741744
## Curve Manipulations
742745
struct RateCombination <: AbstractYield

0 commit comments

Comments
 (0)