portfolio


Solve a Markowitz Portfolio problem with JuMP: $$ \begin{alignat}{2} \min_{0 \leq x \leq 1} \quad & x^T \Sigma x \\ \text{subject to} \quad & \mu x \geq r_{min} \\ & \sum x = 1 \\ \end{alignat} $$


In [24]:
using JuMP, Mosek, DataFrames, PyPlot

In [25]:
data = readtable("seudo_stock_data.csv")
data=data[2:end]


Out[25]:
stock1stock2stock3
10.425.877.59
21.758.7916.13
30.35.335.8
40.245.849.14
51.659.0313.25
60.03.883.23
70.667.089.0
80.35.335.8
90.245.849.14
100.285.516.96
111.659.0313.25
120.385.737.35
13-1.243.916.71
14-0.740.16-2.63
151.413.2418.78
16-0.745.4711.65
174.213.915.98
18-3.592.949.36
191.789.8314.11
20-0.852.276.81
21-0.54-3.34-6.56
22-2.882.4815.69
230.544.358.43
247.3910.984.51
255.667.410.72
261.482.996.75
272.265.358.05
283.5912.0315.61
293.452.49-0.33
301.87.6112.52
&vellip&vellip&vellip&vellip

In [26]:
data = convert(Array{Float64}, data)
plot([i for i in 1:length(data[:,1])],data[:,1],color="r", label="asset1")
plot(data[:,2],color="b",label="asset2")
plot(data[:,3],color="g",label="asset3")
legend()


Out[26]:
PyObject <matplotlib.legend.Legend object at 0x14b524fd0>

In [40]:
n, μ, Σ = length(data[1,:]), vec(mean(data,1)), cov(data)
result=[]
figure(figsize=(4.0,3.0))  
for r_min in 0.0:1.0:5.0
    port = Model(solver=MosekSolver(LOG=0))
    @variable(port, 0  x[1:n]  1)
    @constraint(port, sum(x) == 1)
    @constraint(port, dot(μ,x)  r_min)
    @objective(port, Min, x' * Σ * x)
    solve(port)
    plot([i for i in 1:n], getvalue(x), label=r_min)
end
legend()


Out[40]:
PyObject <matplotlib.legend.Legend object at 0x14d021fd0>

In [ ]: