8
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ Have you looked at Expectation? $\endgroup$
    – rm -rf
    Nov 3, 2013 at 21:08
  • $\begingroup$ Yes, it doesn't work the way I want. $\endgroup$
    – Sooner
    Nov 4, 2013 at 8:00
  • $\begingroup$ 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... $\endgroup$
    – rm -rf
    Nov 4, 2013 at 8:25
  • $\begingroup$ Good idea. (I only found out afterwards). $\endgroup$
    – Sooner
    Nov 4, 2013 at 8:40
  • $\begingroup$ Perhaps it would work if I knew how to specify that a user defined operator commutes with the D operator? (But that still doesn't explain why, in the two examples above using D on av,av is maintained in the first but not in the last.) $\endgroup$
    – Sooner
    Nov 4, 2013 at 15:24

2 Answers 2

3
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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]

f'
(* 1 & *)

The condition was lost.

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$
    – Sooner
    Nov 6, 2013 at 14:21
  • $\begingroup$ (Besides, in addition to the bug you described, there is also a bug when applying Series to an expression like yours, see this post ) $\endgroup$
    – Sooner
    Nov 6, 2013 at 14:23
  • $\begingroup$ @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. $\endgroup$
    – ybeltukov
    Nov 6, 2013 at 14:38
  • $\begingroup$ 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? $\endgroup$
    – Sooner
    Nov 6, 2013 at 14:45
  • $\begingroup$ 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. $\endgroup$
    – ybeltukov
    Nov 6, 2013 at 14:55

Your Answer

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

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