0% found this document useful (0 votes)
82 views9 pages

Untitled: Markowitz Efficient Frontier

This document summarizes the Markowitz portfolio optimization model. It introduces the efficient frontier, which provides the set of optimal portfolios that maximize expected return for a given level of risk. It then demonstrates calculating the efficient frontier for a dataset of stock returns, finding the minimum variance portfolio and tangency portfolio through analytical and numerical optimization methods. The analysis is repeated with a short-sale constraint to generate a new efficient frontier.

Uploaded by

akash srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views9 pages

Untitled: Markowitz Efficient Frontier

This document summarizes the Markowitz portfolio optimization model. It introduces the efficient frontier, which provides the set of optimal portfolios that maximize expected return for a given level of risk. It then demonstrates calculating the efficient frontier for a dataset of stock returns, finding the minimum variance portfolio and tangency portfolio through analytical and numerical optimization methods. The analysis is repeated with a short-sale constraint to generate a new efficient frontier.

Uploaded by

akash srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Untitled

Markowitz Efficient Frontier


• Provides a set of portfolio that provides maximum expected return for the given risk
(Measured by standard deviation )

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")

pricedata= read.csv("pricedata.csv", header = TRUE)

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

Creation of the feasible porfilio


• Feasible portfolios are set of portfolios that investor can create by investing in the
available set of stocks
• In the Markowitzian world, investor are only concerned about the expected return and
variance/ standard deviation of the portfolio
• Expected return= ∑𝑖 𝑤𝑖 𝑅𝑖 = w‘R
• Variance of the portfolio= ∑𝑖 𝑤𝑖 𝑤𝑗 𝑐𝑜𝑣(𝑖, 𝑗) = w‘𝛴w
• Where w is the weight vetor
• R is the expected retrn vector
• 𝛴 is the covraiance Matrix

Expected return and covariance


R= colMeans(returndata_sub)
covmat= cov(returndata_sub)
cormat= cor(returndata_sub)
SD= sqrt(diag(covmat))

Stock_summary= data.frame(Name= stocklist, Return=R, SD= SD)

Simulate the feasible set of portfolio


• We will simulate 10000 set of portfolio by randomly generating different weights for
these five stocks
• “runif” command generates random number between predifined set of two numbers
• We store the SD and mean of the portfolio in the dataframe “Portmat”
Nsim=10000
w= rep(NA,Nstock )
portmat= data.frame(SD= rep(NA,Nsim), ER= rep(NA,Nsim))
for( i in 1: Nsim)
{
w[1:(Nstock-1)]= runif(Nstock-1, min=-1, max=1)
w[Nstock]= 1-sum(w[1:(Nstock-1)])

portmat$ER[i]=t(w)%*%R
portmat$SD[i]=sqrt(t(w)%*%covmat%*%w)
}

Ploting the feasible portfolio


• We will plot all the simulated portfolio in the SD and expected return plane
• We will rely on the ggplot command for the plotiing
ggplot(data= portmat, aes(x=SD, y= ER) )+ geom_point(size=0.01)
## Combination of
the portfolio
• Let portfolio X has the weight vector as 𝑤𝑌 = (0.2,0.1,0.3,0.3,0.1)
• Let portfolio Y has the weight vector as 𝑤𝑌 = (0.1,0.2,0.4,0.1,0.2)
• What would be the weight vector of the portfolio created by 40 % of X and 60% of Y
wx= c(0.2,0.1,0.3,0.3,0.1)
wy= c(0.1,0.2,0.4,0.1,0.2)
wp =0.4*wx +0.6*wy
wp

## [1] 0.14 0.16 0.36 0.18 0.16

Calculating the Efficient Portfolio


• Black has demonstrated that two portfolios on the envelop portfolio is enough to
create the whole envelope portfolio
• Given two portfolio X and Y on the the envelop portoflio, all other portfolio on the
envelop can be represented as a combination of X and Y 𝛼𝑋 + (1 − 𝛼)𝑌 $
-If we can find two portfolios on the envelope, we can create every other efficient portfolios
- Minimum varaiance portfolio - Tangency or market portfolio

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

Minimum variance portfolio : Analytical Solution


