Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkuperman committed Oct 5, 2021
1 parent a9ca46e commit 6c83ec6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion examples/mixture_normal_var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using MarketRisk
ω = [0.75, 0.25]
μ = [0.0, -0.1]
Σ = [0.2, 0.4]
αs = [0.001, 0.01, 0.05, 0.1]
αs = 0.001
h = 10 / 250
nus = [10., 5.]

Expand Down
4 changes: 2 additions & 2 deletions src/expected_tail_loss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ function ExpectedTailLoss(μ, Σ, h, α, distribution)
return ParametricExpectedTailLoss(μ, Σ, h, α, distribution)
end

function compute(etl::ParametricExpectedTailLoss{M, S, H, A, D}) where {M, S, H, A, D <: Normal}
function compute(etl::ParametricExpectedTailLoss{M, S, H, A, D}) where {M<:Real, S<:Real, H<:Real, A<:Real, D<:Normal}
@unpack μ, Σ, h, α, dist = etl

= quantile(dist, α)

return 1 / α * pdf(dist, xα) * sqrt(h) * Σ - h * μ
end

function compute(etl::ParametricExpectedTailLoss{M, S, H, A, D}) where {M, S, H, A, D <: Normal}
function compute(etl::ParametricExpectedTailLoss{M, S, H, A, D}) where {M<:Real, S<:Real, H<:Real, A<:Array{Real}, D <: Normal}
@unpack μ, Σ, h, α, dist = etl

etl = Float64[]
Expand Down
4 changes: 2 additions & 2 deletions src/value_at_risk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function compute(var::ParametricValueAtRisk{V, M, H, A, D}) where {V, M, H<:Real
return var
end

function compute(var::ParametricValueAtRisk{V, M, H, A, MixtureModel{B, D, Normal, F}}) where {V, M, H, A<:Real, B, D, F}
function compute(var::ParametricValueAtRisk{V, M, H, A, MixtureModel{B, D, Normal, F}}) where {V, M, H, A, B, D, F}
@unpack μ, Σ, h, α, dist = var

function opt_func(x, α)
Expand All @@ -57,7 +57,7 @@ function compute(var::ParametricValueAtRisk{V, M, H, A, MixtureModel{B, D, Norma

D_opt(f) = x -> ForwardDiff.derivative(f, float(x))

return - find_zero((x -> opt_func(x, αᵢ), D_opt(opt_func)), 0.0, Roots.Newton())
return - find_zero((x -> opt_func(x, α), D_opt(x -> opt_func(x, α))), 0.0, Roots.Newton())
end

function compute(var::ParametricValueAtRisk{V, M, H, A, MixtureModel{B, D, Normal, F}}) where {V, M, H, A<:Array{Float64}, B, D, F}
Expand Down
29 changes: 17 additions & 12 deletions test/etl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ using MarketRisk
using Test

# Test normal ETL
μ_n = 0.0
Σ_n = 0.3
μ_en = 0.0
Σ_en = 0.3

h_n = 10 / 250
α_n = 0.01
h_en = 10 / 250
α_en = 0.01

etl_n = normal_etl(μ_n, Σ_n, h_n, α_n)
N_en = Normal()

@test etl_n 0.1599129
etl_en = ExpectedTailLoss(μ_en, Σ_en, h_en, α_en, N_en)

@test compute(etl_en) 0.15991285322074647

# Test t-Student ETL
μ_t = 0.0
Σ_t = 0.3
μ_et = 0.0
Σ_et = 0.3

ν_et = 5

h_t = 10 / 250
α_t = 0.01
h_et = 10 / 250
α_et = 0.01

etl_t = normal_etl(μ_t, Σ_t, h_t, α_t)
T_et = TDist(ν_et)

@test etl_t 0.1599129
etl_et = ExpectedTailLoss(μ_et, Σ_et, h_et, α_et, T_et)

@test compute(etl_et) 0.23441282083322523
1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ using Test

@testset "VaR tests" begin include("var.jl") end
@testset "ETL tests" begin include("etl.jl") end

18 changes: 3 additions & 15 deletions test/var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ N_n = Normal()

var_n = ValueAtRisk(m_n, s_n, h_n, α_n, N_n)

@test compute(var_n) 1.490889
@test compute(var_n) 1.4908892538487946

# Test t-Student VaR
μ_t = 0.0
Expand All @@ -31,7 +31,7 @@ T_t = TDist(ν_t)

var_t = ValueAtRisk(μ_t, Σ_t, h_t, α_t, T_t)

@test compute(var_t) 0.15638784
@test compute(var_t) 0.15638781416305675


# Test mixture normal VaR
Expand All @@ -45,17 +45,5 @@ m_nm = MixtureModel(Normal, [(h_nm * μ_nm[i], sqrt(h_nm) * Σ_nm[i]) for i in 1

var_nm = ValueAtRisk(μ_nm, Σ_nm, h_nm, α_nm, m_nm)

@test compute(var_nm) 0.21616624
@test compute(var_nm) 0.21616624418759633

# Test mixture t-Student VaR
ω_tm = [0.75, 0.25]
μ_tm = [0.0, - 0.1]
Σ_tm = [0.2, 0.4]
α_tm = 0.001
h_tm = 10 / 250

m_tm = MixtureModel(Normal, [(h_tm * μ_tm[i], sqrt(h_tm) * Σ_tm[i]) for i in 1:2], ω_tm)

var_tm = ValueAtRisk(μ_tm, Σ_tm, h_tm, α_tm, m_tm)

@test compute(var_tm) 0.3451456

0 comments on commit 6c83ec6

Please sign in to comment.