Markowitz Model Derivation
Markowitz Model Derivation
It is useful to examine the power of using vector algebra with an application. Here we use
vector and summation math to understand how diversification in stock portfolios works.
Diversification occurs when we increase the number of non-perfectly correlated stocks in a
portfolio, thereby reducing portfolio variance. In order to compute the variance of the portfolio
we need to use the portfolio weights w and the covariance matrix of stock returns R , denoted
Σ . We first write down the formula for a portfolio’s return variance:
n n n
′ ′ 2 2
V ar(w R) = w Σw = ∑ w σ + ∑ ∑ wi wj σij
i i
Readers are strongly encouraged to implement this by hand for n = 2 to convince themselves
that the vector form of the expression for variance w Σw
′
is the same thing as the long form
on the right-hand side of the equation above. If returns are independent, then the formula
collapses to:
′ ′ 2 2
V ar(w R) = w Σw = ∑ w σ
i i
i=1
If returns are dependent, and equal amounts are invested in each asset (wi = 1/n, ∀i ):
n 2 n n
1 σ n − 1 σij
′ i
V ar(w R) = ∑ + ∑ ∑
n n n n(n − 1)
i=1 i=1 j=1,i≠j
1 n − 1
2
= ¯i
σ + σ¯
ij
n n
1 1
2
= ¯i
σ + (1 − ) σ¯
ij
n n
https://srdas.github.io/MLBook/PortfolioOptimization.html 1/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
′
V ar(w R) = σ¯
ij
This produces the remarkable result that in a well diversified portfolio, the variances of each
stock’s return does not matter at all for portfolio risk! Further the risk of the portfolio, i.e., its
variance, is nothing but the average of off-diagonal terms in the covariance matrix.
cv_mat = matrix(1,j,j)*cv
diag(cv_mat) = sd^2
w = matrix(1/j,j,1)
sd_p[j] = sqrt(t(w) %*% cv_mat %*% w)
}
plot(sd_p,type="l",col="blue")
https://srdas.github.io/MLBook/PortfolioOptimization.html 2/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
We now explore the mathematics of a famous portfolio optimization result, known as the
Markowitz mean-variance problem. The solution to this problem is still being used widely in
practice. We are interested in portfolios of n assets, which have a mean return which we
denote as E(r p ) , and a variance, denoted V ar(r p ) .
Let w ∈ R
n
be the portfolio weights. What this means is that we allocate each $1 into various
−
−
assets, such that the total of the weights sums up to 1. Note that we do not preclude short-
selling, so that it is possible for weights to be negative as well.
The optimization problem is defined as follows. We wish to find the portfolio that delivers the
minimum variance (risk) while achieving a pre-specified level of expected (mean) return.
1
′
min w Σ w
w
−
−
2 −
−−−−−
subject to
′
w μ = E(r p )
−
− −
−
′
w 1 = 1
− –
−
2
in front of the variance term above, which is for mathematical neatness
as will become clear shortly. The minimized solution is not affected by scaling the objective
function by a constant.
The first constraint forces the expected return of the portfolio to a specified mean return,
denoted E(r p ) , and the second constraint requires that the portfolio weights add up to 1, also
known as the “fully invested” constraint. It is convenient that the constraints are equality
constraints.
This is a Lagrangian problem, and requires that we embed the constraints into the objective
function using Lagragian multipliers {λ1 , λ2 } . This results in the following minimization
problem:
1
′ ′ ′
min L = w Σ w + λ1 [E(r p ) − w μ ] + λ2 [1 − w 1 ]
w ,λ 1 ,λ 2 −−
2 − −−− −−−− −−–
−
−
https://srdas.github.io/MLBook/PortfolioOptimization.html 3/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
18.4 Optimization
∂L
= Σ w − λ1 μ − λ2 1 = 0 (1)
∂w −
−−− −− – –
−
−
∂L
′
= E(r p ) − w μ = 0
∂ λ1 −−−−
∂L
′
= 1 − w 1 = 0
∂ λ2 −−–
The first equation above, is a system of n equations, because the derivative is taken with
respect to every element of the vector w . Hence, we have a total of (n + 2) first-order
−
−
conditions. From (1)
−1
w = Σ (λ1 μ + λ2 1 )
−
− −− –
−1 −1
= λ1 Σ μ + λ2 Σ 1 (2)
−
− –
Premultiply (2) by μ
′
:
−
−
′ ′ −1 ′ −1
μ w = λ1 μ Σ μ + λ2 μ Σ 1 = E(r p )
−−
− − −−− − −
− −−− − –
B A
′ ′ −1 ′ −1
1 w = λ1 1 Σ μ + λ2 1 Σ 1 = 1
–−− –−− −
− –−− –
C
A
Solve for λ1 , λ2
C E(r p ) − A
λ1 =
D
B − AE(r p )
λ2 =
D
2
where D = BC − A
https://srdas.github.io/MLBook/PortfolioOptimization.html 4/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
1 1
−1 −1 −1 −1
w = [B Σ 1 − AΣ μ] + [C Σ μ − AΣ 1 ] ⋅ E(r p )
−
− D −
− – −− −− D −
− −
− −− –
g h
– −
−
This is the expression for the optimal portfolio weights that minimize the variance for given
expected return E(r p ) . We see that the vectors g, h are fixed once we are given the inputs to
– −
−
the problem, i.e., μ and Σ .
−
− −
−
Note 3: We can vary E(r p ) to get a set of frontier (efficient or optimal) portfolios w .
−
−
w = g + h E(r p )
−
− – −−
if E(r p ) = 0, w = g
−
− –
if E(r p ) = 1, w = g + h
−
− – −−
Note that
We create a function to return the optimal portfolio weights. Here is the code for the function to
do portfolio optimization:
https://srdas.github.io/MLBook/PortfolioOptimization.html 5/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
markowitz = function(mu,cv,Er) {
n = length(mu)
wuns = matrix(1,n,1)
A = t(wuns) %*% solve(cv) %*% mu
B = t(mu) %*% solve(cv) %*% mu
18.7 Example
We can enter an example of a mean return vector and the covariance matrix of returns, and
then call the function for a given expected return.
#PARAMETERS
mu = matrix(c(0.02,0.10,0.20),3,1)
n = length(mu)
cv = matrix(c(0.0001,0,0,0,0.04,0.02,0,0.02,0.16),n,n)
print(mu)
## [,1]
## [1,] 0.02
## [2,] 0.10
## [3,] 0.20
print(round(cv,4))
https://srdas.github.io/MLBook/PortfolioOptimization.html 6/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
Er = 0.18
## [,1]
## [1,] -0.3575931
## [2,] 0.8436676
## [3,] 0.5139255
print(sum(wts))
## [1] 1
## [,1]
## [1,] 0.18
## [,1]
## [1,] 0.2967932
https://srdas.github.io/MLBook/PortfolioOptimization.html 7/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
If we change the expected return to 0.10, then we get a different set of portfolio weights.
Er = 0.10
print(wts)
## [,1]
## [1,] 0.3209169
## [2,] 0.4223496
## [3,] 0.2567335
## [,1]
## [1,] 0.1
## [,1]
## [1,] 0.1484205
Note that in the first example, to get a high expected return of 0.18, we needed to take some
leverage, by shorting the low risk asset and going long the medium and high risk assets.
When we dropped the expected return to 0.10, all weights are positive, i.e., we have a long-
only portfolio.
https://srdas.github.io/MLBook/PortfolioOptimization.html 8/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
The quadprog package is an optimizer that takes a quadratic objective function with linear
constraints. Hence, it is exactly what is needed for the mean-variance portfolio problem we
just considered. The advantage of this package is that we can also apply additional inequality
constraints. For example, we may not wish to permit short-sales of any asset, and thereby we
might bound all the weights to lie between zero and one.
The specification in the quadprog package of the problem set up is shown in the manual:
Description
Usage
solve.QP(Dmat, dvec, Amat, bvec, meq=0, factorized=FALSE)
Arguments
\end{lstlisting}
In our problem set up, with three securities, and no short sales, we will have the following
Amat and bvec. The constraints will be modulated by {meq = 2}, which states that the first two
constraints will be equality constraints, and the last three will be greater than equal to
https://srdas.github.io/MLBook/PortfolioOptimization.html 9/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
w 1 μ 1 + w 2 μ 2 + w 3 μ 3 = E(r p )
w1 1 + w2 1 + w3 1 = 1
w1 ≥ 0
w2 ≥ 0
w3 ≥ 0
The code for using the package is as follows. If we run this code we get the following result for
expected return = 0.18, with short-selling allowed.
library(quadprog)
bvec = matrix(c(Er,1),2,1)
if (nss==1) { bvec = t(c(bvec,matrix(0,3,1))) }
sol = solve.QP(cv,dvec,Amat,bvec,meq=2)
print(sol$solution)
This is exactly what is obtained from the Markowitz solution. Hence, the model checks out.
What if we restricted short-selling? Then we would get the following solution.
https://srdas.github.io/MLBook/PortfolioOptimization.html 10/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
library(quadprog)
nss = 1 #Equals 1 if no short sales allowed
wstar = as.matrix(sol$solution)
print(t(wstar) %*% mu)
## [,1]
## [1,] 0.18
## [,1]
## [1,] 0.332265
https://srdas.github.io/MLBook/PortfolioOptimization.html 11/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
Since we can use the Markowitz model to solve for the optimal portfolio weights when the
expected return is fixed, we can keep solving for different values of E(r p ) . This will trace out
the efficient frontier. The program to do this and plot the frontier is as follows.
Sig_vec = matrix(0,50,1)
j = 0
for (Er in Er_vec) {
j = j+1
wts = markowitz(mu,cv,Er)
Sig_vec[j] = sqrt(t(wts) %*% cv %*% wts)
}
plot(Sig_vec,Er_vec,type='l')
print(cbind(Sig_vec,Er_vec))
https://srdas.github.io/MLBook/PortfolioOptimization.html 12/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
## [,1] [,2]
## [1,] 0.021486319 0.01
## [2,] 0.009997134 0.02
## [3,] 0.020681789 0.03
## [4,] 0.038013721 0.04
https://srdas.github.io/MLBook/PortfolioOptimization.html 13/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
We can also simulate to see how the efficient frontier appears as the outer envelope of
candidate portfolios.
w = matrix(rnorm(2*n),n,2)
w = cbind(w,1-rowSums(w))
Exp_ret = w %*% mu
Sd_ret = matrix(0,n,1)
for (j in 1:n) {
wt = as.matrix(w[j,])
https://srdas.github.io/MLBook/PortfolioOptimization.html 14/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
Suppose we have two portfolios on the efficient frontier with weight vectors wp and wq . The
−
− −
−
covariance between these two portfolios is:
′ ′
C ov(r p , r q ) = w p Σ w = [g + h E(r p )] Σ [g + h E(r q )]
−
− − −−−q – −− −
− – −−
Now,
1 1
−1 −1 −1 −1
g + h E(r p ) = [B Σ 1 − AΣ μ] + [C Σ μ − AΣ 1 ] [λ1 B + λ2 A]
– −− D −− – −
− −− D −− −− −
− –
CE(rp )−A B−AE(rp )
+
D/B D/B
https://srdas.github.io/MLBook/PortfolioOptimization.html 15/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
′ ′
C ov(r p , r q ) = w p Σ w q
−
− − −−−
C 1
= [E(r p ) − A/C ][E(r q ) − A/C ] +
D C
C 1
2 2
σp = C ov(r p , r p ) = [E(r p ) − A/C ] +
D C
Therefore,
2 2
σp [E(r p ) − A/C ]
− = 1
2
1/C D/C
which is the equation of a hyperbola in σ, E(r) space with center (0, A/C ) , or
1
2 2
σp = [C E(r p ) − 2AE(r p ) + B],
D
18.12 Combinations
It is easy to see that linear combinations of portfolios on the frontier will also lie on the frontier.
m m
∑ αi w = ∑ αi [ g + h E(r i )]
−−i – −−
i=1 i=1
= g + h ∑ αi E(r i )
– −−
i=1
∑ αi = 1
i=1
18.12.1 Exercise
1. Use your R program to do the following. Set E(r p ) = 0.10 (i.e. return of 10%), and solve
for the optimal portfolio weights for your 3 securities. Call this vector of weights w1 . Next,
set E(r p ) = 0.20 and again solve for the portfolios weights w2 .
2. Take a 50/50 combination of these two portfolios. What are the weights? What is the
expected return?
https://srdas.github.io/MLBook/PortfolioOptimization.html 16/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
3. For the expected return in the previous part, resolve the mean-variance problem to get
the new weights?
4. Compare these weights in part 3 to the ones in part 2 above. Explain your result.
This is a special portfolio of interest, and we will soon see why. Find
E(r q ), s. t. C ov(r p , r q ) = 0
2
A D/C
E(r q ) = − ≡ E(r Z C (p) )
C E(r p ) − A/C
Since Z C (p) exists for all p, all frontier portfolios can be formed from p and Z C (p) .
′
C ov(r p , r q ) = w p Σ w
q
−
− − −−−
′ −1 ′ −1
= λ1 μ Σ Σ w + λ2 1 Σ Σ w
q q
−−−− −
−−− –−− −
−−−
′ ′
= λ1 μ w + λ2 1 w
q q
−−−− –−−
= λ1 E(r q ) + λ2
C ov(r q , r p )
βqp =
2
σp
Therefore, the return on a portfolio can be written in terms of a basic portfolio p and its zero
covariance portfolio Z C (p) . This suggests a regression relationship, i.e.
r q = β0 + β1 r Z C (p) + β2 r p + ξ
We now enhance the portfolio problem to deal with risk less assets. The difference is that the
fully-invested constraint is expanded to include the risk free asset. We require just a single
equality constraint. The problem may be specified as follows.
1
′
min w Σ w
w
−
−
2 −
−−−−−
https://srdas.github.io/MLBook/PortfolioOptimization.html 17/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
′ ′
s. t. w μ + (1 − w 1 ) r f = E(r p )
−
−−− −−–
1
′ ′ ′
min L = w Σ w + λ[E(r p ) − w μ − (1 − w 1 )r f ]
w ,λ −−
2 − −−− −−−− −−–
−
−
∂L
= Σ w − λ μ + λ 1 rf = 0
∂w −
−−− −
− – –
−
−
∂L
′ ′
= E(r p ) − w μ − (1 − w 1 ) r f = 0
∂λ −−−− −−–
Re-aranging, and solving for w and λ , we get the following manipulations, eventually leading
−
−
to the desired solution.
Σ w = λ( μ − 1 r f )
−
−−− −
− –
′
E(r p ) − r f = w ( μ − 1 r f )
−
− −− –
−1
w = λΣ ( μ − 1 rf )
−
− −
− −
− –
′ ′ −1
E(r p ) − r f ≡ ( μ − 1 r f ) w = λ( μ − 1 r f ) Σ ( μ − 1 rf )
−
− – −
− −
− – −
− −
− –
The first and third terms in the equation above then give that
E(r p ) − r f
λ =
′ −1
( μ − 1rf ) Σ ( μ − 1 rf )
−
− – −
− −
− –
Substituting this back into the first foc results in the final solution.
E(r p ) − r f
−1
w = Σ ( μ − 1 rf )
−
− −
− −
− – H
′ −1
where H = ( μ − rf 1 ) Σ ( μ − rf 1 )
−
− – −
− −
− –
### Example
We create a function for the solution to this problem, and then run the model.
https://srdas.github.io/MLBook/PortfolioOptimization.html 18/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
markowitz2 = function(mu,cv,Er,rf) {
n = length(mu)
wuns = matrix(1,n,1)
x = as.matrix(mu - rf*wuns)
H = t(x) %*% solve(cv) %*% x
#PARAMETERS
mu = matrix(c(0.02,0.10,0.20),3,1)
n = length(mu)
cv = matrix(c(0.0001,0,0,0,0.04,0.02,0,0.02,0.16),n,n)
Er = 0.18
rf = 0.01
sol = markowitz2(mu,cv,Er,rf)
print("Wts in stocks")
print(sol)
## [,1]
## [1,] 12.6613704
## [2,] 0.2236842
## [3,] 0.1223932
https://srdas.github.io/MLBook/PortfolioOptimization.html 19/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics
print(1-sum(sol))
## [1] -12.00745
print("Exp return")
## [,1]
## [1,] 0.18
## [,1]
## [1,] 0.1467117
https://srdas.github.io/MLBook/PortfolioOptimization.html 20/20