Re: defining averages over unknown PDF

*To*: mathgroup at smc.vnet.net*Subject*: [mg131950] Re: defining averages over unknown PDF*From*: Sune Jespersen <sunenj at gmail.com>*Date*: Wed, 6 Nov 2013 00:33:43 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*Delivered-to*: l-mathgroup@wolfram.com*Delivered-to*: mathgroup-outx@smc.vnet.net*Delivered-to*: mathgroup-newsendx@smc.vnet.net*References*: <20131105041659.58DD96A05@smc.vnet.net> <20131105171943.GT507@wolfram.com>

Hi Itai Thanks for your reply. Yes, you are of course right and I realized the same thing shortly after my post. In fact I implemented a solution quite similar to yours, In[1]:= av /: D[av[f___], x_] := av[D[f, x]] In[2]:= av[y_ + z_] := av[y] + av[z] In[3]:= av[c_ y_] := c av[y] /; FreeQ[c, x] In[4]:= av[c_] := c /; FreeQ[c, x] In[5]:= D[av[x y], x] Out[5]= y In[6]:= D[av[Exp[-x y]], x] Out[6]= -y av[E^(-x y)] But it seems that it still has the problem when it needs to apply the chain rule, i.e. In[9]:= D[Log[av[Exp[-b x]]], b] Out[9]= -((E^(-b x) x)/av[E^(-b x)]) instead of -(av[(E^(-b x) x)]/av[E^(-b x)]) This seems a bit strange to me, because somehow it must reach a point where it needs to evaluate a derivative, where my rule applies. Perhaps you can offer some insight on this? On 5 Nov, 2013, at 18:19 , Itai Seggev <itais at wolfram.com> wrote: > On Mon, Nov 04, 2013 at 11:16:59PM -0500, Sune wrote: >> Dear all. >> >> I want to do some symbolic manipulations of an expression involving averages over a stochastic variable with an unknown density. Therefore, I figured I could define a function av, which would correspond to the average over this unknown parameter density function. >> I did as follows: >> av[y_ + z_, x_] := av[y, x] + av[z, x]? >> av[c_ y_, x_] := c av[y, x] /; FreeQ[c, x] >> av[c_, x_] := c /; FreeQ[c, x] >> >> So these are basic properties of the average over the distribution of X. Some things work okay, for example >> In[52]:= av[Exp[-x y], x]? >> Out[52]= av[E^(-x y), x] >> and >> In[79]:= D[av[-x y, x], x]? >> Out[79]= -y >> and >> In[80]:= D[av[-x y, x], y]? >> Out[80]= -av[x, x]. >> >> However, the most vital part for my calculations does not work: >> In[81]:= D[av[Exp[-x y], x], y]? >> Out[81]= -E^(-x y) x >> >> It should have been av[-Exp[-x y] x,x]. >> >> Any clues to what I'm doing wrong? I'm thinking that I need to specify some rules for differentiation, but I don't know how. But then I'm wondering how come it got the other expressions for differentiation right. > > Ahh, the subtle treacheries of partial differentiation. Note that by your > definition, > > In[71]:= av[Exp[-x y] + h, x] - av[Exp[-x y], x] > > Out[71]= h > > So that > > In[72]:= Limit[(av[Exp[-x y] + h, x] - av[Exp[-x y], x])/h, h -> 0] > > Out[72]= 1 > > So both your "correct" and "incorrect" answers are consistent with the chain > rule and and the above computation of partial derivatives. So why is D > computing the partial derivative in such a stupid way? Well, it isn't, at > least not directly. D correctly computes the partial derivative as > > f'[x] * Derivative[1, 0][av][f[x], x] + Derivative[0, 1][av][f[x],x] > > But now Derivative helpfully tries compute these partials using pure functions, > and then your definitions kick in, giving 1 and 0 for the partials. In > particular, your third definitions means av[#1,#2]& === #1, and you're doomed. > > So you want to abort the automatic differentiation rules with your own custom > rule, which you can do with the following syntax: > > av /: D[av[f_, x_], y_] /; x =!= y := av[D[f, y], x] > > In[65]:= D[av[Exp[-x y], x], y] > > Out[65]= -av[E^(-x y) x, x] > > -- > Itai Seggev > Mathematica Algorithms R&D > 217-398-0700

**References**:**defining averages over unknown PDF***From:*Sune <sunenj@gmail.com>