Skip to content

Instantly share code, notes, and snippets.

@no-defun-allowed
Created December 28, 2020 22:18
Show Gist options
  • Save no-defun-allowed/cc7215d60357be75db01a6d2a7a719bb to your computer and use it in GitHub Desktop.
Save no-defun-allowed/cc7215d60357be75db01a6d2a7a719bb to your computer and use it in GitHub Desktop.
someone whose only qualifications are getting 90% on a statistics test in high school investigates the Dream speedrun controversy
(declaim (optimize (speed 3) (safety 1)))
(defconstant +pearl-probability+ (float 20/423))
(declaim (inline trade))
(defun trade ()
(if (< (random 1.0) +pearl-probability+)
;; Note that RANDOM returns a value in [0, maximum), i.e.
;; it will only produce numbers below the maximum given.
(+ 4 (random 5))
0))
(defun simulation (&key (simulation-count 1000000)
(maximum-trades 300)
(target 12))
(declare (optimize (speed 3))
(fixnum simulation-count target))
(loop with trade-counts = (make-array maximum-trades
:initial-element 0.0
:element-type 'single-float)
repeat simulation-count
do (loop for pearl-count of-type fixnum = 0
then (+ pearl-count (trade))
for trades from 1 below maximum-trades
when (>= pearl-count target)
do (incf (aref trade-counts trades) 1.0)
(loop-finish))
finally (return (map 'vector
(lambda (x) (/ x simulation-count))
trade-counts))))
(defun compare-results (trade-vector
success-vector
simulation-results)
(labels ((probability-of-success (count)
"Compute the probability that we succeed in fewer than COUNT trades."
(reduce #'+ simulation-results :end count))
(probability-of-failure (count)
"Compute the probability that we fail in COUNT trades."
(- 1 (probability-of-success count))))
(loop for trades across trade-vector
for success? across success-vector
for pr = 1.0 then (* pr
(if success?
(probability-of-success trades)
(probability-of-failure trades)))
finally (return pr))))
(defvar *simulation* (time (simulation :simulation-count 1000000)))
(defvar *trades* #(22 5 24 18 4 1 7 12 26 8 5 20 2 13 10 10 21 20 10 3 18 3))
(defvar *successes* #(t nil nil t nil nil nil nil t t nil nil nil nil t t t t t nil t nil))
(format t "~r to one"
(round (/ (compare-results *trades* *successes* *simulation*))))
;; thirty-four billion forty-eight million one hundred seventy-six thousand one hundred twenty-eight to one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment