-
Notifications
You must be signed in to change notification settings - Fork 119
/
portfolio_optimization.jl
68 lines (52 loc) · 2.19 KB
/
portfolio_optimization.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# # Portfolio Optimization
#
# In this problem, we will find the portfolio allocation that minimizes risk while achieving a given expected return $R_\text{target}$.
#
# Suppose that we know the mean returns $\mu \in \mathbf{R}^n$ and the covariance $\Sigma \in \mathbf{R}^{n \times n}$ of the $n$ assets. We would like to find a portfolio allocation $w \in \mathbf{R}^n$, $\sum_i w_i = 1$, minimizing the *risk* of the portfolio, which we measure as the variance $w^T \Sigma w$ of the portfolio. The requirement that the portfolio allocation achieve the target expected return can be expressed as $w^T \mu >= R_\text{target}$. We suppose further that our portfolio allocation must comply with some lower and upper bounds on the allocation, $w_\text{lower} \leq w \leq w_\text{upper}$.
#
# This problem can be written as
#
# $$
# \begin{array}{ll}
# \text{minimize} & w^T \Sigma w \\
# \text{subject to} & w^T \mu >= R_\text{target} \\
# & \sum_i w_i = 1 \\
# & w_\text{lower} \leq w \leq w_\text{upper}
# \end{array}
# $$
#
# where $w \in \mathbf{R}^n$ is our optimization variable.
using Convex, SCS
## generate problem data
μ = [11.5; 9.5; 6]/100 #expected returns
Σ = [166 34 58; #covariance matrix
34 64 4;
58 4 100]/100^2
n = length(μ) #number of assets
R_target = 0.1
w_lower = 0
w_upper = 0.5;
# 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.
#=
using Random
Random.seed!(123)
n = 15 #number of assets, CHANGE IT?
μ = (6 .+ (11.5-6)*rand(n))/100 #mean
A = randn(n,n)
Σ = (A * A' + diagm(0=>rand(n)))/500; #covariance matrix
=#
#-
w = Variable(n)
ret = dot(w,μ)
risk = quadform(w,Σ)
p = minimize( risk,
ret >= R_target,
sum(w) == 1,
w_lower <= w,
w <= w_upper )
solve!(p, () -> SCS.Optimizer()) #use SCS.Optimizer(verbose = false) to suppress printing
#-
# Optimal portfolio weights:
evaluate(w)
#-
sum(evaluate(w))