Skip to content

Commit 5d71eb4

Browse files
authored
Merge pull request #360 from PaulSoderlind/master
small update of example
2 parents b527714 + 64fb070 commit 5d71eb4

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

docs/examples_literate/portfolio_optimization/portfolio_optimization2.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# # Portfolio Optimization - Markowitz Efficient Frontier
1+
# # Portfolio Optimization - Markowitz Efficient Frontier
22
#
3-
# In this problem, we will find the unconstrained portfolio allocation where we introduce the weighting parameter $\lambda \;(0 \leq \lambda \leq$ 1) and minimize $\lambda * \text{risk} - (1-\lambda)* \text{expected return}$. By varying the values of $\lambda$, we trace out the efficient frontier.
3+
# In this problem, we will find the unconstrained portfolio allocation where we introduce the weighting parameter $\lambda \;(0 \leq \lambda \leq$ 1) and minimize $\lambda * \text{risk} - (1-\lambda)* \text{expected return}$. By varying the values of $\lambda$, we trace out the efficient frontier.
44
#
55
# Suppose that we know the mean returns $\mu \in \mathbf{R}^n$ of each asset and the covariance $\Sigma \in \mathbf{R}^{n \times n}$ between the assets. Our objective is to find a portfolio allocation that minimizes the *risk* (which we measure as the variance $w^T \Sigma w$) and maximizes the *expected return* ($w^T \mu$) of the portfolio of the simulataneously. We require $w \in \mathbf{R}^n$ and $\sum_i w_i = 1$.
66
#
@@ -23,7 +23,7 @@ using Convex, SCS #We are using SCS solver. Install using Pkg.add("SCS")
2323
34 64 4;
2424
58 4 100]/100^2
2525

26-
n = length(μ) #number of assets
26+
n = length(μ) #number of assets
2727

2828
# If you want to try the optimization with more assets, uncomment and run the next cell. It creates a vector or average returns and a variance-covariance matrix that have scales similar to the numbers above.
2929

@@ -47,13 +47,13 @@ w = Variable(n)
4747
ret = dot(w,μ)
4848
risk = quadform(w,Σ)
4949

50-
MeanVarA = zeros(N,2)
50+
MeanVarA = zeros(N,2)
5151
for i = 1:N
5252
λ = λ_vals[i]
5353
p = minimize( λ*risk - (1-λ)*ret,
54-
sum(w) == 1 )
54+
sum(w) == 1 )
5555
solve!(p, SCS.Optimizer(verbose = false))
56-
MeanVarA[i,:]= [evaluate(ret),evaluate(risk)[1]] #risk is a 1x1 matrix
56+
MeanVarA[i,:]= [evaluate(ret),evaluate(risk)]
5757
end
5858

5959
# Now we solve with the bounds $0\le w_i \le 1$
@@ -69,7 +69,7 @@ for i = 1:N
6969
w_lower <= w, #w[i] is bounded
7070
w <= w_upper )
7171
solve!(p, SCS.Optimizer(verbose = false))
72-
MeanVarB[i,:]= [evaluate(ret),evaluate(risk)[1]]
72+
MeanVarB[i,:]= [evaluate(ret),evaluate(risk)]
7373
end
7474

7575
#-
@@ -82,7 +82,7 @@ plot( sqrt.([MeanVarA[:,2] MeanVarB[:,2]]),
8282
title = "Markowitz Efficient Frontier",
8383
xlabel = "Standard deviation",
8484
ylabel = "Expected return",
85-
label = ["no bounds on w","with 0<w<1",])
85+
label = ["no bounds on w" "with 0<w<1"])
8686
scatter!(sqrt.(diag(Σ)),μ,color=:red,label = "assets")
8787

8888
# We now instead impose a restriction on $\sum_i |w_i| - 1$, allowing for varying degrees of "leverage".
@@ -96,7 +96,7 @@ for i = 1:N
9696
sum(w) == 1,
9797
(norm(w, 1)-1) <= Lmax)
9898
solve!(p, SCS.Optimizer(verbose = false))
99-
MeanVarC[i,:]= [evaluate(ret),evaluate(risk)[1]]
99+
MeanVarC[i,:]= [evaluate(ret),evaluate(risk)]
100100
end
101101

102102
#-
@@ -108,7 +108,7 @@ plot( sqrt.([MeanVarA[:,2] MeanVarB[:,2] MeanVarC[:,2]]),
108108
title = "Markowitz Efficient Frontier",
109109
xlabel = "Standard deviation",
110110
ylabel = "Expected return",
111-
label = ["no bounds on w","with 0<w<1","restriction on sum(|w|)"])
111+
label = ["no bounds on w" "with 0<w<1" "restriction on sum(|w|)"])
112112
scatter!(sqrt.(diag(Σ)),μ,color=:red,label = "assets")
113113

114114
#-

0 commit comments

Comments
 (0)