Skip to content

Instantly share code, notes, and snippets.

@saludes
Last active November 27, 2019 12:40
Show Gist options
  • Save saludes/e3fe0617191cda52a98995c3897f701b to your computer and use it in GitHub Desktop.
Save saludes/e3fe0617191cda52a98995c3897f701b to your computer and use it in GitHub Desktop.
Computing the probability that the rounding of y/x be an even number, where x, y are uniformly distributed on [0,1].
#lang racket
(require plot)
(define (ct p)
(match-define (vector x y) p)
(/ y x))
(define (f g)
(λ (p)
(zero? (modulo (inexact->exact (g (ct p))) 2))))
(define p (/ (- 5 pi) 4))
(define (ps N)
(for/list ([k (in-range N)])
(vector (random) (random))))
; do
; > (run)
; to run an experiment
(define (run [N (expt 10 6)])
(define g round)
(define margin 100)
(define p*
(/
(length (filter (f g) (ps N))) N))
(define ϵ (* margin (abs (- p* p))))
(displayln (/ ϵ margin))
(chebyshev ϵ N))
(define (chebyshev ϵ m)
(- 1 (/ (* p (- 1 p)) (* m ϵ ϵ))))
; > (run-plot)
; plot the points of a run
(define (run-plot [N (expt 10 6)])
(plot
(points
(filter (f round) (ps N))
#:sym 'dot)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment