Skip to content

Monte Carlo simulation for VaR estimation

5 messages · David Reiner, andrija djurovic, Arun Kumar Saha

#
I'm guessing that you would want to specify the correlation as given as you have the volatilities,
hence use a covariance matrix in MASS::mvrnorm to generate your 'sh'.
That shuld get you started anyway.
Rolling your own with use of Cholesky is OK, too, of course, but others have done lots of hard work do give you better results.

(
There are many other functions in various packages to generate multivariate normal samples if you look; e.g.,
found 532 matches;  retrieving 20 pages, 400 matches.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
)

HTH,
-- David


-----Original Message-----
From: r-sig-finance-bounces at r-project.org [mailto:r-sig-finance-bounces at r-project.org] On Behalf Of andrija djurovic
Sent: Friday, November 18, 2011 3:02 AM
To: r-sig-finance at r-project.org
Subject: [SPAM] - [R-SIG-Finance] Monte Carlo simulation for VaR estimation - Email found in subject

Dear users,
I need help in understanding steps of Monte Carlo simulation for VaR estimation. Here is the simplified example of what i was trying to do, but I suppose this is not appropriate way for performing MC simulation.

Suppose that our portfolio consist of x1 and x2 (with equal weights), which returns are given:

sh<-matrix(c(rnorm(100,0,0.01),rnorm(100,0,0.015)),ncol=2)
colnames(sh)<-c("x1","x2")
w<-matrix(c(0.5,0.5),ncol=1)
pr<-sh%*%w
pmu<-mean(pr)
sigma<-cov(sh)
pvar<-t(w)%*%sigma%*%w

Now I was trying to generate 10 scenarios (keeping the correlation between
x1 and x2). Then calculate portfolio mean for each scenario and from that mean distribution  calculate 95% VaR. Here I am sure  these aren't good steps and I that I am missing something in part of keeping a correlation between x1 and x2:

l<-list("vector",10)
cd<-chol(cov2cor(sigma))
#generate 10 scenarios#
mu<-apply(sh,2,mean)
sd<-apply(sh,2,sd)
for (i in 1:10)
{
l[[i]]<-matrix(c(rnorm(10,mu[1],sd[1]),rnorm(10,mu[2],sd[2])),ncol=2)%*%cd
}

mc<-sapply(l,function(x) mean(x%*%w))

quantile(mc,p=0.05)


Thanks in advance and any help is really appreciated.

Andrija


_______________________________________________
R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.


This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws.  Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates.

THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND.  TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.
2 days later
#
Hi Andrija, I am not sure why you need to replicate 10 times. Instead you
should want to replicate once with, say, 1000 simulation and then get the
5th percentile of the portfolio return distribution. Here you go:

l<-list("vector",1) 
mu<-apply(sh,2,mean) 
sigma<-cov(sh) 
#generate 1 replication
l<-list("vector",1) 
for (i in 1:1) 
{ 
l[[i]]<-rmnorm(1000,mu,sigma) # this will generate 1000 possible asset
returns based on the same statistical characteristic as in 'sh'
							  # I assume, 'sh' is your historical asset return
} 

l <- l[[1]]
pr_simulated <- apply(l, 1, function(x) return(sum(x * c(0.5, 0.5)))) #
simulated portfolio return
quantile(pr_simulated, 0.05, type = 3) # your Portfolio VaR (95%)

PS: I used rmnorm() function for drawing random number from a multivariate
normal, which is from 'mnormt' package.

HTH,

Thanks and regards,
_____________________________________________________

Arun Kumar Saha, FRM
QUANTITATIVE RISK AND HEDGE CONSULTING SPECIALIST
Visit me at: http://in.linkedin.com/in/ArunFRM
_____________________________________________________


--
View this message in context: http://r.789695.n4.nabble.com/Monte-Carlo-simulation-for-VaR-estimation-tp4082611p4090907.html
Sent from the Rmetrics mailing list archive at Nabble.com.