0% found this document useful (0 votes)
23 views

Analytics of Observational data lec 12

The document discusses Monte Carlo methods, focusing on the generation of pseudo-random numbers and their application in numerical integration. It explains the differences between true random and pseudo-random numbers, various algorithms for generating random numbers, and the properties and limitations of pseudo-random number generators. Additionally, it highlights the importance of initializing seeds for reproducibility in random number generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Analytics of Observational data lec 12

The document discusses Monte Carlo methods, focusing on the generation of pseudo-random numbers and their application in numerical integration. It explains the differences between true random and pseudo-random numbers, various algorithms for generating random numbers, and the properties and limitations of pseudo-random number generators. Additionally, it highlights the importance of initializing seeds for reproducibility in random number generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

12/9/2024

12. MONTE-CARLO METHODS


LECTURER: DR. NGUYEN THI THANH SANG
REFERENCES:
[4].C.14

[12] FRED S KLEINER, E CHENEY, E WARD CHENEY, DAVID KINCAID, DAVID R KINCAID, "NUMERICAL
MATHEMATICS AND COMPUTING", 7TH EDITION, CENGAGE LEARNING, 2013, EISBN-13: 9781133712350

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 1

MONTE-CARLO INTEGRATION

• Overview
1. Generating Psuedo-Random Numbers
2. Multidimensional Integration
a) Handling complex boundaries.
b) Handling complex integrands.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 2

1
12/9/2024

PSEUDO-RANDOM NUMBERS

• Definition of random from Merriam-Webster:


• Main Entry: random
Function: adjective
Date: 1565
1 a : lacking a definite plan, purpose, or pattern b : made, done, or chosen at random <read
random passages from the book>
2 a : relating to, having, or being elements or events with definite probability of occurrence
<random processes> b : being or relating to a set or to an element of a set each of whose
elements has equal probability of occurrence <a random sample>; also : characterized by
procedures designed to obtain such sets or elements <random sampling>

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 3

RANDOM NUMBER

• What is random number ? Is 3 ?


• There is no such thing as single random number
• Random number
• A set of numbers that have nothing to do with the other numbers in the sequence
• In a uniform distribution of random numbers in the range [0,1] , every
number has the same chance of turning up.
• 0.00001 is just as likely as 0.5000
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 4

2
12/9/2024

RANDOM V. PSEUDO-RANDOM

• Random numbers have no defined sequence or formulation. Thus,


for any n random numbers, each appears with equal probability.
• If we restrict ourselves to the set of 32-bit integers, then our
numbers will start to repeat after some very large n. The numbers
thus clump within this range and around these integers.
• Due to this limitation, computer algorithms are restricted to
generating what we call pseudo-random numbers.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 5

MONTE-CARLO METHODS

• 1953, Nicolaus Metropolis


• Monte Carlo method refers to any method that makes use of random numbers
• Simulation of natural phenomena
• Simulation of experimental apparatus
• Numerical analysis

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 6

3
12/9/2024

HOW TO GENERATE RANDOM NUMBERS ?

• Use some chaotic system (Balls in a barrel – Lotto)


• Use a process that is inherently random
• Radioactive decay
• Thermal noise
• Cosmic ray arrival
• Tables of a few million random numbers
• Hooking up a random machine to a computer.
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 7

PSEUDO RANDOM NUMBER GENERATORS


• The closest random number generator that can be obtained by
computer algorithm.
• Usually, a uniform distribution in the range [0,1]
• Most pseudo random number generators have two things in common
• The use of large prime numbers
• The use of modulo arithmetic

• Algorithm generates integers between 0 and M, map to zero and


one.
X n  In / M
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 8

4
12/9/2024

AN EARLY EXAMPLE (JOHN VON


NEUMANN,1946)
• To generate 10 digits of integer
• Start with one of 10 digits integers
• Square it and take middle 10 digits from answer
• Example: 57721566492 = 33317792380594909291

• The sequence appears to be random, but each number is determined from the previous  not random.
• Serious problem : Small numbers (0 or 1) are lumped together, it can get itself to a short loop. For example:
• 61002 = 37210000
• 21002 = 04410000
• 41002 = 16810000
• 51002 = 65610000

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 9

LINEAR CONGRUENTIAL METHOD


• Lehmer, 1948
• Most typical so-called random number generator
• Algorithm : I  (aI  c) mod( m )
n 1 n

• a,c >=0 , m > I0,a,c


• Advantage :
• Very fast
• Problem :
• Poor choice of the constants can lead to very poor sequence
• The relationship will repeat with a period no greater than m (around
m/4)
• C complier RAND_MAX : m = 32767

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 10

10

5
12/9/2024

RANDU GENERATOR

• 1960’s IBM
• Algorithm

I n 1  (65539  I n ) mod( 2 31 )

• This generator was later found to have a serious


problem
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 11

11

1D AND 2D DISTRIBUTION OF RANDU

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 12

12

6
12/9/2024

RANDOM NUMBER ALGORITHMS


• The class of multiplicative congruential random-number generators has the
form: The choice of the coefficients is critical. An example:
ln
xn 
231  1
ln 1   75 ln  mod  231  1
l0  1
l1  75  x1  0.0000078263692594256108903445354152213e-6
l2  710 mod  231  1  710  x2  0.13153778814316624223402060672362
l3  7 mod  2  1  1622650073
15 31
 x3  0.7556053221950332271843372039424
l4   75 *1622650073  mod  231  1  984943658  x4  0.45865013192344928715538665985474
x5  0.53276723741216922058359217857178

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 13

13

USE OF PRIME NUMBERS

• The number 231 – 1 is a prime number, so the remainder when a


number is divided by a prime is rather, well random.
• Notes on the previous algorithm:
• The l’s can reach a maximum value of the prime number.
• Dividing by this number maps the integers into reals within the open
interval (0,1.0).
• Why open interval?
• l0 is called the seed of the random process. We can use anything here.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 14

14

7
12/9/2024

OTHER ALGORITHMS
• Multiply by a large prime and xn 1   2736731631558xn  138  mod 2 48
take the lower-order bits. x0  1
x1  2736731631696
• Here, we use higher-bit integers
to generate 48-bit random x2  216915228954218
numbers. x3  44664858844294
x4  123276424030766
• Drand48() x5  162415264731678
• Ex. 29961701459390
51892741493630
251715685692926
37108576904446
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA 163500647628542
December 9, 2024 15

15

OTHER ALGORITHMS
• Many more such algorithms.
un 1   8t  3  un t is any large number

un
xn  What is this operation?
2q
• Some do not use integers. Integers were just more efficient on old computers.

xn 1    xn  mod 1
5

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 16

16

8
12/9/2024

OTHER ALGORITHMS

• One way to improve the behavior of


random number generator
I n  ( a  I n 1  b  I n  2 ) mod( m )
Has two initial seed and can have a period greater
than m

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 17

17

THE RANMAR GENERATOR

• Available in the CERN Library


• Requires 103 initial seed
• Period : about 1043
• This seems to be the ultimate random number generator

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 18

18

9
12/9/2024

PROPERTIES OF PSEUDO-RANDOM NUMBERS

• Three key properties that you should remember:


1. These algorithms generate periodic sequences (hence not random). To see this,
consider what happens when a random number is generated that matches our initial
seed.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 19

19

PROPERTIES OF PSEUDO-RANDOM NUMBERS

2. The restriction to quantized numbers (a finite-set), leads to problems in high-


dimensional space. Many points end up to be co-planar. For ten-dimensions, and 32-
bit random numbers, this leads to only 126 hyper-planes in 10-dimensional space.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 20

20

10
12/9/2024

3D DISTRIBUTION FROM RANDU

Problems seen when


observed at the right
angle

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 21

21

THE MARSAGLIA EFFECT

• 1968, Marsaglia
• Random numbers fall mainly in the planes
• The maximum number of hyperplanes will be less in the space of higher-
dimensions.
• In the RANDU generator, the replacement of the multiplier from 65539 to
69069 improves performance significantly.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 22

22

11
12/9/2024

PROPERTIES OF PSEUDO-RANDOM NUMBERS

3. The individual digits in the random number may not be independent. There may be a
higher probability that a 3 will follow a 5.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 23

23

AVAILABLE FUNCTIONS

• Standard C Library
• Type in “man rand” on your CIS Unix environment.
• Rather poor pseudo-random number generator.
• Only results in 16-bit integers.
• Has a periodicity of 2**31 though.
• Type in “man random” on your CIS Unix environment.
• Slightly better pseudo-random number generator.
• Results in 32-bit integers.
• Used rand() to build an initial table.
• Has a periodicity of around 2**69.
• #include <stdlib.h>
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 24

24

12
12/9/2024

AVAILABLE FUNCTIONS

• Drand48() – returns a pseudo-random number in the range from zero to one,


using double precision.
• Pretty good routine.
• May not be as portable.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 25

25

INITIALIZING WITH SEEDS

• Most of the algorithms have some state that can be initialized. Many times this
is the last generated number (not thread safe).
• You can set this state using the routines initialization methods (srand, srandom
or srand48).
• Why would you want to do this?

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 26

26

13
12/9/2024

INITIALIZING WITH SEEDS

• Two reasons to initialize the seed:


1. The default state always generates the same sequence of random numbers. Not really
random at all, particularly for a small set of calls. Solution: Call the seed method with
the lower-order bits of the system clock.
2. You need a deterministic process that is repeatable.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 27

27

INITIALIZING WITH SEEDS

• We do not want the mountain to change as the camera moves.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 28

28

14
12/9/2024

MAPPING RANDOM NUMBERS

• Most computer libraries supporting for random numbers only provide random
numbers over a fixed range.
• You need to map this to your desired range.
• Two common cases:
• Random integers from zero to some maximum.
• Random floating-point or double-precision numbers mapped to the range zero to one.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 29

29

