Skip to content

Instantly share code, notes, and snippets.

@shirok
Created March 15, 2019 13:16
Show Gist options
  • Save shirok/7d4d08ef4ea4655da474fb1991cecbd0 to your computer and use it in GitHub Desktop.
Save shirok/7d4d08ef4ea4655da474fb1991cecbd0 to your computer and use it in GitHub Desktop.
(use srfi-27)
(use srfi-42)
(use gauche.sequence)
(define *n* 10000)
(define *points* (make-vector *n* 0))
(define (reset init)
(set! *points* (make-vector *n* init)))
(define (toss)
(dotimes [i *n*]
(if (zero? (random-integer 2))
(dec! (~ *points* i))
(inc! (~ *points* i)))))
(define (bet)
(dotimes [i *n*]
(if (zero? (random-integer 2))
(update! (~ *points* i) (cut * <> 0.95))
(update! (~ *points* i) (cut * <> 1.05)))))
(define (histo)
(define min (floor->exact (find-min *points*)))
(define max (ceiling->exact (find-max *points*)))
(define h (make-vector (- max min -1) 0))
(dotimes [i *n*]
(inc! (~ h (- (round->exact (~ *points* i)) min))))
(do-ec (: y (index x) h)
(print (+ x min) "\t" y)))
;;
;; Run coin-toss
;;
(define (run-toss)
(reset 0)
(dotimes [10] (toss))
(with-output-to-file "after-10-toss" (cut histo))
(dotimes [90] (toss))
(with-output-to-file "after-100-toss" (cut histo))
(dotimes [900] (toss))
(with-output-to-file "after-1000-toss" (cut histo))
)
#|
set terminal png size 640,400
set output 'cointoss.png'
set style data boxes
plot [-100:100] \
'after-10-toss' using (column(1)-0.5):2 title "After 10 tosses", \
'after-100-toss' using (column(1)-0.0):2 title "After 100 tosses", \
'after-1000-toss' using (column(1)+0.5):2 title "After 1000 tosses", \
|#
;;
;; Run point-bet
;;
(define (run-bet)
(reset 10)
(dotimes [10] (bet))
(with-output-to-file "after-10-bet" (cut histo))
(dotimes [90] (bet))
(with-output-to-file "after-100-bet" (cut histo))
(dotimes [900] (bet))
(with-output-to-file "after-1000-bet" (cut histo))
)
#|
set terminal png size 640,400
set output 'coinbet.png'
set style data boxes
plot [0:100] \
'after-10-bet' using (column(1)-0.5):2 title "After 10 bets", \
'after-100-bet' using (column(1)-0.0):2 title "After 100 bets", \
'after-1000-bet' using (column(1)+0.5):2 title "After 1000 bets", \
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment