In Mathematica 8.04 on Windows, I want to display a formula in standard textbook format. The formula is the variance of an $N$-security portfolio. For two securities it is:

$$w_1^2\sigma_1^2+w_2^2\sigma_2^2+2\sigma_{1,2}w_1 w_2$$

To generate this, I have created the following function:

psd[n_Integer] := 
  Sum[Sum[Subscript["w", i] Subscript["w", j] If[i == j, Subsuperscript["σ", i, 2], 
      Subscript["σ", Min[i, j], Max[i, j]]], {i, 1, n}], {j, 1, n}];

This works, but the terms are out of order as shown in this image of the output of psd[2]:

MMA Output of my psd function

I'd like to take the last two terms (with the exponents) and place them in front of the first two. Suppose that I call the output "a", then I can get the parts of a using Take[a,1] and Take[a,-2].

What I would like to be able to do is to put those two parts back together in reverse order as shown in the original equation. Ideally, I'd be able to specify a sort order, but I can't seem to figure that out. I've tried all of the orders for MonomialList with no luck, and when I add the list parts it gets resorted anyway.

The order of the terms doesn't matter for calculation, but this is for presentation and I'd like it to look like it does in every textbook. The point is to demonstrate how the formula grows longer as more securities are added to the portfolio. Except for the ordering, it works perfectly in a Manipulate. Any ideas?

  • $\begingroup$ Sorry Tim, on my screen the first formula looked like it had a minus not a plus. I have deleted my comment. You know you can use LaTeX markup on the site? $\endgroup$
    – Verbeia
    Mar 4 '12 at 21:31

You were definitely on the right track with MonomialList. Here is a solution. Others will probably find nicer ways. Using the trick found here, we first define a Format that looks like "Plus" but doesn't rearrange things:

Format[myPlus[expr__]] := Row[Riffle[{expr}, "+"]]

With this format in hand, we can wrap your original function in the following:

myPolynomial[n_Integer] := myPlus @@ RotateRight[Sort@MonomialList[psd[n]], n]

The Sort places things in what MMA considers canonical order. Lucky for us, it happens to put all the $\sigma_{1,2} w_1w_2$-like terms together first. Then we rotate the list using RotateRight. Since we know there will be n functions of the type $w_1^2\sigma_1^2$, we rotate it that many places. Using the non-rearranging form of plus, we get the desired result!

  • $\begingroup$ Thank you! This works perfectly. I didn't realize that I could use Format to define my own formats. I think that I'll find other uses for that. $\endgroup$
    – Tim Mayes
    Mar 4 '12 at 20:05
  • 2
    $\begingroup$ You could also write myPlus this way: myPlus[expr__] := Plus @@@ HoldForm[{expr}] $\endgroup$
    – Mr.Wizard
    Mar 4 '12 at 22:56
  • $\begingroup$ Ahhh, that's the combination of Plus and HoldForm that I couldn't quite figure out. Thanks @Mr.Wizard $\endgroup$
    – tkott
    Mar 4 '12 at 23:30
  • $\begingroup$ @Mr.Wizard, thanks for the simplification. I kept trying to test HoldForm, but I couldn't get it to work. $\endgroup$
    – Tim Mayes
    Mar 4 '12 at 23:41
  • 1
    $\begingroup$ Here's a slightly better approach: myPolynomial[n_Integer] := Composition[Defer, Plus] @@ RotateRight[Sort@MonomialList[psd[n]], n]. Among other things, you can execute, say, myPolynomial[2], copy the output, and it works nicely as valid input... $\endgroup$
    – J. M.'s torpor
    Apr 11 '13 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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