Description Usage Arguments Details Value References See Also Examples
View source: R/stress_moment.R
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.
1 2 3 4 5 6 7 8 9 10 11 |
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
:
E^Q( f(x) ) = E( f(x) * exp(theta * f(x)) ) = m.
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.
Pesenti2019reverseSWIM
Pesenti2020SSRNSWIM
Csiszar1975SWIM
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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.