Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Monte-Carlo-methods/Racket/monte-carlo-methods-1.rkt
Normal file
32
Task/Monte-Carlo-methods/Racket/monte-carlo-methods-1.rkt
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#lang racket
|
||||
|
||||
(define (in-unit-circle? x y) (<= (sqrt (+ (sqr x) (sqr y))) 1))
|
||||
;; point in ([-1,1], [-1,1])
|
||||
(define (random-point-in-2x2-square) (values (* 2 (- (random) 1/2)) (* 2 (- (random) 1/2))))
|
||||
|
||||
;; Area of circle is (pi r^2). r is 1, area of circle is pi
|
||||
;; Area of square is 2^2 = 4
|
||||
;; There is a pi/4 chance of landing in circle
|
||||
;; .: pi = 4*(proportion passed) = 4*(passed/samples)
|
||||
(define (passed:samples->pi passed samples) (* 4 (/ passed samples)))
|
||||
|
||||
;; generic kind of monte-carlo simulation
|
||||
(define (monte-carlo run-length report-frequency
|
||||
sample-generator pass?
|
||||
interpret-result)
|
||||
(let inner ((samples 0) (passed 0) (cnt report-frequency))
|
||||
(cond
|
||||
[(= samples run-length) (interpret-result passed samples)]
|
||||
[(zero? cnt) ; intermediate report
|
||||
(printf "~a samples of ~a: ~a passed -> ~a~%"
|
||||
samples run-length passed (interpret-result passed samples))
|
||||
(inner samples passed report-frequency)]
|
||||
[else
|
||||
(inner (add1 samples)
|
||||
(if (call-with-values sample-generator pass?)
|
||||
(add1 passed) passed) (sub1 cnt))])))
|
||||
|
||||
;; (monte-carlo ...) gives an "exact" result... which will be a fraction.
|
||||
;; to see how it looks as a decimal we can exact->inexact it
|
||||
(let ((mc (monte-carlo 10000000 1000000 random-point-in-2x2-square in-unit-circle? passed:samples->pi)))
|
||||
(printf "exact = ~a~%inexact = ~a~%(pi - guess) = ~a~%" mc (exact->inexact mc) (- pi mc)))
|
||||
28
Task/Monte-Carlo-methods/Racket/monte-carlo-methods-2.rkt
Normal file
28
Task/Monte-Carlo-methods/Racket/monte-carlo-methods-2.rkt
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#lang racket
|
||||
(define (in-unit-circle? x y) (<= (sqrt (+ (sqr x) (sqr y))) 1))
|
||||
;; Good idea made in another task that:
|
||||
;; The proportions of hits is the same in the unit square and 1/4 of a circle.
|
||||
;; point in ([0,1], [0,1])
|
||||
(define (random-point-in-unit-square) (values (random) (random)))
|
||||
;; generic kind of monte-carlo simulation
|
||||
;; Area of circle is (pi r^2). r is 1, area of circle is pi
|
||||
;; Area of square is 2^2 = 4
|
||||
;; There is a pi/4 chance of landing in circle
|
||||
;; .: pi = 4*(proportion passed) = 4*(passed/samples)
|
||||
(define (passed:samples->pi passed samples) (* 4 (/ passed samples)))
|
||||
|
||||
(define (monte-carlo/2 run-length report-frequency sample-generator pass? interpret-result)
|
||||
(interpret-result
|
||||
(for/fold ((pass 0))
|
||||
([n (in-range run-length)]
|
||||
#:when (when (and (not (zero? n)) (zero? (modulo n report-frequency)))
|
||||
(printf "~a samples of ~a: ~a passed -> ~a~%"
|
||||
n run-length pass (interpret-result pass n)))
|
||||
#:when (call-with-values sample-generator pass?))
|
||||
(add1 pass))
|
||||
run-length))
|
||||
|
||||
;; (monte-carlo ...) gives an "exact" result... which will be a fraction.
|
||||
;; to see how it looks as a decimal we can exact->inexact it
|
||||
(let ((mc (monte-carlo/2 10000000 1000000 random-point-in-unit-square in-unit-circle? passed:samples->pi)))
|
||||
(printf "exact = ~a~%inexact = ~a~%(pi - guess) = ~a~%" mc (exact->inexact mc) (- pi mc)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue