INFO 473 COMPUTER MODELING Dr. Ronald TEHINI

Transcription

INFO 473 COMPUTER MODELING Dr. Ronald TEHINI
INFO 473
COMPUTERMODELING
Dr. Ronald TEHINI
Dr. Ronald Tehini INFO 473, 2014-2015
Syllabus
Part I MonteCarlo Methods: Generalities
Chapter 1 Introduction to Monte Carlo Method and Random numbers
Chapter 2 Monte Carlo integration
Chapter 3 Data analysiswith Monte Carlo
Part II Monte carlo Methods: Applicationsto statistical Physics
Chapter 4 Random walksand applications
Chapter 5 MC simulation of various thermodynamic ensembles
Part II Deterministic Methods : MolecularDynamics(6h)
Chapter 6 Introduction to molecular dynamics
Dr. Ronald Tehini INFO 473, 2014-2015
Professor R.Tehini
Info 473 Computer modeling
Dr. Ronald Tehini INFO 473, 2014-2015
Stochastic methods (ex : Monte Carlo methods):
A class of computational algorithms that rely on repeated
random sampling to compute results.
A few broad areas of applications are:
1. physics
2. chemistry
3. engineering
4. finance and risk analysis
When are MC methods likely to be the methods of choice?
When the problem is many-dimensional and approximations that
factor the problem into products of lower dimensional problems are
inaccurate.
Dr. Ronald Tehini INFO 473, 2014-2015
One can distinguish between two kinds of algorithms:
1. The system being studied is stochastic and the stochasticity of the
algorithm mimics the stochasticity of the actual system. e.g. study of
neutron transport and decay in nuclear reactor by following the
trajectories of a large number of neutrons.
2. The system studied is not stochastic, but nevertheless a stochastic
algorithm is the most efficient, or the most accurate, or the only
feasible method for studying the system. e.g. the solution of a PDE in a
large number of variables, e.g., the solution of the Schrodinger
equation for an N-electron system, with say N = 100 or 1000. (Note: The
fact that the wavefunction has a probabilistic interpretation has
nothing to do with the stochasticity of the algorithm. The
wavefunction itself is perfectly deterministic.)
Dr. Ronald Tehini INFO 473, 2014-2015
The hit and miss method can be used to get
an estimation of the number .
π
Consider simply a unit circle circumscribed by a
square.
We “shoot” randomly N points and then
we count the numbers of points that fall inside the
circleand those who fall inside.
This will lead to an estimation of the number
π
N inside Area of the circle πd 2 / 4 π
≈
≈
≈
2
N
Area of the square
4
d
Dr. Ronald Tehini INFO 473, 2014-2015
π≈
4 N inside
N
Central to any MC simulation are the random numbers. Hence it is
important to have a good source of random numbers available for the
simulations.
An ideal Random generator (Real experiment) provides random number s
with a uniform distribution such as the sequence or the order of appearance
of the numbers is completely random. no way to predict what the next
random number is These numbers are called true random numbers.
Computers are deterministic machines thus no computer generated
sequence of random numbers is truly random. The random numbers
must repeat after a finite (though hopefully very large) period. Also, if N bits
are used to represent the random numbers, then the number of different
numbers generated cannot be larger than 2N. These numbers are called
pseudo-random numbers
Dr. Ronald Tehini INFO 473, 2014-2015
- Mechanically generated random numbers
For examples The machines used in lottery or roulette. Specifically designed machines
which could generate thousands of numbers have been built.
- Electronic hardware-generated random numbers:
Using for example Microprocessorsbut it is a practice that is rarely done . The
problem is that that minute electronic disturbancesfrom the environment could
affect the results produced by it.
There exists also random number generators based on radioactivity but these
Generatorsare not pratical.
-Quantum random generators : These are the last generation of true numbers
they are based on quantum mechanicsuncertainty.
This is a quantum random number generator developed by QUANTI S. Photons - are
sent one by one onto a semi-transparent mirror and detected. The exclusive events
(ref lection - transmission) are associated to "0" - "1" bit values.
Dr. Ronald Tehini INFO 473, 2014-2015
The by far most common way of obtaining random numbers is by using some algorithm
which producesa seemingly random sequence of numbers.
The most common way to implement the generators is to enable initializing them once
with an integer number, called the seed number.
For a given seed number, the generator producesalways the same sequence of numbers.
The seed number could be set from the system clock, or just selected manually. Doing
the latter is actually almost always advisable, since this allowsone to repeat the
simulation identically, i.e. the simulation is repeatable.
Many different algorithmsexist for generating random numbers:
• Linear congruential generators (with or without an additive constant),
• Linear feedback shift register
• Lagged Fibonacci generator
• XORshift algorithm etc.
They are typically subjected to a battery of statistical tests,
The basic requirement is that random number generator used does not have
correlationsthat could significantly impact the system being studied.
Dr. Ronald Tehini INFO 473, 2014-2015
A linear congruent generator generates a pseudo random sequence of
numbers {r i } with
s such as :
a and c are fixed constants.
A number between 0 and 1can then be obtain by diving by M.
In order to obtain long sequence a and M should be large numbers but not so large
that
overf lows. For a 32 bit computer one can use M as large as 231 .
The biggest problem of this method is sequential correlation between successive
calls. This can be a particular problem when generating manydimensional the data
can appear as planes in the many-dimensional space.
Dr. Ronald Tehini INFO 473, 2014-2015
Example: Lets select the constants as a = 7, c = 4 and M = 24 − 1 = 15 with two initial
seeds value r 0 = 4 and r 0 = 11
i
r0 = 4
1
2
r0 = 11
6
2
3
1
3
10
11
4
14
6
5
12
1
6
13
11
7
5
6
8
9
1
9
7
11
10
8
6
11
0
1
12
4
11
With r0 = 4 the periodicity is equal to 12
Whilewith r0 = 11 the periodicity is equal
To 3 which is catastrophic !
This example illustrates many important things:
1. It is not good to have a generator where the result
dependson the seed number.
2. Hence care should be taking in selecting not only
the random number generator but also the
constants in it.
For the linear congruential generator there are
actually quite simple rules which guarantee that the
period reaches the maximum M − 1:
• c and M should have no common prime factors
• a − 1 is divisible with all the prime factors of M
• a − 1 is divisible with 4 if M is divisible with 4.
14
Dr. Ronald Tehini INFO 473, 2014-2015
Shift-register generators
Other random number generators which have become increasingly
popular are so-called shift-register generators. In these generators
each successive number depends on many preceding values (rather than
the last values as in the linear congruential generator).
Example:
rl = (arl − p + crl − p − q ) MOD ( M )
p and q are constants, p > q
Such a generator again produces a sequence of pseudorandom numbers but this time
with a period much larger than M.
The difficult part consist in generating the initial numbers a first algorithm
can be used in order to generate the first sequence of numbers.
Generalized feedback shift register algorithm GSFR
They use the Bitewise exclusive or (XOR) operation :
rl = rl − p ⊕ rl − p − q
Dr. Ronald Tehini INFO 473, 2014-2015
p and q are constants, p > q
I nverse congruent generators
The basic idea of the inverse congruent generators is to generate numbers using
rn +1 = ( arn + c ) MOD ( M )
Where
rn is such as : ( rn rn ) Mod ( M ) = 1
Quadraticcongruential generator
rn +1 = ( arn 2 + brn + c ) MOD ( M )
Nonlinear GFSR generators
rl = rl − p − q ⊕ rl − p A
is a binary matrix of dimensions w ×w where w is the word size (e.g. 32 for an
unsigned 4-byte integer). rl − p must here be considered a row vector for the
vector×matrixoperation to make sense.
Dr. Ronald Tehini INFO 473, 2014-2015
Propertiesof a good random generator:
1. Uniform distribution in the interval [0,1].
2. Correlationsbetween random numbers are negligible
3. The period of the sequence of random numbers is as large as possible.
4. The algorithm should be fast.
Simple methodsto test a random generator:
• Visual test : plot r i in function of i , if a regular pattern appears then the generator is
not random.
• For a test of uniformity evaluate the kth moment of the distribution:
if the numbers are distributed uniformly then we have:
• For testing the near neighbor correlation evaluate
If the numbers are independent and the distribution is uniform the we have:
Dr. Ronald Tehini INFO 473, 2014-2015
So far we have only discussed generating random numbers in a uniform distribution.
Random numbers in any other distribution are almost always generated starting from
random numbersdistributed uniformly between 0 and 1. We will discuss
We want to generate a random number r such as the probability density
function Followssome arbitrary function f ( x) Satisfying the properties :
∞
f ( x) > 0
and
∫
f ( x )dx = 1
−∞
The Cumulative distribution function is given by
x
F ( x ) = P (r < x ) =
∫
−∞
We also define the inverse Cumulative distribution function
Dr. Ronald Tehini INFO 473, 2014-2015
f (u ) du
The I nversion method algorithm:
Let us denote the uniform random numbers generator by Pu(0, 1).
To generate random numbers r distributed as f(x), we should perform the
following steps:
1) Generate a uniformly distributed number u = Pu(0, 1)
2) Calculate r = F−1(u)
Proof:
Consider the probability that a point x is below the given point x :
F ' ( x ) = P( r ≤ x)
−1
Replacing r = F (u ) in the last expression we get :
F ' ( x ) = P ( F −1 ( u ) ≤ x )
Applying the function F on both sides of the inequality in the parentheses, and get:
F ' ( x ) = P( F ( F −1 (u)) ≤ F ( x )) = P (u ≤ F ( x )) = F ( x )
Because P(u ≤ a ) = a (u being uniformly distributed between 0 and 1)
Dr. Ronald Tehini INFO 473, 2014-2015
Example:
⎧ e − x for x ≥ 0
f ( x) = ⎨
⎩0 for x < 0
x
First calculate F ( x ) =
−1
∫
f ( x )dx = 1 − e − x
−∞
Then calculate F (u ) :
u = F (r ) = 1 − e − r
r = − log(u )
Dr. Ronald Tehini INFO 473, 2014-2015
Another way to generate random numbers, which is purely numerical and works for
any finite-valued function in a finite interval, regardless of whether it can be
integrated or inverted. It is called the (von Neumann) rejection method or hit-andmiss method.
Consider a function f(x) Let M be an number Such as :
f ( x) ≤ M
x ∈ [ a, b]
The von Neumann Algorithm reads :
1) Generate a uniformly distributed number x = Pu(a, b)
2) Generate a uniformly distributed number y = Pu(0,M)
3) If y > f(x) this is a miss: return to 1)
4 ) Otherwise this is a hit: return x
Dr. Ronald Tehini INFO 473, 2014-2015
The disadvantage of the method is that there is a lot of numbers that are
generated in vain
Increased calculation time
The probability to get a hit is :
∫
P( hit ) =
b
a
f ( x )dx
M (b − a )
=
1
M (b − a )
if the function is highly peaked, or has a long weak tail, the number of misses will be
enormous.
For example for the normalized exponential function
[0,100] the number of misses is almost 99%
Dr. Ronald Tehini INFO 473, 2014-2015
e− x
on the interval
For many functions it will not be possible to do the analytical approach. But there
may still be a way to do better than the basic hit-and-missalgorithm.
Suppose that exits a function g(x) and a Number A such as :
Ag ( x ) ≥ f ( x )
x ∈ [a , b ]
g(x) represents a probability density function (
Then the algorithm reads :
∞
∫ g ( x )dx = 1 )
−∞
Then the algorithm becomes:
1) Generate a uniformly distributed number
u = Pu(0, 1)
2) Generate a number distributed as g(x):
x = G−1(u)
3 )Generate a uniformly distributed number
y = Pu(0,Ag(x))
4) If y > f(x) this is a miss: return to 1
5) Otherwise this is a hit: return x
Dr. Ronald Tehini INFO 473, 2014-2015