MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Mathematical expectation

Benoît Frénay wrote:

>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?
>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

  • Prev by Date: Re: question about For
  • Next by Date: Re: Inset problems & Export
  • Previous by thread: Mathematical expectation
  • Next by thread: Re: Mathematical expectation