• Min 𝑤′𝛴𝑤 - Objective function
• Constraints 𝑤′1 = 1
𝛴 −1 1
• 𝑤𝑀𝑉 = 1′𝛴 −1 1
covinv= solve(covmat)
unitvec= rep(1, Nstock)
Nume= unitvec %*%covinv
Denom= (t(unitvec) %*%covinv%*%(unitvec))
x= t(Nume)/as.numeric(Denom)
Stock_summary$MV_analytical=as.numeric(x)

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
}

param= runif(Nstock-1, min=-1, max=1)

optimized_param_MV= optim(param,calculate_port_var )

weights_MV= c(optimized_param_MV$par, 1- sum(optimized_param_MV$par))

R_MV=t(weights_MV)%*%R
SD_MV=sqrt(t(weights_MV)%*%covmat%*%weights_MV)

Stock_summary$MV_Num= weights_MV

Tangency or market portfolio


𝑤′𝑅−𝑅𝑓
• Max - Objective function
𝑤′𝛴𝑤
• Constraints 𝑤′1 = 1

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
}

param= runif(Nstock-1, min=0, max=1)

optimized_param_MP= optim(param,calculate_sharpe_ratio, control=


list(fnscale=-1))

weights_MP= c(optimized_param_MP$par, 1- sum(optimized_param_MP$par))

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

## Name Return SD MV_analytical MV_Num MP_analytical


MP_weight
## TCS TCS 18.851077 493.3143 0.25138432 0.25132668 0.509423854
0.50949692
## HUL HUL 18.165481 417.4409 0.40559894 0.40555873 0.675490185
0.67549838
## ITC ITC 8.646037 447.9501 0.29013175 0.29018094 0.055179247
0.05515067
## TM TM 5.766527 744.5166 0.02407899 0.02412967 0.006222955
0.00624617
## TS TS -1.478595 696.0359 0.02880600 0.02880397 -0.246316241 -
0.24639214

Efficient Frontier with the short sale constraints


• Simulate the feasible set of portfolio
• We will restrict weights to be between 0 and 1
Nsim=10000
w= rep(NA,Nstock )
portmat_SS= data.frame(SD= rep(NA,Nsim), ER= rep(NA,Nsim))
for( i in 1: Nsim)
{
w[1:(Nstock-1)]= runif(Nstock-1, min=0, max=1)
w[Nstock]= 1-sum(w[1:(Nstock-1)])

portmat_SS$ER[i]=t(w)%*%R
portmat_SS$SD[i]=sqrt(t(w)%*%covmat%*%w)
}

Ploting the feasible portfolio


ggplot(data= portmat_SS, aes(x=SD, y= ER) )+ geom_point(size=0.01)

Minimum variance portfolio


calculate_port_var= function(x)
{
weights= c(x, 1- sum(x))
variance_p = t(weights)%*%covmat%*%weights
}
param= runif(Nstock-1, min=-100, max=100)

optimized_param_MV= optim(param,calculate_port_var, lower=rep(0,Nstock-1),


upper=rep(1,Nstock-1) )

weights_MV= c(optimized_param_MV$par, 1- sum(optimized_param_MV$par))

R_MV=t(weights_MV)%*%R
SD_MV=sqrt(t(weights_MV)%*%covmat%*%weights_MV)
Stock_summary$MV_weight_SS= weights_MV

Tangency or market portfolio : Numerical Method


Rf= 0

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)

optimized_param_MP= optim(param,calculate_sharpe_ratio, control=


list(fnscale=-1),lower=rep(0,Nstock-1), upper=rep(1,Nstock-1))

weights_MP= c(optimized_param_MP$par, 1- sum(optimized_param_MP$par))

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

## Name Return SD MV_analytical MV_Num MP_analytical


MP_weight
## TCS TCS 18.851077 493.3143 0.25138432 0.25132668 0.509423854
0.50949692
## HUL HUL 18.165481 417.4409 0.40559894 0.40555873 0.675490185
0.67549838
## ITC ITC 8.646037 447.9501 0.29013175 0.29018094 0.055179247
0.05515067
## TM TM 5.766527 744.5166 0.02407899 0.02412967 0.006222955
0.00624617
## TS TS -1.478595 696.0359 0.02880600 0.02880397 -0.246316241 -
0.24639214
## MV_weight_SS MP_weight_SS
## TCS 0.25138438 0.509423879
## HUL 0.40559872 0.675491267
## ITC 0.29013190 0.055176573
## TM 0.02407906 0.006224312
## TS 0.02880594 -0.246316030

You might also like