All of the examples can be found in Jupyter notebook form here.

Worst case risk analysis

Generate data for worst-case risk analysis.

using Random

Random.seed!(2);
n = 5;
r = abs.(randn(n, 1))/15;
Sigma = 0.9 * rand(n, n) .- 0.15;
Sigma_nom = Sigma' * Sigma;
Sigma_nom .-= (maximum(Sigma_nom) - 0.9)
5×5 Array{Float64,2}:
 0.54178     0.45192   0.328892    0.0123621   0.400576
 0.45192     0.694173  0.416407   -0.237798    0.295939
 0.328892    0.416407  0.496781    0.0266768   0.31054 
 0.0123621  -0.237798  0.0266768   0.414028   -0.181312
 0.400576    0.295939  0.31054    -0.181312    0.9     

Form and solve portfolio optimization problem. Here we minimize risk while requiring a 0.1 return.

using Convex, SCS
w = Variable(n);
ret = dot(r, w);
risk = sum(quadform(w, Sigma_nom));
problem = minimize(risk, [sum(w) == 1, ret >= 0.1, norm(w, 1) <= 2])
solve!(problem, () -> SCS.Optimizer(verbose=0));
wval = vec(evaluate(w))
5-element Array{Float64,1}:
 -0.2584111225097882 
  0.5388445424592738 
 -0.24158885787869822
  0.751630898449201  
  0.20952450574832615

Form and solve worst-case risk analysis problem.

Sigma = Semidefinite(n);
Delta = Variable(n, n);
risk = sum(quadform(wval, Sigma));
problem = maximize(risk, [Sigma == Sigma_nom + Delta,
                    diag(Delta) == 0,
                    abs(Delta) <= 0.2,
                    Delta == Delta']);
solve!(problem, () -> SCS.Optimizer(verbose=0));
println("standard deviation = ", round(sqrt(wval' * Sigma_nom * wval), sigdigits=2));
println("worst-case standard deviation = ", round(sqrt(evaluate(risk)), sigdigits=2));
println("worst-case Delta = ");
println(round.(evaluate(Delta), sigdigits=2));
standard deviation = 0.27
worst-case standard deviation = 0.82
worst-case Delta =
[7.0e-7 -0.2 0.19 -0.2 -0.2; -0.2 1.9e-7 -0.2 0.2 0.2; 0.19 -0.2 6.6e-7 -0.2 -0.2; -0.2 0.2 -0.2 -4.3e-7 0.2; -0.2 0.2 -0.2 0.2 6.4e-7]

This page was generated using Literate.jl.