The Iron Bar
October 6, 2015
Our experiment will perform n trials, each of a list of 1001 numbers from 1 to 100, comparing the iron-bar median to the actual median determined by sorting:
(define (rand-list n) (let loop ((n n) (xs (list))) (if (zero? n) xs (loop (- n 1) (cons (+ (randint 100) 1) xs))))) (define (iron-bar n) (do ((i 1 (+ i 1))) ((< n i)) (let* ((xs (rand-list 1001)) (actual-median (list-ref (sort < xs) 500))) (let loop ((med (car xs)) (xs (cdr xs))) (if (null? xs) (for-each display `(,i ": actual median = " ,actual-median ", iron-bar median = " ,med #\newline)) (loop (+ (if (< (car xs) med) -1 1) med) (cdr xs)))))))
And here are some tests of the iron bar:
> (iron-bar 10) 1: actual median = 48, iron-bar median = 49 2: actual median = 53, iron-bar median = 51 3: actual median = 51, iron-bar median = 50 4: actual median = 51, iron-bar median = 50 5: actual median = 52, iron-bar median = 52 6: actual median = 54, iron-bar median = 53 7: actual median = 51, iron-bar median = 54 8: actual median = 50, iron-bar median = 59 9: actual median = 52, iron-bar median = 48 10: actual median = 51, iron-bar median = 47
That’s not bad. Most of the results are within 1 or 2 of the correct answer. Trial 8 is quite bad: that happens when there is a long streak near the end of the data.
We used the random number generator in the Standard Prelude. You can run the program at http://ideone.com/jD90tT.
Haskell:
Scheming with Gambit and a biased distribution. Stealing signum from from above.
Thinking the result seems more or less as it probably should.
Maybe I need more coffee and to read more carefully, but isn’t this just a Beta-Bernoulli setup from Bayesian statistics?
@Graham: I rather thought the whole article was a joke. I actually checked to be sure it wasn’t published April 1 and somebody reposted it late. They even use jargon: volume-conserving, TOW dynamics, quantum dots. And they really lost me when they claimed their iron bar can compute the probabilities of winning on a slot machine that it doesn’t even play. At least I managed to get a decent exercise out of it, and maybe even amuse some of my readers.
A one-liner in J:
> median =: [ + (* @: -)~
Another Haskell version, somewhat golfed.
Dang. Confused median with mean. Second try: