10
$\begingroup$

Say I have a PDF:

PDF[LogNormalDistribution[1.75, 0.65], x]

Calculating it, Mathematica gives me an expression that looks like this:

enter image description here

I want to extract the equation itself so I can use it in other calculations. On a one off basis, I can do that with Part[]:

PDF[LogNormalDistribution[1.75, 0.65], x][[1, 1, 1]]

which gives me the following:

enter image description here

That's fine, but I need something more universal. For instance, for a different PDF that looks like this:

enter image description here

I could get what I need with:

%[[1, 2, 1]] 

You see my dilemma. How do I extract the business part of such expressions in a more universal way so I can use them in other calculations and functions that may need to deal with a variety of such expressions?

$\endgroup$

4 Answers 4

9
$\begingroup$

Here is a way to separate your equation into different subparts. It uses Reap and Sow to tag parts of the expression as either "equation" or "conditions" or "constants".

f = Which[FreeQ[#, x], Sow[#, "constants"],
        MemberQ[{Equal, Unequal, Greater, GreaterEqual, Less, LessEqual, And, Or}, Head[#]], 
        Sow[#, "conditions"],True, Sow[#, "equation"]] &;

Last@Reap[Scan[f, List @@ PDF[LogNormalDistribution[1.75, 0.65], x] // Flatten], 
    {"equation", "conditions", "constants"}] ~Flatten~ 1

(* Out[1]= {{(0.613757 E^(-1.18343 (-1.75 + Log[x])^2))/x}, {x > 0}, {0}} *)

So now you have your equations in the first sublist, conditions in the second and constants in the third. If you need only the first, you need to Reap only that. This should work with your second example too.

Note that you'll have to be careful to ensure that x above does not have any value — using formal symbols is better.

$\endgroup$
1
  • 1
    $\begingroup$ Many thanks. Your suggestion works nicely in my application and you grasped the intent of my question relative to what I needed to do exactly. This might not work in every situation, but in mine where I need to assess pdfs it show work perfectly. $\endgroup$
    – Jagra
    Commented Apr 5, 2012 at 19:41
9
$\begingroup$

Since you get the result in terms of Piecewise, you can use things like Refine or Simplify, particularly when you want to get a result given some additional condition on your variables. In particular,

Refine[PDF[LogNormalDistribution[1.75, 0.65], x], x > 0]

(* ==>  (0.613757 E^(-1.18343 (-1.75+Log[x])^2))/x  *)
$\endgroup$
3
  • 1
    $\begingroup$ @Leonid_Shifrin I'm confused. To get what I want with Refine, it seems I would need to know the condition(s) before hand. Identifying the conditions programmatically seems much like the same problem with which I started. $\endgroup$
    – Jagra
    Commented Apr 5, 2012 at 17:54
  • 1
    $\begingroup$ @Jagra, if you don't know the condition beforehand, explain in words how would you choose what expression you want to extract for the general case (in which you might have more than a single "big formula" $\endgroup$
    – Rojo
    Commented Apr 5, 2012 at 18:17
  • $\begingroup$ @Jagra You have to somehow identify the piece in your function you want to extract. You did this by indicating position of it inside Piecewise, but were apparently unsatisfied because that was fragile. I agree. But to have it not fragile, the only option I see is to use conditions for this, at least if you want to do this programmatically. $\endgroup$ Commented Apr 5, 2012 at 18:17
7
$\begingroup$

"How do I extract the business part of such expressions in a more universal way?"

Well, do you always know what the "business part" of your expression is?

For instance,

pw = Series[Piecewise[{{Cos[x], x <= 0}}, E^x], {x, a, 2}]

gives:

Mathematica graphics

What's the business part there?

I believe you're thinking of the most complex piece in the Piecewise expression. If that's the case you could use a function like LeafCount to provide a measure of complexity.

SortBy[pw[[1, All, 1]], LeafCount][[-1]]

Mathematica graphics

pw = Piecewise[{{x^2 + x, x > 0}, {0, x == 0}, {1, x < 0}}]

Mathematica graphics

SortBy[pw[[1, All, 1]], LeafCount][[-1]]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ While not as relevant as the solution R.M. supplied for my specific application, I very much appreciate your input on this. I learned something which should prove very useful in the future. Thanx. $\endgroup$
    – Jagra
    Commented Apr 5, 2012 at 19:44
1
$\begingroup$

There is a very simple answer here:

pdf = PDF[LogNormalDistribution[1.75, 0.65], x];
pdf[[1, 1, 1]]

(* (0.613757 E^(-1.18343 (-1.75 + Log[x])^2))/x *)

To see that you need {1,1,1}, and not another term use the TreeForm expression:

TreeForm[pdf]
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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