Re: Mathematical expectation

*To*: mathgroup at smc.vnet.net*Subject*: [mg84131] Re: [mg84113] Mathematical expectation*From*: Carl Woll <carlw at wolfram.com>*Date*: Tue, 11 Dec 2007 06:06:14 -0500 (EST)*References*: <200712110140.UAA18271@smc.vnet.net>

Benoît Frénay wrote: >Hello, > >I'm new to Mathematica and I'm trying to define a generic mathematical >expectation operator (called Esp) for i.i.d variables X[i] following the >same distribution as the variable X (i.e. Esp[X[i]] == Esp[X]). My code is > >Esp[X[_]] := Esp[X] >Esp[X[_]^y_] := Esp[X^y] >Esp[Esp[x_]] := Esp[x] >Esp[Esp[x_]^y_] := Esp[x]^y >Esp[x_ + y_] := Esp[x] + Esp[y] >Esp[x_ y_] := Esp[x] Esp[y] /; FreeQ[x, y] && FreeQ[y, x] >Esp[c_] := c /; FreeQ[c, X] >Esp[c_ x_] := c Esp[x] /; NumberQ[c] > >But when I feed > >>> In[14]:= Esp[(X[1] + X[2])^2] >I obtain > >>> Out[14]= Esp[(X[1] + X[2])^2] >In fact, I obtain the correct answer only if I ask Mathematica to expand >the inner term: >In[16]:= Esp[Expand[(X[1] + X[2])^2]] >Out[16]= 2 Esp[X]^2 + 2 Esp[X^2] > >Where am I wrong? > >Thanks, > >Benoît Frénay > > > You don't have a rule for Esp[x_Plus^n_], so naturally nothing happens. Perhaps you can add the rule: Esp[x_Plus^n_] := Esp[Expand[x^n]] Carl Woll Wolfram Research

**References**:**Mathematical expectation***From:*Benoît Frénay <benoit.frenay@uclouvain.be>