       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:= av /: D[av[f___], x_] := av[D[f, x]]
In:= av[y_ + z_] := av[y] + av[z]
In:= av[c_ y_] := c av[y] /; FreeQ[c, x]
In:= av[c_] := c /; FreeQ[c, x]
In:= D[av[x y], x]
Out= y
In:= D[av[Exp[-x y]], x]
Out= -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:= D[Log[av[Exp[-b x]]], b]
Out= -((E^(-b x) x)/av[E^(-b x)])
-(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:= av[Exp[-x y], x]?
>> Out= av[E^(-x y), x]
>> and
>> In:= D[av[-x y, x], x]?
>> Out= -y
>> and
>> In:= D[av[-x y, x], y]?
>> Out= -av[x, x].
>>
>> However, the most vital part for my calculations does not work:
>> In:= D[av[Exp[-x y], x], y]?
>> Out= -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:= av[Exp[-x y] + h, x] - av[Exp[-x y], x]
>
> Out= h
>
> So that
>
> In:= Limit[(av[Exp[-x y] + h, x] - av[Exp[-x y], x])/h, h -> 0]
>
> Out= 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:= D[av[Exp[-x y], x], y]
>
> Out= -av[E^(-x y) x, x]
>
> --
> Itai Seggev
> Mathematica Algorithms R&D
> 217-398-0700

```

• Prev by Date: Manipulate in MATHEMATICA
• Next by Date: Re: ListPlot - assigning a list of colors to a set of points
• Previous by thread: Re: defining averages over unknown PDF
• Next by thread: Re: defining averages over unknown PDF