knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

SWIM - A Package for Sensitivity Analysis

Travis-CI Build Status CRAN_Status_Badge downloads

The SWIM package provides weights on simulated scenarios from a stochastic model, such that stressed model components (random variables) fulfil given probabilistic constraints (e.g. specified values for risk measures), under the new scenario weights. Scenario weights are selected by constrained minimisation of the relative entropy to the baseline model. The SWIM package is based on the papers Pesenti S.M, Millossovich P., Tsanakas A. (2019) "Reverse Sensitivity Testing: What does it take to break the model" and and Pesenti S.M. (2021) "Reverse Sensitivity Analysis for Risk Modelling".

Vignette

The Vignette of the SWIM package is available in html format utstat.toronto.edu/pesenti/SWIMVignette/ and as pdf https://openaccess.city.ac.uk/id/eprint/25845/.

Installation

The SWIM package can be installed from CRAN :

https://CRAN.R-project.org/package=SWIM;

alternatively from GitHub:

https://github.com/spesenti/SWIM

Scope of the SWIM package

The SWIM package provides sensitivity analysis tools for stressing model components (random variables). Implemented stresses using the relative Entropy (Kullback-Leibler divergence) are:

R functions | Stress ----------------------- | ------------------------------- stress() | A wrapper for the stress_ functions stress_VaR() | VaR risk measure, a quantile stress_VaR() | VaR risk measure, a quantile stress_VaR_ES() | VaR and ES risk measures stress_mean() | means stress_mean_sd() | means and standard deviations stress_moment() | moments, functions of moments stress_prob() | probabilities of intervals stress_user() | user defined scenario weights

Implemented stresses using the 2-Wasserstein distance are:

| R functions | Stress
| ------------------------ | ------------------------------------------- | stress_wass() | A wrapper for the stress_w functions
| stress_RM_w() | Risk measure
| stress_RM_mean_sd_w() | Risk measure, means and standard deviations | stress_HARA_RM_w() | Risk measure and HARA utility
| stress_mean_sd_w() | means and standard deviations
| stress_mean_w() | means

Implemented functions allow to graphically display the change in the probability distributions under different stresses and the baseline model as well as calculating sensitivity measures.

Example - Stressing the VaR of a portfolio

Consider a portfolio Y = X1 + X2 + X3 + X4 + X5, where (X1, X2, X3, X4, X5) are correlated normally distributed with equal mean and different standard deviations. We stress the VaR (quantile) of the portfolio loss Y at levels 0.75 and 0.9 with an increase of 10\%.

 # simulating the portfolio 
library(SWIM)
set.seed(0)
SD <- c(70, 45, 50, 60, 75)
Corr <- matrix(rep(0.5, 5^2), nrow = 5) + diag(rep(1 - 0.5, 5))
x <- mvtnorm::rmvnorm(10^5, 
   mean =  rep(100, 5), 
   sigma = (SD %*% t(SD)) * Corr)
data <- data.frame(rowSums(x), x)
names(data) <- c("Y", "X1", "X2", "X3", "X4", "X5")
 # stressing the portfolio 
rev.stress <- stress(type = "VaR", x = data, 
   alpha = c(0.75, 0.9), q_ratio = 1.1, k = 1)

Summary statistics of the baseline and the stressed model can be obtained via the summary() method.

lapply(summary(rev.stress, base = TRUE), FUN = knitr::kable, digits = 2)

Visual display of the change of empirical distribution functions of the portfolio loss Y from the baseline to the two stressed models.

library(spatstat)
plot_cdf(object = rev.stress, xCol = 1, base = TRUE)

Sensitivity and importance rank of portfolio components

Sensitivity measures allow to assess the importance of the input components. Implemented sensitivity measures are the Kolmogorov distance, the Wasserstein distance and Gamma. Gamma, the Reverse Sensitivity Measure, defined for model component Xi, i = 1, ..., 5, and scenario weights w by

Gamma = ( E(Xi * w) - E(Xi) ) / c,

where c is a normalisation constant such that |Gamma| <= 1, see https://doi.org/10.1016/j.ejor.2018.10.003. Loosely speaking, the Reverse Sensitivity Measure is the normalised difference between the first moment of the stressed and the baseline distributions of Xi.

knitr::kable(sensitivity(rev.stress, type = "all"), digits = 2)
plot_sensitivity(rev.stress, xCol = 2:6, type = "Gamma") 

Sensitivity to all sub-portfolios, (Xi + Xj), i,j = 1, ..., 6:

 # sub-portfolios
f <- rep(list(function(x)x[1] + x[2]), 10)
k <- list(c(2, 3), c(2, 4), c(2, 5), c(2, 6), c(3, 4), c(3, 5), c(3, 6), c(4, 5), c(4, 6), c(5, 6))
importance_rank(rev.stress, xCol = NULL, wCol = 1, type = "Gamma", f = f, k = k)

Ranking the input components according to the chosen sensitivity measure, in this example using Gamma.

importance_rank(rev.stress, xCol = 2:6, type = "Gamma")

Visual display of the change of empirical distribution functions and density from the baseline to the two stressed models of X5, the portfolio component with the largest sensitivity. Stressing the portfolio loss Y, results in a distribution function of X5 that has a heavier tail.

library(spatstat)
plot_cdf(object = rev.stress, xCol = 5, base = TRUE)
plot_hist(object = rev.stress, xCol = 5, base = TRUE)


spesenti/SWIM documentation built on Jan. 15, 2022, 11:19 a.m.