Author:

He, J. and S. X. Chen

Title:

He, J. and S. X. Chen (2016) Testing Super-Diagonal Structure in High Dimensional Covariance Matrices, Journal of Econometrics, 194,  283-297

C++ code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// A simulation sample
//
// The returned value "sum" calculates the test statistics Dnq before standardization.
// The returned value "Sigma" calculates the variance of the test statistics Dnq.
#include <iostream>
#include <fstream>
#include <Eigen/Dense>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
using namespace Eigen;
using namespace std;
#define PI 3.141592654
 
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define NTAB 32
#define NDIV (1+(IM-1)/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)
/*Uniform0-1, idum negative*/
double ran1(long *idum)
{
int j;
long k;
static long iy=0;
static long iv[NTAB];
double temp;
if (*idum <= 0 || !iy) {  
    if (-(*idum) < 1) *idum=1; 
    else *idum = -(*idum);
    for (j=NTAB+7;j>=0;j--) { 
        k=(*idum)/IQ;
        *idum=IA*(*idum-k*IQ)-IR*k;
        if (*idum < 0) *idum += IM;
        if (j < NTAB) iv[j] = *idum;
    }
    iy=iv[0];
}
k=(*idum)/IQ; 
*idum=IA*(*idum-k*IQ)-IR*k;
if (*idum < 0) *idum += IM;
j=iy/NDIV; 
iy=iv[j]; 
iv[j] = *idum; 
if ((temp=AM*iy) > RNMX) return RNMX; 
else return temp;
}
/*Uniform0-1, idum negative*/
/*N(0,1)*/
double gasdev(long *idum)
{
 double ran1(long *idum);
 static int iset=0;
 static double gset;
 double fac,rsq,v1,v2;
 if (*idum<0) iset=0;
 if(iset == 0){
  do{
   v1=2.0*ran1(idum)-1.0;
   v2=2.0*ran1(idum)-1.0;
   rsq=v1*v1+v2*v2;
  }while(rsq >= 1.0 || rsq == 0.0);
  fac=sqrt(-2.0*log(rsq)/rsq);
  gset=v1*fac;
  iset=1;
  return v2*fac;
 }else{
  iset=0;
  return gset;
 }
}
/*N(0,1)*/
 
/*Gamma(int ia,1),ia>=1.*/
double gamdev(int ia,long *idum)
{
 double ran1(long *idum);
 int j;
 double am,e,s,v1,v2,x,y;
 if (ia<6) {
  x=1.0;
  for (j=1;j<=ia;j++) x*=ran1(idum);
  x=-log(x);
 }else {
  do{
   do{
    do{
     v1=ran1(idum);
     v2=2.0*ran1(idum)-1.0;
    }while (v1*v1+v2*v2>1.0);
    y=v2/v1;
    am=ia-1;
    s=sqrt(2.0*am+1.0);
    x=s*y+am;
   }while (x<=0.0);
   e=(1.0+y*y)*exp(am*log(x/am)-s*y);
  }while (ran1(idum)>e);
 }
 return x;
}
/*Gamma(int ia,1),ia>=1.*/
 
double P(int i,int j)
{
 double ans=1;
 int i1;
 for(i1=1;i1<=j;i1++)
  ans=ans*i1;
 for(i1=1;i1<=j-i;i1++)
  ans=ans/double(i1);
 return ans;
}
 
int main()
{
 int n,p,i,j,l,rep,v,k0,q,qnum; /*n: sample size; p: dimension; k0: true bandwidth of Sigma */
 n=50;
 p=50;
 rep=1000;
 k0=5;
 qnum=49;
 MatrixXd G,z(n,p),x(n,p),sum(rep,(qnum+1)),sigma(rep,(qnum+1)),O,C,xcolsum,xmean;
 G = MatrixXd::Identity(p,p);
 O = MatrixXd::Ones(n,n);
 for(j=1;j<(k0+1);j++)
 {
  for(i=0;i<p-j;i++)
  {
   G(i+j,i) = 0.4;
  }
 }/* Generate Gamma matrix;*/
 /*calculate the excution time*/
 clock_t tStart = clock();
 for (v=0;v<rep;v++)
 {
  long idum=-(v+12345);
  for (i=0;i<n;i++)
  {
   for (j=0;j<p;j++)
   {
    z(i,j)=gasdev(&idum);
   }
  }
   
  x=z*G.transpose();
  xcolsum = x.colwise().sum();
  xmean = O*x/n;
  C=(x-O*x/n).transpose()*(x-O*x/n)/(n-1);
  /*Sample Covariance Matrix of X*/
 
  for (q=0;q<(qnum+1);q++)
  {
   MatrixXd Y(n,(p-q));
   for (i=0;i<n;i++)
   {
    for (l=0;l<(p-q);l++)
    {
     Y(i,l) = (x(i,l)-xmean(l))*(x(i,(l+q))-xmean(l+q)) - C(l,(l+q));
    }
   }
   MatrixXd YY(n,n),YYcolsum;
   YY=Y*Y.transpose();
 
   double sigma1,sum1,temp1,temp2,temp3;
   sigma1 = 0;
   sum1=0;
   temp1=0;
   temp2=0;
   temp3=0;
   for(i=0;i<n;i++)
   {
    for(j=0;j<i;j++)
    {
     for (l=0;l<p-q;l++)
     {
      temp1 += x(i,l)*x(i,l+q)*x(j,l)*x(j,l+q);
      temp2 += (x(i,l)*x(j,l)*x(j,l+q)*x(j,l+q) - x(i,l)*x(j,l)*x(j,l+q)*xcolsum(l+q));
      temp3 += (x(i,l)*x(j,l+q)*xcolsum(l)*xcolsum(l+q) + x(i,l)*x(i,l)*x(j,l+q)*x(j,l+q) - x(i,l)*x(i,l)*x(j,l+q)*xcolsum(l+q) - x(i,l)*x(j,l+q)*x(j,l+q)*xcolsum(l));
     }
    
    for(j=(i+1);j<n;j++)
    {
     for (l=0;l<p-q;l++)
     {
      temp1 += x(i,l)*x(i,l+q)*x(j,l)*x(j,l+q);
      temp2 += (x(i,l)*x(j,l)*x(j,l+q)*x(j,l+q) - x(i,l)*x(j,l)*x(j,l+q)*xcolsum(l+q));
      temp3 += (x(i,l)*x(j,l+q)*xcolsum(l)*xcolsum(l+q) + x(i,l)*x(i,l)*x(j,l+q)*x(j,l+q) - x(i,l)*x(i,l)*x(j,l+q)*xcolsum(l+q) - x(i,l)*x(j,l+q)*x(j,l+q)*xcolsum(l));
     }
    
    sigma1 += YY(i,i)*YY(i,i);
   
   sum(v,q)=temp1/n/(n-3)+temp2*(2*n-3)/n/(n-1)/(n-2)/(n-3)+temp3/n/(n-1)/(n-2)/(n-3);
   sigma(v,q)=((YY*YY).trace()-sigma1)/n/(n-1);
  }
 }
 
 ofstream f;
 f.open ("Sigma_n50p50normal.txt");
 f << sigma;
 f.close();
 ofstream f1;
 f1.open ("Dnq_n50p50normal.txt");
 f1 << sum;
 f1.close();
 return 0;
}