Update: Starting from V10.0 the build-in Moment
is fast enough for practical use.
I have a multinormal distribution with covariance matrix σ and zero mean. I want to find moment
E[xr11xr22⋯xrnn]=?
Of course, there is a built-in function Moment
, but it is quite slow for big moments
Moment[MultinormalDistribution[{0, 0}, {{σ[1, 1], σ[1, 2]}, {σ[2, 1], σ[2, 2]}}],
{20, 20}]; // AbsoluteTiming // First
1.604658
Is there a more efficient method? It is related to this question and my answer there.
I think that Isserlis' theorem is helpful. It says
E[x1x2⋯xn]=∑∏E[xixj] if n even and 0 if n odd, by definition E[xixj]=σij. Here the notation ∑∏ means summing over all distinct ways of partitioning x1,x2,…,xn into unordered pairs.
For example, x1,x2,x3,x4 can be spitted as (x1,x2),(x3,x4);(x1,x3),(x2,x4);(x1,x4),(x2,x3). Therefore,
E[x1x2x3x4]=σ1,2σ3,4+σ1,3σ2,4+σ1,4σ2,3.
If we want to calculate E[x21x2x3] then we can put two indexes equal (e.g. x4=x2) in the previous equation
E[x21x2x3]=σ1,1σ2,3+2σ1,2σ1,3.
Another examples:
E[x21x22]=σ1,1σ2,2+2σ21,2,
E[x31x2]=3σ1,1σ1,2,
E[x41]=3σ21,1.
For six variables there is 15 terms:
E[x1x2x3x4x5x6]=σ1,2σ3,4σ5,6+σ1,2σ3,5σ4,6+σ1,2σ3,6σ4,5+σ1,4σ2,5σ3,6+σ1,5σ2,4σ3,6+σ1,4σ2,6σ3,5+σ1,6σ2,4σ3,5+σ1,5σ2,6σ3,4+σ1,6σ2,5σ3,4+σ1,3σ2,4σ5,6+σ1,4σ2,3σ5,6+σ1,3σ2,5σ4,6+σ1,5σ2,3σ4,6+σ1,3σ2,6σ4,5+σ1,6σ2,3σ4,5.
As you can see, this statistical problem is just a combinatorics problem: find all possibilities to put r1 balls of type 1, r2 balls of type 2, ..., and rn balls of type n to bins. Each bin can take 2 and only 2 balls.
Answer
Explicit formula
The wolfies' answer gave me an idea that one can derive an explicit formula. Here it is!
$$ E(x_1^{r_1}x_2^{r_2}\cdots x_n^{r_n}) = \sum_{(p)}\prod_{i}\frac{r_i!\,\sigma_{ii}^{p_{ii}}}{(2p_{ii})!!}\prod_{i
where sum is performed over all non-negative integer values of pii and pij with constrain
i−1∑j=1pji+2pii+n∑j=i+1pij=ri,i=1,2,…,n.
Implementation
moment2[x_List] := With[{n = Length[x]},
With[{σii = Table[σ[i, i], {i, n}], σij = Join @@ Table[σ[i, j], {i, n}, {j, i + 1, n}],
pij = Table[Unique[], {n (n - 1)/2}],
pos = n (n - 1)/2 - (n - Min[##] + 1) (n - Min[##])/2 + Abs[# - #2] &},
With[{pii = Table[x[[i]] - Sum[If[i == j, 0, pij[[pos[i, j]]]], {j, n}], {i, n}]/2,
lim = Sequence @@ Join @@ Table[{pij[[pos[i, j]]],
x[[i]] - Sum[If[i == k, 0, pij[[pos[i, k]]]], {k, j - 1}],
0, -If[j == n, 2, 1]}, {i, n}, {j, i + 1, n}]},
With[{arg = Times @@ ((x! σii^pii)/(2 pii)!!) Times @@ (σij^pij/pij!)},
If[Length[{lim}] == 0, arg, Sum[arg, lim]]]]]]
moment2[{19, 20, 21}] // Hash // AbsoluteTiming
moment[{19, 20, 21}] // Hash // AbsoluteTiming
{0.036750, 4700900427412246901}
{2.762643, 4700900427412246901}
It is very fast and requred no memory for memoization (for large moments moment
takes a huge amount of memory). The leaf count is small as in the wolfies' answer.
Derivation of the formula
One variable
At the beginning, let us consider the simplest case with one variable E(xr11). The moment generation function is
m(t1)=exp(12σ11t21).
The first derivatives are
∂m∂t1(t1)=t1σ11exp(12σ11t21),
∂2m∂t21(t1)=(t21σ211+σ11)exp(12σ11t21),
∂3m∂t31(t1)=(t31σ311+3t1σ211)exp(12σ11t21),
∂4m∂t41(t1)=(t41σ411+6t21σ311+3σ211)exp(12σ11t21)
and so on. Then to calculate the moment we need to put t1=0. For the forth moment we have
∂4m∂t41(0)=3σ211.
This process can be represented by the following scheme
We can take the derivative of:
The exponent. It increases the power of t1 by 1 and multiply by σ11. It is represented by the upward arrows (all of them has multiplication factor σ11).
The pre-exponential polynomial. It decreases the power of t1 by 1 and multiply by the current power of t1. It is represented by the downward arrows (their multiplication factors correspond to the vertical position).
At the end we need to come to the zero vertical position. All other terms disappear after substitution t1=0. To obtain the moment we need to sum products of factors of all possible paths.
Example for r1=10:
One can check that we obtain the known result
E(xr11)={(r1−1)!!σr1/211if r1 is even,0if r1 is odd.
For the odd r1 there is simply no paths.
Two variables
With two variables the moment generating function is
m(t1,t2)=exp(12σ11t21+12σ22t22+σ12t1t2).
The moment E(xr11xr22) can be calculated with
E(xr11xr22)=∂r1+r2m∂tr11∂tr22|t1=0,t2=0
For definiteness we will:
A. take all derivatives with respect to t1,
B. then take all derivatives with respect to t2.
At the stage A there are three possibilities:
Increase the power of t1 by 1 and multiply by σ11.
Increase the power of t2 by 1 and multiply by σ12.
Decrease the power of t1 by 1 and multiply by the current power of t1.
After the stage A we substitute t1=0.
At the stage B there are only to possibilities:
Increase the power of t2 by 1 and multiply by σ22.
Decrease the power of t2 by 1 and multiply by the current power of t2.
We don't consider production of powers of t1 because they will be killed at the final substitution t1=0,t2=0.
Let us consider E(x61x42). If at the stage A we always choose cases 1 or 3 (not 2) then it can be represented as multiplication of two full diagrams
If at the stage A we choose case 2 two times then it can be represented as multiplication of the diagrams
The first diagram is smaller by 2 because we consume 2 of 6 derivatives with respect to t1 to produce t2. The binomial coefficient \binom{6}{2} is the number of possible choices of the case 2. The second diagram starts from the position 2 because we produce t_2^2 at the stage \rm A.
One can show that the sum of the diagram with r_2 derivatives and the initial position k_2 is \left\{\begin{array}{ll} \frac{r_2!}{(r_2-k_2)!!}\sigma_{22}^{(r_2-k_2)/2} & \text{if }r_2-k_2\text{ is even},\\ 0 & \text{if }r_2-k_2\text{ is odd}. \end{array}\right.
Therefore, the term \sigma_{11}^{p_{11}}\sigma_{12}^{p_{12}}\sigma_{22}^{p_{22}} in the moment E(x_1^{r_1}x_2^{r_2}) has the coefficient
\frac{(2p_{11})!}{(2p_{11})!!}\frac{r_1!}{(p_{12})!(r_1-p_{12})!}\frac{r_2!}{(r_2-p_{12})!!} =\\ \frac{r_1!}{(2p_{11})!!}\frac{1}{p_{12}!}\frac{r_2!}{(2p_{22})!!}
where I use that 2p_{11}+p_{12}=r_1 and p_{12}+2p_{22}=r_2. This formula tell us the form of the general formula which I wrote in the beginning. One can check that the formula has the same form for any number of variables. I didn't write it here because it is much more complicated. I just give an example for diagrams in the case of three variables.
Three variables
Let us consider E(x_1^7x_2^6x_3^5) and coefficient before \sigma_{11}^2\sigma_{12}^\vphantom{2}\sigma_{13}^2\sigma_{22}^2\sigma_{23}^\vphantom{2}\sigma_{33}^\vphantom{2}.
The coefficient is
4!/4!! Multinomial[1, 2, 4] 5!/4!! Multinomial[1, 5] 5!/2!!
1701000
The general formula returns the same
moment2[{7, 6, 5}]
... + 1701000 σ[1, 1]^2 σ[1, 2] σ[1, 3]^2 σ[2, 2]^2 σ[2, 3] σ[3, 3] + ...
If anybody knows this formula please write where it is published!
Comments
Post a Comment