Commit | Line | Data |
---|---|---|
cbd88fe5 BA |
1 | #include <stdlib.h> |
2 | ||
d08fef42 | 3 | // Index matrix (by columns) |
9ac8ecc0 | 4 | #define mi(i, j, d1, d2) (j*d1 + i) |
cbd88fe5 | 5 | |
d08fef42 | 6 | // Index 3-tensor (by columns, matrices ordered by last dim) |
9ac8ecc0 | 7 | #define ti(i, j, k, d1, d2, d3) (k*d1*d2 + j*d1 + i) |
cbd88fe5 | 8 | |
d08fef42 | 9 | // Empirical cross-moment of order 2 between X size nxd and Y size n |
cbd88fe5 BA |
10 | void Moments_M2(double* X, double* Y, int* pn, int* pd, double* M2) |
11 | { | |
6dd5c2ac BA |
12 | int n=*pn, d=*pd; |
13 | //double* M2 = (double*)calloc(d*d,sizeof(double)); | |
cbd88fe5 | 14 | |
6dd5c2ac BA |
15 | // M2 = E[Y*X^*2] - E[Y*e^*2] = E[Y (X^*2 - I)] |
16 | for (int j=0; j<d; j++) | |
17 | { | |
18 | for (int i=0; i<n; i++) | |
19 | { | |
20 | M2[mi(j,j,d,d)] -= Y[i] / n; | |
21 | for (int k=0; k<d; k++) | |
22 | M2[mi(j,k,d,d)] += Y[i] * X[mi(i,j,n,d)]*X[mi(i,k,n,d)] / n; | |
23 | } | |
24 | } | |
cbd88fe5 BA |
25 | } |
26 | ||
d08fef42 | 27 | // Empirical cross-moment of order 3 between X size nxd and Y size n |
cbd88fe5 BA |
28 | void Moments_M3(double* X, double* Y, int* pn, int* pd, double* M3) |
29 | { | |
6dd5c2ac BA |
30 | int n=*pn, d=*pd; |
31 | //double* M3 = (double*)calloc(d*d*d,sizeof(double)); | |
cbd88fe5 | 32 | |
6dd5c2ac BA |
33 | // M3 = E[Y*X^*3] - E[Y*e*X*e] - E[Y*e*e*X] - E[Y*X*e*e] |
34 | for (int j=0; j<d; j++) | |
35 | { | |
36 | for (int k=0; k<d; k++) | |
37 | { | |
38 | for (int i=0; i<n; i++) | |
39 | { | |
40 | double tensor_elt = Y[i]*X[mi(i,k,n,d)] / n; | |
41 | M3[ti(j,k,j,d,d,d)] -= tensor_elt; | |
42 | M3[ti(j,j,k,d,d,d)] -= tensor_elt; | |
43 | M3[ti(k,j,j,d,d,d)] -= tensor_elt; | |
44 | for (int o=0; o<d; o++) | |
45 | M3[ti(j,k,o,d,d,d)] += Y[i] * X[mi(i,j,n,d)]*X[mi(i,k,n,d)]*X[mi(i,o,n,d)] / n; | |
46 | } | |
47 | } | |
48 | } | |
cbd88fe5 | 49 | } |
4263503b | 50 | |
d08fef42 BA |
51 | // W = 1/N sum( t(g(Zi,theta)) g(Zi,theta) ) |
52 | // with g(Zi, theta) = i-th contribution to all moments (size dim) - real moments | |
074c721a | 53 | void Compute_Omega(double* X, int* Y, double* M, int* pn, int* pd, double* W) |
4263503b | 54 | { |
6dd5c2ac | 55 | int n=*pn, d=*pd; |
b389a46a | 56 | int dim = d + d*d + d*d*d; |
bbdcfe44 | 57 | //double* W = (double*)malloc(dim*dim*sizeof(double)); |
4bf8494d BA |
58 | |
59 | // (Re)Initialize W: | |
60 | for (int j=0; j<dim; j++) | |
61 | { | |
62 | for (int k=0; k<dim; k++) | |
63 | W[j*dim+k] = 0.0; | |
64 | } | |
4bf8494d | 65 | double* g = (double*)malloc(dim*sizeof(double)); |
7737c2fa BA |
66 | for (int i=0; i<n; i++) |
67 | { | |
bbdcfe44 | 68 | // g == gi: |
d08fef42 | 69 | for (int j=0; j<d; j++) |
19d893c4 | 70 | g[j] = Y[i] * X[mi(i,j,n,d)] - M[j]; |
d08fef42 BA |
71 | for (int j=d; j<d+(d*d); j++) |
72 | { | |
73 | int idx1 = (j-d) % d; //num row | |
74 | int idx2 = ((j-d) - idx1) / d; //num col | |
75 | g[j] = 0.0; | |
19d893c4 BA |
76 | if (idx1 == idx2) |
77 | g[j] -= Y[i]; | |
78 | g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[j]; | |
d08fef42 BA |
79 | } |
80 | for (int j=d+d*d; j<dim; j++) | |
81 | { | |
82 | int idx1 = (j-d-d*d) % d; //num row | |
83 | int idx2 = ((j-d-d*d - idx1) / d) %d; //num col | |
84 | int idx3 = (((j-d-d*d - idx1) / d) - idx2) / d; //num "depth" | |
85 | g[j] = 0.0; | |
19d893c4 BA |
86 | if (idx1 == idx2) |
87 | g[j] -= Y[i] * X[mi(i,idx3,n,d)]; | |
88 | if (idx1 == idx3) | |
89 | g[j] -= Y[i] * X[mi(i,idx2,n,d)]; | |
90 | if (idx2 == idx3) | |
91 | g[j] -= Y[i] * X[mi(i,idx1,n,d)]; | |
92 | g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[j]; | |
d08fef42 BA |
93 | } |
94 | // Add 1/n t(gi) %*% gi to W | |
95 | for (int j=0; j<dim; j++) | |
96 | { | |
97 | for (int k=0; k<dim; k++) | |
98 | W[j*dim+k] += g[j] * g[k] / n; | |
99 | } | |
7737c2fa | 100 | } |
d08fef42 | 101 | free(g); |
4263503b | 102 | } |