Skip to content

Commit

Permalink
fix tests and define YieldCurve for Correlated
Browse files Browse the repository at this point in the history
  • Loading branch information
alecloudenback committed Aug 8, 2022
1 parent 2922515 commit d35aad1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/EconomicScenarioGenerators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct Correlated{T,U,R} <: AbstractScenarioGenerator
new{T,U,R}(generators,copula,RNG)
end
end

Base.Broadcast.broadcastable(x::T) where {T<:Correlated} = Ref(x)

function Base.iterate(sgc::Correlated)
n = 1
Expand Down
10 changes: 10 additions & 0 deletions src/Yields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ function YieldCurve(sg::ScenarioGenerator{N,T,R}) where {N,T<:InterestRateModel,

end

function YieldCurve(C::T) where {T<:Correlated}
timestep = first(C.sg).timestep
_, times = __zeros_times(first(C.sg))
_zeros(v) = cumsum(v .* timestep) ./ times
rates = collect(C)
zs = _zeros.(rates)
map(r->Yields.Zero(r,times),zs)

end

function __disc_rate_to_fwd(rate,time)
log(rate) / -time
end
Expand Down
90 changes: 49 additions & 41 deletions test/Correlated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,82 @@
@testset "Correlated" begin
using EconomicScenarioGenerators, Copulas

m = BlackScholesMerton(0.01,0.02,.15,100.)
s = ScenarioGenerator(
1, # timestep
30, # projection horizon
m, # model
)
@testset "Equity" begin
@testset "BSM" begin
m = BlackScholesMerton(0.01, 0.02, 0.15, 100.0)
s = ScenarioGenerator(
1, # timestep
30, # projection horizon
m, # model
)

ss = [s,s] # these don't have to be the exact same
g = GaussianCopula([1. 0.9; 0.9 1.])
c = Correlated(ss,g,StableRNG(1))
ss = [s, s] # these don't have to be the exact same
g = GaussianCopula([1.0 0.9; 0.9 1.0])
c = Correlated(ss, g, StableRNG(1))

x = collect(c)
x = collect(c)

ρ = cor(hcat(ratio.(x)...))
@test ρ[1,2] 0.9 atol = 0.03
ρ = cor(hcat(ratio.(x)...))
@test ρ[1, 2] 0.9 atol = 0.03
end
end

@testset "Interest" begin
@testset "HullWhite" begin
rates =[0.01, 0.01, 0.03, 0.05, 0.07, 0.16, 0.35, 0.92, 1.40, 1.74, 2.31, 2.41] ./ 100
mats = [1/12, 2/12, 3/12, 6/12, 1, 2, 3, 5, 7, 10, 20, 30]
c = Yields.CMT(rates,mats)
rates = [0.01, 0.01, 0.03, 0.05, 0.07, 0.16, 0.35, 0.92, 1.40, 1.74, 2.31, 2.41] ./ 100
mats = [1 / 12, 2 / 12, 3 / 12, 6 / 12, 1, 2, 3, 5, 7, 10, 20, 30]
c = Yields.CMT(rates, mats)

m = HullWhite(.1,.001,c)
m = HullWhite(0.1, 0.01, c)

s = ScenarioGenerator(
.1, # timestep
30., # projection horizon
m
0.1, # timestep
30.0, # projection horizon
m,
StableRNG(1)
)

g = GaussianCopula([1. 0.9; 0.9 1.])
c = Correlated(ss,g,StableRNG(1))
g = GaussianCopula([1.0 0.9; 0.9 1.0])
c = Correlated([s, s], g, StableRNG(1))

x = collect(c)

ρ = cor(hcat(ratio.(x)...))
@test ρ[1,2] 0.9 atol = 0.03
ρ = cor(hcat(diff.(map(y->Yields.rate.(y),x))...))
@test ρ[1, 2] 0.9 atol = 0.03
end

@testset "Vasicek" begin
m = Vasicek(0.136,0.0168,0.0119,Yields.Continuous(0.01))
m = Vasicek(0.136, 0.0168, 0.0119, Yields.Continuous(0.01))
s = ScenarioGenerator(
1., # timestep
30., # projection horizon
m
)
g = GaussianCopula([1. 0.9; 0.9 1.])
c = Correlated(ss,g,StableRNG(1))
.1, # timestep
30.0, # projection horizon
m
)
g = GaussianCopula([1.0 0.9; 0.9 1.0])
c = Correlated([s, s], g, StableRNG(1))

x = collect(c)

ρ = cor(hcat(ratio.(x)...))
@test ρ[1,2] 0.9 atol = 0.03
ρ = cor(hcat(diff.(map(y->Yields.rate.(y),x))...))
@test ρ[1, 2] 0.9 atol = 0.03
end
@testset "CIR" begin
m = CoxIngersollRoss(0.136,0.0168,0.0119,Yields.Continuous(0.01))
m = CoxIngersollRoss(0.136, 0.0168, 0.0119, Yields.Continuous(0.01))
s = ScenarioGenerator(
1., # timestep
30., # projection horizon
m
)
g = GaussianCopula([1. 0.9; 0.9 1.])
c = Correlated(ss,g,StableRNG(1))
.1, # timestep
30.0, # projection horizon
m
)
g = GaussianCopula([1.0 0.9; 0.9 1.0])
c = Correlated([s, s], g, StableRNG(1))

x = collect(c)

ρ = cor(hcat(ratio.(x)...))
@test ρ[1,2] 0.9 atol = 0.03
ρ = cor(hcat(diff.(map(y->Yields.rate.(y),x))...))
@test ρ[1, 2] 0.9 atol = 0.03


@test first(YieldCurve.(c)) isa Yields.AbstractYield
end
end
end

0 comments on commit d35aad1

Please sign in to comment.