home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / statistics < prev    next >
Text File  |  2020-01-01  |  4KB  |  88 lines

  1. #!/usr/local/bin/kermit +
  2. #
  3. # s t a t i s t i c s
  4. #
  5. # Given a file in which each line contains a pair of numbers, X and Y,
  6. # computes and prints the max, min, mean, variance, and standard deviation of
  7. # the X's and Y's, and the correlation coefficient of X and Y.  The numbers in
  8. # file may (but need not) have decimal points and fractional parts.
  9. #
  10. # Usage:       statistics <filename>
  11. # Requires:    C-Kermit 7.0 or later.
  12. # Illustrates: Floating-point computation and formatting, file i/o.
  13. #
  14. # Note that floating-point arithmetic, unlike integer arithmetic, is done with 
  15. # function calls.  The floating point function names all begin with "ffp".
  16. # Also note the use of \fcontent() in the FOPEN statement; this to allow for
  17. # DOS filenames with backslash path-separators.
  18. #
  19. # Author: F. da Cruz, The Kermit Project, Columbia University, July 1999.
  20. # Version 2: Adds IF FLOAT checking.
  21. #
  22. if < \v(argc) 2 exit 1 Usage: \%0 filename  ; A filename is required
  23.  
  24. ; Output function for X and Y statistics.
  25. ; Args: Label, X value, Y value.
  26. ;
  27. define xxout {
  28.   echo {\frpad(\%1:,16)\flpad(\ffprou(\%2,2),10)\%9\flpad(\ffprou(\%3,2),10)}
  29. }
  30.  
  31. fopen /read \%c \fcontents(\%1)         ; Open the data file
  32. if fail exit 1                          ; Check
  33. .\%n = 0                                ; Number of X,Y pairs
  34. .x:sum = 0                              ; Initialize sums
  35. .y:sum = 0                              ; (not strictly necessary)
  36. .x:sum2 = 0
  37. .y:sum2 = 0
  38. .xy:sum = 0
  39.  
  40. while not \f_eof(\%c) {                 ; Until end of input file...
  41.     fread \%c line                      ; Read a line (record)
  42.     if fail break                       ; Check
  43.     if not def line continue            ; Ignore empty lines
  44.     .\%x := \fword(\m(line),1,{ })      ; Extract X and Y from the record
  45.     .\%y := \fword(\m(line),2,{ })
  46.     if ( not ( float \%x && float \%y ) ) exit 1 Bad record: "\m(line)"
  47.     .x:sum := \ffpadd(\m(x:sum),\%x)    ; Get sums and check validity
  48.     .y:sum := \ffpadd(\m(y:sum),\%y)
  49.     if = \%n 0 { .x:max := \%x, .x:min := \%x, .y:max := \%y, .y:min := \%y }
  50.     incr \%n                            ; Count this record
  51.     .x:max  := \ffpmax(\m(x:max),\%x)   ; Accumulate max, min, other stats
  52.     .x:min  := \ffpmin(\m(x:min),\%x)
  53.     .y:max  := \ffpmax(\m(y:max),\%y)
  54.     .y:min  := \ffpmin(\m(y:min),\%y)
  55.     .x:sum2 := \ffpadd(\m(x:sum2),\ffpmult(\%x,\%x)) ; Sum of squares (X)
  56.     .y:sum2 := \ffpadd(\m(y:sum2),\ffpmult(\%y,\%y)) ; Sum of squares (Y)
  57.     .xy:sum := \ffpadd(\m(xy:sum),\ffpmult(\%x,\%y)) ; Sum of products (XY)
  58. }
  59. fclose \%c                              ; Finished reading data - close file
  60. echo
  61. echo Records: \%n
  62. if < \%n 2 exit 1 {Sorry, at least two data pairs are required.}
  63.  
  64. ; Compute means, intermediate quantities, variances.
  65. ;
  66. .x:mean := \ffpdiv(\m(x:sum),\%n)
  67. .y:mean := \ffpdiv(\m(y:sum),\%n)
  68. .x:ss   := \ffpsub(\m(x:sum2),\ffpdiv(\ffpmult(\m(x:sum),\m(x:sum)),\%n))
  69. .y:ss   := \ffpsub(\m(y:sum2),\ffpdiv(\ffpmult(\m(y:sum),\m(y:sum)),\%n))
  70. .xy:ss  := \ffpsub(\m(xy:sum),\ffpdiv(\ffpmult(\m(x:sum),\m(y:sum)),\%n))
  71. .x:var  := \ffpdiv(\m(x:ss),\%n)
  72. .y:var  := \ffpdiv(\m(y:ss),\%n)
  73.  
  74. ; Print the results
  75.  
  76. echo {                       X         Y}
  77. xxout Sum \m(x:sum) \m(y:sum)
  78. xxout Minimum \m(x:min) \m(y:min)
  79. xxout Maximum \m(x:max) \m(y:max)
  80. xxout Mean \m(x:mean) \m(y:mean)
  81. xxout Variance \m(x:var) \m(y:var)
  82. xxout {Std deviation} \ffpsqrt(\m(x:var)) \ffpsqrt(\m(y:var))
  83. echo
  84. echo  Correlation coefficient:   -
  85. \ffpround(\ffpdiv(\m(xy:ss),\ffpsqrt(\ffpmul(\m(x:ss),\m(y:ss)))),2)
  86.  
  87. exit 0
  88.