# Simplification of sums with conditional functions

I am currently trying to calculate some expressions of means of iid (independent and identically distributed) random variables. I try to verify my manual calculations with Mathematica: I defined

m[Subscript[X, i_]] := m[Subscript[X, 1]] /; i != 1
m[Subscript[X, i_] Subscript[X, j_]] := m[Subscript[X, i]] m[Subscript[X, j]] /; i != j


which works fine. ($E(X_j)=E(X_1)$ and $E(X_iX_j)=E(X_i)E(X_j)$ if $i\neq j$)

I know that Mathematica can simplify sums with variable bound like Sum[a, {i, 1, n}] to an.

Now I consider a sum for a fixed $j$:

Sum[m[Subscript[X, i] Subscript[X, j]], {i, 1, n}]


Is it possible to make Mathematica simplify it to $(n-1)E(X_1)^2+E(X_j^2)$?

• Times[a,n] so its a product. What is your point? – meneken17 Jul 14 '17 at 12:35
• Very related: mathematica.stackexchange.com/questions/91700/…. – JimB Jul 14 '17 at 18:02
• An unsolicited comment that I've made in the past: Please consider the use of mathStatica to do such things with relative ease: mathstatica.com/software. – JimB Jul 14 '17 at 20:23

This isn't as general as one would like but it might be a start.

First I would drop the subscripts (Subscript[X,i]) and use indices (X[i]). (There are numerous references at this site explaining why.)

Then I would define the potential outcomes of expectations of powers and products of the independent and identically distributed random variables (and, of course, assuming that all of the appropriate moments exist).

m[X[i_]^k_] := μ[k]
m[X[i_] X[j_]] := μ[1]^2 /; i != j
m[X[i_]^k_ X[j_]] := μ[k] μ[1] /; i != j
m[X[i_] X[j_]^k_] := μ[1] μ[k] /; i != j
m[X[i_]^ki_ X[j_]^kj_] := μ[ki] μ[kj] /; i != j
m[X[i_]] := μ[1]


(Note that this only defines the product of two random variables. Other definitions - or rules - would need to be added for the expectation of 3 or more random variables.)

Then one can evaluate the sum of interest by fixing n and j:

n = 20
j = 2
Sum[m[X[i] X[j]], {i, 1, n}]
(* 19 μ[1]^2+μ[2] *)


I'm sure there must be ways to allow for general n but I don't know enough to be able to do that.

Update:

It's probably more straightforward to apply the expectation function as a rule:

n = 20
j = 2
Sum[X[i] X[j], {i, 1, n}] /. {X[i_]^k_ -> μ[k], X[i_] -> μ[1]}
(* 19 μ[1]^2 + μ[2] *)