Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jaantollander/conditionalvalueatrisk
Provides a concrete Julia implementation for computing the conditional value-at-risk (aka expected shortfall) for discrete probability distributions. Also works as a pseudocode for other languages.
https://github.com/jaantollander/conditionalvalueatrisk
coherent-risk-measure conditional-value-at-risk expected-shortfall risk-measure
Last synced: about 1 month ago
JSON representation
Provides a concrete Julia implementation for computing the conditional value-at-risk (aka expected shortfall) for discrete probability distributions. Also works as a pseudocode for other languages.
- Host: GitHub
- URL: https://github.com/jaantollander/conditionalvalueatrisk
- Owner: jaantollander
- License: mit
- Created: 2020-09-14T11:35:26.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-09-21T06:05:14.000Z (over 3 years ago)
- Last Synced: 2024-05-01T13:26:52.179Z (8 months ago)
- Topics: coherent-risk-measure, conditional-value-at-risk, expected-shortfall, risk-measure
- Language: Julia
- Homepage: https://jaantollander.com/post/measuring-tail-risk-using-conditional-value-at-risk/
- Size: 71.3 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Conditional Value-at-Risk
![](images/distributions.svg)You can copy The Julia code from the file [`conditional-value-at-risk`](conditional-value-at-risk.jl). The example below describes the implementation and how to use it. This repository is related to my article [*Measuring Tail-Risk Using Conditional Value at Risk*](https://jaantollander.com/post/measuring-tail-risk-using-conditional-value-at-risk/), which discusses the definition, properties and implementation of conditional value at risk in more detail.
We can implement the value-at-risk and conditional value-at-risk functions in [Julia](https://julialang.org/) for discrete probability distributions as follow.
```julia
"""Value-at-risk."""
function value_at_risk(x::Vector{Float64}, f::Vector{Float64}, α::Float64)
i = findfirst(p -> p≥α, cumsum(f))
if i === nothing
return x[end]
else
return x[i]
end
end"""Conditional value-at-risk."""
function conditional_value_at_risk(x::Vector{Float64}, f::Vector{Float64}, α::Float64)
x_α = value_at_risk(x, f, α)
if iszero(α)
return x_α
else
tail = x .≤ x_α
return (sum(x[tail] .* f[tail]) - (sum(f[tail]) - α) * x_α) / α
end
end
```Let us create a random discrete probability distribution.
```julia
normalize(v) = v ./ sum(v)
scale(v, low, high) = v * (high - low) + low
n = 10
x = sort(scale.(rand(n), -1.0, 1.0))
f = normalize(rand(n))
α = 0.05
```Next, we assert that the inputs are valid. Note that the states `x` do not have to be unique for the formulation to work.
```julia
@assert issorted(x)
@assert all(f .≥ 0)
@assert sum(f) ≈ 1
@assert 0 ≤ α ≤ 1
```Then, executing the function in Julia REPL gives us a result.
```text
julia> conditional_value_at_risk(x, f, α)
-0.9911100750623101
```