-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
147 lines (108 loc) · 6.67 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
# author: Silvana M. Pesenti
# date: June 01, 2019
output:
rmarkdown::html_vignette:
md_extensions: [
"-autolink_bare_uris"
]
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE, cache = FALSE}
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](https://travis-ci.org/spesenti/SWIM.svg?branch=master)](https://travis-ci.org/spesenti/SWIM) [![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/SWIM)](https://cran.r-project.org/package=SWIM) [![downloads](https://cranlogs.r-pkg.org/badges/grand-total/SWIM)](https://cran.r-project.org/package=SWIM)
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"](https://openaccess.city.ac.uk/id/eprint/18896/) and and Pesenti S.M. (2021) ["Reverse Sensitivity Analysis for Risk Modelling"](https://www.ssrn.com/abstract=3878879).
## 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/](https://openaccess.city.ac.uk/id/eprint/25845/).
## Installation
The SWIM package can be installed from [CRAN](https://CRAN.R-project.org/package=SWIM) :
>
<https://CRAN.R-project.org/package=SWIM>;
alternatively from [GitHub](https://github.com/spesenti/SWIM):
> <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\%.
```{r example, cache = TRUE}
# 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.
```{r summary, echo = FALSE, cache = TRUE}
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.
```{r plot-cdf, cache = TRUE}
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.
```{r sensitivity, cache = TRUE}
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:
```{r sensitivity sub-portfolios, cache = TRUE}
# 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*.
```{r importance rank, cache = TRUE}
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.
```{r plot-cdf-input, cache = TRUE}
library(spatstat)
plot_cdf(object = rev.stress, xCol = 5, base = TRUE)
plot_hist(object = rev.stress, xCol = 5, base = TRUE)
```