Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I did some search here, but seems that no one asked about this.

I want to define a family of independent and identically distributed random variables, $x_1,...,x_n$, and then calculates the expected value of some expressions like $\sum_{i,j=1}^nx_ix_j$. The result would be some function depending on $n$. Is there a way to do this?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Here is a general solution for any distribution whose moments exist ...

Notation Define the power sum $s_r$:

$$s_r=\sum _{i=1}^n X_i^r$$

The Problem

Let $\left(X_1,\ldots,X_n\right)$ denote $n$ iid random variables. This is the same problem as drawing a random sample of size $n$ from a population random variable $X$. The problem is to find:

$$E\Big(\sum_{i,j=1}^n X_i X_j\Big) = E\Big [\Big (\sum_{i=1}^n X_i\Big)^2\Big ] = E\Big [s_1^2\Big]$$

This is a problem known as finding moments of moments: they can be very difficult to solve by hand, but quite easy to solve with the help of a computer algebra system, for any arbitrary symmetric power sum. In this instance, we seek the expectation of $s_1^2$ ... i.e. the 1st raw moment of $s_1^2$ ... so the solution (expressed ToRaw moments of the population) is:

where RawMomentToRaw is a function from the mathStatica package for Mathematica, and where $\acute{\mu }_1$ and $\acute{\mu }_2$ denote the 1st and 2nd raw moments of random variable $X$, whatever its distribution (assuming they exist). All done.

More detail

There is an extensive discussion of moments of moments in Chapter 7 of our book:

  • Rose and Smith, "Mathematical Statistics with Mathematica", Springer, NY

A free download of the chapter is available here:

http://www.mathstatica.com/book/Rose_and_Smith_2002edition_Chapter7.pdf


Examples

Example 1: The Normal Distribution

If $X \sim N(\mu, \sigma^2)$, then: $$\acute{\mu }_1 = E[X] = \mu \quad \text{ and } \quad \acute{\mu }_2 = E[X^2] = \mu^2 + \sigma^2 $$

Substituting in $\acute{\mu }_1$ and $\acute{\mu }_2$ in Out[1] yields the solution: $$E\Big(\sum_{i,j=1}^n X_i X_j\Big) = n \left(n \mu ^2 + \sigma ^2\right)$$

Simple check: The Normal case with $n = 3$

In the case of $n = 3$, the joint pdf of $(X_1, X_2, X_3)$ is say $f(x_1, x_2, x_3)$:

The sum of products we are interested in is:

and the desired expectation is:

which matches perfectly the general $n$-Normal solution derived above, but with $n = 3$.


Example 2: The Uniform Distribution

If $X \sim Uniform(a,b)$ (as considered in both other answers), then: $$\acute{\mu }_1 = E[X] = \frac{a+b}{2} \quad \text{ and } \quad \acute{\mu }_2 = E[X^2] = \frac{1}{3} \left(a^2+a b+b^2\right)$$

Substituting in $\acute{\mu }_1$ and $\acute{\mu }_2$ in Out[1] yields the solution: $$E\Big(\sum_{i,j=1}^n X_i X_j\Big) =\frac{1}{3} n \left(a^2+a b+b^2\right)+\frac{1}{4} (n-1) n (a+b)^2$$

Again, this is different to the other answers posted - and much more complicated. Again, it is easy to perform a quick check:

Simple check: The Uniform case with $n = 3$

In the case of $n = 3$, the joint pdf of $(X_1, X_2, X_3)$ is say $g(x_1, x_2, x_3)$:

and the desired expectation is:

which matches perfectly our general $n$-Uniform solution derived above, with $n = 3$.

share|improve this answer
    
Thanks for pointing out my mistake. –  b.gatessucks Apr 26 at 20:06
    
Thanks wolfies. I have to admit, I wasn't aware about what was the correct expectation and I just start to use Mathematica. That's why I couldn't know if the accepted answer was indeed correct. I just changed this, thank you very much for helping. –  Integral Apr 26 at 22:52
    
But still, you made manipulations with $n$ random variables to explain, but made the codes using only 3 random variables. I still don't know how to define $n$ random variables and calculates the expectation of some expression with them. Could you show the code to calculate $E (\sum_{i,j=1}^nX_iX_j)$ in the first example (and get as answer $n(n\mu^2+\sigma^2)$) ? Thanks. –  Integral Apr 26 at 23:12
    
I was looking for some symbolic calculations, in which $n$ is not assigned to a value. I want that Mathematica calculates some expectations in the same way you get the result $n(n\mu^2 + \sigma^2)$, doing only symbolic calculations. It is possible to get a lot of relevant results this way, and that is what I'm looking for. –  Integral Apr 26 at 23:58
    
I did some tests here, looks like the code d = NormalDistribution[0, \[Sigma]]; Expectation[x^n, x \[Distributed] d] give me a symbolic answer, which is exactly what I want to work with. If Mathematica does that, it should be able to compute expectations of polynomial of random variables iid, and shouldn't be necessary to say how many random variables, the program should be able to deal with a sum and some expression inside the sum, Its just a matter of deal with indexes. –  Integral Apr 27 at 1:25

Here's an example, expected sum of n uniform random variates with specified minimums and maximums:

dt = TransformedDistribution[Sum[v1*v2, {x, 1, n}], 
       {v1 \[Distributed] UniformDistribution[{min1, max1}], 
        v2 \[Distributed] UniformDistribution[{min2, max2}]}];

exp=Expectation[x, x \[Distributed] dt]

(* 1/4 (max1+min1) (max2+min2) n *)

Check it with a simulation:

{min1, max1, min2, max2} = {1, 10, 20, 50};
v1sim = RandomVariate[UniformDistribution[{min1, max1}], {100000}];
v2sim = RandomVariate[UniformDistribution[{min2, max2}], {100000}];
v1sim*v2sim // Total // AccountingForm
exp /. {min1 -> 1, max1 -> 10, min2 -> 20, max2 -> 50, n -> 100000}

(*

19198729.

19250000

*)
share|improve this answer
1  
Maybe I got this wrong, but looks to me that your n random variables are not i.i.d., they all depend on $v_1$ and $v_2$. –  Integral Apr 26 at 4:30
    
The names don't matter -- they are just symbols, and don't have any effect on the answer. It works just as well when written: dt = TransformedDistribution[Sum[x1*x2, {i, 1, n}], {x1 \[Distributed] UniformDistribution[{min1, max1}], x2 \[Distributed] UniformDistribution[{min2, max2}]}]; –  m_goldberg Apr 26 at 4:40
1  
Im having trouble to define define $\sum_{i,j=1}^nx_ix_j$, in the case that the $x_i$ are $N(0,1)$. The expected value should be $n$ but Im always getting $0$. How do I exactly do that sum? –  Integral Apr 26 at 5:45
    
@Integral: If the N in your comment is the normal distribution, then that (0) is the expectation. –  rasher Apr 26 at 6:15
    
Im not sure about that, this sum will have squared terms, and $E(x_i^2) = 1$, because the variance of the $x_i$`s are 1. –  Integral Apr 26 at 6:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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