Anda di halaman 1dari 33

Monte Carlo

Simulation
Fawaz
Dr. A.
hrahsheh
Departm
obeidat
ent of
physics
just

History

hat is Monte Carlo (MC) method ?

Monte Carlo method :is a numerical meth


atistical simulation which utilizes sequen
random numbers to perform the simulati

What the meaning of MC


simulation?

MC simulation is a versatile tool to analys


and evaluate complex measurements
Constructing a model of a system.

Experimenting with the model to


draw inferences of the systems behavi

A simulation model

outputs

Inputs
Decision and
uncontrollable
variables

Measures of
Simulati

performance

on

or behaviour of

model

the system

A simulation model cont..


Model inputs capture the environment of the
problem
The simulation model
Conceptual model: set of assumptions that define
the system
Computer code: the implementation of the
conceptual model
Outputs describe the aspects of system behaviour
that we are interested in

Random numbers
Uniform Random numbers or pseudorandom numbers (PRN) are essentially
independent random variables uniformly
Distributed over the unit interval (0,1).
The

PRNs are good if they are uniformly


distributed,
statistically independent and reproducible.

Linear congruential
generator
Generating a random sequence of numbers
{X1,X2,.,Xk} of length M over the interval
[0,M-1]
Xi=mod(AXi-1+C,M)
R=Xi/M
mod(b,M)=b-int(b/M)*M
Starting value X0 is called seed
M,A and C are nonnegative integers known
Modulus, multiplier and increment, respectively
M is must be prime number(2-1,2-1,..)

Fortran program
program random_num
implicit none
integer,parameter::m=2**21,a=17,c=43
integer::i,n,s
real(8)::R,Xo
read*,n
!n is the number of random points
Xo=27.0d0
!-----------------------do i=1,n
s=A*Xo+c
Xo=mod(s,m)
R=Xo/m
write(*,*) R
end do
!-----------------------end program random_num

Classic Example

Find the value of


?
e the reject and accept method
Or hit and miss method

he area of square=(2r)

The area of circle =


r
area of square 4r 4

area of circle r
2

area of circle
4*
area of square

area.of .circle
area.of .square

Cont
#.of .dots.inside.circle
....

total .number.of .dots

Hit and miss algorithm


Generate two sequences of N of PRN :: Ri,,Rj
Xi=-1+2R i
Yj=-1+2R j
Start from s=zero
If (X+Y<1) s=s+1
# of dots inside circle=s
total number of dots=N

4*S / N

Fortran program
PROGRAM Pi
implicit none
integer,parameter::mm=(2**17)-1,aa=(2**7)-1
REAL(8)::X, Y, Pi
Real(8),dimension(:),allocatable::R
INTEGER::I,N
Pi = 0.0d0
READ*, N
allocate(R(n))
DO I = 1,N
CALL RANDOM_NUMBER(R(n))
CALL RANDOM_NUMBER(R(n+1))
X =-1.0d0+2.0d0*R(n)
Y=-1.0d0+2.0d0*R(N+1)
IF (X*X+Y*Y<1.0d0) Pi=Pi+1.0d0
END DO
Pi=4*Pi/N
PRINT*,Pi
END program pi

Monte Carlo Integration

Hit and miss


method
Sample sampled
mean
mportance
method
method

Hit and Miss method


b

I f ( x ) dx
a

a, b R

erate two sequence of N of PRN i&


(Rj=1,2,.,N
i,R j)

0 f(x) Ymax ,for


X i (b-a)
(a,b)
Xi=a+R
Yi=Ymax R j
start from s=0
if Yj<f(x) s=s+1

I=Ymax(b-a) S/N

program Hit_miss
implicit none
real(8),dimension(:),allocatable::R
real(8)::X,Y,m,a,b,integ
integer::i,N,s
read*,a !the lower value of the interval
read*,b !the upper value of the interval
M=f(b)
if (dabs(f(a))>dabs(f(b))) M=f(a)
read*,N
!the number of random numbers
allocate(R(n))
s=0
do i=1,N
call random_number(R(n))
call random_number(R(n+1))
X=a+R(n)*(b-a)
Y=M*R(n+1)
if (y<f(x)) s=s+1
end do
INTEG=M*(b-a)*s/N
print*,integ
contains
real(8) function F(X)
real(8),intent(in)::X
F=2.0*X+1.0
end function F
end program Hit_miss

Cont.

ere is a part of the function under X-axis


b

f ( x)dx
s area.under.curve

N
Total .area
( M R)(b a )
a

s
f ( x)dx ( M R)(b a)
N
b

now be corrected for the fact that the line y =


d as the baseline for the integral instead of th
is is accomplished by subtracting the rectang
a).
s
then f ( x)dx ( M R)(b a) R(b a)
b

Types of distribution

Uniform distribution Gaussian or normal distribut

Sample Mean method


Rewrite I

Where
( xp.d.f
)0

f ( x ) dx
a

By

I h( x) ( x)dx
a

is
b

( x ) dx 1
a

h( x ) f ( x ) ( x )

Theorem.
If x1,x2,x3,..,xN are i,i,d uniformly
1
distributed on [a,b],then
f f (x )
I f ( x)dx (b a ) f ,
b

