# Expectation of product of independent random variables (using Expectation function)

I'm trying with no luck to get Expectation to calculate the expectation of expressions containing sums of products of independent random variables.

For instance, take $$\mathbb{E}[\sum_{i=1}^n\sum_{j=1}^n X_iX_j]$$, where $$(X_1,\ldots,X_n)$$ are mutually independent with $$X_i \sim \mathcal{N}(a_i,b_i)$$. Can Expectation perform this calculation?

EDIT

Could get the expectation of a product right by using Piecewise:

f[i_, j_] :=
Piecewise[{{Expectation[
x[i] x[j], {x[j] \[Distributed]
NormalDistribution[\[Mu][j], \[Sigma][j]],
x[i] \[Distributed] NormalDistribution[\[Mu][i], \[Sigma][i]]}],
i != j}, {Expectation[x[i]^2,
x[i] \[Distributed] NormalDistribution[\[Mu][i], \[Sigma][i]]],
i == j}}]


But still couldn't obtain $$\sum_{i,j}f_{i,j}$$.

• Depending on what kind of sums you have you might consider the following related post: mathematica.stackexchange.com/questions/50606/…. Maybe one or more additional examples help us give you a more general approach.
– JimB
Jul 2, 2020 at 3:02

I'm pretty sure this doesn't answer the question the way you want (and I would want also) but using Expectation works if you specify n:

n = 5;
Expectation[Sum[x[i] x[j], {i, n}, {j, n}],
Table[x[i] \[Distributed] NormalDistribution[μ[i], σ[i]], {i, n}]] // FullSimplify
(* (μ + μ + μ + μ + μ)^2 + σ^2 + σ^2 + σ^2 + σ^2 + σ^2 *)


From that results one can see the general form:

n =.;
Sum[\[Mu][i], {i, n}]^2 + Sum[\[Sigma][i]^2, {i, n}] • I've managed to get the expectation of the product, but still couldn't obtain the sum. If that is Maybe that is as far as we can get without having recourse to your inductive approach Jul 3, 2020 at 0:04

Same setup as in the answer by @JimB (fixed n) - just for fun.

 proc = ItoProcess[{
\[DifferentialD]x1[t] == \[Sigma]1 \[DifferentialD]w1[t],
\[DifferentialD]x2[t] == \[Sigma]2 \[DifferentialD]w2[t],
\[DifferentialD]x3[t] == \[Sigma]3 \[DifferentialD]w3[t]},
(x1[t] + x2[t] + x3[t])^2, {{x1, x2, x3}, {\[Mu]1, \[Mu]2, \[Mu]3}}, {t, 0},
{w1 \[Distributed] WienerProcess[],
w2 \[Distributed] WienerProcess[],
w3 \[Distributed] WienerProcess[]}];

Mean[proc]
(* (\[Mu]1+\[Mu]2+\[Mu]3)^2+\[Sigma]1^2+\[Sigma]2^2+\[Sigma]3^2 *)