Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Monte Carlo integration in Racket #781

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
(map #(* % %))
(reduce +))
(* r r)))

(defn rand-point [r]
"return a random point from (0,0) inclusive to (r,r) exclusive"
(repeatedly 2 #(rand r)))

(defn monte-carlo [n r]
"take the number of random points and radius return an estimate to
pi"
Expand All @@ -22,11 +24,12 @@ pi"
(if (in-circle? (rand-point r) r)
(inc count)
count))))))

(defn -main []
(let [constant-pi Math/PI
computed-pi (monte-carlo 10000000 2) ;; this may take some time on lower end machines
difference (Math/abs (- constant-pi computed-pi))
error (* 100 (/ difference constant-pi))]
(println "world's PI: " constant-pi
",our PI: " (double computed-pi)
",our PI: " (double computed-pi)
",error: " error)))
39 changes: 21 additions & 18 deletions contents/monte_carlo_integration/code/racket/monte_carlo.rkt
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#lang racket
(define (in_circle x y)
(< (+ (sqr x) (sqr y)) 1)
)
#lang racket/base

(define (monte_carlo_pi n)
(* (/ (local ((define (monte_carlo* n count)
(require racket/local)
(require racket/math)

(define (in-circle x y)
"Checks if a point is in a unit circle"
(< (+ (sqr x) (sqr y)) 1))

(define (monte-carlo-pi n)
"Returns an approximation of pi"
(* (/ (local ((define (monte-carlo-pi* n count)
(if (= n 0)
count
(monte_carlo_pi* (sub1 n)
(if (in_circle (random) (random))
(add1 count)
count
)
)
)
)) (monte_carlo_pi* n 0)
) n) 4)
)

(monte-carlo-pi* (sub1 n)
(if (in-circle (random) (random))
(add1 count)
count)))))
(monte-carlo-pi* n 0)) n) 4))

(display (monte_carlo_pi 1000))
(define nsamples 5000000)
(define pi-estimate (monte-carlo-pi nsamples))
(displayln (string-append "Estimate (rational): " (number->string pi-estimate)))
(displayln (string-append "Estimate (float): " (number->string (real->single-flonum pi-estimate))))
(displayln (string-append "Error:" (number->string (* (/ (abs (- pi-estimate pi)) pi) 100))))
4 changes: 2 additions & 2 deletions contents/monte_carlo_integration/monte_carlo_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ each point is tested to see whether it's in the circle or not:
{% sample lang="lua" %}
[import:2-4, lang="lua"](code/lua/monte_carlo.lua)
{% sample lang="racket" %}
[import:2-4, lang:"lisp"](code/racket/monte_carlo.rkt)
[import:6-8, lang:"racket"](code/racket/monte_carlo.rkt)
{% sample lang="scala" %}
[import:3-3, lang:"scala"](code/scala/monte_carlo.scala)
{% sample lang="lisp" %}
Expand Down Expand Up @@ -188,7 +188,7 @@ Feel free to submit your version via pull request, and thanks for reading!
{% sample lang="lua" %}
[import, lang="lua"](code/lua/monte_carlo.lua)
{% sample lang="racket" %}
[import, lang:"lisp"](code/racket/monte_carlo.rkt)
[import, lang:"racket"](code/racket/monte_carlo.rkt)
{% sample lang="scala" %}
[import, lang:"scala"](code/scala/monte_carlo.scala)
{% sample lang="lisp" %}
Expand Down