i 1

Cont

1
choose ( x) b a

h(and
x) (b a ) f ( x)

m the theorem
hen an estimate of I is
(b a )

I
f (x )
N
N

i 1

an calculate the value of error from the varian


var(I)

error

F(x)
f

(b a )
var( I)
var( f )
N
2

var( f ) f f
2

1
1
f ( x ) [ f ( x )]
N
N
N

i 1

i 1

Sample Mean MC
algorithm
Generate sequence of N of PRN : Ri

Compute Xi=a+Ri (b-a)


compute f(Xi) , i=1,2,3,.,N
use I (b a ) f ( x )
N

i 1

te:: if f(x) is not square integrable ,then


mate will still converge to the true value
The error estimate becomes unreliable.

program Sampled_Mean
implicit none
real(8),dimension(:),allocatable::R
real(8)::a,b,sum,integ,X
integer::i,N
read*,a ! Lower value
read*,b ! upper value
read*,n ! number of random points
allocate(R(n))
sum=0.0d0
do i=1,n
call random_number(R(n))
call random_number(R(n+1))
X=a+R(n)*(b-a)
sum=sum+f(x)
int=((b-a)/N)*sum
end do
write(*,*) "integ=",integ
contains
real(8) function F(X)
real(8),intent(in)::X
F=2*X+1.0d0
end function F
end program Sampled_Mean

eneralization to multidimensional cas

Rewrite d-dimension integral


I f ( x ) dx by I h( x ) ( x ) dx
cause the uniform distribution ,choose
1
1
( x)

V
(b a )

i 1

h( x) [ (b a )] f ( x ) Vf ( x )
i 1

( x) 0 and ( x ) dx 1

The estimate of I is

1
V

I
h( x )
f (X )
N
N
N

i 1

i 1

Importance Sampled method

quite obvious that most of the integral comes


the
n of the peak. But if we generate points evenl
the
rval
[a,idea
b], most
points
wont be
in the peak a
The
behind
importance
sampling
f(x) into another,
flatter
,and
is to transform
contribution
function
to the total will be relatively sma
which is then MC
integrated of
course there has to be
a back-transformation

ant properties of continuous and discrete

teps of Importance Sampled method


b

rewriteI f ( x ) dx by
a

where

h( x )

f ( x)
g ( x)

I h( x ) dG ( x )whereG ( x )
a

I h( x ) g ( x )dx
a

g ( x ) dx
0

w make a variable change


u G (x) and du dG (x)
1
x G (u ) Then

f (G (u ))
I
du
g (G (u ))
G (b)

G(a)

Cont.

e value of the integration equal to the averag

1
f (G (u ))
I

N
g (G (u ))
1

i 1

rror calculated by variance method


1
f
var( I ) var( )
N
g

error var(I )
2

f
f
f
var( )
g
g
g
2

1 f
f
var( I ) [ ]
N g
g
2

note that, the functionG (x)

must be invertible

xample (Importance-Sampled metho


find

I e dx
x2

In this region, the function decreases


x
from 1 to 1/e. The simple
exponential
e
function
does the same, so lets
calculate
use that for g(x) . We first have to
1
1
e 1
x
g , so we
1
e dx normalize
0

n our normalized weighting function is

e e
g ( x)
e 1
x

Cont....
e
G ( x) g ( x)dx
dx
e 1
x

1 x

(1 e )e
G ( x)
e 1
x

u G (x)

let

e 1
x G (u ) log (1 u
)
e
Then
1

e 1
G (u ) log (1 u
)
e
1

Cont

note:: from the last result we redistribute the


ccording to the pdf (g(x)) , then the new val
G )(uare
) uniform random numbers us
.e.,
To find the value of
1

1 f (G (u ))
I
N g (G (u ))
N

i 1

on the interval of integration ,where the avera


alue is the estimator value for the original
ntegral.

program important_sample
implicit none
real(8)::sum,u,R,integ
integer::i,N
read*,N
sum=0.0
do i=1,N
call random_number(R)
u=G_inv(R)
sum=sum+f(u)/g(u)
end do
integ=sum/N
write(*,*) integ
contains
real(8) function F(x)
real(8),intent(in)::x
F=dexp(-x**2)
end function F
real(8) function g(x)
real(8),intent(in)::x
g=dexp(-x)
end function g
real(8) function G_inv(x)
real(8),intent(in)::x
G_inv=-dlog(1-x*1.718d0/2.718d0)
end function G_inv
end program important_sample

Why monte carlo ?


Method

Basic MC
Trapezoidal rule
Simpsons rule

Conv.rate in Conv.rate in d-dim


one dim

om the best methods to find the partition fun


lly , then you can solve the stochastic proces

se we want to solve the following integral Usin


umerical method ,(i.e. Trapezoidal or Simpson)

I f ( x )dx Where

i=1,2,3,,30

So we generate a grid in the domain of integration


a grid with 10 nodes on each coordinate axis in 30
[ 0,1] be chosen .in this case, we have 10 abs
Suppose a time of 10 second is necessary for

One value of the function . Therefore , a tim


10 years will be necessary for evaluating

Thank you

Anda mungkin juga menyukai