home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / xstats < prev   
Lisp/Scheme  |  2020-01-01  |  3KB  |  78 lines

  1. #!/usr/local/bin/wermit +
  2.  
  3. ; Linear regression using S-Expressions.
  4. ;
  5. ; Input:
  6. ;   A data file containing two columns of numbers.
  7. ; Output:
  8. ;   Means, minima, maxima, variances, standard deviations,
  9. ;   and linear correlation coefficient.
  10. ; Illustrates:
  11. ;   S-Expressions, floating-point arithmetic, file i/o, field extraction
  12. ; Requires:
  13. ;   C-Kermit 7.1 or K95 1.1.21 or later.
  14. ; Author:
  15. ;   F. da Cruz, Columbia University, Nov 2000
  16. ;
  17. if < \v(argc) 2 exit 1 Usage: \%0 filename
  18. if LLT \v(version) 701199 exit 1 Insufficient Kermit version
  19.  
  20. fopen /read \%c \fcontents(\%1)                ; Open the data file
  21. if fail exit 1                                 ; Check
  22.  
  23. xecho Reading...
  24.  
  25. (setq xsum 0 ysum 0 xsum2 0 ysum2 0 xysum 0 n 0)
  26. (setq t1 \v(ftime))
  27.  
  28. ; Loop through elements and accumulate sums, maxima, and minima
  29.  
  30. while not \f_eof(\%c) {
  31.     fread \%c line                              ; Read a line (record)
  32.     if fail break                               ; Check for EOF
  33.     if not def line continue                    ; Ignore empty lines
  34.     .x := \fword(\m(line),1)                    ; Extract X and Y
  35.     .y := \fword(\m(line),2)
  36.     if ( not float \m(x) || not float \m(y) ) { ; Verify record
  37.         exit 1 BAD DATA (line \m(n)): [\m(line)]
  38.     }
  39.     (if (== n 0) (setq xmin x ymin y xmax x ymax y)) ; Init max and mins
  40.     (++ n)                                      ; Count record
  41.     (setq xmax (max xmax x) ymax (max ymax y))  ; X and Y maxima
  42.     (setq xmin (min xmin x) ymin (min ymin y))  ; X and Y minima
  43.     (++ xsum x ysum y)                          ; X and Y sums
  44.     (++ xsum2 (^ x 2) ysum2 (^ y 2))            ; Sum of X and Y squares
  45.     (++ xysum (* x y))                          ; Sum of XY products
  46. }
  47. fclose \%c                                      ; Done reading - close file
  48.  
  49. ; Calculate results
  50.  
  51. (setq xmean (/ xsum n) ymean (/ ysum n))        ; Mean X and Y
  52. (setq xss (- xsum2 (/ (^ xsum 2) n)))           ; Intermediate values
  53. (setq yss (- ysum2 (/ (^ ysum 2) n)))
  54. (setq xvar (/ xss n) yvar (/ yss n))            ; X and Y variance
  55. (setq sdx (sqrt xvar) sdy (sqrt yvar))          ; Std deviation in X and Y
  56. (setq tmp (* xss yss))                          ; More intermediate values
  57. (setq xyss (- xysum (/ (* xsum ysum) n)))
  58. (setq cc (if tmp (/ xyss (sqrt tmp)) 1.0))      ; Correlation coefficient
  59.  
  60. ; Display the results
  61.  
  62. (setq t2 (- \v(ftime) t1))
  63. echo Done (\ffpround(t2,2) sec)
  64. echo Points: \m(n)
  65.  
  66. define xxout {
  67.   echo {\frpad(\%1:,16)\flpad(\ffprou(\%2,2),10)\%9\flpad(\ffprou(\%3,2),10)}
  68. }
  69. echo {                       X         Y}
  70. xxout Miminum xmin ymin
  71. xxout Maximum xmax ymax
  72. xxout Mean  xmean ymean
  73. xxout Variance xvar yvar
  74. xxout {Std Deviation} sdx sdy
  75. echo
  76. echo  Correlation coefficient: \flpad(\ffpround(cc,2),11)
  77. exit 0
  78.