program sum (vector #0 v)
 {return v#(v-v+1); }

program mean (vector #0 v)
 {return sum(v)/length(v);}

program sumsq (vector #0 v)
 {return v#v;}

program variance (vector #0 v)
 {return evaluate(mean(v^2) - (mean(v))^2);}

program stdev (vector #0 v)
 {return sqrt(variance(v));}

program max (vector #0 v)
'MAXIMUM VALUE OF A VECTOR'
{let m -10000000;
  do
   i=1
   until i > length(v)
    returning m
   {if m < v(i)
     then
       {let m v(i);}
     let i i+1;
   }
}

program min (vector #0 v)
'MINIMUM VALUE OF A VECTOR'
{let m 10000000;
  do
   i=1
   until i > length(v)
    returning m
   {if m > v(i)
     then
       {let m v(i);}
     let i i+1;
   }
}


program leastsquare (vector #0 vecx) (vector #0 vecy)
'PRODUCES EQUATION OF LINE OF BEST FIT'
 {local (equation lsfit)
  {let sxy sum( (vecx-mean(vecx))*(vecy-mean(vecy)));
   let sxx sumsq(vecx-mean(vecx));
   let slope  sxy/sxx;
   let yint  mean(vecy) - slope*mean(vecx);
   let equation lsfit y = yint + slope*x;
    return lsfit;}
 }

program getr (equation eqn)
'USED IN RESIDUAL'
 {let variable right ;
   lisp ( SETQ RIGHT (CADDR EQN));
   return right; }

program residual (vector #0 vx)
'COMPUTES RESIDUALS'
(vector #0 vy)
{let equation lsfit leastsquare(vx,vy);
 let lsfn(x) value getr(lsfit);
 make r vy - lsfn(vx);}

command showresid
'DISPLAYS RESIDUALS'
   (vector #0 v1) (vector #0 v2)
{let vector #0 res residual(v1,v2);
  create "Residuals" "Graph2"  1  40 40 98 80  false;
  select "Residuals";
  penwidth 2;
let minabsc be min(v1)-.05*abs(min(v1));
let maxabsc be max(v1)+.05*abs(max(v1));
let minord be min(res)-.05*abs(min(res));
let maxord be max(res)+.05*abs(max(res));
  abscissa minabsc, maxabsc;
  ordinate minord,maxord;
  plot [v1,res]
 }




command put (vector #0 vec) (expression col) 
'ENTERS VECTOR vec INTO COLUMN col'
 { let le length(vec);
   do
    i=1
    until i > le
      {entry i col vec(i);
       let i i+1;}
   updatetable col;
 } 

