Skip to content

Instantly share code, notes, and snippets.

@pkhuong
Last active August 2, 2019 20:40
Show Gist options
  • Select an option

  • Save pkhuong/2f947f9a3f9e51c2ec3133b5758deb5c to your computer and use it in GitHub Desktop.

Select an option

Save pkhuong/2f947f9a3f9e51c2ec3133b5758deb5c to your computer and use it in GitHub Desktop.
;; load https://github.com/pkhuong/csm
(defun player-one ()
(* (random 1d0) (random 1d0)))
(defun player-two ()
(expt (random 1d0) 2))
(defun player-one-wins ()
(> (player-one) (player-two)))
;;; Quick check to see what the ratio looks like
(loop repeat 1000 count (player-one-wins))
;; 438
;;; Now let's estimate player one's win rate in a statistically sound
;;; manner.
(csm:csm-driver (lambda (i) ;; Returns the truth value for the `i'th` data point
(declare (ignore i))
(player-one-wins))
0.5d0 1d-200 ;; We want to compare against P(True) = 0.5, with p < 1e-200.
:stream *standard-output*) ;; And log some stuff to stdout.
;; # obs win rate bounds on rate log confidence
1 0.000e+0 0.000e+0 1.000e+0 -4.339e-16
10 4.000e-1 0.000e+0 1.000e+0 -3.536e-1
20 4.500e-1 0.000e+0 1.000e+0 -5.269e-1
30 4.000e-1 0.000e+0 1.000e+0 -3.975e-1
40 4.000e-1 0.000e+0 10.000e-1 -3.699e-1
50 3.800e-1 0.000e+0 10.000e-1 -1.390e-1
60 3.833e-1 8.763e-10 10.000e-1 -9.248e-2
70 4.286e-1 7.594e-8 10.000e-1 -5.223e-1
80 4.250e-1 4.084e-7 10.000e-1 -4.701e-1
90 3.889e-1 5.255e-7 9.999e-1 7.963e-2
100 3.900e-1 1.888e-6 9.998e-1 1.438e-1
200 3.750e-1 4.125e-4 9.908e-1 1.675e+0
300 4.133e-1 5.001e-3 9.759e-1 8.189e-1
400 4.225e-1 1.374e-2 9.559e-1 8.862e-1
500 4.360e-1 2.660e-2 9.382e-1 5.281e-1
600 4.350e-1 3.796e-2 9.173e-1 9.129e-1
700 4.300e-1 4.783e-2 8.957e-1 1.660e+0
800 4.338e-1 6.007e-2 8.799e-1 1.701e+0
900 4.356e-1 7.128e-2 8.648e-1 1.873e+0
1000 4.320e-1 7.957e-2 8.479e-1 2.623e+0
2000 4.320e-1 1.498e-1 7.505e-1 6.501e+0
3000 4.280e-1 1.876e-1 6.952e-1 1.191e+1
4000 4.320e-1 2.176e-1 6.658e-1 1.441e+1
5000 4.396e-1 2.436e-1 6.496e-1 1.413e+1
6000 4.402e-1 2.593e-1 6.327e-1 1.691e+1
7000 4.444e-1 2.751e-1 6.231e-1 1.699e+1
8000 4.444e-1 2.851e-1 6.119e-1 1.969e+1
9000 4.452e-1 2.943e-1 6.034e-1 2.162e+1
10000 4.453e-1 3.016e-1 5.955e-1 2.414e+1
20000 4.485e-1 3.449e-1 5.552e-1 4.410e+1
30000 4.477e-1 3.625e-1 5.349e-1 6.935e+1
40000 4.471e-1 3.731e-1 5.226e-1 9.539e+1
50000 4.486e-1 3.822e-1 5.162e-1 1.129e+2
60000 4.464e-1 3.858e-1 5.081e-1 1.478e+2
70000 4.461e-1 3.899e-1 5.032e-1 1.749e+2
T ;; Differs from 0.5
T
0.4450143485995507d0 ;; Estimated win rate
77011 ;; # observations
34271 ;; # player 1 wins
0.39143475992053295d0 ;; 1 - 1e-200 CI for the player 1 win rate (bottom end)
0.49946392295016256d0 ;; " " (top end)
;;; Let's run for 10x more iterations to get a tighter CI.
(csm:csm-driver (lambda (i)
(declare (ignore i))
(player-one-wins))
0.5d0 1d-200 :min-count 770000)
;; T
;; T
;; 0.44424935064935067d0
;; 770000
;; 342072
;; 0.4271688302818859d0
;; 0.4614183786803768d0
;;; Looks like player 1's win rate is in [0.427, 0.462], or we found a
;;; flaw in Mersenne Twister.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment