Title: | Scenario Weights for Importance Measurement |
---|---|
Description: | An efficient sensitivity analysis for stochastic models based on Monte Carlo samples. Provides weights on simulated scenarios from a stochastic model, such that stressed 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 Pesenti S.M., Millossovich P., Tsanakas A. (2019) "Reverse Sensitivity Testing: What does it take to break the model" <openaccess.city.ac.uk/id/eprint/18896/> and Pesenti S.M. (2021) "Reverse Sensitivity Analysis for Risk Modelling" <https://www.ssrn.com/abstract=3878879>. |
Authors: | Silvana M. Pesenti [aut, cre] |
Maintainer: | Silvana M. Pesenti <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2025-02-07 04:28:39 UTC |
Source: | https://github.com/spesenti/swim |
Provides the empirical distribution function of a stressed SWIM model component (random variable) or KDE distribution of a stressed SWIMw model component under the scenario weights.
cdf(object, xCol = 1, wCol = 1)
cdf(object, xCol = 1, wCol = 1)
object |
A |
xCol |
Numeric or character, (name of) the columns of the underlying data
of the |
wCol |
Numeric, the columns of the scenario weights
of the |
The empirical or KDE distribution function (a function) of
the xCol
component of the stressed model with weights
wCol
. The empirical distribution function can be
evaluated at a vector.
Silvana M. Pesenti, Zhuomin Mao
See plot_cdf
for plotting the empirical or KDE
distribution function of the stressed model and
quantile_stressed
for sample quantiles of
a stressed model.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) grid <- seq(min(x$normal), max(x$normal), length.out = 5) ## stressed empirical distribution function cdf(res1, xCol = 1, wCol = 1)(grid) ## baseline empirical distribution function ecdf(x$normal)(grid)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) grid <- seq(min(x$normal), max(x$normal), length.out = 5) ## stressed empirical distribution function cdf(res1, xCol = 1, wCol = 1)(grid) ## baseline empirical distribution function ecdf(x$normal)(grid)
Provides the empirical distribution of a stressed model component (random variable) under the scenario weights
cdf_stressed(object, xCol = 1, wCol = "all", grid, base = FALSE)
cdf_stressed(object, xCol = 1, wCol = "all", grid, base = FALSE)
object |
A |
xCol |
Numeric, (name of) the column of the underlying data
of the |
wCol |
Vector or characters, the columns of the scenario weights
of the |
grid |
Vector, the empirical distribution of the |
base |
Logical, if TRUE, statistics under the baseline are also returned (default = "FALSE").] |
The cdf_stressed
returns the values of the empirical distribution function
of the xCol
model components for weights wCol
. In contrast, the cdf
function
returns a function, analogous to the ecdf
from the base package. The
function cdf_stressed
is the cdf
function applied to grid.
A matrix containing the empirical distribution function
applied to grid
of the xCol
components of the
stressed model with weights wCol
.
Kent Wu
See cdf
for the empirical distribution function of a stressed
model component and quantile_stressed
for sample quantiles of
a stressed model.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) grid <- seq(min(x$normal), max(x$normal), length.out = 5) ## stressed empirical distribution function cdf_stressed(res1, xCol = 1, wCol = "all", grid = grid)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) grid <- seq(min(x$normal), max(x$normal), length.out = 5) ## stressed empirical distribution function cdf_stressed(res1, xCol = 1, wCol = "all", grid = grid)
Convert SWIMw to SWIM
convert_SWIMw_to_SWIM(object)
convert_SWIMw_to_SWIM(object)
object |
A |
Convert a SWIMw object into a SWIM object
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "VaR"
;
specs
, a list, each component corresponds to
a different stress and contains k
,
alpha
and q
.
See SWIM
for details.
Kent Wu
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) convert_SWIMw_to_SWIM(res1) ## End(Not run)
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) convert_SWIMw_to_SWIM(res1) ## End(Not run)
Provides the correlation of stressed model components (random variable) under the scenario weights.
cor_stressed( object, xCol = c(1, 2), wCol = "all", method = "Pearson", base = FALSE )
cor_stressed( object, xCol = c(1, 2), wCol = "all", method = "Pearson", base = FALSE )
object |
A |
xCol |
Numeric or character vector, (names of) the columns of
the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
method |
Character, one of |
base |
Logical, if |
cor_stressed
: The correlation coefficient of
stressed model components, subject to the calculated scenario weights.
cor_stressed
returns a list of correlation matrices
corresponding to different stresses. Entries of the matrices denote
correlation coefficients between stressed model components specified by xCol
.
Kent Wu
See var_stressed
and sd_stressed
compute
stressed variance and standard deviations under the scenario weights, respectively.
See cor
for unweighted correlations between model components, while
cor_stressed
return correlations between stressed model components
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed correlation cor_stressed(res1, xCol = c(1, 2), wCol = 1, base=TRUE)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed correlation cor_stressed(res1, xCol = c(1, 2), wCol = 1, base=TRUE)
A dataset containing total aggregate losses from three sub-portfolios, generated through a binomial credit model.
credit_data
credit_data
A data frame with 100,000 rows and 7 variables:
total aggregate loss of a portfolio consisting of three homogeneous sub-portfolios L1, L2 and L3
aggregate loss of sub-portfolio 1
aggregate loss of sub-portfolio 2
aggregate loss of sub-portfolio 3
(conditional) default probability of sub-portfolio 1
(conditional) default probability of sub-portfolio 2
(conditional) default probability of sub-portfolio 3
For a detailed case study of the credit data set using SWIM
see
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Provides the Value-at-Risk (VaR) and the Expected Shortfall (ES) for components (random variables) of a stochastic model.
ES_stressed( object, alpha = 0.95, xCol = "all", wCol = 1, base = FALSE, gamma = NULL ) VaR_stressed(object, alpha = 0.95, xCol = "all", wCol = 1, base = FALSE)
ES_stressed( object, alpha = 0.95, xCol = "all", wCol = 1, base = FALSE, gamma = NULL ) VaR_stressed(object, alpha = 0.95, xCol = "all", wCol = 1, base = FALSE)
object |
A |
alpha |
Numeric vector, the levels of the stressed VaR and ES
( |
xCol |
Numeric or character vector, (names of) the columns of
the underlying data
of the |
wCol |
Numeric, the column of the scenario weights
of the |
base |
Logical, if |
gamma |
Function that defines the gamma of the risk measure. If null,
the Expected Shortfall (ES) will be used. |
ES_stressed
: The ES of a stressed model is the
ES of a chosen stressed model component, subject to the calculated scenario
weights. The ES at level alpha
of a stressed model
component is given by:
where VaR_u^W
is the VaR of the stressed model component, defined below.
VaR_stressed
: The VaR of a model is the VaR (quantile) of
a chosen stressed model component, subject to the calculated scenario weights.
The VaR at level alpha
of a stressed model component with
stressed distribution function F^W is defined as its
left-quantile at alpha:
The function VaR_stressed
provides the empirical quantile, whereas
the function quantile_stressed
calculates quantiles of model
components with different interpolations.
ES_stressed
: Returns a matrix with the empirical or KDE
ES's at level alpha
of
model components specified in xCol
, under the scenario weights
wCol
.
VaR_stressed
: Returns a matrix with the empirical or KDE VaR's
at level alpha
of
model components specified in xCol
, under the scenario weights
wCol
.
ES_stressed
: Expected Shortfall of a stressed model
VaR_stressed
: Value-at-Risk of a stressed model.
Silvana M. Pesenti, Zhuomin Mao
See quantile_stressed
for quantiles other than the
empirical quantiles and cdf
for the empirical or KDE distribution
function of a stressed model.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed ES quantile_stressed(res1, probs = seq(0.9, 0.99, 0.01), xCol = 1, wCol = 2, type = "i/n") quantile(x[, 1], probs = seq(0.9, 0.99, 0.01), type = 1) VaR_stressed(res1, alpha = seq(0.9, 0.99, 0.01), xCol = 1, wCol = 2, base = TRUE) ## the ES of both model components under stress one ES_stressed(res1, alpha = seq(0.9, 0.99, 0.01), xCol = "all", wCol = 1) ## the ES of both model components under stress two ES_stressed(res1, alpha = seq(0.9, 0.99, 0.01), xCol = "all", wCol = 2)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed ES quantile_stressed(res1, probs = seq(0.9, 0.99, 0.01), xCol = 1, wCol = 2, type = "i/n") quantile(x[, 1], probs = seq(0.9, 0.99, 0.01), type = 1) VaR_stressed(res1, alpha = seq(0.9, 0.99, 0.01), xCol = 1, wCol = 2, base = TRUE) ## the ES of both model components under stress one ES_stressed(res1, alpha = seq(0.9, 0.99, 0.01), xCol = "all", wCol = 1) ## the ES of both model components under stress two ES_stressed(res1, alpha = seq(0.9, 0.99, 0.01), xCol = "all", wCol = 2)
Extracting the data (realisations of the stochastic model), the
scenario weights, the functions generating the scenario weights,
or the specifications of the stress from an object of class
SWIM
or SWIMw
.
get_data(object, xCol = "all") get_weights(object, wCol = "all") get_weightsfun(object, wCol = "all") get_specs(object, wCol = "all") summary_weights(object, wCol = "all")
get_data(object, xCol = "all") get_weights(object, wCol = "all") get_weightsfun(object, wCol = "all") get_specs(object, wCol = "all") summary_weights(object, wCol = "all")
object |
A |
xCol |
Numeric or character vector, (names of) the columns of
the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
get_data
: A data.frame containing the realisations of
the stochastic model on which the object
is based.
get_weights:
A data.frame containing the scenario
weights of the object
. Columns corresponds
to different stresses.
get_weightsfun
: A list containing functions, which,
when applied to a column of the data, generate the
scenario weights of the object
. The corresponding stressed
columns can be obtained via get_specs
.
Use get_weights
if the SWIM
object only contains
scenario weights and not a list of functions.
get_specs
: A data.frame containing specifications
of the stresses with each row corresponding to a different
stress. Only a selection of the specifications is returned;
however, all input variables are stored in the object
.
See also SWIM
.
summary_weights
: print a list containing summary statistics
of the stresses with each element being a table for a different stress.
The summary statistics include minimum, maximum, standard deviation,
Gini coefficient, entropy and effective sample size.
Gini coefficient uses the formula .
Effective Sample Size is equal to n / (1+Var(W)), see Equation (9.13) in Owen, Art B. "Monte Carlo theory, methods and examples." (2013).
get_data
: extracting data.
get_weights
: extracting scenario weights.
get_weightsfun
: extracting weight functions.
get_specs
: extracting information of the stress.
summary_weights
: extracting summary statistics of scenario weights.
Silvana M. Pesenti
## continuing example in stress_VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05, k = 1) ## returning the underlying data all(get_data(res1) == x) ## the scenario weights w <- get_weights(res1) get_weightsfun(res1) get_specs(res1) ## now add a stress on the means of both variables res1 <- stress(type = "mean", x = res1, k = 1:2, new_means = c(0.5,1.5)) get_specs(res1) ## the required moments for a stress of type "mean" are not displayed ## the type of stress and the specs for the second stress can be ## extracted directly from the SWIM object. res1$type[[2]] res1$specs[[2]]
## continuing example in stress_VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05, k = 1) ## returning the underlying data all(get_data(res1) == x) ## the scenario weights w <- get_weights(res1) get_weightsfun(res1) get_specs(res1) ## now add a stress on the means of both variables res1 <- stress(type = "mean", x = res1, k = 1:2, new_means = c(0.5,1.5)) get_specs(res1) ## the required moments for a stress of type "mean" are not displayed ## the type of stress and the specs for the second stress can be ## extracted directly from the SWIM object. res1$type[[2]] res1$specs[[2]]
Provides the importance ranks of the components (random variables) of a stressed model for different sensitivity measures.
importance_rank( object, xCol = "all", wCol = "all", type = c("Gamma", "Wasserstein", "reverse", "all"), f = NULL, k = NULL, s = NULL )
importance_rank( object, xCol = "all", wCol = "all", type = c("Gamma", "Wasserstein", "reverse", "all"), f = NULL, k = NULL, s = NULL )
object |
A |
xCol |
Numeric or character vector, (names of) the columns
of the underlying data of the |
wCol |
Vector, the columns of the scenario weights
of the |
type |
Character, one of |
f |
A function, or list of functions, that, applied to
|
k |
A vector or list of vectors, same length as |
s |
A function that, applied to |
For the definition of the sensitivity
measures (type
), see sensitivity
.
A data.frame containing the importance ranks of the
stressed model for different sensitivity measures. Small values
correspond to large sensitivities. Different rows correspond
to different random variables. The first two rows specify the
stress
and type
of the sensitivity measure on
which the ranking is calculated.
Silvana M. Pesenti
See sensitivity
for the values of the
sensitivity measures, plot_sensitivity
for plotting
sensitivity measures and summary
for a
summary statistic of a stressed model.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "log-normal" = rlnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) importance_rank(res1, wCol = 1:2, type = "Gamma") ## sensitivity of log-transformed data importance_rank(res1, wCol = 1, type = "all", f = list(function(x)log(x), function(x)log(x)), k = list(1, 2))
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "log-normal" = rlnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) importance_rank(res1, wCol = 1:2, type = "Gamma") ## sensitivity of log-transformed data importance_rank(res1, wCol = 1, type = "all", f = list(function(x)log(x), function(x)log(x)), k = list(1, 2))
Provides the mean of stressed model components (random variables) under the scenario weights.
mean_stressed(object, xCol = "all", wCol = "all", base = FALSE)
mean_stressed(object, xCol = "all", wCol = "all", base = FALSE)
object |
A |
xCol |
Numeric or character vector, (names of) the columns of
the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
base |
Logical, if |
mean_stressed
: Sample mean of chosen stressed model components, subject to the calculated scenario weights.
A matrix containing the means of the xCol
components of the stressed model with weights wCol
.
Kent Wu
See var_stressed
and sd_stressed
compute
stressed variance and standard deviations under the scenario weights, respectively.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed mean mean_stressed(res1, xCol = "all", wCol = "all", base = TRUE)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed mean mean_stressed(res1, xCol = "all", wCol = "all", base = TRUE)
This function is a method
for an object of class
SWIM
.
## S3 method for class 'SWIM' merge(x, y, ...)
## S3 method for class 'SWIM' merge(x, y, ...)
x , y
|
Objects of class |
... |
Additional arguments will be ignored. |
Merges two objects of class SWIM
, that
are based on the same data.
An object of class SWIM
containing:
x
, a data.frame containing the data;
new_weights
, a list, each component corresponds to
a different stress and is either a vector of scenario weights or a
function, that applied to a column of x
, generates the
vectors of scenario weights;
type
, a list, each component corresponds to a
different stress and specifies the type of the stress;
specs
, a list, each component corresponds to
a different stress and contains a list with the specifications
of what has been stressed.
See SWIM
for details.
Silvana M. Pesenti
This function is a method
for an object of class
SWIMw
.
## S3 method for class 'SWIMw' merge(x, y, ...)
## S3 method for class 'SWIMw' merge(x, y, ...)
x , y
|
Objects of class |
... |
Additional arguments will be ignored. |
Merges two objects of class SWIMw
, that
are based on the same data.
A SWIMw
object containing:
x
, a data.frame containing the data;
h
, bandwidths;
u
, vector containing the gridspace on [0, 1]
lam
, vector containing the lambda's of the optimized model
str_fY
, function defining the densities of the stressed component;
str_FY
, function defining the distribution of the stressed component;
str_FY_inv
, function defining the quantiles of the stressed component;
gamma
, function defining the risk measure;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type
, specifies the stress type
specs
, a list, each component corresponds to
a different stress and contains a list with the specifications
of what has been stressed.
See SWIM
for details.
Zhuomin Mao
Plots the empirical distribution function of a stressed SWIM model component (random variable) or KDE distribution function of a stressed SWIMw model component under the scenario weights.
plot_cdf( object, xCol = 1, wCol = "all", base = FALSE, n = 500, x_limits, y_limits, displ = TRUE )
plot_cdf( object, xCol = 1, wCol = "all", base = FALSE, n = 500, x_limits, y_limits, displ = TRUE )
object |
A |
xCol |
Numeric or character, (name of) the column of the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
base |
Logical, if |
n |
Integer, the number of points used to plot
|
x_limits |
Vector, the limits of the x-axis of the plot, the
value for |
y_limits |
Vector, the limits of the y-axis of the plot, the
value for |
displ |
Logical, if |
If displ = TRUE
, a plot displaying the empirical or KDE
distribution function of the stochastic model under the
scenario weights.
If displ = FALSE
, a data.frame for customised plotting with
ggplot
. The data.frame contains the columns: the column,
xCol
, of the data of the stressed model,
stress
(the stresses) and value
(the values).
Denote by res
the return of the function call, then
ggplot
can be called via:
Note that the ggplot2 default of stat_ecdf
does not
take weight
as an aesthetic. We use the workaround
by Nicolas Woloszko, see Note below.
This function is based on the ggplot stat_ecdf
function. However, the stat_ecdf
does not allow for
specifying weights
, thus the function is based on
the workaround by Nicolas Woloszko, see
https://github.com/NicolasWoloszko/stat_ecdf_weighted.
Silvana M. Pesenti, Zhuomin Mao
See cdf
for the empirical or KDE distribution function
of a stressed model and quantile_stressed
for
sample quantiles of a stressed model.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(10 ^ 5), "gamma" = rgamma(10 ^ 5, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.75, 0.95), q_ratio = 1.15) plot_cdf(res1, xCol = 1, wCol = 1:2, base = TRUE) plot_cdf(res1, xCol = 1, wCol = 1:2, base = TRUE, x_limits = c(0, 5), y_limits = c(0.5, 1))
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(10 ^ 5), "gamma" = rgamma(10 ^ 5, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.75, 0.95), q_ratio = 1.15) plot_cdf(res1, xCol = 1, wCol = 1:2, base = TRUE) plot_cdf(res1, xCol = 1, wCol = 1:2, base = TRUE, x_limits = c(0, 5), y_limits = c(0.5, 1))
Plots the histogram of a stressed model component (random variable) under the scenario weights.
plot_hist( object, xCol = 1, wCol = "all", base = FALSE, x_limits, displ = TRUE, binwidth, displLines = FALSE )
plot_hist( object, xCol = 1, wCol = "all", base = FALSE, x_limits, displ = TRUE, binwidth, displLines = FALSE )
object |
A |
xCol |
Numeric or character, (name of) the column of the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
base |
Logical, if |
x_limits |
Vector, the limits of the x-axis of the plot, the
value for |
displ |
Logical, if |
binwidth |
Numeric, the width of the bins used to plot
the histogram, the |
displLines |
Logical, if |
If displ = TRUE
, a histogram of the stochastic model
under the scenario weights.
If displ = FALSE
, a data.frame for customised plotting with
ggplot
. The data.frame contains the columns: the column,
xCol
, of the data of the stressed model, stress
(the stresses) and value
(the values).
Denote by res
the return of the function call, then
ggplot
can be called via:
See cdf
and plot_cdf
for
values and plotting of the empirical distribution
function of a stressed model, respectively, and
quantile_stressed
for sample quantiles of
a stressed model.
## example with a stress on VaR set.seed(0) x <- data.frame("gamma" = rgamma(10^5, shape = 2)) res1 <- stress(type = "VaR", x = x, alpha = c(0.75, 0.95), q_ratio = 1.1) plot_hist(res1, xCol = "gamma", wCol = 1:2, base = TRUE, binwidth = 0.4) plot_hist(res1, xCol = "gamma", wCol = 1:2, base = TRUE, binwidth = 0.4, displLines = TRUE)
## example with a stress on VaR set.seed(0) x <- data.frame("gamma" = rgamma(10^5, shape = 2)) res1 <- stress(type = "VaR", x = x, alpha = c(0.75, 0.95), q_ratio = 1.1) plot_hist(res1, xCol = "gamma", wCol = 1:2, base = TRUE, binwidth = 0.4) plot_hist(res1, xCol = "gamma", wCol = 1:2, base = TRUE, binwidth = 0.4, displLines = TRUE)
Plots the empirical quantile function of a stressed SWIM model component (random variable) or KDE quantile function of a stressed SWIMw model component under the scenario weights.
plot_quantile( object, xCol = 1, wCol = "all", base = FALSE, n = 500, x_limits, y_limits, displ = TRUE )
plot_quantile( object, xCol = 1, wCol = "all", base = FALSE, n = 500, x_limits, y_limits, displ = TRUE )
object |
A |
xCol |
Numeric or character, (name of) the column of the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
base |
Logical, if |
n |
Integer, the number of points used to plot
( |
x_limits |
Vector, the limits of the x-axis of the plot, the
value for |
y_limits |
Vector, the limits of the y-axis of the plot, the
value for |
displ |
Logical, if |
If displ = TRUE
, a plot displaying the empirical or KDE
quantile function of the stochastic model under the
scenario weights.
If displ = FALSE
, a data.frame for customised plotting with
ggplot
. The data.frame contains the following columns:
grid
, the grid points to plot the quantiles,
stress
(the stresses) and value
(the quantile values).
Denote by res
the return of the function call, then
ggplot
can be called via:
Silvana M. Pesenti, Zhuomin Mao
See quantile_stressed
for sample quantiles of a
stressed model and plot_cdf
for plotting empirical or KDE
distribution functions under scenario weights.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(10 ^ 5), "gamma" = rgamma(10 ^ 5, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.75, 0.95), q_ratio = 1.15) plot_quantile(res1, xCol = 1, wCol = 1:2, base = TRUE) plot_quantile(res1, xCol = 1, wCol = 1:2, base = TRUE, x_limits = c(0.8, 1), y_limits = c(0, 5))
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(10 ^ 5), "gamma" = rgamma(10 ^ 5, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.75, 0.95), q_ratio = 1.15) plot_quantile(res1, xCol = 1, wCol = 1:2, base = TRUE) plot_quantile(res1, xCol = 1, wCol = 1:2, base = TRUE, x_limits = c(0.8, 1), y_limits = c(0, 5))
Plots the sensitivity measures for components (random variables) of a stochastic model under the scenario weights.
plot_sensitivity( object, xCol = "all", wCol = "all", type = c("Gamma", "Kolmogorov", "Wasserstein", "reverse", "all"), f = NULL, k = NULL, s = NULL, displ = TRUE, p = 1 )
plot_sensitivity( object, xCol = "all", wCol = "all", type = c("Gamma", "Kolmogorov", "Wasserstein", "reverse", "all"), f = NULL, k = NULL, s = NULL, displ = TRUE, p = 1 )
object |
A |
xCol |
Numeric or character vector, (names of) the columns
of the underlying data of the |
wCol |
Vector, the columns of the scenario weights
of the |
type |
Character, one of |
f |
A function, or list of functions, that, applied to
|
k |
A vector or list of vectors, same length as |
s |
A function that, applied to |
displ |
Logical, if |
p |
Numeric vector, the p-th moment of Wasserstein distance ( |
For the definition of the sensitivity
measures (type
), see sensitivity
.
Note that the Kolmogorov distance is the same for all inputs under the same stress for a SWIM object. Thus, it should only be used to compare different stresses, not individual components.
If displ = TRUE
, a plot displaying the sensitivity
measures of the stochastic model under the scenario weights.
If displ = FALSE
, a data.frame for customised plotting with
ggplot
. The data.frame
contains the columns: stress
(the stresses), type
(the types of sensitivity), X_all
(the random variables),
value
(the values of the sensitivities).
Denote by result
the return of the function call, then
ggplot
can be called via:
Silvana M. Pesenti
See sensitivity
for the values of the
sensitivity measures of a stressed model and
importance_rank
for ranking of random
variables according to their sensitivities.
## Consider the portfolio Y = X1 + X2 + X3 + X4 + X5, ## where (X1, X2, X3, X4, X5) are correlated normally ## distributed with equal mean and different standard deviations, ## see the README for further details. 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)) if (!requireNamespace("mvtnorm", quietly = TRUE)) stop("Package \"mvtnorm\" needed for this function to work. Please install it.") 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") rev.stress <- stress(type = "VaR", x = data, alpha = c(0.75, 0.9), q_ratio = 1.1, k = 1) sensitivity(rev.stress, type = "all") plot_sensitivity(rev.stress, xCol = 2:6, type = "Gamma") plot_sensitivity(rev.stress, xCol = 6, wCol = 1, type = "all")
## Consider the portfolio Y = X1 + X2 + X3 + X4 + X5, ## where (X1, X2, X3, X4, X5) are correlated normally ## distributed with equal mean and different standard deviations, ## see the README for further details. 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)) if (!requireNamespace("mvtnorm", quietly = TRUE)) stop("Package \"mvtnorm\" needed for this function to work. Please install it.") 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") rev.stress <- stress(type = "VaR", x = data, alpha = c(0.75, 0.9), q_ratio = 1.1, k = 1) sensitivity(rev.stress, type = "all") plot_sensitivity(rev.stress, xCol = 2:6, type = "Gamma") plot_sensitivity(rev.stress, xCol = 6, wCol = 1, type = "all")
Plots the scenario weights of a stressed model against a model component.
plot_weights( object, xCol = 1, wCol = "all", n, x_limits, y_limits, displ = TRUE )
plot_weights( object, xCol = 1, wCol = "all", n, x_limits, y_limits, displ = TRUE )
object |
A |
xCol |
Numeric or character, (name of) the column of the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
n |
Integer, the number of points used to plot
( |
x_limits |
Vector, the limits of the x-axis of the plot, the
value for |
y_limits |
Vector, the limits of the y-axis of the plot, the
value for |
displ |
Logical, if |
If displ = TRUE
, a plot displaying the scenario
weights of a stochastic model against a model component.
If displ = FALSE
, a data.frame for customised plotting with
ggplot
. The data.frame contains the following columns:
grid
, the grid points to plot the quantiles,
stress
(the stresses) and value
(the quantile values).
Denote by res
the return of the function call, then
ggplot
can be called via:
See plot_quantile
for plotting sample quantiles of a
stressed model and plot_cdf
for plotting empirical
distribution functions.
## example with a stress with \code{credit_data} data set: data("credit_data") ## two stresses in VaR model_stress <- stress_VaR(credit_data, alpha = c(0.9, 0.95), q_ratio = 1.1, k =1) plot_weights(model_stress, xCol = "L", wCol = 1:2) ## additional stress on VaR and ES model_stress <- stress_VaR_ES(model_stress, alpha = 0.9, q_ratio = 1.1, s_ratio = 1.2, k =1) plot_weights(model_stress, xCol = "L", wCol = "all", n = 1000, x_limits = c(0, 3500), y_limits = c(0, 10))
## example with a stress with \code{credit_data} data set: data("credit_data") ## two stresses in VaR model_stress <- stress_VaR(credit_data, alpha = c(0.9, 0.95), q_ratio = 1.1, k =1) plot_weights(model_stress, xCol = "L", wCol = 1:2) ## additional stress on VaR and ES model_stress <- stress_VaR_ES(model_stress, alpha = 0.9, q_ratio = 1.1, s_ratio = 1.2, k =1) plot_weights(model_stress, xCol = "L", wCol = "all", n = 1000, x_limits = c(0, 3500), y_limits = c(0, 10))
Provides sample quantiles for components (random variables) of a stochastic model, corresponding to distribution functions under the scenario weights.
quantile_stressed( object, probs = seq(0, 1, 0.25), xCol = "all", wCol = 1, type = c("quantile", "(i-1)/(n-1)", "i/(n+1)", "i/n"), base = FALSE )
quantile_stressed( object, probs = seq(0, 1, 0.25), xCol = "all", wCol = 1, type = c("quantile", "(i-1)/(n-1)", "i/(n+1)", "i/n"), base = FALSE )
object |
A |
probs |
Vector of probabilities with values
in |
xCol |
Numeric or character vector, (names of) the columns of
the underlying data
of the |
wCol |
Numeric, the column of the scenario weights
of the |
type |
Character, one of |
base |
Logical, if |
type
defines the choice of algorithm used for
calculating the estimate of the sample quantiles.
"quantile"
corresponds to the default interpolation used in
quantile
. Further options are
"(i-1)/(n-1)", "i/(n+1)", "i/n"
the inverse of the
empirical distribution function, using, respectively,
(wt - 1)/T, wt/(T+1), wt/T
, where wt
is the
cumulative weight and T
the total weight (usually total
sample size). See wtd.quantile
for further details on type
, on which
quantile_stressed
is based. type
is ignored for
when evaluating quantiles for SWIMw
objects.
Returns a matrix with estimates of the distribution quantiles
at the probabilities, probs
, under the scenario weights
wCol
.
Silvana M. Pesenti, Zhuomin Mao
See wtd.quantile
on which the function
quantile_stressed
is based.
See cdf
for the empirical distribution function of
a stressed model.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed sample quantiles quantile_stressed(res1, probs = seq(0.9, 0.99, 0.01), wCol = 2)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed sample quantiles quantile_stressed(res1, probs = seq(0.9, 0.99, 0.01), wCol = 2)
Rename Stressed Models
rename(object, names, k = 1)
rename(object, names, k = 1)
object |
A |
names |
Character vector, the new names of k-th stressed model. |
k |
Numeric vector, the k-th stressed model of object to rename. ( |
Get a new SWIM
object with desired name
An renamed object of class SWIM
containing:
x
, a data.frame containing the data;
new_weights
, a list, each component corresponds to
a different stress and is either a vector of scenario weights or a
function, that applied to a column of x
, generates the
vectors of scenario weights;
type
, a list, each component corresponds to a
different stress and specifies the type of the stress;
specs
, a list, each component corresponds to
a different stress and contains a list with the specifications
of what has been stressed.
See SWIM
for details.
Kent Wu
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) res1 <- rename(res1, "VaR_09", 1)
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) res1 <- rename(res1, "VaR_09", 1)
Provides the standard deviation and variance of stressed model components (random variables) under the scenario weights.
sd_stressed(object, xCol = "all", wCol = "all", base = FALSE) var_stressed(object, xCol = "all", wCol = "all", base = FALSE)
sd_stressed(object, xCol = "all", wCol = "all", base = FALSE) var_stressed(object, xCol = "all", wCol = "all", base = FALSE)
object |
A |
xCol |
Numeric or character vector, (names of) the columns of
the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
base |
Logical, if |
sd_stressed
: The standard deviation of
a chosen model component, subject to the calculated scenario weights.
var_stressed
: The variance of
a chosen stressed model component, subject to the calculated scenario weights.
sd_stressed
: Return the standard deviation of the xCol
component of the stressed model with weights wCol
.
The quantity can be evaluated at a vector.
var_stressed
: Return the variance of the xCol
component of the stressed model with weights wCol
.
The quantity can be evaluated at a vector.
sd_stressed
: Sample standard deviation of model components
var_stressed
: Sample variance of model components
Kent Wu
See mean_stressed
for means of stressed model components,
and cor_stressed
for correlations between stressed model components.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed standard deviation sd_stressed(res1, xCol = "all", wCol = "all", base = TRUE) ## stressed variance var_stressed(res1, xCol = "all", wCol = "all", base = TRUE)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) ## stressed standard deviation sd_stressed(res1, xCol = "all", wCol = "all", base = TRUE) ## stressed variance var_stressed(res1, xCol = "all", wCol = "all", base = TRUE)
Provides different sensitivity measures that compare the stressed and the baseline model.
sensitivity( object, xCol = "all", wCol = "all", type = c("Gamma", "Kolmogorov", "Wasserstein", "reverse", "all"), f = NULL, k = NULL, s = NULL, p = 1 )
sensitivity( object, xCol = "all", wCol = "all", type = c("Gamma", "Kolmogorov", "Wasserstein", "reverse", "all"), f = NULL, k = NULL, s = NULL, p = 1 )
object |
A |
xCol |
Numeric or character vector, (names of) the columns
of the underlying data of the |
wCol |
Vector, the columns of the scenario weights
of the |
type |
Character, one of |
f |
A function, or list of functions, that, applied to
|
k |
A vector or list of vectors, same length as |
s |
A function that, applied to |
p |
Numeric vector, the p-th moment of Wasserstein distance ( |
Provides sensitivity measures that compare the stressed and the baseline model. Implemented sensitivity measures:
Gamma
, the Reverse Sensitivity Measure, defined
for a random variable Y
and scenario weights w
by
where c
is a normalisation constant such that
|Gamma| <= 1
, see
(Pesenti et al. 2019). Loosely speaking, the
Reverse Sensitivity Measure is the normalised difference
between the first moment of the stressed and the baseline
distributions of Y
.
Kolmogorov
, the Kolmogorov distance, defined for
distribution functions F,G
by
Wasserstein
, the Wasserstein distance of order 1, defined
for two distribution functions F,G
by
reverse
, the General Reverse Sensitivity Measure, defined
for a random variable Y
, scenario weights w
, and a function
s:R -> R
by
where c
is a normalisation constant such that
|epsilon| <= 1
. Gamma
is a special instance of
the reverse sensitivity measure when s
is the identity function.
If f
and k
are provided, the sensitivity of the
transformed data is returned.
A data.frame containing the sensitivity measures of the
stressed model with rows corresponding to different random
variables. The first two rows specify the stress
and
type
of the sensitivity measure.
Silvana M. Pesenti, Zhuomin Mao
Pesenti SM, Millossovich P, Tsanakas A (2019). “Reverse sensitivity testing: What does it take to break the model?” European Journal of Operational Research, 274(2), 654–670.
See importance_rank
for ranking of random
variables according to their sensitivities,
plot_sensitivity
for plotting
sensitivity measures and summary
for
summary statistics of a stressed model.
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "log-normal" = rlnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) sensitivity(res1, wCol = 1, type = "all") ## sensitivity of log-transformed data sensitivity(res1, wCol = 1, type = "all", f = list(function(x)log(x), function(x)log(x)), k = list(1,2)) ## Consider the portfolio Y = X1 + X2 + X3 + X4 + X5, ## where (X1, X2, X3, X4, X5) are correlated normally ## distributed with equal mean and different standard deviations, ## see the README for further details. ## Not run: 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)) if (!requireNamespace("mvtnorm", quietly = TRUE)) stop("Package \"mvtnorm\" needed for this function to work. Please install it.") 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") rev.stress <- stress(type = "VaR", x = data, alpha = c(0.75, 0.9), q_ratio = 1.1, k = 1) sensitivity(rev.stress, type = "all") ## sensitivity to sub-portfolios X1 + X2 and X3 + X4 sensitivity(rev.stress, xCol = NULL, type = "Gamma", f = rep(list(function(x)x[1] + x[2]), 2), k = list(c(2, 3), c(4, 5))) plot_sensitivity(rev.stress, xCol = 2:6, type = "Gamma") importance_rank(rev.stress, xCol = 2:6, type = "Gamma") ## End(Not run)
## example with a stress on VaR set.seed(0) x <- as.data.frame(cbind( "log-normal" = rlnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05) sensitivity(res1, wCol = 1, type = "all") ## sensitivity of log-transformed data sensitivity(res1, wCol = 1, type = "all", f = list(function(x)log(x), function(x)log(x)), k = list(1,2)) ## Consider the portfolio Y = X1 + X2 + X3 + X4 + X5, ## where (X1, X2, X3, X4, X5) are correlated normally ## distributed with equal mean and different standard deviations, ## see the README for further details. ## Not run: 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)) if (!requireNamespace("mvtnorm", quietly = TRUE)) stop("Package \"mvtnorm\" needed for this function to work. Please install it.") 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") rev.stress <- stress(type = "VaR", x = data, alpha = c(0.75, 0.9), q_ratio = 1.1, k = 1) sensitivity(rev.stress, type = "all") ## sensitivity to sub-portfolios X1 + X2 and X3 + X4 sensitivity(rev.stress, xCol = NULL, type = "Gamma", f = rep(list(function(x)x[1] + x[2]), 2), k = list(c(2, 3), c(4, 5))) plot_sensitivity(rev.stress, xCol = 2:6, type = "Gamma") importance_rank(rev.stress, xCol = 2:6, type = "Gamma") ## End(Not run)
Provides weights on simulated scenarios from a baseline stochastic model, such that stressed 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.
stress( type = c("VaR", "VaR ES", "mean", "mean sd", "moment", "prob", "user"), x, ... )
stress( type = c("VaR", "VaR ES", "mean", "mean sd", "moment", "prob", "user"), x, ... )
type |
Type of stress, one of |
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
... |
Arguments to be passed on, depending on |
An object of class SWIM
, see SWIM
for details.
Silvana M. Pesenti
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) summary(res)
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) summary(res)
Provides weights on simulated scenarios from a baseline stochastic
model, such that a stressed model component (random variable) fulfills a
constraint on its HARA utility defined by a
, b
and
eta
parameter and risk measure defined by a gamma
function and evaluated at a given level alpha
. Scenario weights are
selected by constrained minimisation of the Wasserstein distance to the
baseline model.
stress_HARA_RM_w( x, alpha = 0.8, a, b, eta, q_ratio = NULL, q = NULL, hu_ratio = NULL, hu = NULL, k = 1, h = 1, gamma = NULL, names = NULL, log = FALSE, method = "Nelder-Mead" )
stress_HARA_RM_w( x, alpha = 0.8, a, b, eta, q_ratio = NULL, q = NULL, hu_ratio = NULL, hu = NULL, k = 1, h = 1, gamma = NULL, names = NULL, log = FALSE, method = "Nelder-Mead" )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
alpha |
Numeric, vector, the level of the Expected Shortfall. ( |
a |
Numeric vector, input to HARA utility function. |
b |
Numeric vector, input to HARA utility function. |
eta |
Numeric vector, input to HARA utility function. |
q_ratio |
Numeric, vector, the ratio of the stressed RM to
the baseline RM (must be same length as |
q |
Numeric, vector, the stressed RM at level
|
hu_ratio |
Numeric, vector, the ratio of the HARA utility to the
baseline HARA utility. |
hu |
Numeric, vector, the stressed HARA utility with parameters
|
k |
Numeric, the column of |
h |
Numeric, a multiplier of the default bandwidth using Silverman’s rule (default |
gamma |
Function of one variable, that defined the gamma of the risk measure. ( |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
method |
The method to be used in [stats::optim()]. ( |
This function implements stresses on distortion risk measures.
Distortion risk measures are defined by a square-integrable function
gamma
where
The distortion risk measure for some gamma
and distribution
G
is calculated as:
Expected Shortfall (ES) is an example of a distortion risk measure.
The ES at level alpha
of a random variable with distribution
function F is defined by:
The HARA Utility is defined by
.
A SWIMw
object containing:
x
, a data.frame containing the data;
h
, h is a multiple of the Silverman’s rule;
u
, vector containing the gridspace on [0, 1];
lam
, vector containing the lambda's of the optimized model;
str_fY
, function defining the densities of the stressed component;
str_FY
, function defining the distribution of the stressed component;
str_FY_inv
, function defining the quantiles of the stressed component;
gamma
, function defining the risk measure;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "HARA RM"
;
specs
, a list, each component corresponds to
a different stress and contains k
, alpha
, a
, b
,
eta
, q
, and hu
.
See SWIM
for details.
Zhuomin Mao
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Pesenti SM (2021). “Reverse Sensitivity Analysis for Risk Modelling.” Available at SSRN 3878879.
Other stress functions:
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "HARA RM", x = x, a=1, b=5, eta=0.5, alpha=0.95, q_ratio=1.05, hu_ratio=1.05, k=1) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_HARA_RM_w(x = x, a=1, b=5, eta=0.5, alpha=0.95, q_ratio=1.05, hu_ratio=1.05, k=2) summary(res2) ## End(Not run)
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "HARA RM", x = x, a=1, b=5, eta=0.5, alpha=0.95, q_ratio=1.05, hu_ratio=1.05, k=1) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_HARA_RM_w(x = x, a=1, b=5, eta=0.5, alpha=0.95, q_ratio=1.05, hu_ratio=1.05, k=2) summary(res2) ## End(Not run)
Provides weights on simulated scenarios from a baseline stochastic model, such that stressed model components (random variables) fulfil the mean constraints. Scenario weights are selected by constrained minimisation of the relative entropy to the baseline model.
stress_mean(x, k, new_means, normalise = TRUE, names = NULL, log = FALSE, ...)
stress_mean(x, k, new_means, normalise = TRUE, names = NULL, log = FALSE, ...)
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
k |
Numeric vector, the columns of |
new_means |
Numeric vector, same length as |
normalise |
Logical. If true, values of |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
... |
Additional arguments to be passed to
|
The function stress_mean
is a wrapper for the
function stress_moment
. See stress_moment
for details on the additional arguments to ...
and
the underlying algorithm.
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list, each component corresponds to
a different stress and is a vector of scenario weights;
type = "mean"
;
specs
, a list, each component corresponds to
a different stress and contains k
and new_means
.
See SWIM
for details.
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
See stress_mean_sd
for stressing means
and standard deviations jointly, and stress_moment
for
moment constraints.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
set.seed(0) x <- data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2), "beta" = rbeta(1000, shape1 = 2, shape2 = 2))) ## stressing means res1 <- stress(type = "mean", x = x, k = 1:3, new_means = c(1, 1, 0.75)) summary(res1) res1$specs ## calling stress_mean directly res2 <- stress_mean(x = x, k = 1:3, new_means = c(1, 1, 0.75)) summary(res2) ## See also examples in stress_moment and stress_mean_sd.
set.seed(0) x <- data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2), "beta" = rbeta(1000, shape1 = 2, shape2 = 2))) ## stressing means res1 <- stress(type = "mean", x = x, k = 1:3, new_means = c(1, 1, 0.75)) summary(res1) res1$specs ## calling stress_mean directly res2 <- stress_mean(x = x, k = 1:3, new_means = c(1, 1, 0.75)) summary(res2) ## See also examples in stress_moment and stress_mean_sd.
Provides weights on simulated scenarios from a baseline stochastic model, such that stressed model components (random variables) fulfil the mean and standard deviation constraints. Scenario weights are selected by constrained minimisation of the relative entropy to the baseline model.
stress_mean_sd( x, k, new_means, new_sd, normalise = TRUE, names = NULL, log = FALSE, ... )
stress_mean_sd( x, k, new_means, new_sd, normalise = TRUE, names = NULL, log = FALSE, ... )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
k |
Numeric vector, the columns of |
new_means |
Numeric vector, same length as |
new_sd |
Numeric vector, same length as |
normalise |
Logical. If true, values of |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
... |
Additional arguments to be passed to
|
The function stress_mean_sd
is a wrapper for the
function stress_moment
. See stress_moment
for details on the additional arguments to ...
and
the underlying algorithm.
For stressing means only, see stress_mean
,
for stressing higher moments and functions of moments,
see stress_moment
.
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list, each component corresponds to
a different stress and is a vector of scenario weights;
type = "mean"
;
specs
, a list, each component corresponds to
a different stress and contains k
, new_means
and
new_sd
.
See SWIM
for details.
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
set.seed(0) x <- data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2), "beta" = rbeta(1000, shape1 = 2, shape2 = 2))) ## stressing mean and sd of column 1 res1 <- stress(type = "mean sd", x = x, k = 1, new_means = 0.1, new_sd = 1.1, method = "Newton", control = list(maxit = 1000, ftol = 1E-15)) summary(res1) ## calling stress_mean_sd directly res2 <- stress_mean_sd(x = x, k = 1, new_means = 0.1, new_sd = 1.1, method = "Newton", control = list(maxit = 1000, ftol = 1E-15)) ## See also examples in stress_moment.
set.seed(0) x <- data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2), "beta" = rbeta(1000, shape1 = 2, shape2 = 2))) ## stressing mean and sd of column 1 res1 <- stress(type = "mean sd", x = x, k = 1, new_means = 0.1, new_sd = 1.1, method = "Newton", control = list(maxit = 1000, ftol = 1E-15)) summary(res1) ## calling stress_mean_sd directly res2 <- stress_mean_sd(x = x, k = 1, new_means = 0.1, new_sd = 1.1, method = "Newton", control = list(maxit = 1000, ftol = 1E-15)) ## See also examples in stress_moment.
Provides weights on simulated scenarios from a baseline stochastic model, such that a stressed model component (random variable) fulfils a constraint on its mean and standard deviation. Scenario weights are selected by constrained minimisation of the Wasserstein distance to the baseline model.
stress_mean_sd_w( x, new_means, new_sd, k = 1, h = 1, names = NULL, log = FALSE, method = "Nelder-Mead", ... )
stress_mean_sd_w( x, new_means, new_sd, k = 1, h = 1, names = NULL, log = FALSE, method = "Nelder-Mead", ... )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
new_means |
Numeric, the stressed mean. |
new_sd |
Numeric, the stressed standard deviation. |
k |
Numeric, the column of |
h |
Numeric, a multiplier of the default bandwidth using Silverman’s rule (default |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
method |
The method to be used in [stats::optim()]. ( |
... |
Additional arguments to be passed to
|
A SWIMw
object containing:
x
, a data.frame containing the data;
h
, h is a multiple of the Silverman’s rule;
u
, vector containing the gridspace on [0, 1];
lam
, vector containing the lambda's of the optimized model;
str_fY
, function defining the densities of the stressed component;
str_FY
, function defining the distribution of the stressed component;
str_FY_inv
, function defining the quantiles of the stressed component;
gamma
, function defining the risk measure;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "mean sd"
;
specs
, a list, each component corresponds to
a different stress and contains k
, new_means
and new_sd
.
See SWIM
for details.
Zhuomin Mao
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Pesenti SM (2021). “Reverse Sensitivity Analysis for Risk Modelling.” Available at SSRN 3878879.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "mean sd", x = x, k = 1, new_means=1, new_sd=0.9) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_mean_sd_w(x = x, new_means=2.2, new_sd=1.5, k = 2) summary(res2) ## End(Not run)
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "mean sd", x = x, k = 1, new_means=1, new_sd=0.9) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_mean_sd_w(x = x, new_means=2.2, new_sd=1.5, k = 2) summary(res2) ## End(Not run)
Provides weights on simulated scenarios from a baseline stochastic model, such that a stressed model component (random variable) fulfils a constraint on its mean. Scenario weights are selected by constrained minimisation of the Wasserstein distance to the baseline model.
stress_mean_w( x, new_means, k = 1, h = 1, names = NULL, log = FALSE, method = "Nelder-Mead", ... )
stress_mean_w( x, new_means, k = 1, h = 1, names = NULL, log = FALSE, method = "Nelder-Mead", ... )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
new_means |
Numeric, the stressed mean. |
k |
Numeric, the column of |
h |
Numeric, a multiplier of the default bandwidth using Silverman’s rule (default |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
method |
The method to be used in [stats::optim()]. ( |
... |
Additional arguments to be passed to
|
A SWIMw
object containing:
x
, a data.frame containing the data;
h
, h is a multiple of the Silverman’s rule;
u
, vector containing the gridspace on [0, 1];
lam
, vector containing the lambda's of the optimized model;
str_fY
, function defining the densities of the stressed component;
str_FY
, function defining the distribution of the stressed component;
str_FY_inv
, function defining the quantiles of the stressed component;
gamma
, function defining the risk measure;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "mean"
;
specs
, a list, each component corresponds to
a different stress and contains k
and new_means
.
See SWIM
for details.
Zhuomin Mao
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Pesenti SM (2021). “Reverse Sensitivity Analysis for Risk Modelling.” Available at SSRN 3878879.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "mean", x = x, k = 1, new_means=1) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_mean_w(x = x, new_means=2.2, k = 2) summary(res2) ## End(Not run)
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "mean", x = x, k = 1, new_means=1) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_mean_w(x = x, new_means=2.2, k = 2) summary(res2) ## End(Not run)
Provides weights on simulated scenarios from a baseline stochastic model, such that stressed model components (random variables) fulfill the moment constraints. Scenario weights are selected by constrained minimisation of the relative entropy to the baseline model.
stress_moment( x, f, k, m, normalise = TRUE, show = FALSE, names = NULL, log = FALSE, ... )
stress_moment( x, f, k, m, normalise = TRUE, show = FALSE, names = NULL, log = FALSE, ... )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
f |
A function, or list of functions, that, applied to
|
k |
A vector or list of vectors, same length as |
m |
Numeric vector, same length as |
normalise |
Logical. If true, values of |
show |
Logical. If true, print the result of the call to
|
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
... |
Additional arguments to be passed to
|
The moment constraints are given by E^Q( f(x) ) = m
,
where E^Q
denotes the expectation under the stressed
model. stress_moment
solves the subsequent set of equations
with respect to theta, using nleqslv
from package
nleqslv
:
There is no guarantee that the set of equations
has a solution, or that the solution is unique. SWIM
will
return a warning if the termination code provided by nleqslv
is
different from 1 (convergence has been achieved). It is recommended to
check the result of the call to nleqslv
using the "show" argument. The
user is referred to the nleqslv
documentation for
further details.
Normalising the data may help avoiding numerical issues when the range of values is wide.
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list, each component corresponds to
a different stress and is a vector of scenario weights;
type = "moment"
;
specs
, a list, each component corresponds to
a different stress and contains f
, k
and m
.
See SWIM
for details.
The function call will print a message containing the termination code returned by the call to nleqslv
and a table with the required and achieved moment, and the absolute and relative error.
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
See stress_mean
for stressing means and
stress_mean_sd
for stressing mean and standard
deviation jointly.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
set.seed(0) x <- data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2), "beta" = rbeta(1000, shape1 = 2, shape2 = 2))) ## stressing covariance of columns 1, 2 while leaving the means unchanged res1 <- stress_moment(x = x, f = list(function(x)x, function(x)x, function(x)x[1] * x[2]), k = list(1, 2, c(1, 2)), m = c(0, 2, 0.5), method = "Newton", control = list(maxit = 1000, ftol = 1E-10)) ## means under the stressed model summary(res1) apply(x, 2, stats::weighted.mean, w = get_weights(res1)) ## covariance of columns 1,2 under the stressed model stats::weighted.mean(x[, 1] * x[, 2], w = get_weights(res1)) ## stressing jointly the tail probabilities of columns 1, 3 res2 <- stress_moment(x = x, f = list(function(x)(x > 1.5), function(x)(x > 0.9)), k = list(1, 3), m = c(0.9, 0.9)) summary(res2) ## probabilities under the stressed model mean((x[, 1] > 1.5) * get_weights(res2)) mean((x[, 3] > 0.9) * get_weights(res2))
set.seed(0) x <- data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2), "beta" = rbeta(1000, shape1 = 2, shape2 = 2))) ## stressing covariance of columns 1, 2 while leaving the means unchanged res1 <- stress_moment(x = x, f = list(function(x)x, function(x)x, function(x)x[1] * x[2]), k = list(1, 2, c(1, 2)), m = c(0, 2, 0.5), method = "Newton", control = list(maxit = 1000, ftol = 1E-10)) ## means under the stressed model summary(res1) apply(x, 2, stats::weighted.mean, w = get_weights(res1)) ## covariance of columns 1,2 under the stressed model stats::weighted.mean(x[, 1] * x[, 2], w = get_weights(res1)) ## stressing jointly the tail probabilities of columns 1, 3 res2 <- stress_moment(x = x, f = list(function(x)(x > 1.5), function(x)(x > 0.9)), k = list(1, 3), m = c(0.9, 0.9)) summary(res2) ## probabilities under the stressed model mean((x[, 1] > 1.5) * get_weights(res2)) mean((x[, 3] > 0.9) * get_weights(res2))
Provides weights on simulated scenarios from a baseline stochastic model, such that a stressed model component (random variable) fulfils constraints on probability of disjoint intervals. Scenario weights are selected by constrained minimisation of the relative entropy to the baseline model.
stress_prob(x, prob, lower = NULL, upper, k = 1, names = NULL, log = FALSE)
stress_prob(x, prob, lower = NULL, upper, k = 1, names = NULL, log = FALSE)
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
prob |
Numeric vector, stressed probabilities corresponding to
the intervals defined through |
lower |
Numeric vector, left endpoints of the intervals. |
upper |
Numeric vector, right endpoints of the intervals. |
k |
Numeric, the column of |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
The intervals are treated as half open intervals, that is
the lower endpoint are not included, whereas the upper endpoint
are included. If upper = NULL
, the intervals
are consecutive and prob
cumulative.
The intervals defined through lower
and upper
must
be disjoint.
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list of functions, that applied to the
k
th column of x
, generate the vectors of scenario
weights. Each component corresponds to a different stress;
type = "prob"
;
specs
, a list, each component corresponds to
a different stress and contains k
, lower
,
upper
and prob
.
See SWIM
for details.
Silvana M. Pesenti
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_user()
,
stress_wass()
,
stress()
set.seed(0) x <- rnorm(1000) ## consecutive intervals res1 <- stress(type = "prob", x = x, prob = 0.008, upper = -2.4) # probability under the stressed model cdf(res1, xCol = 1)(-2.4) ## calling stress_prob directly ## multiple intervals res2 <- stress_prob(x = x, prob = c(0.008, 0.06), lower = c(-3, -2), upper = c(-2.4, -1.6)) # probability under the stressed model cdf(res2, xCol = 1)(c(-2.4, -1.6)) - cdf(res2, xCol = 1)(c(-3, -2))
set.seed(0) x <- rnorm(1000) ## consecutive intervals res1 <- stress(type = "prob", x = x, prob = 0.008, upper = -2.4) # probability under the stressed model cdf(res1, xCol = 1)(-2.4) ## calling stress_prob directly ## multiple intervals res2 <- stress_prob(x = x, prob = c(0.008, 0.06), lower = c(-3, -2), upper = c(-2.4, -1.6)) # probability under the stressed model cdf(res2, xCol = 1)(c(-2.4, -1.6)) - cdf(res2, xCol = 1)(c(-3, -2))
Provides weights on simulated scenarios from a baseline stochastic
model, such that a stressed model component (random variable) fulfils a
constraint on its mean, standard deviation, and risk measure
defined by a gamma
function and
evaluated at a given level alpha
. Scenario weights are
selected by constrained minimisation of the Wasserstein distance to the
baseline model.
stress_RM_mean_sd_w( x, alpha = 0.8, new_means, new_sd, q_ratio = NULL, q = NULL, k = 1, h = 1, gamma = NULL, names = NULL, log = FALSE, method = "Nelder-Mead", ... )
stress_RM_mean_sd_w( x, alpha = 0.8, new_means, new_sd, q_ratio = NULL, q = NULL, k = 1, h = 1, gamma = NULL, names = NULL, log = FALSE, method = "Nelder-Mead", ... )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
alpha |
Numeric, vector, the level of the Expected Shortfall. ( |
new_means |
Numeric, the stressed mean. |
new_sd |
Numeric, the stressed standard deviation. |
q_ratio |
Numeric, vector, the ratio of the stressed RM to
the baseline RM (must be same length as |
q |
Numeric, vector, the stressed RM at level
|
k |
Numeric, the column of |
h |
Numeric, a multiplier of the default bandwidth using Silverman’s rule (default |
gamma |
Function of one variable, that defined the gamma of the risk measure. ( |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
method |
The method to be used in [stats::optim()]. ( |
... |
Additional arguments to be passed to
|
This function implements stresses on distortion risk measures.
Distortion risk measures are defined by a square-integrable function
gamma
where
The distortion risk measure for some gamma
and distribution
G
is calculated as:
Expected Shortfall (ES) is an example of a distortion risk measure.
The ES at level alpha
of a random variable with distribution
function F is defined by:
A SWIMw
object containing:
x
, a data.frame containing the data;
h
, h is a multiple of the Silverman’s rule;
u
, vector containing the gridspace on [0, 1];
lam
, vector containing the lambda's of the optimized model;
str_fY
, function defining the densities of the stressed component;
str_FY
, function defining the distribution of the stressed component;
str_FY_inv
, function defining the quantiles of the stressed component;
gamma
, function defining the risk measure;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "RM mean sd"
;
specs
, a list, each component corresponds to
a different stress and contains k
, alpha
,
q
, new_means
, and new_sd
.
See SWIM
for details.
Zhuomin Mao
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Pesenti SM (2021). “Reverse Sensitivity Analysis for Risk Modelling.” Available at SSRN 3878879.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "RM mean sd", x = x, alpha = 0.9, q_ratio = 1.05, new_means=1, new_sd=0.9) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_RM_mean_sd_w(x = x, alpha = 0.9, q_ratio = 1.05, new_means=2.2, new_sd=1.5, k = 2) summary(res2) ## End(Not run)
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "RM mean sd", x = x, alpha = 0.9, q_ratio = 1.05, new_means=1, new_sd=0.9) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_RM_mean_sd_w(x = x, alpha = 0.9, q_ratio = 1.05, new_means=2.2, new_sd=1.5, k = 2) summary(res2) ## End(Not run)
Provides weights on simulated scenarios from a baseline stochastic model, such that a stressed model component (random variable) fulfils a constraint on its risk measure defined by a gamma function. The default risk measure is the Expected Shortfall at level alpha.
stress_RM_w( x, alpha = 0.8, q_ratio = NULL, q = NULL, k = 1, h = 1, gamma = NULL, names = NULL, log = FALSE, method = "Nelder-Mead" )
stress_RM_w( x, alpha = 0.8, q_ratio = NULL, q = NULL, k = 1, h = 1, gamma = NULL, names = NULL, log = FALSE, method = "Nelder-Mead" )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
alpha |
Numeric, vector, the level of the Expected Shortfall. ( |
q_ratio |
Numeric, vector, the ratio of the stressed RM to
the baseline RM (must be of the same length as alpha or gamma). |
q |
Numeric, vector, the stressed RM at level
|
k |
Numeric, the column of |
h |
Numeric, a multiplier of the default bandwidth using Silverman’s rule (default |
gamma |
Function of one variable, that defined the gamma of the risk measure. ( |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
method |
The method to be used in [stats::optim()]. ( |
This function implements stresses on distortion risk measures.
Distortion risk measures are defined by a square-integrable function
gamma
where
The distortion risk measure for some gamma
and distribution
G
is calculated as:
Expected Shortfall (ES) is an example of a distortion risk measure.
The ES at level alpha
of a random variable with distribution
function F is defined by:
A SWIMw
object containing:
x
, a data.frame containing the data;
h
, h is a multiple of the Silverman’s rule;
u
, vector containing the gridspace on [0, 1];
lam
, vector containing the lambda's of the optimized model;
str_fY
, function defining the densities of the stressed component;
str_FY
, function defining the distribution of the stressed component;
str_FY_inv
, function defining the quantiles of the stressed component;
gamma
, function defining the risk measure;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "RM"
;
specs
, a list, each component corresponds to
a different stress and contains k
, alpha
, and
q
.
See SWIM
for details.
Zhuomin Mao
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Pesenti SM (2021). “Reverse Sensitivity Analysis for Risk Modelling.” Available at SSRN 3878879.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_RM_w(x = x, alpha = 0.9, q_ratio = 1.05, k = 2) summary(res2) # dual power distortion with beta = 3 # gamma = beta * u^{beta - 1}, beta > 0 gamma <- function(u){ .res <- 3 * u^2 return(.res) } res3 <- stress_wass(type = "RM", x = x, gamma = gamma, q_ratio = 1.05) summary(res3) ## End(Not run)
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) summary(res1) ## calling stress_RM_w directly ## stressing "gamma" res2 <- stress_RM_w(x = x, alpha = 0.9, q_ratio = 1.05, k = 2) summary(res2) # dual power distortion with beta = 3 # gamma = beta * u^{beta - 1}, beta > 0 gamma <- function(u){ .res <- 3 * u^2 return(.res) } res3 <- stress_wass(type = "RM", x = x, gamma = gamma, q_ratio = 1.05) summary(res3) ## End(Not run)
Returns a SWIM
object with scenario weights defined by the user.
stress_user( x, new_weights = NULL, new_weightsfun = NULL, k = 1, names = NULL, log = FALSE )
stress_user( x, new_weights = NULL, new_weightsfun = NULL, k = 1, names = NULL, log = FALSE )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
new_weights |
A vector, matrix or data frame containing scenario
weights. Columns of |
new_weightsfun |
A list of functions, that applied to
the |
k |
Numeric, the column of |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list, each component corresponds to
a different stress and is either a vector of scenario weights (if new_weights
is provided) or (if new_weightsfun
is provided) a
function, that applied to the k
th column of x
, generates the
vectors of scenario weights;
type = "user"
;
specs
, a list, each component corresponds to
a different stress and contains k
.
See SWIM
for details.
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_wass()
,
stress()
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "user", x = x, new_weightsfun = function(x)x ^ 2, k = 1) ## plot user defined weights against the first column of x. plot(x$normal, get_weights(res1), pch = ".")
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "user", x = x, new_weightsfun = function(x)x ^ 2, k = 1) ## plot user defined weights against the first column of x. plot(x$normal, get_weights(res1), pch = ".")
Provides weights on simulated scenarios from a baseline stochastic model, such that a stressed model component (random variable) fulfils a constraint on its quantile at a given level, also known as Value-at-Risk (VaR). Scenario weights are selected by constrained minimisation of the relative entropy to the baseline model.
stress_VaR( x, alpha, q_ratio = NULL, q = NULL, k = 1, names = NULL, log = FALSE )
stress_VaR( x, alpha, q_ratio = NULL, q = NULL, k = 1, names = NULL, log = FALSE )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
alpha |
Numeric vector, the levels of the stressed VaR. |
q_ratio |
Numeric vector, the ratio of the stressed VaR to the
baseline VaR. |
q |
Numeric vector, the stressed VaR at level
|
k |
Numeric, the column of |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
The stressed VaR is the quantile of the chosen model component,
subject to the calculated scenario weights.
The VaR at level alpha
of a random variable with
distribution
function F is defined as its left-quantile at alpha:
If one of alpha, q
(q_ratio
) is a vector,
the stressed VaR's of the k
th column of x
, at levels
alpha
, are equal to q
.
The stressed VaR specified, either via q
or q_ratio
, might not equal
the attained empirical VaR of the model component. In this
case, stress_VaR
will display a message
and the specs
contain
the achieved VaR.
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "VaR"
;
specs
, a list, each component corresponds to
a different stress and contains k
,
alpha
and q
.
See SWIM
for details.
Silvana M. Pesenti
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) ## calling stress_VaR directly ## stressing "gamma" res2 <- stress_VaR(x = x, alpha = 0.9, q_ratio = c(1.03, 1.05), k = 2) get_specs(res2) summary(res2)
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) ## calling stress_VaR directly ## stressing "gamma" res2 <- stress_VaR(x = x, alpha = 0.9, q_ratio = c(1.03, 1.05), k = 2) get_specs(res2) summary(res2)
Provides weights on simulated scenarios from a baseline stochastic model, such that a stressed model component (random variable) fulfils a constraint on its Value-at-Risk (VaR) and Expected Shortfall (ES) risk measures, both evaluated at a given level. Scenario weights are selected by constrained minimisation of the relative entropy to the baseline model.
stress_VaR_ES( x, alpha, q_ratio = NULL, s_ratio = NULL, q = NULL, s = NULL, k = 1, normalise = FALSE, names = NULL, log = FALSE )
stress_VaR_ES( x, alpha, q_ratio = NULL, s_ratio = NULL, q = NULL, s = NULL, k = 1, normalise = FALSE, names = NULL, log = FALSE )
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
alpha |
Numeric vector, the levels of the stressed VaR. |
q_ratio |
Numeric vector, the ratio of the stressed VaR to the
baseline VaR. |
s_ratio |
Numeric, vector, the ratio of the stressed ES to
the baseline ES. |
q |
Numeric vector, the stressed VaR at level
|
s |
Numeric, vector, the stressed ES at level
|
k |
Numeric, the column of |
normalise |
Logical. If true, values of the columns to be stressed are linearly normalised to the unit interval. |
names |
Character vector, the names of stressed models. |
log |
Boolean, the option to print weights' statistics. |
The VaR at level alpha
of a random variable with
distribution function F is defined as its left-quantile at alpha
:
The ES at level alpha
of a random variable with distribution
function F is defined by:
The stressed VaR and ES are the risk measures of the chosen model
component, subject to the calculated scenario weights. If one
of alpha, q, s
(q_ratio, s_ratio
) is
a vector, the stressed VaR's and ES's of the k
th column of
x
, at levels alpha
, are equal to q
and s
, respectively.
The stressed VaR specified, either via q
or q_ratio
, might not equal
the attained empirical VaR of the model component. In this
case, stress_VaR
will display a message
and the specs
contain
the achieved VaR. Further, ES is then calculated on the bases of the achieved VaR.
Normalising the data may help avoiding numerical issues when the range of values is wide.
A SWIM
object containing:
x
, a data.frame containing the data;
new_weights
, a list of functions, that applied to
the k
th column of x
, generates the vectors of scenario
weights. Each component corresponds to a different stress;
type = "VaR ES"
;
specs
, a list, each component corresponds to
a different stress and contains k
, alpha
,
q
and s
.
See SWIM
for details.
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress_wass()
,
stress()
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR ES", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05, s_ratio = 1.08) ## calling stress_VaR_ES directly ## stressing "gamma" res2 <- stress_VaR_ES(x = x, alpha = 0.9, q_ratio = 1.03, s_ratio = c(1.05, 1.08), k = 2) get_specs(res2) summary(res2)
set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR ES", x = x, alpha = c(0.9, 0.95), q_ratio = 1.05, s_ratio = 1.08) ## calling stress_VaR_ES directly ## stressing "gamma" res2 <- stress_VaR_ES(x = x, alpha = 0.9, q_ratio = 1.03, s_ratio = c(1.05, 1.08), k = 2) get_specs(res2) summary(res2)
Provides weights on simulated scenarios from a baseline stochastic model, such that stressed random variables fulfill given probabilistic constraints (e.g. specified values for risk measures), under the new scenario weights. Scenario weights are selected by constrained minimisation of the Wasserstein Distance to the baseline model.
stress_wass(type = c("RM", "mean sd", "RM mean sd", "HARA RM"), x, ...)
stress_wass(type = c("RM", "mean sd", "RM mean sd", "HARA RM"), x, ...)
type |
Type of stress, one of |
x |
A vector, matrix or data frame
containing realisations of random variables. Columns of |
... |
Arguments to be passed on, depending on |
An object of class SWIMw
, see SWIM
for details.
Zhuomin Mao
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
Other stress functions:
stress_HARA_RM_w()
,
stress_RM_mean_sd_w()
,
stress_RM_w()
,
stress_VaR_ES()
,
stress_VaR()
,
stress_mean_sd_w()
,
stress_mean_sd()
,
stress_mean_w()
,
stress_mean()
,
stress_moment()
,
stress_prob()
,
stress_user()
,
stress()
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) summary(res) ## End(Not run)
## Not run: set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) summary(res) ## End(Not run)
This function is a methods
for an object of class
SWIM
or SWIMw
. Provides summary statistics of the stochastic model,
stressed using the scenario weights.
## S3 method for class 'SWIM' summary(object, ..., xCol = "all", wCol = "all", base = FALSE) ## S3 method for class 'SWIMw' summary(object, ..., xCol = "all", wCol = "all", base = FALSE)
## S3 method for class 'SWIM' summary(object, ..., xCol = "all", wCol = "all", base = FALSE) ## S3 method for class 'SWIMw' summary(object, ..., xCol = "all", wCol = "all", base = FALSE)
object |
A |
... |
Additional arguments will be ignored. |
xCol |
Numeric or character vector, (names of) the columns of
the underlying data
of the |
wCol |
Vector, the columns of the scenario weights
of the |
base |
Logical, if |
summary.SWIM
returns a list with components
corresponding to different stresses. Components contain a
summary statistic of each column of the data of the
SWIM
object:
mean |
The sample mean. |
sd |
The sample standard deviation. |
skewness |
The sample skewness. |
ex kurtosis |
The sample excess kurtosis |
1st Qu. |
The 25% quantile. |
Median |
The median, 50% quantile. |
3rd Qu. |
The 75% quantile. |
summary.SWIM
: Summarising Stressed Models
summary.SWIMw
: Summarising Stressed Models
Silvana M. Pesenti
Zhuomin Mao
## Example with the Relative Entropy ## continuing example in stress_VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) summary(res1, xCol = "normal", base = TRUE) ## Example with the Wasserstein distance ## Not run: resW <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) summary(resW, xCol = "normal", base = TRUE) ## End(Not run)
## Example with the Relative Entropy ## continuing example in stress_VaR set.seed(0) x <- as.data.frame(cbind( "normal" = rnorm(1000), "gamma" = rgamma(1000, shape = 2))) res1 <- stress(type = "VaR", x = x, alpha = 0.9, q_ratio = 1.05) summary(res1, xCol = "normal", base = TRUE) ## Example with the Wasserstein distance ## Not run: resW <- stress_wass(type = "RM", x = x, alpha = 0.9, q_ratio = 1.05) summary(resW, xCol = "normal", base = TRUE) ## End(Not run)
The SWIM
package provides weights on simulated scenarios
from a stochastic model, such that a stressed model component
(random variable) 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 or Wasserstein distance to the baseline model.
The SWIM
(Scenario Weights for Importance Measurement)
package provides weights on simulated scenarios from a stochastic
model, such that stressed 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 or
Wasserstein distance to the
baseline model.
The SWIM
package is based on the reverse sensitivity
framework developed by (Pesenti et al. 2019) and (Pesenti 2021).
Consider the random vector X = (X1,...,Xn)
. Let P
represent the probability measure under which all simulated
scenarios have the same probability. First, take the
approach of minimizing the relative entropy. Then, for a random variable
Xi
, the package solves:
subject to constraints on the distribution
of Xi
under Q
,
where D(P | Q)
is the Kullback-Leibler divergence
(relative entropy) between P
and Q
.
The approach of minimizing the Wasserstein distance of order 2 proceeds
as follows: Let F be the distribution function of the random variable Xi
under P
, then the package solves
subject to constraints on G
, W_{2}(G, F)
is the 2-Wasserstein distance
between G
and F
. The solution to the above minimisation problem is the
distribution of Xi
under Q
. The current implementation of the Wasserstein
approach is based on Kernel density estimation with Gaussian kernels.
For both approaches, the scenario weights are then formed via the Radon-Nikodym
derivative dQ / dP
. The weighting generates a model for which the joint distribution
of (X1,...,Xn)
is stressed.
Different elements of X
can be understood as
inputs or outputs of a model. For example, consider a model
Y = g(Z)
with input vector Z = (Z1,...,Z(n-1))
.
One can then identify X1 = Y
and X2 = Z1,...,Xn
= Z(n-1)
. Subsequently, the user of the SWIM
package can
stress the model output or any of the inputs, measuring the
resulting impact on the distributions of other variables.
Scenario weights for the following stresses are provided:
stress
|
calls one of the functions below by
using type
|
stress_VaR |
for stressing the VaR
(type = "VaR" ) |
stress_VaR_ES |
for stressing the VaR and
ES jointly (type = "VaR ES" ) |
stress_mean
|
for stressing means
(type = "mean" ) |
stress_mean_sd |
for stressing means and
standard deviations (type = "mean std" ) |
stress_moment |
for stressing moments
(type = "moment" ) |
stress_prob |
for stressing the probabilities
of intervals
(type = "prob" ) |
stress_user |
for user defined scenario weights
(type = "user" ) |
Scenario weights for the following stresses are provided:
stress_wass
|
calls one of the functions below by
using type
|
stress_RM_w |
for stressing the distortion risk measure (RM)
(type = "RM" ) |
stress_mean_sd_w |
for stressing mean and
standard deviation (type = "mean sd" ) |
stress_RM_mean_sd_w |
for stressing the RM, mean and
standard deviation (type = "RM mean sd" ) |
stress_HARA_RM_w |
for stressing the HARA utility and RM
(type = "HARA RM" ) |
stress_mean_w |
for stressing mean (type = "mean" )
|
SWIM
objectA SWIM object is generated by applying a stress function subject to a relative entropy minimisation.
An object of class SWIM
contains a list of:
x
, a data.frame containing realisations of a random
vector;
new_weights
, a list, each component corresponds to
a different stress and is either a vector of scenario weights or a
function, that applied to the k
th column of x
,
generates the vectors of scenario weights;
type
: a list, each component corresponds to a
different stress and specifies the type of the stress;
specs
, a list, each component corresponds to
a different stress and contains a list with the specifications
of what has been stressed.
Specifications depend on the type
of stress:
type = "VaR"
: k
, the column of x
on which the stress is applied to; alpha
, the level of
the stressed VaR; q
, the stressed VaR at level
alpha
.
type = "VaR ES"
: k
, the column of x
on which the stress is applied to; alpha
, the level of the
stressed VaR and ES; q
, the stressed VaR at level
alpha
.
type = "mean"
: k
, the columns of x
on which the stress is applied to; new_means
, the
stressed means.
type = "mean sd"
: k
, the columns of x
on which the stress is applied to; new_means
, the
stressed means; new_sd
, the stressed standard deviations.
s
, the stressed ES at level alpha
.
type = "moment"
: f
, the list of functions,
that, applied to x
, constitute the moment constraints;
k
, the columns of x
on which each function in
f
operates on; m
, the stressed moments of
f(x)
.
type = "prob"
: k
, the column of x
on which the stress is applied to; lower
, the left
endpoints of the intervals; upper
, the right endpoints
of the intervals; prob
, stressed probabilities
corresponding to the intervals defined through lower
and upper
.
type = "user"
: k
, the column of x
on which the stress is applied to.
SWIMw
objectA SWIMw object is generated by applying a stress function
subject to a Wasserstein minimisation. The Wasserstein
minimisation approach assumes that all model components,
(random variables) are continuously distributed. If only
the stressed model component is continuously distributed,
the SWIMw stress should be converted to a SWIM object, see
convert_SWIMw_to_SWIM
.
An object of class SWIMw
contains a list of:
x
, a data.frame containing realisations of a random
vector;
new_weights
: a list, each component corresponds to
a different stress and is either a vector of scenario weights or a
function, that applied to the k
th column of x
,
generates the vectors of scenario weights;
type
: a list, each component corresponds to a
different stress and specifies the type of the stress;
h
: a list, each component corresponds to a different stress
and specifies the bandwidth;
u
: a list, each component corresponds to a different stress
and is a vector containing the gridspace on [0, 1];
lam
: a list, each component corresponds to a different stress
and is vector containing the lambda's of the optimized model;
str_fY
: a list, each component corresponds to a different
stress and is a function defining the densities of the stressed component;
str_FY
: a list, each component corresponds to a different
stress and is a function defining the distribution of the stressed component;
str_FY_inv
: a list, each component corresponds to a different
stress and is a function defining the quantiles of the stressed component;
gamma
: a list, each component corresponds to a different
stress and is a function defining the risk measure;
specs
: a list, each component corresponds to
a different stress and contains a list with the specifications
of what has been stressed.
Specifications depend on the type
of stress:
type = "RM"
: k
, the column of x
on which the stress is applied to; alpha
, the level of
the RM; q
, the stressed RM at level
alpha
.
type = "mean sd"
: k
, the columns of x
on which the stress is applied to; new_mean
, the
stressed mean; new_sd
, the stressed standard deviation.
type = "RM mean sd"
: k
, the column of x
on which the stress is applied to; alpha
, the level of the
stressed RM; q
, the stressed RM at level
alpha
; new_mean
, the
stressed mean; new_sd
, the stressed standard deviation.
type = "HARA RM"
: k
, the column of x
on which the stress is applied to; alpha
, the level of
the stressed RM; q
, the stressed RM at level
alpha
; a
a parameter of the HARA utility function;
b
, a parameter of the HARA utility function;
eta
a parameter of the HARA utility function;
hu
, the stressed HARA utility with parameters
a
, b
, and eta
.
Pesenti SM, Millossovich P, Tsanakas A (2019).
“Reverse sensitivity testing: What does it take to break the model?”
European Journal of Operational Research, 274(2), 654–670.
Pesenti S BAMPTA (2020).
“Scenario Weights for Importance Measurement (SWIM) - An R package for sensitivity analysis.”
Annals of Actuarial Science 15.2 (2021): 458-483. Available at SSRN: https://www.ssrn.com/abstract=3515274.
Csiszar I (1975). “I-divergence geometry of probability distributions and minimization problems.” The Annals of Probability, 146–158.
See get_data
for extracting the data,
x
; get_weights
for extracting the scenario
weights, new_weights
; get_weightsfun
for
extracting the functions generating the scenario weights; and
get_specs
for extracting the specifications of
the stress on an object of class SWIM
.