Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}];
share|improve this question
6  
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
up vote 11 down vote accepted

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]]}&[
             FindMinimum[{-vP.vx + mu vx.mS.vx,Plus@@vx==1&&And@@Thread[vx>=0]}, vx, 
                          Method->"QuadraticProgramming"]
         ]
      ,{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, 
    PlotRangePadding -> None, 
    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]
   }}]

enter image description hereenter image description here

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.