# How could I expand the formula in terms of $X_{i}^{2}$ and $X_{i}X_{j}$ using Mathematica?

I have the following formula, where $X_{i}$, $i=1,2,...,n$ are variables:

$F_{1}(X)$=$\frac{1}{n}\sum\limits_{k=1}^{n}\left ( a_{0}X_{k}+a_{1}X_{k}(\frac{k}{n})+a_{1}\sum\limits_{i=k}^{n}X_{i}\frac{1}{n}-a_{0}\frac{1}{n}\sum\limits_{i=1}^{n}X_{i} -2a_{1}\frac{1}{n}\sum\limits_{i=1}^{n}X_{i}\frac{i}{n}\right )^2.$

As Daniel requested, here is the Mathematica code for the formula (not sure it is correct):

Sum[(a[0]*x[k] + a[1]*x[k]*(k/n) + a[1]*Sum[x[i]/n, {i, k, n}] -
a[0]*Sum[x[j]/n, {j, 1, n}] - 2*a[1]*Sum[x[j]*(j/n)*(1/n),
{j, 1, n}])^2,
{k, 1, n}]


where $a_0$ and $a_1$ are just arbitrary fixed coefficients.

How can I use Mathematica to simplify/expand the above formula in the form as

$F(X)$=$\sum coef*X_{i}^2+\sum\sum coef*X_{i}X_{j}$

in terms of the $X$ squared terms and cross terms?

• Would be helpful to have your input in proper Mathematica format. – Daniel Lichtblau Dec 19 '15 at 21:01
• @DanielLichtblau, thx Daniel , I want to follow your idea, but since this is my first time experience with Mathematica. It is hard for me even to write them in formal style. I will see what I can do. – lzstat Dec 19 '15 at 21:07
• Just copy and paste from the notebook. The typical behavior at least for code is that it will translate fine. The example might require first changing the Cell > Format to InputForm, depending on whether there is anything fancy in it (e.g. subscripts). – Daniel Lichtblau Dec 19 '15 at 21:16
• @DanielLichtblau, I just mimic others and add the mathematica code for my formula in the text. – lzstat Dec 19 '15 at 22:20
• Looks fine to me. – Daniel Lichtblau Dec 19 '15 at 22:41

I'm not aware that Mathematica does much with simplifying summations symbolically (although I really hope I'm wrong). So with the help of Mathematica to check on my brute force summation algebra, the following should be the coefficients desired:

$$F(X)=\sum_{k=1}^{n} {c_k x_k^2} + \sum_{k=1}^{n-1} \sum_{i=k+1}^{n} {c_{ki} x_k x_i}$$

with

$c_k=(a_0^2 (-1 + n) n^2 + 2 a_0 a_1 n (k (-2 + n) + n) + a_1^2 k (k (-4 + n) + 3 n))/n^4$

$c_{ki}=2 (2 a_1 k + a_0 n) (-a_0 n + a_1 (-2 i + n))/n^4$

(* Set the number of terms *)
n = 20;

(* Define original function *)
f1 = (1/n) Sum[(a0 x[k] + a1 x[k] k/n + (a1/n) Sum[x[i], {i, k, n}] -
(a0/n) Sum[x[i], {i, n}] - (2 a1/n) Sum[x[i] i/n, {i, 1, n}])^2, {k, n}];

(* Define function in a special format *)
ck[k_] := (a0^2 (-1 + n) n^2 + 2 a0 a1 n (k (-2 + n) + n) +
a1^2 k (k (-4 + n) + 3 n))/n^4
cki[k_, i_] := 2 (2 a1 k + a0 n) (-a0 n + a1 (-2 i + n))/n^4
f2 = Sum[ck[k] x[k]^2, {k, n}] +
Sum[Sum[cki[k, i] x[k] x[i], {i, k + 1, n}], {k, n - 1}];

(* Check for a difference in the functions *)
Expand[f1 - f2]
(* 0 *)


Update

Below is the brute force way I did the algebra. I'm sure there are more elegant and efficient ways to do this.

First, collect terms, simplify the coefficients, and expand the square:

Work on each term individually to get things in the desired format:

Combine all of the terms

Substitute back in the original coefficients and simplify:

n =.;
k =.;
i =.;
bk = a0 + a1 k/n;
bi = a0 + a1 i/n;
c = a1/n;
dk = a0/n + 2 a1 k/n^2;
di = a0/n + 2 a1 i/n^2;
ck = FullSimplify[(1/n) (bk^2 + c^2 k + n dk^2 + 2 c bk - 2 bk dk - 2 c k dk)]
(* (a0^2 (-1 + n) n^2 + 2 a0 a1 n (k (-2 + n) + n) + a1^2 k (k (-4 + n) + 3 n))/ n^4 *)
cki = FullSimplify[(1/n)(2c^2 k + 2n dk di + 2c bk - 2(bk di + bi dk) - 2c(k di + i dk))]
(* (2(2 a1 k+a0 n)*((-a0) n+a1 (-2 i+n)))/n^4 *)

• how do you get expression for $c_{k}$, $c_{ki}$ ? you did by hand ? I did not work it out by hand. I am checking it. thank you so much anyway. If it is true, I will mention your help in my article. This is a variance formula I deduced using empirical likelihood approach. The reason to simplify this is to compare with another variance estimator provided by Stigler (1979). – lzstat Dec 20 '15 at 16:23
• @Izstat. I've added in a description of the algebraic steps. – JimB Dec 20 '15 at 17:36
• thank you so much Jim. I think it is true as far as my experiments now. I will try to compare with another estimator, still a lot to do. If you are interested, you could look at my another post in cross validation(stats.stackexchange.com/questions/186275/… first formula is provided by Stigler , the second one is what I derived using Empirical likelihood approach. The research is still in half way, I will let you know when I finish this article. – lzstat Dec 20 '15 at 17:47