(program (define SUM (V) (program  (eval {V#(V- V+ 1)})

 ))  2, SUM, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , V,  , 0 ; SUM(V) ; PROGRAM SUM (VECTOR #0 V)
 {RETURN V#(V-V+1); } |
 (define MEAN (V) (program  (eval {SUM(V)/length(V)})

 ))  2, MEAN, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , V,  , 0 ; MEAN(V) ; PROGRAM MEAN (VECTOR #0 V)
 {RETURN SUM(V)/LENGTH(V);} |
 (define SUMSQ (V) (program  (eval {V#V})

 ))  2, SUMSQ, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , V,  , 0 ; SUMSQ(V) ; PROGRAM SUMSQ (VECTOR #0 V)
 {RETURN V#V;} |
 (define VARIANCE (V) (program  (eval {eval(MEAN(V^2) - (MEAN(V))^2)})

 ))  2, VARIANCE, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , V,  , 0 ; VARIANCE(V) ; PROGRAM VARIANCE (VECTOR #0 V)
 {RETURN EVALUATE(MEAN(V^2) -(MEAN(V))^2);} |
 (define STDEV (V) (program  (eval {sqrt(VARIANCE(V))})

 ))  2, STDEV, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , V,  , 0 ; STDEV(V) ; PROGRAM STDEV (VECTOR #0 V)
 {RETURN SQRT(VARIANCE(V));} |
 (define MAX (V) (program (make M {-10000000 } ) 
 (loop (I {1 } ) (UNTIL {I > length(V) } ) (do (program (if {M < V(I) } THEN (program (make M {V(I) } ) 
 )) 
 (make I {I+ 1 } ) 
 )) (result {M }))
 ))  2, MAX, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , V,  , 0 ; MAX(V) ; PROGRAM MAX (VECTOR #0 V)

{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;
   }
} |
 (define MIN (V) (program (make M  10000000  ) 
 (loop (I {1 } ) (UNTIL {I > length(V) } ) (do (program (if {M > V(I) } THEN (program (make M {V(I) } ) 
 )) 
 (make I {I+ 1 } ) 
 )) (result {M }))
 ))  2, MIN, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , V,  , 0 ; MIN(V) ; PROGRAM MIN (VECTOR #0 V)

{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;
   }
} |
 (define LEASTSQUARE (VECX VECY) (program (let (LSFIT LSFIT ) (program (make SXY {SUM( (VECX- MEAN(VECX))*(VECY- MEAN(VECY))) } ) 
 (make SXX {SUMSQ(VECX- MEAN(VECX)) } ) 
 (make SLOPE {SXY/SXX } ) 
 (make YINT {MEAN(VECY) - SLOPE*MEAN(VECX) } ) 
 (make LSFIT {Y & YINT + SLOPE*X } ) 
  (eval LSFIT)

 )) 
 ))  2, LEASTSQUARE, 2, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , VECX,  , 0 ; 0, 1, 0,  , VECY,  , 0 ; LEASTSQUARE(VECX,VECY) ; PROGRAM LEASTSQUARE (VECTOR #0 VECX) (VECTOR #0 VECY)

 {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;}
 } |
 (define GETR (EQN) (program ((lambda (!x) (set !x !x)) 'RIGHT) 
 ( SETQ RIGHT (CADDR EQN))
  (eval RIGHT)

 ))  2, GETR, 1, 2,  0, 9, 0,  , retval,  , 0 ; 0, 4, 0,  , EQN,  , 0 ; GETR(EQN) ; PROGRAM GETR (EQUATION EQN)

 {LET VARIABLE RIGHT ;
   LISP ( SETQ RIGHT (CADDR EQN));
   RETURN RIGHT; } |
 (define RESIDUAL (VX VY) (program (make LSFIT {LEASTSQUARE(VX,VY) } ) 
 (make LSFN (buildf '(X) {GETR(LSFIT) } )) 
 (make R {VY - LSFN(VX) } ) 
 ))  2, RESIDUAL, 2, 2,  0, 9, 0,  , retval,  , 0 ; 0, 1, 0,  , VX,  , 0 ; 0, 1, 0,  , VY,  , 0 ; RESIDUAL(VX,VY) ; PROGRAM RESIDUAL (VECTOR #0 VX)

(VECTOR #0 VY)
{LET EQUATION LSFIT LEASTSQUARE(VX,VY);
 LET LSFN(X) VALUE GETR(LSFIT);
 MAKE R VY -LSFN(VX);} |
  8, SHOWRESID, 2,  0, 1, 0,  , V1,  , 0 ; 0, 1, 0,  , V2,  , 0 ;  : (program (make RES {RESIDUAL(V1,V2) } ) 
 (createwindow "Residuals" "Graph2" {1} {40} {40} {98} {80} nil)

 (select "Residuals")

 (penwidth 2)

 (make MINABSC {MIN(V1)- .05*abs(MIN(V1)) } ) 
 (make MAXABSC {MAX(V1)+ .05*abs(MAX(V1)) } ) 
 (make MINORD {MIN(RES)- .05*abs(MIN(RES)) } ) 
 (make MAXORD {MAX(RES)+ .05*abs(MAX(RES)) } ) 
 (abscissa (eval {MINABSC}) (eval MAXABSC))

 (ordinate (eval {MINORD}) (eval MAXORD))

 (plot (vector (eval ({vector[V1,RES]
 }  1)) (eval ({vector[V1,RES]
 }  2)) )) 
 ); COMMAND SHOWRESID (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]
 } |
  8, PUT, 2,  0, 1, 0,  , VEC,  , 0 ; 0, 0, 0,  , COL,  , 0 ;  : (program (make LE {length(VEC) } ) 
 (loop (I {1 } ) (UNTIL {I > LE } ) (do (program (setentry {I} {COL} (eval {VEC(I)}))

 (make I {I+ 1 } ) 
 )) (result NIL))
  (updatedata COL)

 ); COMMAND PUT (VECTOR #0 VEC) (EXPRESSION COL) 

 { LET LE LENGTH(VEC);
   DO
    I=1
    UNTIL I > LE
      {ENTRY I COL VEC(I);
       LET I I+1;}
   UPDATETABLE COL;
 } |
 )