PORTFOLIO OPTIMIZATION-1
This chapter illustrates how to extract data on returns on 5
Indian Companies that are listed in the NSE and use to create an optimum
portfolio and draw the efficient frontier.. The following R program shows how
this data for 5 Indian Companies such as Adani Power, Suzlon Energy, NTPC,
CESC, GVK has been extracted from Quandl and converted into a CSV file for
repeated usage.
Creating Dataset
> library(Quandl)
> Quandl.auth
("vFTSoFd2sWdPtJfKz-mn")
> FADANI1314<-
Quandl("GOOG/NSE_ADANIPOWER", trim_start="2013-01-01",
trim_end="2014-07-30")
> FSUZ1314<-
Quandl("NSE/SUZLON", trim_start="2013-01-01",
trim_end="2014-07-30")
> FNTPC1314<- Quandl("NSE/NTPC",
trim_start="2013-01-01", trim_end="2014-07-30")
> FCESC1314<- Quandl("NSE/CESC",
trim_start="2013-01-01", trim_end="2014-07-30")
> FGVK1314<-
Quandl("NSE/GVKPIL", trim_start="2013-01-01",
trim_end="2014-07-30")
> library("plyr", lib.loc="~/R/win-library/3.1")
> CADANI <- FADANI1314[,c('Date','Close')]
> CCESC <- FCESC1314[,c('Date','Close')]
> CGVK <- FGVK1314[,c('Date','Close')]
> CNTPC <- FNTPC1314[,c('Date','Close')]
> CSUZ <- FSUZ1314[,c('Date','Close')]
> Stocks <-
merge(CADANI,CCESC,by.x='Date',by.y='Date')
> Stocks <-
rename(Stocks,c("Close.x"="Adani","Close.y"="CESC"))
> Stocks <-
merge(Stocks,CGVK,by.x='Date',by.y='Date')
> Stocks <-
rename(Stocks,c("Close"="GVK"))
> Stocks <-
merge(Stocks,CNTPC,by.x='Date',by.y='Date')
> Stocks <-
rename(Stocks,c("Close"="NTPC"))
> Stocks <-
merge(Stocks,CSUZ,by.x='Date',by.y='Date')
> Stocks <-
rename(Stocks,c("Close"="SUZ"))
> write.csv(Stocks,"India5.csv")
The main concept of portfolio optimization (which won theNobel Prize for Harry Markowitz in 1990) is based on the correlation between
investment products, we can reduce the risk (which in this case is measured by
variance) of the portfolio and still get the desired expected return.
> India5 <- read.csv("India5.csv")
> View(India5)
> assets <- India5[,3:7]
> View(assets)
Calculate the Returns
> returns <- log(tail(assets, -1) / head(assets, -1))
To Calculate the Optimum Portfolio Weights
> OptWeights <- function(return, mu = 0.005) {
+ Q <- rbind(cov(return), rep(1, ncol(assets)), colMeans(return))
+ Q <- cbind(Q, rbind(t(tail(Q, 2)), matrix(0, 2, 2)))
+ b <- c(rep(0, ncol(assets)), 1, mu)
+ head(solve(Q, b),-2)
+ }
Call the function with data and note that the weights add up to 1
> OptWeights(returns)
Adani CESC GVK NTPC SUZ
-0.6421529 2.6189265 -0.3917414 -0.7363362 0.1513039
> sum(OptWeights(returns))
[1] 1
Defining a function to create the Graph of Efficient Frontier
> frontier <- function(return,minRet,maxRet){
+ Q <- cov(return)
+ n <- ncol(assets)
+ r <- colMeans(return)
+ Q1 <- rbind(Q,rep(1,n),r)
+ Q1 <- cbind(Q1,rbind(t(tail(Q1,2)),matrix(0,2,2)))
+ rbase <- seq(minRet,maxRet,length=100)
+ s <- sapply(rbase,function(x){
+ y <- head(solve(Q1,c(rep(0,n),1,x)),n)
+ y %*% Q %*% y
+ })
+ plot(s,rbase,xlab="Variance",ylab="Return", main = "Custom")
+ }
Plot the Efficient Frontier between two values of Return
> frontier(returns,-0.0005,0.05)
On the
variance-return plane, the desired return-minimum variance curve is called
Portfolio Frontier.
However we note that some of the weights
are negative, which means that the portfolio allows "short" positions.
It is possible to bar short positions.
No comments:
Post a Comment