## Calculating Statistics

### October 4, 2013

This looks like an assignment from a beginning programming class. I expect most beginning programmers would read the input into an array and compute the various statistics after the input is exhausted. Instead, we will read each line and build a frequency table of int/count pairs in an avl tree, then look up the various statistics in the tree:

`(define (read-file file-name)`

(with-input-from-file file-name

(lambda ()

(let ((t (make-dict <)))

(do ((x (read) (read))) ((eof-object? x) t)

(set! t (t 'update (lambda (k v) (+ v 1)) x 1)))))))

`(define (count t) (t 'fold (lambda (k v b) (+ v b)) 0))`

`(define (sum t) (t 'fold (lambda (k v b) (+ (* k v) b)) 0))`

`(define (mean t) (/ (sum t) (count t)))`

`(define (median t)`

(let ((m (car (t 'nth 0))) (med (/ (count t) 2)))

(t 'fold (lambda (k v b) (when (< (+ v b) med) (set! m k)) (+ v b)) 0)

m))

`(define (mode t)`

(let* ((x (t 'nth 0)) (m-key (car x)) (m-val (cdr x)))

(t 'for-each (lambda (k v)

(when (< m-val v) (set! m-key k) (set! m-val v))))

m-key))

`(define (minimum t) (car (t 'nth 0)))`

`(define (maximum t) (car (t 'nth (- (t 'size) 1))))`

Here we set up a test file and compute some statistics:

`> (with-output-to-file "test-stats"`

(lambda ()

(do ((i 25 (- i 1))) ((zero? i))

(display (randint 10)) (newline))))

> (define stats (read-file "test-stats"))

> (stats 'to-list)

((0 . 1) (1 . 3) (2 . 2) (3 . 3) (4 . 1)

(5 . 2) (6 . 3) (7 . 1) (8 . 5) (9 . 4))

> (count stats)

25

> (sum stats)

131

> (mean stats)

131/25

> (median stats)

5

> (mode stats)

8

> (minimum stats)

0

> (maximum stats)

9

We used avl trees from the Standard Prelude, and randint for testing. You can see the program at http://programmingpraxis.codepad.org/8gUf5Ye7.

The median for the list of numbers in the example is 6, not 5.

In Python.For the median I use: middle element if odd number of elements, else average of middle 2 elements.

Nothing special. Ruby.

It’s tricky to determine the “best” approach without some knowledge of the expected data.

Only a few dozen values, and all values are less than a thousand? Just read them all into an array of regular old integers, and do the math afterwards. The code will be simple, easy to read, and easy to maintain.

Tens of millions of values? You might want to do something more clever, like keep a running total of count, total, min and max, and then do some hashtables for frequency (for the mode) and .. I don’t know, maybe some clever post-processing with the frequency tables for the median.

Values could be > 4,294,967,295? Then you need to do some careful thinking. :0)

But if you can be reasonably confident the values will be small enough (and few enough) that their sum fits comfortably in a regular old integer, and if the code is unlikely to be run more than a few dozen times on any given day, then I’d suggest the seasoned veteran programmer would take the simple approach and reduce the maintenance burden.