SWIM

Scenario Weights for Importance Measurement (SWIM) - an R Package for Sensitivity Analysis

https://github.com/spesenti/swim

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 2 DOI reference(s) in README
  • Academic publication links
  • Committers with academic emails
    1 of 5 committers (20.0%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.1%) to scientific vocabulary
Last synced: 11 months ago · JSON representation

Repository

Scenario Weights for Importance Measurement (SWIM) - an R Package for Sensitivity Analysis

Basic Info
  • Host: GitHub
  • Owner: spesenti
  • Language: HTML
  • Default Branch: master
  • Size: 35.2 MB
Statistics
  • Stars: 8
  • Watchers: 0
  • Forks: 1
  • Open Issues: 1
  • Releases: 0
Created about 7 years ago · Last pushed over 4 years ago
Metadata Files
Readme

README.Rmd

---
# author: Silvana M. Pesenti
# date: June 01, 2019
output: 
  rmarkdown::html_vignette:
    md_extensions: [ 
      "-autolink_bare_uris" 
    ]
---



```{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) :

> 
;

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\%.
 
```{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)
```

Owner

  • Name: Silvana Pesenti
  • Login: spesenti
  • Kind: user
  • Location: Toronto
  • Company: Univeristy of Toronto

Assistant Professor Insurance Risk Management

GitHub Events

Total
  • Watch event: 1
Last Year
  • Watch event: 1

Committers

Last synced: over 2 years ago

All Time
  • Total Commits: 487
  • Total Committers: 5
  • Avg Commits per committer: 97.4
  • Development Distribution Score (DDS): 0.622
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Silvana Pesenti s****i@h****m 184
Silvana Pesenti s****i@u****a 157
Kent Wu k****f@g****m 76
judymao 4****o 42
pmilloss 4****s 28
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 1
  • Total pull requests: 73
  • Average time to close issues: N/A
  • Average time to close pull requests: about 4 hours
  • Total issue authors: 1
  • Total pull request authors: 4
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.07
  • Merged pull requests: 69
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • stanleyrazor (1)
Pull Request Authors
  • spesenti (58)
  • kentwhf (8)
  • pmilloss (4)
  • judymao (3)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 245 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 5
  • Total maintainers: 1
cran.r-project.org: SWIM

Scenario Weights for Importance Measurement

  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 245 Last month
Rankings
Forks count: 21.9%
Stargazers count: 24.2%
Dependent packages count: 29.8%
Average: 33.8%
Dependent repos count: 35.5%
Downloads: 57.6%
Maintainers (1)
Last synced: 11 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.5.0 depends
  • Hmisc * imports
  • Rdpack >= 0.7 imports
  • ggplot2 * imports
  • nleqslv * imports
  • plyr * imports
  • reshape2 * imports
  • stats * imports
  • Weighted.Desc.Stat * suggests
  • bookdown * suggests
  • ggpubr * suggests
  • knitr * suggests
  • mvtnorm * suggests
  • rmarkdown * suggests
  • spelling * suggests
  • testthat * suggests