Lec11 Random Sampling
Lec11 Random Sampling
3
Autocorrelation
• There are some serious flaws in the linear congruential
generator. The numbers it produces are quite bad random
numbers. They are not random enough!
• To characterize the correlation in the series of numbers 𝑋𝑖 , we
use the definition of autocorrelation
1 𝑁
σ𝑖=1 𝑋𝑖 − 𝜇 𝑋𝑖+𝜏 − 𝜇 σ 𝑁
𝑖=1 𝑋𝑖 − 𝜇 𝑋𝑖+𝜏 − 𝜇
𝑅 𝜏 = 𝑁 = 𝑁
𝜎 2 σ𝑖=1 𝑋𝑖 − 𝜇 𝑋𝑖 − 𝜇
• 𝑅 𝜏 is between -1 and 1. If the data is random, 𝑅 𝜏 should be
near zero for any and all 𝜏. If non-random, then one or more of
the autocorrelations will be significantly non-zero.
• The autocorrelations between the values of successive random
number will introduce errors into physics calculations that are of
significant size yet hard to detect. For serious computations in
4 physics, it is important to use high quality random numbers.
Other random number generators
• Luckily, there are many other random number generators that
give much better random numbers. Usually the generator of
choice for physics calculations seems to be so-called Mersenne
twister, which is a “generalized feedback shift-register
generator”. It is quite complicated to program, but fortunately
we don’t have to do that. Many software like python provides a
version. It comes in the random package, which contains the
following useful functions:
random() Gives a random float number uniformly in
[0,1), including 0 but not including 1
randrange(n) Gives a random integer from 0 to n-1
randrange(m,n) Gives a random integer from m to n-1
randrange(m,n,k) Gives a random integer from m to n-1 in
steps of k
5
Seed(n) Set up the seed value
Other place to use random number-cipher
• In fact, random number generators is VERY VERY important in
your daily life: they are used in cryptography, the creation of
secret codes or ciphers.
• A simple example of a cipher is the substitution cipher: take
the letters of the alphabet and add a constant to them, e.g. if
the constant is three, then you change A to D, B to E, C to F
and so forth. Decoding the massage just changes D to A, E to B,
F to C and so forth.
• The basic substitution cipher is very easy to break. You only
need to try less than 100 times. In modern cryptography, the
rule is to assume the worst: even the thief knows the principles
behind the cipher you are using, they will not be able to read
your message.
• Instead of adding the same constant to each letter, let us use
different constants for different message and different position
6
of the your message.
Random cipher
• The best way to create a varying cipher is to use random
number!!! If all the numbers in the constant sequence is random,
it is very hard to guess what the next number in the sequence
will be. They need to try all possible combinations. For example,
the message has 𝑀 characters and the number of characters in
the character table is 𝑁, then they need to try around 𝑁 𝑀 times!
• You can make one-time pad using real random numbers, while
this method has a big disadvantage: you have to create the pad
and get it to your friend without anyone else seeing it, also both
parties have to guard it very carefully against theft, and it can
only be used once.
• A much better choice is to use pseudorandom numbers. You do
not the entire pad to use it, and you just need to know the seed
for the random number generator, which is called a key.
• Making a good secret code and making a good random number
7 generator are basically the same problem.
Monte Carlo Method: one sentence summary
𝐼 = 𝐶𝑖
𝑖=1
• Plot the histogram for all the value of 𝜋 for all the samples.
12
Buffon's needle for Pi
• Find a needle or stick with the length to be 𝑙.
• Find a large paper and plot multiple parallel lines with equal
space 𝑡 > 𝑙.
• Drop the stick to the paper and count the times 𝑚 with the stick
crossing the lines and the total number 𝑀 you dropped.
13
Some interesting facts or guess about Pi
• How many digits have human ever got for Pi?
22,459,157,718,361 from Peter Trueb in 2016 after 105 days’
calculation by using a super computer with 72 cores.
https://www.youtube.com/watch?v=a6bT_DVwo7M
15
Monte Carlo method for integral
• We have learned lots of method to do the integral, however, for
some pathological functions, all the methods fail, e.g.
2
2
1
𝐼 = න sin 𝑑𝑥
0 𝑥 2−𝑥
• It is well behaved in the middle of its
range, but it varies infinitely fast at
the edges, which makes the integral
challenging. On the other hand, we
know the value of integral is finite
and since the whole figure fits in a
2 × 1 rectangle.
19
Mean value method and rectangle method
• In the mean value method, the final formula is
𝑁
1
𝐼 = 𝑏−𝑎 𝑓 𝑥 = 𝑏−𝑎 𝑓 𝑥𝑖
𝑁
𝑖=1
• It is exactly same as the formula in trapezoidal method
𝑁 𝑁
𝑏−𝑎
𝐼 = ℎ𝑓 𝑎 + 𝑘 − 1 ℎ = 𝑓 𝑥𝑘
𝑁
𝑘=1 𝑘=1
• The only difference is that in mean value method, we use
random number to get 𝑥𝑖 , while in trapezoidal method, we get 𝑥𝑖
one by one starting from 𝑎.