# How to implement a formal expectation operator over an unknown distribution?

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

 av[x]


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*)


Then

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


gives

(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

-
Have you looked at Expectation? – R. M. 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... – R. M. Nov 4 '13 at 8:25
Good idea. (I only found out afterwards). – Sune Nov 4 '13 at 8:40
Thanks for the update :) – R. M. Nov 4 '13 at 9:06

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)]) *)


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


What's going on? Sune

-

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]


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


P.S. There is a possible bug

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

f'
(* 1 & *)


The condition was lost.

-
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