NON-RECTANGULAR AREAS

• In 2D, we may want points randomly distributed over some region.


• Square – independently determine x and y.
• Rectangle - ???
• Circle - ???
• Wrong way – independently determine r and .

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 30

30

15
12/9/2024

MONTE-CARLO TECHNIQUES

• Problem: What is the probability that 10 dice throws add up exactly to 32?

• Exact Way. Calculate this exactly by counting all possible ways of making 32
from 10 dice.

• Approximate (Lazy) Way. Simulate throwing the dice (say 500 times), count
the number of times the results add up to 32, and divide this by 500.

• Lazy Way can get quite close to the correct answer quite quickly.
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 31

31

MONTE-CARLO TECHNIQUES

• Sample Applications
• Integration
• System simulation
• Computer graphics - Rendering.
• Physical phenomena - radiation transport
• Simulation of Bingo game
• Communications - bit error rates
• VLSI designs - tolerance analysis

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 32

32

16
12/9/2024

b
SIMPLE EXAMPLE: . a
p ( x)dx
• Method 1: Analytical Integration
• Method 2: Quadrature
• Method 3: MC -- random sampling the area enclosed by a<x<b and 0<y<max (p(x))
b  # 
 a
p ( x)dx  max( p ( x))(b  a )
 # #


P(x) P(x)

a b x a b
IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 33

33

b
SIMPLE EXAMPLE: . a
p ( x)dx

• Intuitively:
b  # 

a
p ( x)dx  max( p ( x))(b  a )
 # #


 AreaBox  Probability  y  f  x 

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 34

34

17
12/9/2024

Shape of High Dimensional Region


• Higher dimensional shapes can be complex.
• How to construct weighted points in a grid
that covers the region R ?

Problem :
mean-square distance from the origin

  ( x  y )dxdy
2 2

 r 2 
  dxdy

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 35

35

Integration over simple shape ?

1 inside R
s
0 outside R

0.5 0.5
 dx  dy( x  y ) s( x, y )
2 2
r 2
 0.5 0.50.5 0.5
0.5 dx 0.5 dys( x, y )
Grid must be fine enough !

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 36

36

18
12/9/2024

MONTE-CARLO INTEGRATION
D’: rectangular
• Integrate a function over a complicated
domain
• D: complicated domain.
• D’: Simple domain, superset of D. D
• Pick random points over D’:
D’: circle
• Counting: N: points over D
• N’: points over D’
VolumeD N

VolumeD N 
D

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 37

37

EXERCISES

• Generate random numbers in an open interval (0,6) whose probability density


function is f(x) = |x-|, using MC integration . Simulate their
distribution.
• Generate random numbers in an open interval (-1,2) whose probability
density function is f(x) = x2, using MC integration . Simulate
their distribution.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 38

38

19
12/9/2024

ESTIMATING  USING MONTE CARLO


• The probability of a random point lying
inside the unit circle: (x,y)

M
N
• If pick a random point N times and M of
those times the point lies inside the unit
circle:


• If N becomes very large, P=P0

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 39

39

ESTIMATING  USING MONTE CARLO

• Results:
• N= 10,000 Pi= 3.104385
• N= 100,000 Pi= 3.139545
• N= 1,000,000 Pi= 3.139668
• N= 10,000,000 Pi= 3.141774
• …

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 40

40

20
12/9/2024

ESTIMATING  USING MONTE CARLO

double x, y, pi;
const long m_nMaxSamples = 100000000;
long count=0;
for (long k=0; k<m_nMaxSamples; k++) {
x=2.0*drand48() – 1.0; // Map to the range [-1,1]
y=2.0*drand48() – 1.0;
if (x*x+y*y<=1.0) count++;
}
pi=4.0 * (double)count / (double)m_nMaxSamples;

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 41

41

STANDARD QUADRATURE

• We can find numerical value of a definite integral by the definition:

f lim f Δ

where points xi are uniformly spaced.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 42

42

21
12/9/2024

ERROR IN QUADRATURE

• Consider integral in d dimensions:


 f( X )dx 1dx 2 dx d   f( Xi ) x d

• The error with N sampling points is


|  f(X )dX   f(X )x d |  N 1/ d

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 43

43

MONTE CARLO ERROR

• From probability theory one can show that the Monte Carlo error decreases
with sample size N as

1

N
independent of dimension d.

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 44

44

22
12/9/2024

GENERAL MONTE CARLO

• If the samples are not drawn uniformly but with some probability distribution
P(X), we can compute by Monte Carlo:

1 N

 f( X )P( X ) d X 
N
 f(X )
i 1
i

Where P(X) is normalized,  P(X ) d X 1

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 45

45

EXERCISES

What is
the output?

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 46


Book: Introduction to Numerical Programming: A Practical Guide for Scientists and Engineers Using Python and
C/C++

46

23
12/9/2024

THANK YOU FOR LISTENING

IT142IU - ANALYTICS FOR OBSERVATIONAL DATA December 9, 2024 47

47

24

You might also like