Portfolio Optimization
Portfolio Optimization
Motivation
What is a Portfolio and why is it useful?
• A portfolio is a specific combination of securities, usually defined by portfolio weights
that sum to 1.
• 𝜔 = 𝜔1 , 𝜔2 , 𝜔3 … … 𝜔𝑛
• If 𝑁𝑖 is the number of shares of an asset and 𝑃𝑖 is the price of the share of the same
asset
𝑁 𝑖 𝑃𝑖
• 𝜔𝑖 = , where 𝑁𝑖 is the real number which can be negative
𝑁1 𝑃1 +⋯………+𝑁𝑛 𝑃𝑛
• 1 = 𝜔1 + 𝜔2 + 𝜔3 … … + 𝜔𝑛
• Portfolio weights can sum to 0 (dollar-neutral portfolios) and weights can be positive
or negative.
• Portfolio weights summarize all relevant information
Positions
• 𝑁𝑖 is the real number which can be negative
• This implies that some weights are negative and others are more than
1
• What do you mean by weights more than 1.
• You are buying more than what you can own? Someone is loaning you
a stock.
• Weights can sum to zero is more complex.
Motivation
• Example:
• Your investment account of 1,00,000 consists of three stocks: 200
shares of stock A, 1000 shares of stock B, 750 shares of stock C. Your
portfolio is summarized by the following weights:
• 𝐸 𝑟𝑝 = σ𝑁
𝑖=1 𝜔𝑖 𝐸(𝑟𝑖 )
𝐸 𝑟1
𝐸 𝑟2
• 𝐸 𝑟 = 𝐸 𝑟
.3 ➔𝐸 𝑟 𝑇
= [𝐸 𝑟1 , 𝐸 𝑟2 , 𝐸 𝑟3 … . . 𝐸 𝑟𝑁 ]
.
𝐸 𝑟𝑁
• 𝐸 𝑟𝑝 = σ𝑁 T 𝑇
𝑖=1 𝜔𝑖 𝐸 𝑟𝑖 = Ω 𝐸 𝑟 = 𝐸 𝑟 Ω
• 𝑉𝑎𝑟 𝑟𝑝 = σ𝑁
𝑖=1(𝜔𝑖 )
Three Universes
Three universes or paradigms for portfolio analysis with
R. xts
1. The tidyverse is a collection of R packages for doing
data science in a certain way. The package is called • EXTENSIBLE
so because TIME SERIES
(1) each variable has its own column (2) each
observation is a row (3) each value is a cell
2. An xts object is a matrix, that also, always, has a time
index for the order of the data. It holds a time series,
meaning it holds the observations and the times at
which they occurred.
3. tidyquant: This universe takes a lot of the best
features of xts, PerformanceAnalytics and the tidyquant tidyverse
tidyverse and lets them play well together.
PerformanceAnalytics
• In the xts world, there are two crucial packages that we will use:
quantmod and PerformanceAnalytics. quantmod is how we will
access the internet and pull in pricing data. That data will arrive to us
formatted as an xts object.
• It has several useful functions for analyzing portfolio performance in
an xts object, such as StdDev(), SharpeRatio(), SortinoRatio(),
CAPM.Beta().
#install.packages("gapminder")
############TO SHOW THE PIPING############
Pipe function library(gapminder)
head(gapminder)
p1<-PGMV(log_ret_xts,
1. Global Minimum Variance Portfolio percentage=FALSE,optctrl=ctrl())
p1
(PGMV from FRAPO) a<-Weights(p1)%*% cov(log_ret_xts)
2. Most Diversified Portfolio (PMD from a
t(Weights(p1))
FRAPO) varPGMV<-sum(Weights(p1)*a)
• Instead of using the variance-covariance
stdevPGMV<-sqrt(varPGMV)
matrix of the asset returns, the correlation stdevPGMV
matrix is utilized as dispersion measure #######Most diversified portfolio
• Weights are then recovered by rescaling
the optimal solution with the assets’ p2<-PMD(log_ret_xts,percentage=FALSE,optctrl=ctrl())
p2
standard deviations and normalizing such a<-Weights(p2)%*% cov(log_ret_xts)
that the weights sum to one. a
varPMD<-sum(Weights(p1)*a)
stdevPMD<-sqrt(varPMD)
stdevPMD
Maximize Diversification Ratio
> stdevPMD
[1] 0.01169978
> stdevPGMV
[1] 0.01167769
Lower Variance
σ𝑁
𝑖>𝑗 𝜔𝑖 𝜎𝑖
𝐷𝑅 𝑤 =
𝜔 ′ Σ𝜔
Minimum tail dependence portfolio
• Correlations are calculated in the tails only. Only in
the extreme cases or tails the correlations are
calculated
• Var-Covar matrix is not considered
EmpTC
• Pairwise tail dependent coefficients are considered Methods
• What is the tail? How many values of the tail are
taken?
EVT
• Tail = 𝑁
• Correlation between the worst 11 is considered. tdc(log_ret_xts, method="EmpTC",lower=TRUE)
• Estimated non-parametrically by either the cor(log_ret_xts)
empirical tail copula (method= EmpTC) or based
on the stable tail-dependence
function(method=“EVT”)
Most common portfolios
• Minimum tail dependent portfolio
(PMTD from FRAPO)
• Variance-Covariance matrix is replaced
with the matrix of the lower tail p3<-PMTD(log_ret_xts,method="EmpTC",
dependence coefficients percentage=FALSE,optctrl=ctrl())
• First step: Derive optimal solution if TDC- p3
matrix is used with main-diagonal
elements are set to one.
• Second step: Re-scale optimal weight
vectors by assets volatility (riskiness).
• Tail Dependent Coefficient (tdc)->
pairwise tail dependence coefficients
between N series
PMTD
• For a long-only investor, the minimum tail-dependent portfolio
solution is returned by calling PMTD(). The function body is akin to
that of the function PGMV() by which the solution of a global
minimum-variance portfolio is returned. But instead of employing the
variance-covariance matrix as a measure of dispersion, the TDC matrix
returned from the call to tdc() is utilized.
Diversification measures
Equal risk contributed portfolios (PERC)
• Equal risk contributed portfolio weights p5<-PERC(cov(log_ret_xts),percentage=TRUE,
• Objective function is the standard deviation of optctrl = ctrl())
the marginal risk contributions, which is a5<-Weights(p5)%*% cov(log_ret_xts)
minimal if all the contributions are equal. a5
• The solution of an equal risk contribution varPERC<-sum(Weights(p5)*a5)
portfolio can be determined with the function stdevPERC<-sqrt(varPERC)
PERC(). The function’s closure takes four stdevPERC
arguments: Sigma for the variance-covariance
matrix, par for the initial weight vector to be
used, percentage as a logical switch governing
whether the weights should be returned as
percentages (the default) or decimal figures,
and the argument optctrl which is passed
down to the rp() function of the package cccp
(see Pfaff 2015) in order to carry out the
optimization.
Comparing the GMVw <- Weights(PGMV(log_ret_xts))
MDPw <- Weights(PMD(log_ret_xts))
allocations MTDw <- Weights(PMTD(log_ret_xts))
ERCw<-Weights(p5)
• The allocations according to the
four portfolio optimizations are ## Graphical displays of allocations
then extracted by means of the oldpar <- par(no.readonly = TRUE)
Weights() method and assigned par(mfrow = c(2, 2))
to the objects GMVw, MDPw, dotchart(GMVw, xlim = c(0, 40), main = "GMV Allocation", pch
MTDw, and ERCw for the global- = 19)
minimum variance, most- dotchart(MDPw - GMVw, xlim = c(-20, 20), main = "MDP vs.
diversified, minimum tail-
dependent, and equal-risk GMV", pch = 19)
contributed solutions, abline(v = 0, col = "gray")
respectively. The weights are dotchart(MTDw - GMVw, xlim = c(-20, 20), main = "MTD vs.
then collected in the matrix GMV", pch = 19)
object W. This object is then abline(v = 0, col = "gray")
used in the call to the apply() dotchart(ERCw - GMVw, xlim = c(-20, 20), main = "ERC vs.
function to determine the
marginal risk contributions for GMV", pch = 19)
each of the four optimal weight abline(v = 0, col = "gray")
vectors. par(oldpar)
GMV allocation
• The GMV solution is characterized by a dominant allocation in Infy
followed by ITC, Reliance, ICICI and SBI
This portfolio concentration is quite typical for GMV
approaches and governed by the low volatility of the
dominating firms.
Most Diversified Portfolio vs GMV