Saturday 6 December 2014

FINANCIAL DATA MODELLING WITH R- PORTFOLIO OPTIMIZATION (PART-II)


PORTFOLIO OPTIMIZATION-2


According to Modern Portfolio Theory, an investor can effectively reduce the total risk of his portfolio through mean-variance optimization (diversification). The analysis in the previous post created portfolios that had negative weights, or "short" positions. To overcome we search for options to block short positions and arrived at a couple of other websites that demonstrated portfolio optimization with more complex models.

Installing the “tseries” package
 
Function portfolio.optim in package “tseries” is called for getting efficient frontier and solve.QP from package “quandprog” is used for portfolio construction.
 
> library("tseries")
> India5 <- read.csv("India5.csv")
> assets <- India5[,3:7]
> returns <- log(tail(assets, -1) / head(assets, -1))
 
Create an optimum portfolio with expected means defined and shorts allowed
 
> w2 <- portfolio.optim(as.matrix(returns),pm = 0.005,shorts = TRUE,riskless= FALSE)
> w2$pw
[1] -0.6421529  2.6189265 -0.3917414 -0.7363362  0.1513039
> sum(w2$pw)
[1] 1
 
Create an optimum portfolio with expected means defined and shorts NOT ALLOWED
 
> w2 <- portfolio.optim(as.matrix(returns),shorts = FALSE,riskless = FALSE)
> w2$pw
[1] 0.07895285 0.24635246 0.05768448 0.60260070 0.01440952
> sum(w2$pw)
[1] 1
> w2$pm
[1] 0.0003442065
 
Create a function to create the frontier using portfolio.optim
 
> frontier2 <- function(return,minRet,maxRet){
+     rbase <- seq(minRet,maxRet,length=100)
+     s <- sapply(rbase,function(x){
+         p2 <- portfolio.optim(as.matrix(returns),pm = x, shorts = TRUE)
+         p2$ps^2
+     })
+     plot(s,rbase,xlab="Variance",ylab="Return", main="w/Portfolio.Optim")
+ }
> frontier2(returns,-0.0005,0.05)
 
It is observed that with Short positions allowed (i.e. shorts=true), the portfolio weights are the same as what was obtained in the previous program ( check PORTFOLIO OPTIMIZATION-1 ), and as expected they sum up to 1.
 
> w2$pw
[1] -0.6421529  2.6189265 -0.3917414 -0.7363362  0.1513039
> sum(w2$pw)
[1] 1
 
But when shorts are not allowed that is shorts= False, we see altogether a different portfolio is created.

> w2$pw
[1] 0.07895285 0.24635246 0.05768448 0.60260070 0.01440952
> sum(w2$pw)
[1] 1
> w2$pm
[1] 0.0003442065
 
However, when short positions are not allowed, it is not possible to specify the portfolio returns beforehand ( as it was, in the shorts allowed case, where pm = 0.005). With this constraint, the portfolio returns has dropped to 0.000344.



The efficient frontier remains the same as before-

No comments:

Post a Comment