Skip to main content

calculus and analysis - How to efficiently find moments of a multinormal distribution?


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[xr11xr22xrnn]=?


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[x1x2xn]=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


i1j=1pji+2pii+nj=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


mt1(t1)=t1σ11exp(12σ11t21),


2mt21(t1)=(t21σ211+σ11)exp(12σ11t21),


3mt31(t1)=(t31σ311+3t1σ211)exp(12σ11t21),



4mt41(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


4mt41(0)=3σ211.


This process can be represented by the following scheme


enter image description here


We can take the derivative of:




  1. 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).





  2. 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:


enter image description here


One can check that we obtain the known result


E(xr11)={(r11)!!σ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+r2mtr11tr22|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:





  1. Increase the power of t1 by 1 and multiply by σ11.




  2. Increase the power of t2 by 1 and multiply by σ12.




  3. 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:




  1. Increase the power of t2 by 1 and multiply by σ22.




  2. 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


enter image description here


If at the stage A we choose case 2 two times then it can be represented as multiplication of the diagrams


enter image description here


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}.


enter image description here


enter image description here


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

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]