Untitled: Markowitz Efficient Frontier
Untitled: Markowitz Efficient Frontier
Setting up
library(dplyr) # library to manipulate data
library(reshape2) # library to melt and cast
library(ggplot2) # Library used to plot the datasets
The dataset
• We will use the NYSE stocks return data set
• For the demonstration purpose,first we will use only selected 5 stocks
• “read.csv” would allow us to read the daily price data for the stocks
setwd("C:/Users/USER/Desktop/test_r")
Return Calculation
• Simple return =
𝑃𝑡+1
−1
𝑃𝑡
• Log return =
𝑃𝑡+1
𝑙𝑛( )
𝑃𝑡
• for all practical purposes, both are fine
The R code
returndata= log(pricedata[2:nrow(pricedata),2:ncol(pricedata)])-
log(pricedata[1:(nrow(pricedata)-1),2:ncol(pricedata)])
returndata= returndata*25000
stocklist= c("TCS","HUL", "ITC","TM","TS")
returndata_sub= returndata[,stocklist]
Nstock=5
portmat$ER[i]=t(w)%*%R
portmat$SD[i]=sqrt(t(w)%*%covmat%*%w)
}
Matrix notation
• Expected return of the portfolio = w’R
• Variance of the portfolio = 𝑤′𝛴𝑤
What is optimization
• Finding a set of paramaeter that maximizes the objective function
• Constraints: restriction on the parameter space
• Analytical solution
• Numerical solution
Numerical Optimization
• Create an objective function to be minimized
• Use optim function to find optimized set of parameters values
calculate_port_var= function(x)
{
weights= c(x, 1- sum(x))
variance_p = t(weights)%*%covmat%*%weights
}
optimized_param_MV= optim(param,calculate_port_var )
R_MV=t(weights_MV)%*%R
SD_MV=sqrt(t(weights_MV)%*%covmat%*%weights_MV)
Stock_summary$MV_Num= weights_MV
Analytical solution
• Calculate 𝑧 = 𝛴 −1 ((𝑅 − 𝑅𝑓 ))
𝑧
• 𝑊𝑀𝑃 = 𝑠𝑢𝑚(𝑧)
Rf= 0
covinv= solve(covmat)
z= covinv%*%(R-Rf)
x= as.numeric(z)/sum(z)
Stock_summary$MP_analytical= x
Numerical solution
calculate_sharpe_ratio= function(x)
{
weights= c(x, 1- sum(x))
R_p = t(weights)%*%R
SD_p = sqrt(t(weights)%*%covmat%*%weights)
SR= (R_p-Rf)/ SD_p
}
R_MP=t(weights_MP)%*%R
SD_MP=sqrt(t(weights_MP)%*%covmat%*%weights_MP)
Stock_summary$MP_weight= weights_MP
Efficient portfolio
• Efficient portfolio would be the combination of the Tangency and Minimum Variance
portfolio
alpha= seq(from=-1,to= 1, by= 0.01)
Efficient_port= data.frame(alpha= alpha, SD= NA, ER=NA)
for(j in 1:length(alpha))
{
a= alpha[j]
wp =a*weights_MV +(1-a)*weights_MP
Efficient_port$ER[j] = t(wp)%*%R
Efficient_port$SD[j] = sqrt(t(wp)%*%covmat%*%wp)
The plot
ggplot(data= portmat, aes(x=SD, y= ER) )+ geom_point(size=0.001)+
geom_point(data= Efficient_port, aes(x=SD, y= ER), colour = 'red' )
short sale constraints
• In the earlier example, we assumed investor can short sale the stocks
Stock_summary
portmat_SS$ER[i]=t(w)%*%R
portmat_SS$SD[i]=sqrt(t(w)%*%covmat%*%w)
}
R_MV=t(weights_MV)%*%R
SD_MV=sqrt(t(weights_MV)%*%covmat%*%weights_MV)
Stock_summary$MV_weight_SS= weights_MV
calculate_sharpe_ratio= function(x)
{
weights= c(x, 1- sum(x))
R_p = t(weights)%*%R
SD_p = sqrt(t(weights)%*%covmat%*%weights)
SR= (R_p-Rf)/ SD_p
}
Optimization
param= runif(Nstock-1, min=-10, max=10)
R_MP=t(weights_MP)%*%R
SD_MP=sqrt(t(weights_MP)%*%covmat%*%weights_MP)
Stock_summary$MP_weight_SS= weights_MP
Efficient portfolio
• Efficient portfolio would be the combination of the Tangency and Minimum Variance
portfolio
alpha= seq(from=-5,to= 5, by= 0.01)
Efficient_port_SS= data.frame(alpha= alpha, SD= NA, ER=NA)
for(j in 1:length(alpha))
{
a= alpha[j]
wp =a*weights_MV +(1-a)*weights_MP
Efficient_port_SS$ER[j] = t(wp)%*%R
Efficient_port_SS$SD[j] = sqrt(t(wp)%*%covmat%*%wp)
Efficient portfolio
ggplot(data= portmat_SS, aes(x=SD, y= ER) )+ geom_point(size=0.0001)+
geom_point(data= Efficient_port_SS, aes(x=SD, y= ER), colour = 'red'
,size=0.001)
Summary
Stock_summary