Monte Carlo Methods Monte Carlo Methods: Fall 2010

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Monte Carlo Methods

I, at any rate, am convinced that He does not throw dice. Albert Einstein
1

Fall 2010

Pseudo Random Numbers: 1/3


Random numbers are numbers occur in a random way. y are generated g by y an algorithm, g , they y are If they not actually very random. Hence, they are usually referred to as pseudo random numbers. In Fortran 90, two subroutines help generate random numbers: RANDOM_SEED() and RANDOM_NUMBER(). The generated random numbers are uniform because the probability to get each of these numbers is equal.
2

Pseudo Random Numbers: 2/3


RANDOM_S SEED() () must be called, , with or without actual arguments, before any use of RANDOM_NUMBER() () or before y you wish to re-seed the random number sequence. RANDOM_NUMBER(x) RANDOM NUMBER(x) takes a REAL actual argument, which is a variable or an array element The generated random number is element. returned with this argument. The Th generated t d random d number b i is i in [0,1). [0 1) Scaling and translation may be needed.
3

Pseudo Random Number: 3/3


Simulate the throwing of two dice n times. Array count() of 12 elements is initialized to 0, and p and q are the random random numbers representing throwing two dice. What does INT(6*x)+1 INT(6* )+1 mean?
Count(0) is always 0. CALL RANDOM_SEED() Why? y DO i = 1, 1 n CALL RANDOM_NUMBER(x) p = INT(6*x) + 1 CALL RANDOM_NUMBER(x) RANDOM NUMBER( ) q = INT(6*x) + 1 count(p+q) = count(p+q) + 1 END DO 4 WRITE(*,*) (count(i), i=1, 12)

Monte Carlo Methods


Monte Carlo techniques have their origin in WW2. Scientists found out problems in neutron diffusion were intractable by conventional methods and a probabilistic approach was developed. Then, it was found that this probabilistic approach could be used to solve deterministic problems. In particular, it is useful in evaluating integrals of multiple dimensions. dimensions

Computing : 1/3
The unit circle (i.e i e., radius = 1) has an area of . Consider the area in the first quadrant as shown h b below. l It Its area i is /4 0.785398 0 785398 If we generate n pairs of random numbers (x,y), representing n points in the unit square, and count the pairs in the circle, say k, the area is approximately k/n.

Computing : 2/3
In the following, g, n is the number of random number pairs to be generated, count counts the number of p pairs in the circle, , and r is the ratio. Hence, r /4 if enough number of (x,y) pairs are generated. generated
count = 0 CALL RANDOM_SEED DO i = 1, n CALL RANDOM_NUMBER(x) _ ( ) CALL RANDOM_NUMBER(y) IF (x*x + y*y < 1.0) count = count + 1 END DO r = REAL(count)/n

Computing : 3/3
The following shows some results. Due to randomness, the results may be different if this program is run again. again
n 10 100 1000 10000 100000 1000000 in circle 9 72 804 7916 78410 785023 ratio 0.9 0 72 0.72 0.804 0.7916 0.7841 0.7850 ratio /4 = 0.785398 0 785398

Integration: 1/2
The same idea can be applied to integration. Let us integrate 1/(1+x2) on [0,1]. This function is bounded by the unit square square. We may generate n random number pairs and count t the th number b of f pairs i k in i the th area t to b be integrated. The ratio k/n is approximately the i t integration. ti
1
f ( x) =

1 1 1 0 1 + x 2 dx = tan (1) tan (0) = 4


1
1 1 + x2
9

Integration: 2/2
The following shows the results.
ratio /4 = 0.785398 0 785398 n in area ratio 10 9 09 0.9 100 77 0.77 1000 781 0.781 10000 7940 0.794 100000 78646 0.786 1000000 784546 0.785
10

CALL RANDOM_SEED() count = 0 DO i = 1, n CALL RANDOM_NUMBER(x) CALL RANDOM_NUMBER(y) (y) fx = 1/(1 + x*x) IF (y <= fx) count=count+1 END DO r = REAL(count)/n

Buffon Needle Problem: 1/4


Suppose the floor is divided into infinite number of parallel lines with a constant gap G. If we throw a needle of length L to the floor randomly, what is the probability of the needle crossing a dividing line? This is the Buffon needle problem. The exact probability b bilit is i (2/)(L/G). ) If L = G = 1, the probability is 2/ 0.63661.
G G L
11

Buffon Needle Problem: 2/4


We need two random numbers: for the angle between the needle and a dividing line, and d the distance from one tip of the needle to the lower dividing line. If d+Lsin() is less than 0 or larger than G, the needle crosses a dividing line.
d+Lsin()

L G
d

Lsin()
12

Buffon Needle Problem: 3/4


g gap p and length g are g gap p and needle length. g The generated random number is scaled by gap and the angle by 2.
count = 0 DO i = 1, n CALL RANDOM_NUMBER(x) distance = x*gap g p ! distance in [0,gap) g p CALL RANDOM_NUMBER(angle) angle = angle*2*PI ! angle in [0,2) total = distance + length length*sin(angle) sin(angle) IF (0 < total .AND. total < gap) count = count + 1 END DO ratio = REAL(n-count)/n REAL(n count)/n
13

Buffon Needle Problem: 4/4


The following has the simulated results with gap and needle length being 1.
Exact value = 2/ 0.63661.

n 10 100 1000 10000 100000 1000000

in area ratio 8 0.8 61 0.61 631 0.631 6340 0.634 0 634 63607 0.63607 636847 0.63685 0 63685

14

The End

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy