@ This GAUSS proc evaluates correct correlation matrix @

proc covary(kv,xmat);
    /*
  inputs:
    kv = no. of explanatory variables (excluding constant term)
    xmat = (nx x ny) matrix of distances between observations
        (typically should have nx = ny = sample size)
  globals:
    kqopt = 1 means calculate correlations using Theorem 2.2
    kqopt = 2 means calculate correlations using Table 1
  output:
    xc = (nx x ny) matrix of correlations between observations */

local kodd, k0, G0, Gh, hoo, kb, xc, xrow, xcol, xall;

xmat = xmat/2;  @ now the elements of xmat correspond to h in
    Theorem 2.2 and Table 1 @
xrow = rows(xmat);
xcol = cols(xmat);
xall = xrow*xcol;
xmat = reshape(xmat,1,xall);
xmat = minc(xmat|ones(1,xall));
xmat = reshape(xmat,xrow,xcol);  @ now the elements of xmat are no
   larger than unity @

if kqopt == 1;
  kb = kv - 1;
  kodd = (kb % 2);    @ kodd is 1 if kb is odd and 0 if kb is even @
  @ ==============  First calculate G0 = G<kb>(0,1)  ============== @
  if kodd == 0;
     G0 = 1;  @ This is G<0>(0,1) @
  elseif kodd == 1;
     G0 = (pi/4);  @ This is G<1>(0,1) @
  else;
     "integer error; this line should not be executed";
      "kodd is";;kodd;
      end;
  endif;
  k0 = kodd;
  do until k0 >= kb;
     k0 = k0+2;
     G0 = (k0/(1 + k0))*G0;
  endo;

  @ ==============  Next calculate Gh = G<kb>(h,1)  ============== @
  if kodd == 0;
      Gh = 1 - xmat;   @ This is G<0>(h,1) @
  elseif kodd == 1;
      Gh = (pi/4) - 0.5*xmat.*sqrt(1 - xmat^2)
          - 0.5*arcsin(xmat); @ This is G<1>(h,1) @
  else;
      "integer error; this line should not be executed";
  endif;
  k0 = kodd;
  do until k0 >= kb;
     k0 = k0 + 2;
     Gh = (k0/(k0+1))*Gh - (xmat/(1+k0)).*(1 - xmat^2)^(k0/2);
  endo;

  hoo = Gh/G0;

 elseif kqopt == 2;
  if kv==1;
     hoo = 1 - xmat;
  elseif kv == 2;
     hoo = 1 - (2/pi)*(xmat.*sqrt(1 - xmat^2) + arcsin(xmat));
  elseif kv == 3;
     hoo = 1 - (3/2)*xmat + (1/2)*xmat^3;
  elseif kv == 4;
     hoo = 1 - (2/pi)*( (2*xmat/3).*((1 - xmat^2)^1.5) );
     hoo = hoo - (2/pi)*( xmat.*sqrt(1 - xmat^2) + arcsin(xmat)  );
  elseif kv == 5;
     hoo = 1 - 1.5*xmat + (1/2)*xmat^3 - (3/8)*xmat.*(1 - xmat^2)^2;
  else;
     "Table 1 only calculates the case for 5 variables or less";
      "Change global parameter kqopt to 1";
     end;
  endif;

 endif;

retp(hoo);
endp;


