# Stressed VaR
# Assume that our losses are lognormally distributed
# with mean 1 and standard deviation 2.
# Naturally we can use other distributions, or
# we can use actual data!
# This is just an example.
# Notice that, since we are generating our losses randomly,
# my values will be different from yours.
# If you want to use my values, you can download
# them from the course platform.
# Let's generate 100 losses
losses=rlnorm(100,1,2)
# or
# losses=read.table("losses_svar.txt",head=T),
# and then
# losses=losses$x
# if you use my data.
# We can plot the losses
hist(losses, col=3)
# Let's compute the 95% VaR
quantile(losses,0.95,type=3)
# I suggest you to type ? quantile to check
# again the properties of this function.
# Let's now compute the 95% S-VaR.
# First we sort the original losses, from the smallest
# to the largest.
losses=sort(losses)
# Now we select the worst 50%
worst_losses=losses[51:100]
# The 95% S-VaR is therefore
quantile(worst_losses,0.95,type=3)
# Much bigger!
# And now, notice this:
quantile(losses,0.975,type=3)
# Can you say why? :-)