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

Lec11 Random Sampling

computational physics

Uploaded by

kit56142962
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lec11 Random Sampling

computational physics

Uploaded by

kit56142962
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Random number

• To mimic randomness our computer programs themselves will


need to have an element of randomness in them and for that
we need random numbers.
• The accurate random number is usually related to some
physical processes such as the rolling of dice, coin flipping, the
shuffling of playing cards, or a particle following quantum
mechanics. These random number generator can be called
hardware random number generator. However, Because of the
mechanical nature of these techniques, generating large
numbers of sufficiently random numbers required a lot of work
and/or time. Thus, results would sometimes be collected and
distributed as random number tables.
• In real calculations, in fact, we use pseudorandom numbers,
which are not really random at all. They only look random,
being generated by a deterministic formula referred to
1 (inaccurately) as a random number generator.
Linear congruential random number generator
• Consider the following equation 𝑥 ′ = 𝑎𝑥 + 𝑐 mod 𝑚, where 𝑎,
𝑐 and 𝑚 are integer constants and 𝑥 is an integer variable.
• Given a value for 𝑥, this equation takes that value and turns it
into a new integer value 𝑥 ′ . Now suppose we take that new
value and plug it back in on the right-hand side of the equation
again, and so on, generating a stream of integers.
• This is a linear congruential random number generator. It
generates a string of apparently random integers, one after
another. There are a few things to notice: 1) It is not actually
random; 2) The numbers generated are always positive (or zero)
and less than 𝑚; 3) It matters what values you choose for 𝑎, 𝑐
and 𝑚. Only some choices work! 4) For a particular choice of
the constants 𝑎, 𝑐 and 𝑚, you can still get different sequences
of random number by choosing different starting values for 𝑥.
The initial value is called the seed for the random number
2 generator.
The codes and the results

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

• In one sentence, Monte Carlo Method is used to calculate the


summation of a very large number of (almost infinite) terms
based on a random process.

• This method is quite impressive, because we use a random


process to calculate the answer to an exact, nonrandom
question!!! It is really genius and amazing!!!
8
Monte Carlo calculation for Pi
• The trick to use a new method is always to transfer your new
problems to the problems, which can be handled by the new
method, Monte Carlo method.

• Monte Carlo Method is used to calculate the summation of a


very large number of (almost infinite) terms based on a random
process.

• Another important trick is to use the definition of the concepts in


your problems. Then, what’s the definition of Pi?

• Pi is original defined as the ratio of a circle's circumference to


its diameter. It can be defined as the ratio of a circle’s area and
its diameter’s square. Here, we use the second definition, then
to calculate Pi, we need to calculate the area very accurately!
9
Discretize your variable
• To calculate Pi, we need to calculate the
area in the circle.
• We first discretize 𝑥 and 𝑦 (𝑁 points),
and make a grid over the square. Then
the total number of small square it 𝑁 2 .
• We count the number of small square is
𝑀, then 𝜋 ≈ 𝑀/𝑁 2
• The larger number of points 𝑁, the more accurate the results.
Could we choose infinite 𝑁? Yes, we can! We can use a point
to replace the small square.
• First, we randomly choose the positions of 𝑀 points. Then by
counting the number of points falling in the circle 𝑚, we can
estimate the area of circle and then the value of 𝜋
𝑚
𝜋≈
10 𝑀
Practice: actual calculations and error estimation

• Write a function, which could get one sample to calculate the


𝑚
value of 𝜋 ≈
𝑀

• Get 𝑁 different samples and the corresponding value of 𝜋

• the value of 𝜋 for all the samples.

• Plot the histogram for all the value of 𝜋 for all the samples.

• Calculate the autocorrelation function of 𝜋 for all the samples.

• Estimate the confidence interval (error estimation)


11
Results of Pi calculations

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.

• After many times,


you will get
𝑀 2𝑙
𝜋≈
𝑚 𝑡
• You can play or
write a code for this.
Have Fun!

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.

• How many digits does a human can memorize?


70030 from Guinness Book.

• If we have Pi up to 𝑁 digits, are the number of 0,1, 2…,9 almost


equal? Any proof?
We believe that the answer is yes, but no one can demonstrate
it for now! You will be famous if you can demonstrate it.

• All the number in Pi appears in random order? If so, can we use


the calculations of Pi as pseudo random number generator?
I think the answer is also yes, but I did not find a solid proof.
14
Last remark about Pi

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.

• We can use the exactly same method to calculate the area


under the curve as we do for the calculation of Pi and then get
the results for the integral.
16
Mean value method
• As shown for the calculations of Pi, even among Monte Carlo
method, there are different ways to do the actual calculations.
For the integral, the most common and better ways are the
mean value method.
𝑏
• For a general integration, we can want to get 𝐼 = ‫𝑓 𝑎׬‬ 𝑥 𝑑𝑥,
and the average value of 𝑓 𝑥 in the range from 𝑎 to 𝑏 is
𝑏
1 𝐼
𝑓 𝑥 = න 𝑓 𝑥 𝑑𝑥 =
𝑏−𝑎 𝑎 𝑏−𝑎
• Then, we get 𝐼 = 𝑏 − 𝑎 𝑓 𝑥
• By using Monte Carlo, we can easily get the estimated value of
𝑓 𝑥 . We can randomly choose the points between 𝑎 and 𝑏,
1
and then we get 𝑓 𝑥 = σ𝑁 𝑖=1 𝑓 𝑥𝑖 , then we can get
𝑁
𝑁
1
𝐼 = 𝑏−𝑎 𝑓 𝑥 =𝐼 = 𝑏−𝑎 ෍ 𝑓 𝑥𝑖
𝑁
17 𝑖=1
More discussion
• Compared to the methods we learned before for the integral,
Monte Carlo method is quite inefficient with error only
proportional to 1/ 𝑁. Therefore, if other non-Monte-Carlo
methods can do your job, use them.
• However, for many problems (integral), Monte Carlo method is
the only available method, which can give us the right
estimation, and all other methods fail. In general, the problems
are typically the pathological integrands and the integral in high
dimensions. It was shown that Monte Carlo method is the best
method for the integral with the dimensionality large than 8.
• The mean value method can be very easily generalized to the
high-dimension problems. Just replace the 1D random number
𝑥𝑖 to be a random high-dimension vector 𝒓𝑖
𝑁
1
𝐼=𝑉 𝑓 𝑥 = 𝑉 ෍ 𝑓 𝒓𝑖
𝑁
18 𝑖=1
The codes

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 𝑎.

• The randomness in mean value method gives the fundamental


differences, which uses the idea of sampling. This idea allows
us to dramatically improve the efficiency by using importance
20 sampling!!!

You might also like