Examples
In the following, some simple usage examples are given.
Basics
We will start with some simple basics that are necessary for all following examples.
Besides the actual rearrangement_algorithm
package, we will use the
scipy.stats module
to specify the marginal distributions.
The .ppf
method of a distribution implements the quantile function, which we
will heavily use in the following examples.
Basic Rearrange
1import numpy as np
2import rearrangement_algorithm as ra
3
4# create a matrix (10x3) where each columns contains numbers 0 to .9
5X = np.tile(np.linspace(0, 1, 10), (3, 1)).T
6print(X)
7
8# the row sums are varying a lot
9print(np.sum(X, axis=1))
10
11# rearrange it
12X_ra = ra.basic_rearrange(X, min)
13print(X_ra)
14
15# the row sums are now very balanced
16print(np.sum(X_ra, axis=1))
Comonotonic Rearrangement
The first example is to generate a comonotonic rearrangement of random variables with given marginals. For the following example, we will use \(X_1\sim\exp(1)\), \(X_2\sim\mathcal{N}(0, 1)\), and \(X_3\sim\mathcal{U}[0, 1]\).
The important function to create the comonotonic rearrangement is
rearrangement_algorithm.create_comonotonic_ra()
.
1from scipy import stats
2import rearrangement_algorithm as ra
3
4# create the random variables
5X1 = stats.expon() # exponential distribution
6X2 = stats.norm() # normal distribution
7X3 = stats.uniform() # uniform distribution
8
9# quantile functions are given by the .ppf method in scipy
10qf = [X1.ppf, X2.ppf, X3.ppf]
11
12# create rearrangement
13level = 0.5
14X_ra_lower, X_ra_upper = ra.create_comonotonic_ra(level, qf, 10)
15
16# all columns are ordered in increasing order
17print(X_ra_lower)
18print(X_ra_upper)
Bounds on the Value-at-Risk (VaR)
One relevant use case of the rearrangement algorithm is the numerical approximation of bounds on the value-at-risk (VaR) of dependent risks.
The following example illustrates the use of the
rearrangement_algorithm.bounds_VaR()
function using the following
example from the paper “Computation of sharp bounds on the distribution of a
function of dependent risks” (Puccetti, Rüschendorf, 2012).
Given three Pareto(2)-distributed random variables, we calcuate the lower bound on the VaR (based on the sum of the three random variables). For comparison, the numbers can be found in the above paper (Table 1).
1from scipy import stats
2import rearrangement_algorithm as ra
3
4# create the quantile functions
5qf = [stats.pareto(2, loc=-1).ppf]*3
6
7# set the parameters
8alpha = 0.5102
9num_steps = 500
10
11# calcuate the bounds on the VaR
12lower_under, lower_over = ra.bounds_VaR(1.-alpha, qf, method="lower",
13 num_steps=num_steps)
14VaR_under = lower_under[0]
15VaR_over = lower_over[0]
16
17# the expected value is around 0.5
18expected = 0.5
19print("{:.5f} <= {:.3f} <= {:.5f}".format(VaR_under, expected, VaR_over))
Bounds on the Expected Value
TODO
Bounds on the Survival Probability
TODO