I have a small toy script in Mathematica that I am trying to use to evaluate the pdf of $Y$ where $Y=X^2$, and $X$ is uniformly distributed in $[0,a]$.

The script is,

assum = {a > 0}; 
Subscript[P, X][x_] := If[Inequality[0, Less, x, LessEqual, a], 1/a, 0];

Y[x_] := x^2

Subscript[F, Y][y_] := Integrate[If[Y[x] <= y, 1, 0]*Subscript[P, X][x], 
   {x, -Infinity, Infinity}]

Subscript[P, Y][y_] := D[Subscript[F, Y][y], y]

It shows the correct form of the pdf:

Simplify[Subscript[P, Y][y], assum]

Piecewise[{{1/(2*a*Sqrt[y]), y > 0 && a^2 >= y}}, 0]

However, it doesn't evaluate the expression correctly:

Simplify[Subscript[P, Y][a*(a/2)], assum]

During evaluation of In[194]:= General::ivar:a^2/2 is not a valid variable. >>

D[1/Sqrt[2], a^2/2]

The session, in pretty printing looks like this:

enter image description here


$P[y]$ is defined to be $\partial_yF[y]$. If you now call $P[2]$ for example, this is translated to $\partial_2F[2]$, which of course does not make any sense.

One way to get around this behavior is not taking the derivative with respect to y, but with respect to the first argument instead. The following example assigns f[y] to be the derivative of g[y]:

g[y_] := y^2
f[y_] := Derivative[1][g][y]
2 x

Instead of Derivative[1][g][y] you could also have used the shorthand notation g'[y]. Read the explicit long version I used as an operator applied to multiple things: Derivative[1] takes a function and calculates its first derivative with respect to the first argument. Derivative[1][g] is that derivative (as a pure function), and Derivative[1][g][y] is that pure function applied to a value y.

The script above has one problem however: Every time you call f[x], it re-calculates the derivative, which can take some time if your function is more complicated and you need a lot of data points. If you use functions instead of patterns (i.e. no :=) you can also get around this problem:

g = #^2 &
f = g'
#1^2 &
2 #1 &
2 x
  • $\begingroup$ Thanks! I didn't realise := wasn't the right way to define functions. $\endgroup$ – highBandWidth Mar 20 '12 at 5:54
  • $\begingroup$ @highBandWidth err, both = and := are right way to define functions. There is a big difference between them however, and you should learn the difference. See for example mathematica.stackexchange.com/questions/2639/… and SetDelayed: reference.wolfram.com/mathematica/ref/SetDelayed.html $\endgroup$ – tkott Mar 20 '12 at 10:53
  • 1
    $\begingroup$ @tkott That's not really true. = and := define pattern-based replacement rules, which may look like functions in many cases, but are handled differently internally. Defining actual functions is done using Function. Related: mathematica.stackexchange.com/questions/704/… $\endgroup$ – David Mar 20 '12 at 17:54
  • $\begingroup$ @David Sorry you're absolutely right. I just used the wrong terminology. $\endgroup$ – tkott Mar 20 '12 at 18:18

Or you can just keep your code the way it is (with the SetDelayed), but force the derivation to be executed at the definition by using Evaluate:

Subscript[P, Y][y_] := Evaluate[D[Subscript[F, Y][y], y]]

This gives you the correct result:

In[107]:= Simplify[Subscript[P, Y][a*(a/2)], assum]
Out[107]= 1/(Sqrt[2] a^2)

This would be the most robust way to do it (at least it is how the Mathematica 8 help does it).

  • $\begingroup$ This is how I would have done it, until I learned that one can also just use Subscript[P, Y][y_] = D[Subscript[F, Y][y], y]. The only difference to the original form is that I don't use SetDelayed but Set, so that the y is used literally and not replaced by its value at the time the "function" is called. This way is just a little shorter than using Evaluate. $\endgroup$ – Jens Jun 10 '12 at 18:16
  • $\begingroup$ True, but in general I try to avoid the use of Set, as it can give rise to difficult-to-debug naming conflicts. $\endgroup$ – freddieknets Jun 10 '12 at 21:41

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.