In [1]:

using JuMP
using Ipopt






## Simple MVO

\begin{align} &\text{minimize} && w^\top\Sigma w \\ &\text{subject to} && \mu^\top w\geq r_{\min} \\ & && \mathbf{1}^\top w = 1 \\ & && w \succeq 0 \\ & && \sum_{i=1}^{\infty}{w_i} \succeq 0 \\ \end{align}


In [2]:

# generate data
n = 5
Σ = randn(n,n)
# Σ = let
#     S = randn(n,n)
#     S'S + eye(n)
# end
μ = randn(n)
r_min = mean(μ)




Out[2]:

0.08975286547400149




In [3]:

m = Model(solver=IpoptSolver())

@defVar(m, w[1:n] ≥ 0)
@setObjective(m, Min, dot(w,Σ*w))

dot(μ,w) ≥ r_min
dot(ones(n),w) == 1
end)




In [4]:

m




Out[4]:

\begin{alignat*}{1}\min\quad & -0.05053104700466323 w_{1}^2 + 2.390934450108892 w_{1}\timesw_{2} - 0.28554554178343283 w_{2}^2 - 0.23428162603034944 w_{1}\timesw_{3} - 1.4107282232765646 w_{2}\timesw_{3} + 0.8599947024830101 w_{3}^2 - 1.311645194187152 w_{1}\timesw_{4} - 0.9744464979295524 w_{2}\timesw_{4} + 1.2937865357457476 w_{3}\timesw_{4} - 0.6227344167782718 w_{4}^2 + 0.10548427747818312 w_{1}\timesw_{5} + 0.22632214720884647 w_{2}\timesw_{5} + 0.15962709976158698 w_{3}\timesw_{5} - 0.4922373107302065 w_{4}\timesw_{5} - 0.5933820234118684 w_{5}^2\\ \text{Subject to} \quad & 0.26612308720231453 w_{1} + 0.17096470354440468 w_{2} + 0.6342283031038511 w_{3} - 1.1385853774680643 w_{4} + 0.5160336109875014 w_{5} \geq 0.08975286547400149\\ & w_{1} + w_{2} + w_{3} + w_{4} + w_{5} = 1\\ & w_{i} \geq 0 \quad\forall i \in \{1,2,\dots,4,5\}\\ \end{alignat*}




In [6]:

m.objVal




Out[6]:

0




In [12]:

x = getValue(w)




WARNING: Variable value not defined for component of w. Check that the model was properly solved.

Out[12]:

5-element Array{Float64,1}:
NaN
NaN
NaN
NaN
NaN




In [11]:

status = solve(m)




******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
******************************************************************************

This is Ipopt version 3.12.4, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

Number of nonzeros in equality constraint Jacobian...:        5
Number of nonzeros in inequality constraint Jacobian.:        5
Number of nonzeros in Lagrangian Hessian.............:       25

Total number of variables............................:        5
variables with only lower bounds:        5
variables with lower and upper bounds:        0
variables with only upper bounds:        0
Total number of equality constraints.................:        1
Total number of inequality constraints...............:        1
inequality constraints with only lower bounds:        1
inequality constraints with lower and upper bounds:        0
inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
0  3.5638383e-04 9.50e-01 8.43e-01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
1  1.4477633e-01 2.22e-16 1.81e+01  -1.7 4.73e-01    -  5.19e-02 1.00e+00f  1
2  1.4007600e-01 0.00e+00 4.83e-01  -1.7 4.83e-03   2.0 1.00e+00 1.00e+00f  1
3  1.2577789e-01 0.00e+00 4.48e-01  -1.7 1.34e-02   1.5 1.00e+00 1.00e+00f  1
4  6.7243317e-02 2.22e-16 5.77e-01  -1.7 5.20e-02   1.0 1.00e+00 1.00e+00f  1
5  3.7213602e-02 0.00e+00 6.95e-01  -1.7 2.35e-02   1.5 1.00e+00 1.00e+00f  1
6 -2.2423603e-01 0.00e+00 1.47e+00  -1.7 1.49e-01   1.0 1.00e+00 1.00e+00f  1
7 -3.8758707e-01 0.00e+00 1.73e+00  -1.7 6.58e-02   1.4 1.00e+00 1.00e+00f  1
8 -7.7032211e-01 2.22e-16 2.20e+00  -1.7 3.27e-01   0.9 1.00e+00 4.25e-01f  1
9 -1.3632065e+00 0.00e+00 7.31e+00  -1.7 1.26e+00   0.5 8.38e-01 1.52e-01f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
10 -1.3703096e+00 0.00e+00 2.52e+00  -1.7 1.66e-02    -  1.00e+00 3.41e-01f  2
11 -1.3549140e+00 1.11e-16 2.00e-07  -1.7 5.62e-03    -  1.00e+00 1.00e+00f  1
12 -1.4342534e+00 2.22e-16 4.28e-03  -3.8 1.84e-02    -  9.85e-01 9.64e-01f  1
13 -1.4351525e+00 2.22e-16 1.50e-09  -3.8 1.83e-04    -  1.00e+00 1.00e+00f  1
14 -1.4357476e+00 0.00e+00 1.84e-11  -5.7 1.31e-04    -  1.00e+00 1.00e+00f  1
15 -1.4357548e+00 0.00e+00 2.55e-14  -8.6 1.60e-06    -  1.00e+00 1.00e+00f  1

Number of Iterations....: 15

(scaled)                 (unscaled)
Objective...............:  -1.4357548282652326e+00   -1.4357548282652326e+00
Dual infeasibility......:   2.5509802434222750e-14    2.5509802434222750e-14
Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   2.5094220144700591e-09    2.5094220144700591e-09
Overall NLP error.......:   2.5094220144700591e-09    2.5094220144700591e-09

Number of objective function evaluations             = 18
Number of objective gradient evaluations             = 16
Number of equality constraint evaluations            = 18
Number of inequality constraint evaluations          = 18
Number of equality constraint Jacobian evaluations   = 16
Number of inequality constraint Jacobian evaluations = 16
Number of Lagrangian Hessian evaluations             = 15
Total CPU secs in IPOPT (w/o function evaluations)   =      0.132
Total CPU secs in NLP function evaluations           =      0.157

EXIT: Optimal Solution Found.

Out[11]:

:Optimal




In [ ]: