Analytics of Observational data lec 12
Analytics of Observational data lec 12
[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
MONTE-CARLO INTEGRATION
• Overview
1. Generating Psuedo-Random Numbers
2. Multidimensional Integration
a) Handling complex boundaries.
b) Handling complex integrands.
1
12/9/2024
PSEUDO-RANDOM NUMBERS
RANDOM NUMBER
2
12/9/2024
RANDOM V. PSEUDO-RANDOM
MONTE-CARLO METHODS
3
12/9/2024
4
12/9/2024
• 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
10
5
12/9/2024
RANDU GENERATOR
• 1960’s IBM
• Algorithm
I n 1 (65539 I n ) mod( 2 31 )
11
12
6
12/9/2024
13
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
16
8
12/9/2024
OTHER ALGORITHMS
17
18
9
12/9/2024
19
20
10
12/9/2024
21
• 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.
22
11
12/9/2024
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.
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
25
• 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?
26
13
12/9/2024
27
28
14
12/9/2024
• 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.
29
NON-RECTANGULAR AREAS
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
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
34
17
12/9/2024
Problem :
mean-square distance from the origin
( x y )dxdy
2 2
r 2
dxdy
35
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.50.5 0.5
0.5 dx 0.5 dys( x, y )
Grid must be fine enough !
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
37
EXERCISES
38
19
12/9/2024
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
39
• 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
• …
40
20
12/9/2024
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;
41
STANDARD QUADRATURE
f lim f Δ
→
42
21
12/9/2024
ERROR IN QUADRATURE
43
• From probability theory one can show that the Monte Carlo error decreases
with sample size N as
1
N
independent of dimension d.
44
22
12/9/2024
• 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
45
EXERCISES
What is
the output?
46
23
12/9/2024
47
24