Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I need to do some simplification of an expression involving averages over a stochastic variable (in order to verify a long analytical calculation). The easiest way to do that, I figured, were if I could implement an operator which would basically be short-hand for the averaging procedure, with all the appropriate properties. Then of course this operator would be present in the final expression, which is fine, and would enable me to compare easily with my own calculations. So assuming I use x for the stochastic variable, I tried defining av using

av[y_ + z_] := av[y] + av[z]  
av[c_ y_] := c av[y] /; FreeQ[c, x]
av[c_] := c /; FreeQ[c, x]  

Then when I write

D[av[x y], y]

I get


which is fine, but when I write

D[av[Exp[-x y]], y]

I get

-E^(-x y) y  

instead of -y av[Exp[-x y]] as I want, i.e., the av is removed somehow.

I tried using UpValue for teaching Mathematica that it could interchange differentiation and av, but apparently that is not the problem. I might be going about this entirely the wrong way, but I'd be grateful for any input. Note the builtin Expectation function does not accomplish it either - e.g., it doesn't handle the derivatives as a proper average operator would. For example

h[y_] := Expectation[y, x \[Distributed] pp] (*pp unknown density*)


D[h[Exp[-x y]], x]


(Expectation^(0,1))[E^(-x y),x\[Distributed]pp] (Distributed^(1,0))[x,pp]-E^(-x y) y   
(Expectation^(1,0))[E^(-x y),x\[Distributed]pp]

whereas I wanted

-y h[Exp[-y x]]

(i.e moving the derivative inside the averaging h). Sune

share|improve this question
Have you looked at Expectation? –  The Toad Nov 3 '13 at 21:08
Yes, it doesn't work the way I want. –  Sune Nov 4 '13 at 8:00
Then why don't you include that in the question (i.e. how Expectation doesn't satisfy your needs)? It's much easier that way than having to guess... –  The Toad Nov 4 '13 at 8:25
Good idea. (I only found out afterwards). –  Sune Nov 4 '13 at 8:40
Thanks for the update :) –  The Toad Nov 4 '13 at 9:06

2 Answers 2

I think I got it.

av /: D[av[f___], x_] := av[D[f, x]]
av[y_ + z_] := av[y] + av[z]
av[c_ y_] := c av[y] /; FreeQ[c, x]
av[c_] := c /; FreeQ[c, x]
D[av[x y], x]

(* y *)

D[av[Exp[-x y]], x]
(* -y av[E^(-x y)] *)

I wasn't using UpValues correctly before.

EDIT: Well, turns out there's still a problem:

D[Log[av[Exp[-b x]]], b]
(* -((E^(-b x) x)/av[E^(-b x)]) *)

instead of

-(av[(E^(-b x) x)]/av[E^(-b x)])

What's going on? Sune

share|improve this answer

Just define exactly what you want:

av[expr_] := Integrate[f[x] expr, {x, -∞, ∞}];

Here f[x] is undefined function.

D[av[Exp[-x y]], y]

enter image description here

D[Log[av[Exp[-b x]]], b]

enter image description here

P.S. There is a possible bug

f[c_] := c /; FreeQ[c, x]

(* 1 & *)

The condition was lost.

share|improve this answer
I did that for a specific case, but I have reasons for wanting something a bit more general. For example, I'm already generating a lot of output, which I intend to compare with my analytical computations, and having a compact output directly containing av would make it so much simpler to compare and less prone to mistakes. Also, it would make it easier to have Mathematica use the other rules that I supplied in its simplification process. And finally, it is sometimes a more complicated multidimensional interval over a complex region. –  Sune Nov 6 '13 at 14:21
(Besides, in addition to the bug you described, there is also a bug when applying Series to an expression like yours, see this post ) –  Sune Nov 6 '13 at 14:23
@Sune I tried to implement av, but the main problem for me were derivatives. D[av[f[x,y]],x] produces a function of x outside av. –  ybeltukov Nov 6 '13 at 14:38
That happened to me originally, but the order of the rules is crucial. If you restart the kernel and paste it exactly as I wrote it down above, do you still have that problem? –  Sune Nov 6 '13 at 14:45
It works not always, e.g. y Sin[x y]. Also, in your post you write D[...,x]. Did you mean D[...,y]? There is no derivative of x after averaging. In the previous comment I have the same typo. –  ybeltukov Nov 6 '13 at 14:55

Your Answer


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.