using Convex, ECOS #We are using ECOS solver. Install using Pkg.add("ECOS") # generate problem data srand(0); #Set the seed n = 5; # Assume that we have portfolio of 5 assets. R = 5 * randn(n); A = randn(n, 5); Q = A * A' + diagm(rand(n)); w_lower = 0; w_upper = 1; risk = zeros(2000); # Initialized the risk and the return vectors. ret = zeros(2000); # lambda varies in the interval(0,1) in the steps of 1/2000. w = Variable(length(R)); #Defining constraints c1 = sum(w) == 1; c2 = w_lower <= w; c3 = w <= w_upper; for i in 1:2000 λ = i/2000; #Defining Objective function objective = λ * quadform(w,Q) - (1-λ)* w' *R; p = minimize(objective, c1,c2,c3); solve!(p, ECOSSolver(verbose = false)); risk[i] = (w.value' * Q * w.value)[1]; ret[i] = (w.value'R)[1]; #println("\$i ","\$(λ*risk[i] - (1-λ)*ret[i]) ","\$p.optval"); end using PyPlot #Install PyPlot if you don't have it installed. Pkg.add("PyPlot") plot(risk,ret) title("Markowitz Efficient Frontier"); xlabel("Expected Risk-Variance"); ylabel("Expected Return");