Random variables are hence characterized by a domain which contains all possible values that the random value may take.. To give you another example of possible random number spare time
Trang 1Eleven new attempts may results in a totally different sequence of numbers and so forth Repeat-ing this exercise the next evenRepeat-ing, will most likely never give you the same sequences Thus, we say that the outcome of this hobby of ours is truly random
Random variables are hence characterized by a domain which contains all possible values that the random value may take This domain has a corresponding PDF.
To give you another example of possible random number spare time activities, consider the radioactive decay of an-particle from a certain nucleus Assume that you have at your disposal
a Geiger-counter which registers every say 10ms whether an -particle reaches the counter or not If we record a hit as 1 and no observation as zero, and repeat this experiment for a long time, the outcome of the experiment is also truly random We cannot form a specific pattern from the above observations The only possibility to say something about the outcome is given by the PDF, which in this case the well-known exponential function
withbeing proportional with the half-life
integra-tion
With this definition of a random variable and its associated PDF, we attempt now a clarification
of the Monte-Carlo strategy by using the evaluation of an integral as our example
In the previous chapter we discussed standard methods for evaluating an integral like
I = Z 1 0 f(x)dx
N X i=1
! i f(x i
iare the weights determined by the specific integration method (like Simpson’s or Tay-lor’s methods) withx
ithe given mesh points To give you a feeling of how we are to evaluate the above integral using Monte-Carlo, we employ here the crudest possible approach Later on we will present slightly more refined approaches This crude approach consists in setting all weights equal 1,!
i
= 1 Recall also thatdx = h = (b a)=N whereb = 1,a = 0in our case andh is the step size We can then rewrite the above integral as
I = Z 1 0
f (x)dx
1 N
N X i=1 f(x i
but this is nothing but the average off over the interval [0,1], i.e.,
Z 1 0
In addition to the average valuehfithe other important quantity in a Monte-Carlo calculation
is the variance 2
or the standard deviation We define first the variance of the integral with
Trang 2to be
2 f
= 1 N
N X i=1 f(x i ) 2 1 N
N X i=1 f(x i )
! 2
or
2 f
2
i hf i
2
which is nothing but a measure of the extent to whichf deviates from its average over the region
of integration
If we consider the results for a fixed value of N as a measurement, we could however re-calculate the above average and variance for a series of different measurements If each such measumerent produces a set of averages for the integral I denoted hfi l, we have for M mea-sumerements that the integral is given by
hIi M
= 1 M
M X
l =1
hf i l
The variance for these series of measurements is then forM = N
2 N
= 1 N 2
4 h 1 N
N X i=1
f (x i )
! 2
1 N
N X i=1
f (x i )i
! 2 3
5
Splitting the sum in the first term on the right hand side into a sum withi = jand one withi 6= j
we assume that in the limit of a large number of measurements only terms withi = j survive, yielding
2 N
1 N hf 2
i hf i
2
=
2 f N
We note that
1 p N
The aim is to have
N as small as possible after N samples. The results from one sample represents, since we are using concepts from statistics, a ’measurement’
The scaling in the previous equation is clearly unfavorable compared even with the trape-zoidal rule In the previous chapter we saw that the trapetrape-zoidal rule carries a truncation error O(h
2
), with h the step length In general, methods based on a Taylor expansion such as the trapezoidal rule or Simpson’s rule have a truncation error which goes like O(h
k , withk 1 Recalling that the step size is defined ash = (b a)=N, we have an error which goes like N
k However, Monte Carlo integration is more efficient in higher dimensions To see this, let
us assume that our integration volume is a hypercube with sideLand dimension d This cube
d points and therefore the error in the result scales asN
k=d for the traditional methods The error in the Monte carlo integration is however independent of d and scales as 1=
p
N, always! Comparing this error with that of the traditional methods, shows that Monte Carlo integration is more efficient than an order-k algorithm when
Trang 3Below we list a program which integrates
Z 1 0 dx 1
1 + x 2
=
4
where the input is the desired number of Monte Carlo samples Note that we transfer the variable idumin order to initialize the random number generator from the functionran0 The variable idumgets changed for every sampling This variable is called the seed.
What we are doing is to employ a random number generator to obtain numbersx
i in the in-terval[0; 1℄through e.g., a call to one of the library functionsran0,ran1,ran2 These functions will be discussed in the next section Here we simply employ these functions in order to generate
a random variable All random number generators produce in a pseudo-random form numbers in the interval[0; 1℄using the so-called uniform probability distributionp(x)defined as
p(x) =
1
witha = 0ogb = 1 If we have a general interval[a; b℄, we can still use these random number generators through a variable change
withxin the interval[0; 1℄
The present approach to the above integral is often called ’crude’ or ’Brute-Force’ Monte-Carlo Later on in this chapter we will study refinements to this simple approach The reason for doing so is that a random generator produces points that are distributed in a homogenous way in the interval[0; 1℄ If our function is peaked around certain values ofx, we may end up sampling function values wheref (x)is small or near zero Better schemes which reflect the properties of the function to be integrated are thence needed
The algorithm is as follows
Choose the number of Monte Carlo samplesN
Perform a loop over N and for each step generate a a random number x
i in the interval [0; 1℄trough a call to a random number generator
Use this number to evaluatef(x
i )
Evaluate the contributions to the mean value and the standard deviation for each loop
AfterN samples calculate the final mean value and the standard deviation
The following program implements the above algorithm using the library function ran0 Note the inclusion of the file
Trang 4# i n c l u d e < i o s t r e a m >
# i n c l u d e " l i h "
u s i n g namespace s t d ;
/ / Here we d e f i n e v a r i o u s f u n c t i o n s c a l l e d by t h e main program / / t h i s f u n c t i o n d e f i n e s t h e f u n c t i o n t o i n t e g r a t e
d o u b l e f u n c (d o u b l e x ) ;
/ / Main f u n c t i o n b e g i n s h e r e
i n t main ( )
{
i n t i , n ;
l o n g idum ;
d o u b l e crude_m c , x , sum_sigma , fx , v a r i a n c e ;
c o u t < < " R a i n t h n u m e o M o t e - C a r o s a m p e s " < < e n d l ;
c i n > > n ;
c r u d e _ m c = sum _sigm a = 0 ; idum = 1 ;
/ / e v a l u a t e t h e i n t e g r a l w i t h t h e a c r u d e Monte C a r l o m e t h o d
f o r ( i = 1 ; i < = n ; i ++) {
x= r a n 0 (& idum ) ;
f x = f u n c ( x ) ;
c r u d e _ m c + = f x ; sum _sigm a + = f xf x ; }
c r u d e _ m c = c r u d e _ m c / ( (d o u b l e) n ) ;
sum _sigm a = sum _sigm a / ( (d o u b l e) n ) ;
v a r i a n c e = sum_sigma c r u d e _ m cc r u d e _ m c ;
/ / f i n a l o u t p u t
c o u t < < " v a r a n e = " < < v a r i a n c e < < " I n e g r l = "
< < c r u d e _ m c < < " E x t = " < < M_PI / 4 < < e n d l ; } / / end o f main program
/ / t h i s f u n c t i o n d e f i n e s t h e f u n c t i o n t o i n t e g r a t e
d o u b l e f u n c (d o u b l e x )
{
d o u b l e v a l u e ;
v a l u e = 1 / ( 1 + xx ) ;
r e t u r n v a l u e ;
} / / end o f f u n c t i o n t o e v a l u a t e
Trang 5The following table list the results from the above program as function of the number of Monte Carlo samples
Table 9.1: Results forI =
R 1 0 dx1=(1 + x
2 )as function of number of Monte Carlo samplesN The exact answer is7:85398E 01with 6 digits
N
We note that as N increases, the standard deviation decreases, however the integral itself never reaches more than an agreement to the third or fourth digit Improvements to this crude Monte Carlo approach will be discussed
As an alternative, we could have used the random number generator provided by the compiler through the functionsrand, as shown in the next example
/ / c r u d e mc f u n c t i o n t o c a l c u l a t e p i
# i n c l u d e < i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( )
{
c o n s t i n t n = 1 0 0 0 0 0 0 ;
d o u b l e x , fx , p i , i n v e r s _ p e r i o d , p i 2 ;
i n t i ;
i n v e r s _ p e r i o d = 1 / RAND_MAX;
s r a n d ( t i m e (NULL) ) ;
p i = p i 2 = 0 ;
f o r ( i = 0 ; i <n ; i ++)
{
x = d o u b l e( r a n d ( ) )i n v e r s _ p e r i o d ;
f x = 4 / ( 1 + xx ) ;
p i + = f x ;
p i 2 + = f xf x ;
}
p i / = n ; p i 2 = p i 2 / n p i p i ;
Trang 6c o u t < < " p = " < < p i < < " s i m ^ 2 " < < p i 2 < < e n d l ;
r e t u r n 0 ;
}
We give here an example of how a system evolves towards a well defined equilibrium state Consider a box divided into two equal halves separated by a wall At the beginning, time
t = 0, there areN particles on the left side A small hole in the wall is then opened and one particle can pass through the hole per unit time
After some time the system reaches its equilibrium state with equally many particles in both halves,N=2 Instead of determining complicated initial conditions for a system ofN particles,
we model the system by a simple statistical model In order to simulate this system, which may consist ofN 1particles, we assume that all particles in the left half have equal probabilities
of going to the right half We introduce the labeln
l to denote the number of particles at every time on the left side, andn
r
l for those on the right side The probability for a move
to the right during a time steptisn
l
= N The algorithm for simulating this problem may then look like as follows
Choose the number of particlesN
Make a loop over time, where the maximum time should be larger than the number of particlesN
For every time stept there is a probabilityn
l
=N for a move to the right Compare this probability with a random numberx
Ifx n
l
=N, decrease the number of particles in the left half by one, i.e.,n
l
l
1 Else, move a particle from the right half to the left, i.e.,n
l
l + 1
Increase the time by one unit (the external loop)
In this case, a Monte Carlo sample corresponds to one time unitt
The following simple C-program illustrates this model
/ / P a r t i c l e s i n a box
# i n c l u d e < i o s t r e a m >
# i n c l u d e < f s t r e a m >
# i n c l u d e < i o m a n i p >
# i n c l u d e " l i h "
u s i n g namespace s t d ;
o f s t r e a m o f i l e ;
i n t main (i n t a r g c , ch ar a r g v [ ] )
{
Trang 7ch ar o u t f i l e n a m e ;
i n t i n i t i a l _ n _ p a r t i c l e s , max_time , t i m e , random_n , n l e f t ;
l o n g idum ;
/ / Read i n o u t p u t f i l e , a b o r t i f t h e r e a r e t o o f e w command l i n e
a r g u m e n t s
i f ( a r g c < = 1 ) {
c o u t < < " B d U s g : " < < a r g v [ 0 ] < <
" r a a s o t u t f l o n s a e l i e " < < e n d l ;
e x i t ( 1 ) ;
}
e l s e{
o u t f i l e n a m e = a r g v [ 1 ] ;
}
o f i l e open ( o u t f i l e n a m e ) ;
/ / Read i n d a t a
c o u t < < " I n i i a l n m e o p a t i l e s = " < < e n d l ;
c i n > > i n i t i a l _ n _ p a r t i c l e s ;
/ / s e t u p o f i n i t i a l c o n d i t i o n s
n l e f t = i n i t i a l _ n _ p a r t i c l e s ;
m ax_tim e = 1 0 i n i t i a l _ n _ p a r t i c l e s ;
idum = 1 ;
/ / s a m p l i n g o v e r number o f p a r t i c l e s
f o r( t i m e = 0 ; t i m e < = m ax_tim e ; t i m e ++) {
random _n = ( (i n t ) i n i t i a l _ n _ p a r t i c l e sr a n 0 (& idum ) ) ;
i f ( random _n < = n l e f t ) {
n l e f t = 1 ;
}
e l s e{
n l e f t + = 1 ;
}
o f i l e < < s e t i o s f l a g s ( i o s : : s h o w p o i n t | i o s : : u p p e r c a s e ) ;
o f i l e < < s e t w ( 1 5 ) < < t i m e ;
o f i l e < < s e t w ( 1 5 ) < < n l e f t < < e n d l ;
}
r e t u r n 0 ;
} / / end main f u n c t i o n
The enclosed figure shows the development of this system as function of time steps We note that forN = 1000 after roughly2000time steps, the system has reached the equilibrium state There are however noteworthy fluctuations around equilibrium
If we denotehn
l
ias the number of particles in the left half as a time average after equilibrium
is reached, we can define the standard deviation as
q hn 2 l
l i 2
This problem has also an analytic solution to which we can compare our numerical
Trang 8simula-300 400 500 600 700 800 900 1000
n
l
(t)
t
MC simulation with N=1000
result
Figure 9.1: Number of particles in the left half of the container as function of the number of time steps The solution is compared with the analytic expression N = 1000
tion Ifn
l
(t)are the number of particles in the left half aftertmoves, the change inn
l (t)in the time intervaltis
l (t) N
n l (t) N
and assuming thatn
l andtare continuous variables we arrive at
dn l (t) dt
= 1
2n l (t) N
whose solution is
n l (t) = N 2
1 + e 2t=N
with the initial conditionn
l (t = 0) = N
Radioactive decay is among one of the classical examples on use of Monte-Carlo simulations Assume that a the timet = 0we haveN (0)nuclei of typeX which can decay radioactively At
a timet > 0we are left with N(t) nuclei With a transition probability!, which expresses the probability that the system will make a transition to another state during oen second, we have the following first-order differential equation
(9.22)
Trang 9whose solution is
N (t) = N(0)e
!t
(9.23) where we have defined the mean lifetime ofX as
= 1
!
If a nucleusX decays to a daugther nucleusY which also can decay, we get the following coupled equations
dN X (t) dt
X N X
and
dN Y (t) dt
Y N Y
X N X
The program example in the next subsection illustrates how we can simulate such a decay process through a Monte Carlo sampling procedure
The program is split in four tasks, a main program with various declarations,
/ / R a d i o a c t i v e d e c a y o f n u c l e i
# i n c l u d e < i o s t r e a m >
# i n c l u d e < f s t r e a m >
# i n c l u d e < i o m a n i p >
# i n c l u d e " l i h "
u s i n g namespace s t d ;
o f s t r e a m o f i l e ;
/ / F u n c t i o n t o r e a d i n d a t a f r o m s c r e e n
v o i d i n i t i a l i s e (i n t& , i n t& , i n t& , d o u b l e& ) ;
/ / The Mc s a m p l i n g f o r n u c l e a r d e c a y
v o i d m c _ s a m p l i n g (i n t , i n t , i n t , d ou b le , i n t) ;
/ / p r i n t s t o s c r e e n t h e r e s u l t s o f t h e c a l c u l a t i o n s
v o i d o u t p u t (i n t , i n t , i n t ) ;
i n t main (i n t a r g c , ch ar a r g v [ ] )
{
ch ar o u t f i l e n a m e ;
i n t i n i t i a l _ n _ p a r t i c l e s , max_time , n u m b e r _ c y c l e s ;
d o u b l e d e c a y _ p r o b a b i l i t y ;
i n t n c u m u l a t i v e ;
/ / Read i n o u t p u t f i l e , a b o r t i f t h e r e a r e t o o f e w command l i n e
a r g u m e n t s
i f ( a r g c < = 1 ) {
c o u t < < < < a r g v [ 0 ] < <
Trang 10" r a a s o t u t f l o n s a e l i e " < < e n d l ;
e x i t ( 1 ) ;
}
e l s e{
o u t f i l e n a m e = a r g v [ 1 ] ;
}
o f i l e open ( o u t f i l e n a m e ) ;
/ / Read i n d a t a
i n i t i a l i s e ( i n i t i a l _ n _ p a r t i c l e s , max_time , n u m b e r _ c y c l e s ,
d e c a y _ p r o b a b i l i t y ) ;
n c u m u l a t i v e = new i n t [ m ax_tim e + 1 ] ;
/ / Do t h e mc s a m p l i n g
m c _ s a m p l i n g ( i n i t i a l _ n _ p a r t i c l e s , max_time , n u m b e r _ c y c l e s ,
d e c a y _ p r o b a b i l i t y , n c u m u l a t i v e ) ;
/ / P r i n t o u t r e s u l t s
o u t p u t ( max_time , n u m b e r _ c y c l e s , n c u m u l a t i v e ) ;
d e l e t e [ ] n c u m u l a t i v e ;
r e t u r n 0 ;
} / / end o f main f u n c t i o n
the part which performs the Monte Carlo sampling
v o i d m c _ s a m p l i n g (i n t i n i t i a l _ n _ p a r t i c l e s , i n t max_time ,
i n t n u m b e r _ c y c l e s , d o u b l e d e c a y _ p r o b a b i l i t y ,
i n t n c u m u l a t i v e ) {
i n t c y c l e s , t i m e , np , n _ u n s t a b l e , p a r t i c l e _ l i m i t ;
l o n g idum ;
idum = 1; / / i n i t i a l i s e random number g e n e r a t o r
/ / l o o p o v e r m onte c a r l o c y c l e s
/ / One m onte c a r l o l o o p i s one s a m p l e
f o r ( c y c l e s = 1 ; c y c l e s < = n u m b e r _ c y c l e s ; c y c l e s ++) {
n _ u n s t a b l e = i n i t i a l _ n _ p a r t i c l e s ;
/ / a c c u m u l a t e t h e number o f p a r t i c l e s p e r t i m e s t e p p e r t r i a l
n c u m u l a t i v e [ 0 ] + = i n i t i a l _ n _ p a r t i c l e s ;
/ / l o o p o v e r e a c h t i m e s t e p
f o r ( t i m e = 1 ; t i m e < = m ax_tim e ; t i m e ++) {
/ / f o r e a c h t i m e s t e p , we c h e c k e a c h p a r t i c l e
p a r t i c l e _ l i m i t = n _ u n s t a b l e ;
f o r ( np = 1 ; np < = p a r t i c l e _ l i m i t ; np ++) {
i f ( r a n 0 (& idum ) < = d e c a y _ p r o b a b i l i t y ) {
n _ u n s t a b l e = n _ u n s t a b l e 1;
}
} / / end o f l o o p o v e r p a r t i c l e s
n c u m u l a t i v e [ t i m e ] + = n _ u n s t a b l e ;