rm(list = ls())

#libraries

#Loading the required packages
library(tseries)
## Warning: package 'tseries' was built under R version 3.5.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
library(fPortfolio)
## Loading required package: timeDate
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.5.3
## Loading required package: fBasics
## Loading required package: fAssets
library(quantmod)
## Warning: package 'quantmod' was built under R version 3.5.3
## Loading required package: xts
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.5.3
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
## 
##     time<-
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## 
## Attaching package: 'TTR'
## The following object is masked from 'package:fBasics':
## 
##     volatility
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(caTools)
## Warning: package 'caTools' was built under R version 3.5.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:timeSeries':
## 
##     filter, lag
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 3.5.3
## 
## Attaching package: 'PerformanceAnalytics'
## The following objects are masked from 'package:timeDate':
## 
##     kurtosis, skewness
## The following object is masked from 'package:graphics':
## 
##     legend
library(crayon)
## 
## Attaching package: 'crayon'
## The following object is masked from 'package:ggplot2':
## 
##     %+%
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.5.3
library(portfolio)
## Warning: package 'portfolio' was built under R version 3.5.3
## Loading required package: grid
## Loading required package: lattice
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## The following object is masked from 'package:fPortfolio':
## 
##     getData
## 
## Attaching package: 'portfolio'
## The following object is masked from 'package:TTR':
## 
##     getYahooData
library(ROI)
## Warning: package 'ROI' was built under R version 3.5.3
## ROI: R Optimization Infrastructure
## Registered solver plugins: nlminb, clp, glpk, quadprog.
## Default solver: auto.
## 
## Attaching package: 'ROI'
## The following object is masked from 'package:fBasics':
## 
##     vech
library(zoo)
library(DEoptim)
## Warning: package 'DEoptim' was built under R version 3.5.3
## Loading required package: parallel
## 
## DEoptim package
## Differential Evolution algorithm in R
## Authors: D. Ardia, K. Mullen, B. Peterson and J. Ulrich
library(ROI.plugin.glpk)
## Warning: package 'ROI.plugin.glpk' was built under R version 3.5.3
library(ROI.plugin.quadprog)
## Warning: package 'ROI.plugin.quadprog' was built under R version 3.5.3
library(corrplot)
## corrplot 0.84 loaded
library(PortfolioAnalytics)
## Loading required package: foreach
## Warning: package 'foreach' was built under R version 3.5.3
## 
## Attaching package: 'PortfolioAnalytics'
## The following objects are masked from 'package:ROI':
## 
##     is.constraint, objective
source("https://faculty.washington.edu/ezivot/econ424/portfolio.r")
#SBI
Sbi <- tseries::get.hist.quote(instrument = "SBIN.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## time series ends   2018-07-01
nrow(Sbi)
## [1] 133
Sbi <- na.omit(Sbi)

#Apollo
Apollo <- tseries::get.hist.quote(instrument = "APOLLOHOSP.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(Apollo)
## [1] 133
Apollo <- na.omit(Apollo)

#YESBANK
YESBANK <- tseries::get.hist.quote(instrument = "YESBANK.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(YESBANK)
## [1] 133
YESBANK <- na.omit(YESBANK)

#Vedanta
Vedanta <- tseries::get.hist.quote(instrument = "VEDL.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(Vedanta)
## [1] 133
Vedanta <- na.omit(Vedanta)

#Piramal
Piramal <- tseries::get.hist.quote(instrument = "PEL.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(Piramal)
## [1] 133
Piramal <- na.omit(Piramal)

#ZEETV
ZeeTV <- tseries::get.hist.quote(instrument = "ZEEL.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(ZeeTV)
## [1] 133
ZeeTV <- na.omit(ZeeTV)

#LIC
LIC <- tseries::get.hist.quote(instrument = "LICHSGFIN.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(LIC)
## [1] 133
LIC <- na.omit(LIC)

#HERO
hero <- tseries::get.hist.quote(instrument = "HEROMOTOCO.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(hero)
## [1] 133
hero <- na.omit(hero)

#Britannia
Britannia <- tseries::get.hist.quote(instrument = "BRITANNIA.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(Britannia)
## [1] 133
Britannia <- na.omit(Britannia)

#DLF
DLF <- tseries::get.hist.quote(instrument = "DLF.NS", start = "2007-07-01", end = "2018-07-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", compression = "m", retclass = "zoo")
## time series ends   2018-07-01
nrow(DLF)
## [1] 133
DLF <- na.omit(DLF)

#create vector that contains names of the assets

portfolio.names = c("Sbi", "Apollo", "YESBANK", "Britannia", "DLF", "hero", "Piramal", "Vedanta", "ZeeTV", "LIC")

#merging all the stock prices

Portfolio.Prices <- merge(Sbi, Apollo, YESBANK, Britannia, DLF, hero, Piramal, Vedanta, ZeeTV, LIC)

Portfolio.Names <- names(Portfolio.Prices)

names(Portfolio.Prices) = Portfolio.Names
head(Portfolio.Prices)
##            Adjusted.Sbi Adjusted.Apollo Adjusted.YESBANK
## 2007-07-01     45.59076        214.7895         24.33723
## 2007-08-01     44.93076        214.9004         23.72752
## 2007-09-01     54.63101        216.9792         26.56430
## 2007-10-01     58.06607        216.6439         27.89284
## 2007-11-01     64.64130        216.1745         29.63854
## 2007-12-01     66.57160        235.0403         32.00038
##            Adjusted.Britannia Adjusted.DLF Adjusted.hero Adjusted.Piramal
## 2007-07-01           73.15133     540.5875      449.2775         186.8723
## 2007-08-01           67.23600     527.6071      442.9987         186.8723
## 2007-09-01           69.15675     672.2906      510.4188         194.6934
## 2007-10-01           72.86964     837.5570      495.6225         208.2589
## 2007-11-01           70.58516     828.1682      492.7180         212.5153
## 2007-12-01           69.69122     951.5568      475.9056         248.1250
##            Adjusted.Vedanta Adjusted.ZeeTV Adjusted.LIC
## 2007-07-01         38.52100       130.9777     22.14137
## 2007-08-01         41.58368       123.5770     21.92880
## 2007-09-01         53.98755       137.8094     28.33504
## 2007-10-01        101.90095       133.2744     41.98002
## 2007-11-01         94.21157       116.0606     40.72829
## 2007-12-01        104.32703       131.8433     45.18019

#calculate the returns for the portfolio

Portfolio.Returns = Return.calculate(Portfolio.Prices, method="simple")
Portfolio.Returns = na.omit(Portfolio.Returns)

head(Portfolio.Returns)
##            Adjusted.Sbi Adjusted.Apollo Adjusted.YESBANK
## 2007-08-01  -0.01447653    0.0005162497     -0.025052602
## 2007-09-01   0.21589317    0.0096735196      0.119556395
## 2007-10-01   0.06287759   -0.0015455075      0.050012123
## 2007-11-01   0.11323690   -0.0021665741      0.062586250
## 2007-12-01   0.02986181    0.0872712276      0.079688024
## 2008-01-01  -0.03123508   -0.0556348474      0.008624241
##            Adjusted.Britannia Adjusted.DLF Adjusted.hero Adjusted.Piramal
## 2007-08-01        -0.08086427  -0.02401167  -0.013975413       0.00000000
## 2007-09-01         0.02856723   0.27422589   0.152190372       0.04185276
## 2007-10-01         0.05368815   0.24582576  -0.028988596       0.06967609
## 2007-11-01        -0.03135030  -0.01120981  -0.005860206       0.02043829
## 2007-12-01        -0.01266474   0.14898988  -0.034121886       0.16756274
## 2008-01-01        -0.01829031  -0.23605411  -0.005384986      -0.13291497
##            Adjusted.Vedanta Adjusted.ZeeTV Adjusted.LIC
## 2007-08-01       0.07950685    -0.05650360 -0.009600266
## 2007-09-01       0.29828708     0.11517049  0.292137847
## 2007-10-01       0.88748966    -0.03290802  0.481558749
## 2007-11-01      -0.07545933    -0.12916072 -0.029817233
## 2007-12-01       0.10736956     0.13598683  0.109307214
## 2008-01-01      -0.20487281    -0.13010620 -0.267903783
nrow(Portfolio.Returns)
## [1] 132

#calculating the expected return for the portfolio

Expected.Returns <-  colMeans(Portfolio.Returns[sapply(Portfolio.Returns, is.numeric)])

#Define column names
names(Portfolio.Returns) <- Portfolio.Names
names(Expected.Returns) = Portfolio.Names

#COVARIANCE matrix of portfolio

covmat = round(cov(Portfolio.Returns),5)
dimnames(covmat) = list(portfolio.names,portfolio.names)

#covariance matrix
covmat
##               Sbi   Apollo  YESBANK Britannia     DLF     hero Piramal
## Sbi       0.01413  0.00152  0.01450   0.00160 0.01297  0.00367 0.00418
## Apollo    0.00152  0.00622 -0.00608  -0.00022 0.00507 -0.00010 0.00085
## YESBANK   0.01450 -0.00608  0.43881   0.03111 0.02448  0.00329 0.01433
## Britannia 0.00160 -0.00022  0.03111   0.01545 0.00169  0.00113 0.00171
## DLF       0.01297  0.00507  0.02448   0.00169 0.02993  0.00364 0.00514
## hero      0.00367 -0.00010  0.00329   0.00113 0.00364  0.00678 0.00161
## Piramal   0.00418  0.00085  0.01433   0.00171 0.00514  0.00161 0.00851
## Vedanta   0.00724  0.00159  0.00161  -0.00112 0.01296  0.00423 0.00595
## ZeeTV     0.00524  0.00285  0.00498   0.00146 0.00924  0.00151 0.00330
## LIC       0.01109  0.00260  0.03387   0.00185 0.01620  0.00468 0.00559
##            Vedanta   ZeeTV     LIC
## Sbi        0.00724 0.00524 0.01109
## Apollo     0.00159 0.00285 0.00260
## YESBANK    0.00161 0.00498 0.03387
## Britannia -0.00112 0.00146 0.00185
## DLF        0.01296 0.00924 0.01620
## hero       0.00423 0.00151 0.00468
## Piramal    0.00595 0.00330 0.00559
## Vedanta    0.02728 0.00478 0.01242
## ZeeTV      0.00478 0.00890 0.00620
## LIC        0.01242 0.00620 0.02023

#Convert the returns to a time series object to utilize the functions of fPortfolio package

Portfolio.Returns <- as.timeSeries(Portfolio.Returns)
#Equally Weighted Portfolio
#Assign equal weights to all the assets
equal_weightage = rep(1,10)/10

#getPortfolio function is in portfolio.R file  
#Invoke the function call directly from portfolio.R or copy the function code to current notebook.
#Create a portfolio by assigning equal weights to all the assets.
Equalweight.Portfolio <- getPortfolio(er=Expected.Returns,cov.mat=covmat,weights=equal_weightage)

#components of portfolio objects
#names(EqualWeight.Portfolio)

#get the weights for equally waited portfolio 
eql.weights <- as.vector(Equalweight.Portfolio$weights)


#create Global Minimum Portfolio (means no preset target return)
glbmin.Portfolio = minvariancePortfolio(Portfolio.Returns, spec = portfolioSpec(), 
                                         constraints = c("min=0","max=1","LongOnly"))
#get weights for global minimum portfolio
global.min.Weights = getWeights(glbmin.Portfolio)

#get the expected return and std.Deviation of the global minimum portfolio 
Global.Min.Portfolio <- getPortfolio(Expected.Returns, covmat,
                                     weights=as.vector(global.min.Weights))

#For minimum variance and efficient portfolios we need to set the target return.
Spec.Obj = portfolioSpec() 

#Invoke Specifications object
setType(Spec.Obj) = "CVaR"
## Solver set to solveRquadprog
## setSolver: solveRglpk
#set type Varaince 
setSolver(Spec.Obj) = "solveRglpk.CVAR"

#Solver method to be used to resolve weights
setTargetReturn(Spec.Obj) <- 0.015 

#set a target return of 1.5%
#Create Minimum Variance Portfolio for a  target return of 1.5%
MVPortfolio <- minvariancePortfolio(Portfolio.Returns, Spec.Obj, constraints = c("min=0","max=1","LongOnly"))
min.variance.weights <- getWeights(MVPortfolio)
Min.Variance.Portfolio <- getPortfolio(Expected.Returns, covmat, min.variance.weights)

#Create Efficient portfolio for a target return of 1.5%
eff.Portfolio <- efficientPortfolio(Portfolio.Returns, Spec.Obj, constraints = c("min=0", "max=1", "LongOnly"))
eff.weights <- getWeights(eff.Portfolio)
Efficient.Portfolio <- getPortfolio(er = Expected.Returns, cov.mat = covmat, weights = as.vector(eff.weights))

#Need to specify the target risk for tangency and max return portfolios
#Set risk free rate to rk.free(0.005) in the specifications object.
rk.free <- 0.005
Spec.Obj.rkf <- portfolioSpec()
setType(Spec.Obj.rkf) = "CVaR"
## Solver set to solveRquadprog
## setSolver: solveRglpk
#set type Varaince 
setSolver(Spec.Obj.rkf) = "solveRglpk.CVAR"

#Solver method to be used to resolve weights 
setRiskFreeRate(Spec.Obj.rkf) <- rk.free

#Tangency portfolio or min. risk portfolio is the portfolio of risky assets with the highest sharpe's slope
tan.Portfolio <- tangencyPortfolio(Portfolio.Returns, Spec.Obj.rkf, constraints = c("min=0","max=1","LongOnly"))

#get weights for tangency portfolio
tan.weights=getWeights(tan.Portfolio)
#Tangency.Portfolio
Tangency.Portfolio <- getPortfolio(Expected.Returns, covmat, tan.weights)


#set target risk for Max return portfolio
Spec.Obj.risk <- portfolioSpec()
setType(Spec.Obj.risk) = "CVaR"
## Solver set to solveRquadprog
## setSolver: solveRglpk
#set type Varaince 
setSolver(Spec.Obj.risk) = "solveRglpk.CVAR"

#Solver method to be used to resolve weights 
setTargetRisk(Spec.Obj.risk) <- 0.005

#Maximum Return Portfolio
MR.Portfolio <- maxreturnPortfolio(Portfolio.Returns, Spec.Obj.risk,
                                   constraints =c("min=0", "max=1", "LongOnly"))
max.return.weights <- getWeights(MR.Portfolio)
Max.Return.Portfolio <- getPortfolio(Expected.Returns, covmat, max.return.weights)

#Preparing the data required to draw and visualize the Portfolios
#Reassigning names with new line character for better readability for plots

Portfolio.Names <- c("Sbi", "Apollo", "YESBANK", "Britannia", "DLF", "hero", "Piramal", "Vedanta", "ZeeTV", "LIC")
names(eql.weights) <- Portfolio.Names
dfe = as.data.frame(cbind(Portfolio.Names, as.numeric(round(eql.weights, 2))))
colnames(dfe) = c("Assets", "Weights")

names(global.min.Weights) = Portfolio.Names
dfg = as.data.frame(cbind(Portfolio.Names, as.numeric(round(global.min.Weights, 2))))
colnames(dfg) = c("Assets", "Weights")
global.min.Weights
##        Sbi     Apollo    YESBANK  Britannia        DLF       hero 
## 0.00000000 0.37665532 0.00000000 0.12401840 0.00000000 0.30934754 
##    Piramal    Vedanta      ZeeTV        LIC 
## 0.16244531 0.00000000 0.02753343 0.00000000
min.variance.weights
##       Adjusted.Sbi    Adjusted.Apollo   Adjusted.YESBANK 
##          0.1308780          0.2956935          0.0000000 
## Adjusted.Britannia       Adjusted.DLF      Adjusted.hero 
##          0.1568356          0.0000000          0.2517951 
##   Adjusted.Piramal   Adjusted.Vedanta     Adjusted.ZeeTV 
##          0.1647978          0.0000000          0.0000000 
##       Adjusted.LIC 
##          0.0000000
names(min.variance.weights) = Portfolio.Names
dfmv = as.data.frame(cbind(Portfolio.Names, as.numeric(round(min.variance.weights, 2))))
colnames(dfmv) = c("Assets", "Weights")

names(eff.weights) = Portfolio.Names
dfeff = as.data.frame(cbind(Portfolio.Names, as.numeric(round(eff.weights, 2))))
colnames(dfeff) = c("Assets", "Weights")

names(tan.weights) = Portfolio.Names
dft <- as.data.frame(cbind(Portfolio.Names, as.numeric(round(tan.weights, 2))))
colnames(dft) <- c("Assets", "Weights")
names(max.return.weights) = Portfolio.Names
dfm <- as.data.frame(cbind(Portfolio.Names, as.numeric(round(max.return.weights, 2))))
colnames(dfm) <- c("Assets", "Weights")
#Plotting:
#Plot Equal weightage Portfolio weights
#barplot(eql.weights, col="skyblue", cex.names = 0.7)

ggplot(data=dfe, aes(x=dfe$Assets, y=dfe$Weights, fill="red")) + 
  geom_bar(stat="identity", position="stack",width=0.5) + 
  geom_text(data=dfe, aes(label=dfe$Weights), 
            position =position_stack(vjust=0.95), size = 2)+
  scale_x_discrete(name = "") +
  scale_y_discrete(name= "") +
  ggtitle("Equal Weightage Portfolio") +
  theme_bw() +
  theme(legend.position = "none",
        axis.line.x = element_line(size = 0.2, colour = "black"),
        plot.title = element_text(size=8, face="bold"))+
  scale_fill_brewer(type="qual")+ theme(axis.text.x = element_text(angle = 70, hjust = 1))

#Here we have equal weightages to all the assets in our portfolio.

#Equally weighted Portfolio
cat("Expected return: ", round(Equalweight.Portfolio$er, 4), 
    "\nStd.Deviation/Risk: ", round(Equalweight.Portfolio$sd, 4))
## Expected return:  0.0279 
## Std.Deviation/Risk:  0.1072
#plot the global minimum portfolio weights
ggplot(data=dfg, aes(x=dfg$Assets, y=dfg$Weights, fill="green")) + 
  geom_bar(stat="identity", position="stack",width=0.5) + 
  geom_text(data=dfg, aes(label=dfg$Weights), 
            position =position_stack(vjust=0.95), size = 3)+
  scale_x_discrete(name = "") +
  scale_y_discrete(name= "") +
  ggtitle("Global Minimum Portfolio") +
  theme_bw() +
  theme(legend.position = "none",
        axis.line.x = element_line(size = 0.3, colour = "black"),
        plot.title = element_text(size=8, face="bold"))+
  scale_fill_brewer(type="qual")+theme(axis.text.x = element_text(angle = 70,hjust = 1))

#plot the Minimum Variance portfolio weights

ggplot(data=dfmv, aes(x=dfmv$Assets, y=dfmv$Weights, fill="green")) + 
  geom_bar(stat="identity", position="stack",width=0.5) + 
  geom_text(data=dfmv, aes(label=dfmv$Weights), 
            position =position_stack(vjust=0.95), size = 3)+
  scale_x_discrete(name = "") +
  scale_y_discrete(name= "") +
  ggtitle("Minimum Variance Portfolio") +
  theme_bw() +
  theme(legend.position = "none",
        axis.line.x = element_line(size = 0.3, colour = "black"),
        plot.title = element_text(size=8, face="bold"))+
  scale_fill_brewer(type="qual")+theme(axis.text.x = element_text(angle = 70,hjust = 1))

#Plot the Efficient Protfolio weights

ggplot(data=dfeff, aes(x=dfeff$Assets, y=dfeff$Weights, fill="green")) + 
  geom_bar(stat="identity", position="stack",width=0.5) + 
  geom_text(data=dfeff, aes(label=dfeff$Weights), 
            position =position_stack(vjust=0.95), size = 3)+
  scale_x_discrete(name = "") +
  scale_y_discrete(name= "") +
  ggtitle("Efficient Portfolio") +
  theme_bw() +
  theme(legend.position = "none",
        axis.line.x = element_line(size = 0.2, colour = "black"),
        plot.title = element_text(size=8, face="bold"))+
  scale_fill_brewer(type="qual")+theme(axis.text.x = element_text(angle = 70,hjust = 1))

#Efficient Portfolio

cat("Expected return: ", round(Efficient.Portfolio$er, 4), 
    "\nStd.Deviation/Risk: ", round(Efficient.Portfolio$sd, 4))
## Expected return:  0.015 
## Std.Deviation/Risk:  0.0609
#plot the tangency portfolio weights
ggplot(data=dft, aes(x=dft$Assets, y=dft$Weights, fill="green")) +
  geom_bar(stat="identity", position="stack",width=0.5) + 
  geom_text(data=dft, aes(label=dft$Weights), 
            position =position_stack(vjust=0.95), size = 3)+
  scale_x_discrete(name = "") +
  scale_y_discrete(name= "") +
  ggtitle("Tangency Portfolio") +
  theme_bw() +
  theme(legend.position = "none",
        axis.line.x = element_line(size = 0.2, colour = "black"),
        plot.title = element_text(size=8, face="bold"))+
  scale_fill_brewer(type="qual")+theme(axis.text.x = element_text(angle = 70,hjust = 1))

#Tangency Portfolio

cat("Expected Return: ", round(Tangency.Portfolio$er, 4), 
    "\nStd.Deviation/Risk: ", round(Tangency.Portfolio$sd, 4))
## Expected Return:  0.0336 
## Std.Deviation/Risk:  0.1362
#plot the Maximum Return portfolio weights


ggplot(data=dfm, aes(x=dfm$Assets, y=dfm$Weights, fill="green")) +
  geom_bar(stat="identity", position="stack",width=0.5) + 
  geom_text(data=dfm, aes(label=dfm$Weights), 
            position =position_stack(vjust=0.95), size = 3)+
  scale_x_discrete(name = "") +
  scale_y_discrete(name= "") +
  ggtitle("Max Return Portfolio") +
  theme_bw() +
  theme(legend.position = "none",
        axis.line.x = element_line(size = 0.2, colour = "black"),
        plot.title = element_text(size=8, face="bold"))+
  scale_fill_brewer(type="qual")+theme(axis.text.x = element_text(angle = 70,hjust = 1))

#Maximum Return Portfolio

cat("The Expected Return: ", round(Max.Return.Portfolio$er, 4), 
    "\nStd.Deviation/Risk: ", round(Max.Return.Portfolio$sd, 4))
## The Expected Return:  0.0211 
## Std.Deviation/Risk:  0.0528
#Efficient frontier
Eff.Portfolio.Frontier <- portfolioFrontier(Portfolio.Returns, spec=portfolioSpec(), 
                                            constraints = "LongOnly")
Eff.Portfolio.Frontier
## 
## Title:
##  MV Portfolio Frontier 
##  Estimator:         covEstimator 
##  Solver:            solveRquadprog 
##  Optimize:          minRisk 
##  Constraints:       LongOnly 
##  Portfolio Points:  5 of 50 
## 
## Portfolio Weights:
##    Adjusted.Sbi Adjusted.Apollo Adjusted.YESBANK Adjusted.Britannia
## 1        0.0000          0.0000           0.0000             0.0000
## 13       0.0000          0.1956           0.0000             0.3190
## 25       0.0000          0.0000           0.1993             0.6259
## 37       0.0000          0.0000           0.5792             0.4208
## 50       0.0000          0.0000           1.0000             0.0000
##    Adjusted.DLF Adjusted.hero Adjusted.Piramal Adjusted.Vedanta
## 1        1.0000        0.0000           0.0000           0.0000
## 13       0.0000        0.1582           0.2163           0.0231
## 25       0.0000        0.0000           0.0000           0.0000
## 37       0.0000        0.0000           0.0000           0.0000
## 50       0.0000        0.0000           0.0000           0.0000
##    Adjusted.ZeeTV Adjusted.LIC
## 1          0.0000       0.0000
## 13         0.0000       0.0878
## 25         0.0000       0.1748
## 37         0.0000       0.0000
## 50         0.0000       0.0000
## 
## Covariance Risk Budgets:
##    Adjusted.Sbi Adjusted.Apollo Adjusted.YESBANK Adjusted.Britannia
## 1        0.0000          0.0000           0.0000             0.0000
## 13       0.0000          0.0832           0.0000             0.4774
## 25       0.0000          0.0000           0.6495             0.2927
## 37       0.0000          0.0000           0.9375             0.0625
## 50       0.0000          0.0000           1.0000             0.0000
##    Adjusted.DLF Adjusted.hero Adjusted.Piramal Adjusted.Vedanta
## 1        1.0000        0.0000           0.0000           0.0000
## 13       0.0000        0.0966           0.1997           0.0225
## 25       0.0000        0.0000           0.0000           0.0000
## 37       0.0000        0.0000           0.0000           0.0000
## 50       0.0000        0.0000           0.0000           0.0000
##    Adjusted.ZeeTV Adjusted.LIC
## 1          0.0000       0.0000
## 13         0.0000       0.1206
## 25         0.0000       0.0578
## 37         0.0000       0.0000
## 50         0.0000       0.0000
## 
## Target Returns and Risks:
##      mean    Cov   CVaR    VaR
## 1  0.0062 0.1730 0.2977 0.2359
## 13 0.0258 0.0610 0.0973 0.0608
## 25 0.0454 0.1861 0.1687 0.0907
## 37 0.0650 0.4063 0.2901 0.1320
## 50 0.0863 0.6624 0.4429 0.2489
## 
## Description:
##  Wed Aug 28 11:10:49 2019 by user: Vinodh Chennu
eff.weights <- getWeights(Eff.Portfolio.Frontier)
Eff.Portfolio.Frontier
## 
## Title:
##  MV Portfolio Frontier 
##  Estimator:         covEstimator 
##  Solver:            solveRquadprog 
##  Optimize:          minRisk 
##  Constraints:       LongOnly 
##  Portfolio Points:  5 of 50 
## 
## Portfolio Weights:
##    Adjusted.Sbi Adjusted.Apollo Adjusted.YESBANK Adjusted.Britannia
## 1        0.0000          0.0000           0.0000             0.0000
## 13       0.0000          0.1956           0.0000             0.3190
## 25       0.0000          0.0000           0.1993             0.6259
## 37       0.0000          0.0000           0.5792             0.4208
## 50       0.0000          0.0000           1.0000             0.0000
##    Adjusted.DLF Adjusted.hero Adjusted.Piramal Adjusted.Vedanta
## 1        1.0000        0.0000           0.0000           0.0000
## 13       0.0000        0.1582           0.2163           0.0231
## 25       0.0000        0.0000           0.0000           0.0000
## 37       0.0000        0.0000           0.0000           0.0000
## 50       0.0000        0.0000           0.0000           0.0000
##    Adjusted.ZeeTV Adjusted.LIC
## 1          0.0000       0.0000
## 13         0.0000       0.0878
## 25         0.0000       0.1748
## 37         0.0000       0.0000
## 50         0.0000       0.0000
## 
## Covariance Risk Budgets:
##    Adjusted.Sbi Adjusted.Apollo Adjusted.YESBANK Adjusted.Britannia
## 1        0.0000          0.0000           0.0000             0.0000
## 13       0.0000          0.0832           0.0000             0.4774
## 25       0.0000          0.0000           0.6495             0.2927
## 37       0.0000          0.0000           0.9375             0.0625
## 50       0.0000          0.0000           1.0000             0.0000
##    Adjusted.DLF Adjusted.hero Adjusted.Piramal Adjusted.Vedanta
## 1        1.0000        0.0000           0.0000           0.0000
## 13       0.0000        0.0966           0.1997           0.0225
## 25       0.0000        0.0000           0.0000           0.0000
## 37       0.0000        0.0000           0.0000           0.0000
## 50       0.0000        0.0000           0.0000           0.0000
##    Adjusted.ZeeTV Adjusted.LIC
## 1          0.0000       0.0000
## 13         0.0000       0.1206
## 25         0.0000       0.0578
## 37         0.0000       0.0000
## 50         0.0000       0.0000
## 
## Target Returns and Risks:
##      mean    Cov   CVaR    VaR
## 1  0.0062 0.1730 0.2977 0.2359
## 13 0.0258 0.0610 0.0973 0.0608
## 25 0.0454 0.1861 0.1687 0.0907
## 37 0.0650 0.4063 0.2901 0.1320
## 50 0.0863 0.6624 0.4429 0.2489
## 
## Description:
##  Wed Aug 28 11:10:49 2019 by user: Vinodh Chennu
plot(Eff.Portfolio.Frontier, c(1,2,3,4))

#Interpretation:
#The frontier curve is the collection of 50 portfolios with different risk to return ratios.
#The portfolios upside of frontier curve are considered as superior and 
#those downside are considered as inferior portfolios.
#The portfolio with min risk to return ratio is the mid point of curve(marked with red), which is the best portfolio.
#Points away from the curve are the individual assets with their corresponding returns and risks.
#Obviously portfolios have the least risk to return ratios when comapred to individual assets.


#Constructing Return-Risk Matrix of the 6 portfolios
ReturnRiskMatrix <- matrix(c(round(Equalweight.Portfolio$er, 4), round(Equalweight.Portfolio$sd, 4), 
                             round(Global.Min.Portfolio$er, 4), round(Global.Min.Portfolio$sd, 4),
                             round(Min.Variance.Portfolio$er, 4), round(Min.Variance.Portfolio$sd, 4),
                             round(Tangency.Portfolio$er, 4), round(Tangency.Portfolio$sd, 4), 
                             round(Efficient.Portfolio$er, 4), round(Efficient.Portfolio$sd, 4),
                             round(Max.Return.Portfolio$er, 4), round(Max.Return.Portfolio$sd, 4)), 
                           nrow = 6, ncol=2, byrow=TRUE)

colnames(ReturnRiskMatrix) <- c("Return", "Risk")
rownames(ReturnRiskMatrix) <- c("Equally weighted Portfolio", "Global Minimum Portfolio", 
                                "Min Variance Portfolio", "Tangency Portfolio", 
                                "Efficient Portfolio", "Max Return Portfolio")
#Convert the matrix into a dataframe
ReturnRiskMatrix <- as.data.frame(ReturnRiskMatrix)
#Calculate Returnto Risk ratio for each portfolio
ReturntoRiskRatio = c(Equalweight.Portfolio$er/Equalweight.Portfolio$sd,
                      Global.Min.Portfolio$er/Global.Min.Portfolio$sd,
                      Min.Variance.Portfolio$er/Min.Variance.Portfolio$sd,
                      Tangency.Portfolio$er/Tangency.Portfolio$sd,
                      Efficient.Portfolio$er/Efficient.Portfolio$sd,
                      Max.Return.Portfolio$er/Max.Return.Portfolio$sd)
ReturntoRiskRatio <-round(ReturntoRiskRatio, 4)
names(ReturntoRiskRatio) <- "Return/Risk Ratio"


#Column bind the risktoreturnratios to the risk-return matrix
ReturnRiskMatrix <- cbind(ReturnRiskMatrix, ReturntoRiskRatio)
ReturnRiskMatrix
##                            Return   Risk ReturntoRiskRatio
## Equally weighted Portfolio 0.0279 0.1072            0.2604
## Global Minimum Portfolio   0.0197 0.0500            0.3945
## Min Variance Portfolio     0.0211 0.0528            0.3994
## Tangency Portfolio         0.0336 0.1362            0.2465
## Efficient Portfolio        0.0150 0.0609            0.2464
## Max Return Portfolio       0.0211 0.0528            0.3994