/*GAUSS CODE FOR GMM ESTIMATION OF THE NUMBER OF LATENT FACTORS BY MARCOS FABRICIO PEREZ OCTUBER 20 2008 */ new; cls; seed=1; rndseed seed; fabtime1=date; /*INITIAL DATA*/ bandw = 0 ; @ if bandw = 0, White-Correction, if auto =0 set bandw =0 @ defw=0; @if defw=1 andrews, if defw = 0 newey west@ numero2=100; @number of different partitions used in estimation@ changeornot=1; @1 if we do change partitions@ nameout="put_a_output_name_here" ; /* LOAD DATA HERE AND CALL IT "companybase" THIS IS AN t BY n MATRIX OF THE VARAIBLES TO BE ANALIZED */ companybase=rndns(300,10,seed); // for example with random numbers we should have 0 factors// //DO NOT CHANGE FROM HERE /*START ITERATIONS FOR CHANGE OF BASES*/ base=cols(companybase); tt=rows(companybase); company=companybase; numinstr=floor(base/2); iterchan=1; do while iterchan<=numero2; if changeornot==1; alfa=seqa(1,1,base); phil=rndi(base,1); thao=alfa~phil; lista = sortc(thao,2); lista1=lista[.,1]'; if iterchan==1; basmat=lista1; else; basmat=basmat|lista1; endif; company=company[.,lista1]; endif; // TIMER cls; format /rd 12,4; locate 4,1; locate 4,30; "iterchan" iterchan; fabtime2=ethsec(fabtime1,date); locate 6,1; "time running " etstr(fabtime2); if iterchan>5; fabtime3=(fabtime2/(iterchan-1))*numero2; locate 8,1; "time left " etstr(fabtime3-fabtime2); locate 10,1; "total time " etstr(fabtime3); endif; /*BAI'S METHOD*/ proc factorn(x,n); local a,b,c,d,factor,loadings; {a,b}=eigv(x'x); c=a~b'; d=rev(sortc(c,1)); loadings=sqrt(n)*d[.,2:n+1]'; factor=x*loadings/n; retp(factor); endp; proc factort(x,t); local a,b,c,d,factor; {a,b}=eigv(x*x'); c=a~b'; d=rev(sortc(c,1)); factor=sqrt(t)*d[.,2:t+1]'; retp(factor); endp; /* load data FOR BAI'S METHOD*/ data=company; kmax=8; @maximum amount of factors to be tested@ /* # of Observations*/ T=rows(data); N=rows(data'); /* Factor calculation*/ if N