

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/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


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]  [RSIGFinance] 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/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.
This email 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/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


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]  [RSIGFinance] 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/rsigfinance>  Subscriberposting only. If you want to post, subscribe first.
>  Also note that this is not the rhelp list where general R questions
> should go.
>
>
> This email 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/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


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_____________________________________________________


Thank you for answer Arun. This was very helpful and now I understand how
it should be done.
Andrija
On Mon, Nov 21, 2011 at 9:39 AM, Arun.stat < [hidden email]>wrote:
> 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/MonteCarlosimulationforVaRestimationtp4082611p4090907.html> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rsigfinance>  Subscriberposting only. If you want to post, subscribe first.
>  Also note that this is not the rhelp list where general R questions
> should go.
>
[[alternative HTML version deleted]]
_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.

