# Monte Carlo simulation for VaR estimation

5 messages
Open this post in threaded view
|

## Monte Carlo simulation for VaR estimation

 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) >From this I calculated portfolio returns, mean and variance: 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         [[alternative HTML version deleted]] _______________________________________________ [hidden email] 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.
Open this post in threaded view
|

## Re: [SPAM] - Monte Carlo simulation for VaR estimation - Email found in subject

 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., > require(sos) > ???"random multivariate normal " 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: [hidden email] [mailto:[hidden email]] On Behalf Of andrija djurovic Sent: Friday, November 18, 2011 3:02 AM To: [hidden email] 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) >From this I calculated portfolio returns, mean and variance: 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         [[alternative HTML version deleted]] _______________________________________________ [hidden email] 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. _______________________________________________ [hidden email] 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.
Open this post in threaded view
|

## Re: [SPAM] - Monte Carlo simulation for VaR estimation - Email found in subject

 David, thanks for answer. First I don't know why my mail was sent in Spam and sorry if i made some mistake for that. I accept your suggestion and use mvrnorm form MASS. l<-list("vector",10) mu<-apply(sh,2,mean) sigma<-cov(sh) #generate 10 scenatrios l<-list("vector",10) for (i in 1:10) { l[[i]]<-mvrnorm(10,mu,sigma) } But after this I still don't know what to do in order to estimate VaR from this generated 10 scenatrios. Is this a good way or not: mc<-sapply(l,function(x) mean(x%*%w)) quantile(mc,p=0.05) Thanks for help and sorry if this is a too simple or begginer question but I not so experienced with this. Andrija On Fri, Nov 18, 2011 at 3:51 PM, David Reiner <[hidden email]>wrote: > 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., > > require(sos) > > ???"random multivariate normal " > 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: [hidden email] [mailto: > [hidden email]] On Behalf Of andrija djurovic > Sent: Friday, November 18, 2011 3:02 AM > To: [hidden email] > 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) > > >From this I calculated portfolio returns, mean and variance: > > 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 > >        [[alternative HTML version deleted]] > > _______________________________________________ > [hidden email] 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. >         [[alternative HTML version deleted]] _______________________________________________ [hidden email] 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.