Joshua Cole

Probability Confuses

July 19, 2011

When your intuition disagrees with the math on a probability problem, simulate. A few lines of code settles arguments that have stumped PhDs, doctors, and sports fans alike. The Monty Hall problem is the famous case: even people with the highest recorded IQs drew fire from credentialed experts for getting the right answer.

Probability problems have been confusing brilliant people since Pascal and Fermat first disagreed about how to divide a pot. The pattern repeats. Here is a problem from Introduction to Algorithms:

A deck of 10 cards, each bearing a distinct number from 1 to 10, is shuffled to mix the cards thoroughly. Three cards are removed one at a time from the deck. What is the probability that the three cards are selected in sorted (increasing) order?

The answer is 1/6: for any set of three cards there are six possible orderings, and exactly one is sorted. But twenty manual deals might not show a single hit, which is enough to convince someone watching that 1/6 is wrong. Small samples are treacherous.

Six thousand draws tell a different story:

;; A deck of ten cards each bearing a distinct number
;; between one and ten are shuffled in order to mix the
;; cards thoroughly. Three cards are removed from the
;; deck one at a time. What is the probability that the
;; three cards are selected in sorted order?

;; First let's create the deck…
(def cards (range 1 11)) ;; > [1 2 3 … 9 10]

;; Next we draw cards from a shuffled deck by
;; using shuffle and take
(defn draw-cards [] (take 3 (shuffle cards)))

;; Testing this function in the REPL we get…
;; user> (draw-cards)
;; (2 7 8)
;; user> (draw-cards)
;; (9 8 4)

;; Now we need a way to tell whether or not the cards
;; are in sorted order. We can use a comparison
;; operator to do that pretty simply
(defn ascending? [draws] (apply < draws))

;; Now let's say we try drawing a few thousand times…
;; How frequently will the cards be in ascending order?

(count (filter ascending? (repeatedly 6000 draw-cards)))
;; 1027

1027 out of 6000 is about 17% – close enough to 1/6 that the math checks out. Simulation is the cheapest way to calibrate probabilistic intuition. Write the ten-line program before you argue.

Found this useful? Share it with someone who might benefit.