0% found this document useful (0 votes)
54 views20 pages

Markowitz Model Derivation

The document discusses portfolio optimization and the Markowitz model. It explains how diversification reduces portfolio variance by increasing the number of uncorrelated stocks. It presents the formula for calculating a portfolio's return variance using portfolio weights and the covariance matrix. It then introduces the Markowitz mean-variance optimization problem, which aims to find the minimum-variance portfolio subject to achieving a target expected return. The solution uses Lagrange multipliers to embed the constraints into the objective function.

Uploaded by

vaiperder21
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)
54 views20 pages

Markowitz Model Derivation

The document discusses portfolio optimization and the Markowitz model. It explains how diversification reduces portfolio variance by increasing the number of uncorrelated stocks. It presents the formula for calculating a portfolio's return variance using portfolio weights and the covariance matrix. It then introduces the Markowitz mean-variance optimization problem, which aims to find the minimum-variance portfolio subject to achieving a target expected return. The solution uses Lagrange multipliers to embed the constraints into the objective function.

Uploaded by

vaiperder21
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/ 20

25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics

Chapter 18 Being Mean with Variance:


Markowitz Optimization

18.1 Diversification of a portfolio

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

i=1 i=1 j=1,i≠j

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

The first term is the average variance, denoted σ


¯1
2
divided by n , and the second is the
average covariance, denoted σ¯
ij multiplied by factor (n − 1)/n . As 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.

sd=0.50; cv=0.05; m=100


sd_p = matrix(0,m,1)
for (j in 1:m) {

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

18.2 Markowitz Portfolio Problem

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
− –

Note that we have a 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.

18.3 The Solution by Lagrange Multipliers

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

To minimize this function, we take derivatives with respect to ,


w λ1 , and λ2 , to arrive at the


first order conditions:

∂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

Also premultiply (2) by 1



:

′ ′ −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

18.5 Notes on the solution

Note 1: Since Σ is positive definite, Σ


−1
is also positive definite: B > 0, C > 0 .

− −

Note 2: Given solutions for λ1 , λ2 , we solve for w .



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

w = g + h E(r p ) = [1 − E(r p )] g + E(r p )[ g + h ]



− – −− – – −−

Hence these 2 portfolios g, g + h “generate” the entire frontier.


– – −−

18.6 The Function

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

C = t(wuns) %*% solve(cv) %*% wuns


D = B*C - A^2
lam = (C*Er-A)/D
gam = (B-A*Er)/D
wts = lam[1]*(solve(cv) %*% mu) + gam[1]*(solve(cv) %*% wuns)
g = (B[1]*(solve(cv) %*% wuns) - A[1]*(solve(cv) %*% mu))/D[1]

h = (C[1]*(solve(cv) %*% mu) - A[1]*(solve(cv) %*% wuns))/D[1]


wts = g + h*Er
}

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

## [,1] [,2] [,3]


## [1,] 1e-04 0.00 0.00
## [2,] 0e+00 0.04 0.02
## [3,] 0e+00 0.02 0.16

The output is the vector of optimal portfolio weights.

Er = 0.18

#SOLVE PORTFOLIO PROBLEM


wts = markowitz(mu,cv,Er)
print(wts)

## [,1]
## [1,] -0.3575931

## [2,] 0.8436676
## [3,] 0.5139255

print(sum(wts))

## [1] 1

print(t(wts) %*% mu)

## [,1]
## [1,] 0.18

print(sqrt(t(wts) %*% cv %*% wts))

## [,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

18.8 A different expected return

If we change the expected return to 0.10, then we get a different set of portfolio weights.

Er = 0.10

#SOLVE PORTFOLIO PROBLEM


wts = markowitz(mu,cv,Er)

print(wts)

## [,1]
## [1,] 0.3209169

## [2,] 0.4223496
## [3,] 0.2567335

print(t(wts) %*% mu)

## [,1]

## [1,] 0.1

print(sqrt(t(wts) %*% cv %*% wts))

## [,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

18.9 Numerical Optimization with Constraints

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

This routine implements the dual method of Goldfarb and


Idnani (1982, 1983) for solving quadratic programming
problems of the form min(-d^T b + 1/2 b^T D b) with the
constraints A^T b >= b_0.
(note: b here is the weights vector in our problem)

Usage
solve.QP(Dmat, dvec, Amat, bvec, meq=0, factorized=FALSE)

Arguments

Dmat matrix appearing in the quadratic function to be minimized.


dvec vector appearing in the quadratic function to be minimized.
Amat matrix defining the constraints under which we want
to minimize the quadratic function.
bvec vector holding the values of b_0 (defaults to zero).
meq the first meq constraints are treated as equality

constraints, all further as inequality constraints


(defaults to 0).
factorized logical flag: if TRUE, then we are passing R^(-1)
(where D = R^T R) instead of the matrix D in the
argument Dmat.

\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

constraints. The constraints will be of the form ′


A w ≥ b0 , i.e.,

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.

#SOLVING THE PROBLEM WITH THE "quadprog" PACKAGE


Er = 0.18

library(quadprog)

nss = 0 #Equals 1 if no short sales allowed


Bmat = matrix(0,n,n) #No Short sales matrix
diag(Bmat) = 1
Amat = matrix(c(mu,1,1,1),n,2)
if (nss==1) { Amat = matrix(c(Amat,Bmat),n,2+n) }
dvec = matrix(0,n,1)

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)

## [1] -0.3575931 0.8436676 0.5139255

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

#SOLVING THE PROBLEM WITH THE "quadprog" PACKAGE


Er = 0.18

library(quadprog)
nss = 1 #Equals 1 if no short sales allowed

Bmat = matrix(0,n,n) #No Short sales matrix


diag(Bmat) = 1
Amat = matrix(c(mu,1,1,1),n,2)
if (nss==1) { Amat = matrix(c(Amat,Bmat),n,2+n) }
dvec = matrix(0,n,1)
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)

## [1] 0.0 0.2 0.8

wstar = as.matrix(sol$solution)
print(t(wstar) %*% mu)

## [,1]
## [1,] 0.18

print(sqrt(t(wstar) %*% cv %*% wstar))

## [,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

18.10 The Efficient Frontier

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.

#TRACING OUT THE EFFICIENT FRONTIER


Er_vec = as.matrix(seq(0.01,0.5,0.01))

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

## [5,] 0.056141450 0.05


## [6,] 0.074486206 0.06
## [7,] 0.092919536 0.07
## [8,] 0.111397479 0.08
## [9,] 0.129900998 0.09
## [10,] 0.148420529 0.10

## [11,] 0.166950742 0.11


## [12,] 0.185488436 0.12
## [13,] 0.204031572 0.13
## [14,] 0.222578791 0.14
## [15,] 0.241129149 0.15

## [16,] 0.259681974 0.16


## [17,] 0.278236773 0.17
## [18,] 0.296793176 0.18
## [19,] 0.315350898 0.19
## [20,] 0.333909721 0.20

## [21,] 0.352469471 0.21


## [22,] 0.371030008 0.22
## [23,] 0.389591219 0.23
## [24,] 0.408153014 0.24
## [25,] 0.426715315 0.25
## [26,] 0.445278059 0.26

## [27,] 0.463841194 0.27


## [28,] 0.482404674 0.28
## [29,] 0.500968460 0.29
## [30,] 0.519532521 0.30
## [31,] 0.538096827 0.31

## [32,] 0.556661353 0.32


## [33,] 0.575226080 0.33
## [34,] 0.593790987 0.34
## [35,] 0.612356059 0.35
## [36,] 0.630921280 0.36

https://srdas.github.io/MLBook/PortfolioOptimization.html 13/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics

## [37,] 0.649486639 0.37


## [38,] 0.668052123 0.38
## [39,] 0.686617722 0.39

## [40,] 0.705183428 0.40


## [41,] 0.723749232 0.41
## [42,] 0.742315127 0.42
## [43,] 0.760881106 0.43
## [44,] 0.779447163 0.44

## [45,] 0.798013292 0.45


## [46,] 0.816579490 0.46
## [47,] 0.835145750 0.47
## [48,] 0.853712070 0.48
## [49,] 0.872278445 0.49
## [50,] 0.890844871 0.50

We can also simulate to see how the efficient frontier appears as the outer envelope of
candidate portfolios.

#SIMULATE THE EFFICIENT FRONTIER


n = 10000

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,])

Sd_ret[j] = sqrt(t(wt) %*% cv %*% wt)


}
plot(Sd_ret,Exp_ret,col="red")
lines(Sig_vec,Er_vec,col="blue",lwd=6)

https://srdas.github.io/MLBook/PortfolioOptimization.html 14/20
25/10/2023 22:10 Data Science: Theories, Models, Algorithms, and Analytics

18.11 Covariances of frontier portfolios

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

After much simplification:

′ ′
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

which is a parabola in E(r), σ space.

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

Carry out the following analyses:

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

Suppose it exists, then the solution is:

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

Substitute in for λ1 , λ2 and rearrange to get

E(r q ) = (1 − βqp )E[r Z C (p) ] + βqp E(r p )

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 + ξ

which is nothing but a factor model, i.e. with orthogonal factors.

18.13 Portfolio problem with riskless assets

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 )

−−− −−–

The Lagrangian specification of the problem is as follows.

1
′ ′ ′
min L = w Σ w + λ[E(r p ) − w μ − (1 − w 1 )r f ]
w ,λ −−
2 − −−− −−−− −−–

The first-order conditions for the problem are as follows.

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

− −− –

Take the first equation and proceed as follows:

−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

wts = (solve(cv) %*% x) * (Er-rf)/H[1]


}

We run the code here.

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

## [1] "Wts in stocks"

print(sol)

## [,1]
## [1,] 12.6613704
## [2,] 0.2236842

## [3,] 0.1223932

print("Wts in risk free asset")

## [1] "Wts in risk free asset"

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] "Exp return"

print(rf + t(sol) %*% (mu-rf))

## [,1]

## [1,] 0.18

print("Std Dev of return")

## [1] "Std Dev of return"

print(sqrt(t(sol) %*% cv %*% sol))

## [,1]
## [1,] 0.1467117

https://srdas.github.io/MLBook/PortfolioOptimization.html 20/20

You might also like