# Risk-return trade-off — solving a convex optimzation

I try to convert a convex optimisation (cvxopt) example to the Wolfram Language from python. Source of the python code: Risk-return trade-off

Please advise me on how to convert the following python code into Wolfram Language.

xs = [ qp(mu*S, -pbar, G, h, A, b)['x'] for mu in mus ]


I have completed the first few lines

n = 4;
S =
{{ 4.0^-2.0, 6^-3.0, -4.0^-3, 0.0},
{6.0^-3.0, 1.0^-2.0, 0.0, 0.0},
{-4.0^-3, 0.0, 2.5^-3, 0.0},
{0.0, 0.0, 0.0, 0.0}};
pbar = {.12, .10, .07, .03};
G = -1*IdentityMatrix[4];
h = MatrixForm[{1.00, 1.00, 1.00, 1.00}];
b = MatrixForm[{1.00}];
numN = 100;
mus = Table[10.0^(5.0*i/100 - 1.0), {i, 100}];

-
A piece of advice. Never write an expression such as h = MatrixForm[{1.00, 1.00, 1.00, 1.00}]; Be aware that MatrixForm is a tool for pretty-printing output. It returns an expression unusable in further computation. Write MatrixForm[h = {1.00, 1.00, 1.00, 1.00}] if you want formatted output. Omit MatrixForm entirely if don't want to see any output from the assignment. – m_goldberg Feb 13 at 15:11
@m_goldberg,Thank you. – Nelson Mok Feb 15 at 7:00

I did the same exercise myself the other day. Here is one way to do it.

edit: as noted by @DanielLichtblau, calling the method "QuadraticProgramming" in FindMinimum for this problem is much faster than the default ("Automatic")

mS = {{0.04, 0.006, -0.004, 0.}, {0.006, 0.01, 0., 0.}, {-0.004, 0.,
0.0025, 0.}, {0., 0., 0., 0.}};
vP = {0.12, 0.1, 0.07, 0.03};
vx = Array[x, 4];
muRange = 10.^(5 Range[0, 100, 1]/100 - 1);

s = Monitor[
Table[
{#[[1]], vx/.#[[2]]}&[
]
,{mu, muRange}
]
,mu
];


Plotting the results:

Grid[{{
ListPlot[{Sqrt[#.mS.#], vP.#} & /@ s[[All, 2]], Joined -> True,
PlotStyle -> Blue, Frame -> True, AspectRatio -> 0.75,
Axes -> False,
FrameLabel -> {Text["standard deviation"],
Text["expected return"]}, PlotRange -> {0, 0.16},
PlotLabel -> Text["Risk-return trade-off curve (Fig. 4.12a)"],
ImageSize -> 250],
ListLinePlot[
Transpose /@ ({Sqrt[#.mS.#] & /@ s[[All, 2]], #} & /@
Transpose[Accumulate /@ s[[All, 2]]]), PlotStyle -> Blue,
Epilog -> {Text["x(1)", {0.15, 0.5}], Text["x(2)", {0.10, 0.7}],
Text["x(1)", {0.05, 0.7}], Text["x(4)", {0.01, 0.7}]},
FrameLabel -> {Text["standard deviation"], Text["allocation"]},
PlotLabel -> Text["Optimal allocations (Fig. 4.12b)"],
ImageSize -> 250]
}}]


-
Dear Stelios, Thank you. – Nelson Mok Feb 13 at 12:55
@NelsonMok You are welcome. – Stelios Feb 13 at 13:00
+1, honesty pays off – Louis Feb 13 at 15:07
Can get better speed with Method -> "QuadraticProgramming" (but this is quite nice regardless). – Daniel Lichtblau Feb 13 at 20:32
We really should implement Method->"SpanishInquisition" since nobody would ever expect it... – Daniel Lichtblau Feb 13 at 22:42