[R-SIG-Finance] VaR again
Brian G. Peterson
brian at braverock.com
Thu Apr 30 17:16:53 CEST 2009
res,
We've got functions in PerformanceAnalytics that do this as well,
including for non-normally distributed portfolio instruments. They're
in the current CRAN version, and improved and expanded in the version
we're getting ready for release.
We extended Qian's two-instrument example to n-instrument portfolios in
our paper in JoR "Estimation and Decomposition of Downside Risk for
Portfolios with Non-Normal Returns", and added modified expected
shortfall and its portfolio counterpart as well.
Regards,
- Brian
resident76 wrote:
> megh,
>
> Like you, I agree with Brian as well about the normality issue. I have the
> following code to estimate fractional contributions to risk assuming normal
> returns (for calculating var-cov matrix at least). I tried to make it
> general so you can slap it in with a minimal effort. It should work for N
> assets, just as long as you define vector of weights to reflect the number
> of assets...
>
> The calculations are based on a paper by Edward Qian: On the Financial
> Interpretation of Risk Contribution: Risk Budgets Do Add Up, located at:
>
> http://papers.ssrn.com/sol3/papers.cfm?abstract_id=684221
>
> hope that helps out.
>
> res
>
> #----------------------------------------------------------------------------------------------
> # Standard mean-variance portfolio estimates
> # Use mean returns on closing prices for covariance matrix construction
>
> # The following code assumes you have a vector of weights such as:
> # wts <- matrix(c(0.4,0.6),ncol=2,nrow=1,byrow=T)
> # and a matrix of asset prices where the columns are the individual assets
> such as:
> # assets <- cbind(open, high, low, close) = cbind(O,H,L,C)
> # so for multiple assets:
> # assets <- cbind(O1,H1,L1,C1, O2,H2,L2,C2, ..., On,Hn,Ln,Cn)
> # then transform assets to log assets by:
> # ln.assets <- log(assets)
>
> # assets.labels is a vector of length N of the asset names:
> # assets.labels <- c("SBUX", "SPX", "C", ..., "asset-N")
> # annualization is the factor used to adjust the volatility to annual
> volatility
> # Rf is the risk-free rate
> #
>
>
> ind <- 1:length(assets.labels)
> assets.rtn <- ln.assets[2:N,(4*ind)] - ln.assets[1:(N-1),(4*ind)]
> cov.rtn <- cov(assets.rtn); colnames(cov.rtn) = assets.labels ;
> rownames(cov.rtn) = assets.labels
> cor.rtn <- cor(assets.rtn); colnames(cor.rtn) = assets.labels ;
> rownames(cor.rtn) = assets.labels
>
> vol.scen <- matrix(0.0,ncol=2,nrow=nrow(wts))
> vol.scen[,1] <- sqrt(wts%*%cov.rtn%*%t(wts))
> vol.scen[,2] <- vol.scen[,1]*sqrt(annualization)
> vol.scen <- t(vol.scen)
>
> # Expected Returns & Sharpe Ratio
> E.rtn <- sum(wts*colMeans(assets.rtn))- Rf
> Srp <- E.rtn/vol.scen[1,1]
>
> cat("Standard Portfolio E(R) & Vol Given Scenario Weights:", "\n")
> print(round(vol.scen*100,2))
> cat("E(R): ", round(E.rtn*100,4), "\n")
> cat("Sharpe: ", round(Srp*100,4), "\n")
> cat("Weights: ", "\n")
> print(round(wts,3))
> cat("Assest Exposure: ",sum(wts), "\n")
> cat("-----------------------------------------------------------------------------------------",
> "\n")
>
>
> #----------------------------------------------------------------------------------------------
> # Proportional Contribution to Risk Estimate
> # --start with partial derivatives wrt weights--
>
> partials <- matrix(0.0,ncol=1 ,nrow=length(wts))
> for(i in 1:length(wts)){
> partials[i,] <- sum(wts%*%cov.rtn[i,])
> }
> partials <- partials*(1/vol.scen[1,1])
> frac.contrib <- t(wts)*partials*(1/vol.scen[1,1])
> rownames(frac.contrib) = assets.labels ; colnames(frac.contrib) = ann.name
>
> cat("Percentage Contribution To Total Risk:", "\n")
> print(round(frac.contrib*100,2))
> cat("-----------------------------------------------------------------------------------------",
> "\n")
> #----------------------------------------------------------------------------------------------
>
>
>
>
>
>
--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
More information about the R-SIG-Finance
mailing list