A simple algorithm that will generate a sequence of integers between 0 and m is:

Similar documents
functions Poisson distribution Normal distribution Arbitrary functions

Chapter 4: Monte Carlo Methods. Paisan Nakmahachalasint

Monte Carlo. Lecture 15 4/9/18. Harvard SEAS AP 275 Atomistic Modeling of Materials Boris Kozinsky

Random Number Generators

Lecture 2 : CS6205 Advanced Modeling and Simulation

Topics in Computer Mathematics

Hands-on Generating Random

Theoretical Cryptography, Lectures 18-20

Random processes and probability distributions. Phys 420/580 Lecture 20

Numerical Methods I Monte Carlo Methods

From Random Numbers to Monte Carlo. Random Numbers, Random Walks, Diffusion, Monte Carlo integration, and all that

Lecture 10. Variance and standard deviation

Math123 Lecture 1. Dr. Robert C. Busby. Lecturer: Office: Korman 266 Phone :

[POLS 8500] Review of Linear Algebra, Probability and Information Theory

STAT2201. Analysis of Engineering & Scientific Data. Unit 3

Scientific Computing: Monte Carlo

Lecture 16. Lectures 1-15 Review

Discrete Math, Fourteenth Problem Set (July 18)

Lecture 6: The Pigeonhole Principle and Probability Spaces

Monte Carlo Techniques

If we want to analyze experimental or simulated data we might encounter the following tasks:

Computer Applications for Engineers ET 601

The Exciting Guide To Probability Distributions Part 2. Jamie Frost v1.1

Mathematical Modelling Lecture 13 Randomness

Lecture 4: Two-point Sampling, Coupon Collector s problem

Lecture 7: Pseudo Random Generators

Lecture 2 Binomial and Poisson Probability Distributions

Bayesian Analysis. Justin Chin. Spring 2018

7 Random samples and sampling distributions

Sampling Distributions Allen & Tildesley pgs and Numerical Recipes on random numbers

Bayesian Methods for Machine Learning

Reduction of Variance. Importance Sampling

IE 581 Introduction to Stochastic Simulation. One page of notes, front and back. Closed book. 50 minutes. Score

Lecture Notes 2 Random Variables. Discrete Random Variables: Probability mass function (pmf)

Bayesian inference. Fredrik Ronquist and Peter Beerli. October 3, 2007

Today: Fundamentals of Monte Carlo

Randomized Algorithms

CPSC 467: Cryptography and Computer Security

Physics 116C The Distribution of the Sum of Random Variables

Lecture 5: Two-point Sampling

Class 12. Random Numbers

Lecture 7 and 8: Markov Chain Monte Carlo

2 Random Variable Generation

Lecture 2. Binomial and Poisson Probability Distributions

1. Introductory Examples

Sources of randomness

Chapter 3. Chapter 3 sections

19 : Slice Sampling and HMC

Computational Physics (6810): Session 13

Integration of Rational Functions by Partial Fractions

Computer Science, Informatik 4 Communication and Distributed Systems. Simulation. Discrete-Event System Simulation. Dr.

How does the computer generate observations from various distributions specified after input analysis?

Monte Carlo and cold gases. Lode Pollet.

Today: Fundamentals of Monte Carlo

Integration of Rational Functions by Partial Fractions

Pseudorandom Generators

Differentiation and Integration

How does the computer generate observations from various distributions specified after input analysis?

Independent Events. Two events are independent if knowing that one occurs does not change the probability of the other occurring

Machine Learning using Bayesian Approaches

CSC 446 Notes: Lecture 13

Pseudo-Random Numbers Generators. Anne GILLE-GENEST. March 1, Premia Introduction Definitions Good generators...

Monte Carlo Methods. PHY 688: Numerical Methods for (Astro)Physics

6.045: Automata, Computability, and Complexity (GITCS) Class 17 Nancy Lynch

Probability Theory. Introduction to Probability Theory. Principles of Counting Examples. Principles of Counting. Probability spaces.

Pseudorandom Generators

Random Number Generation. Stephen Booth David Henty

Quick-and-Easy Factoring. of lower degree; several processes are available to fi nd factors.

Statistics and data analyses

B.N.Bandodkar College of Science, Thane. Random-Number Generation. Mrs M.J.Gholba

CSC 2541: Bayesian Methods for Machine Learning

Week 1 Quantitative Analysis of Financial Markets Distributions A

20.1 2SAT. CS125 Lecture 20 Fall 2016

16 : Approximate Inference: Markov Chain Monte Carlo

Lecture 5: Pseudo-Random Generators and Pseudo-Random Functions

Brandon C. Kelly (Harvard Smithsonian Center for Astrophysics)

Markov Chains Handout for Stat 110

RVs and their probability distributions

E X A M. Probability Theory and Stochastic Processes Date: December 13, 2016 Duration: 4 hours. Number of pages incl.

Lecture 20. Randomness and Monte Carlo. J. Chaudhry. Department of Mathematics and Statistics University of New Mexico

REVIEW: Waves on a String

The random variable 1

CSE 103 Homework 8: Solutions November 30, var(x) = np(1 p) = P r( X ) 0.95 P r( X ) 0.

Probability, For the Enthusiastic Beginner (Exercises, Version 1, September 2016) David Morin,

Monte Carlo Methods in Engineering

Bell-shaped curves, variance

x x 1 0 1/(N 1) (N 2)/(N 1)

Statistical Methods in Particle Physics

CMPSCI 250: Introduction to Computation. Lecture #15: The Fundamental Theorem of Arithmetic David Mix Barrington 24 February 2014

Simulation. Version 1.1 c 2010, 2009, 2001 José Fernando Oliveira Maria Antónia Carravilla FEUP

Lecture 9. Expectations of discrete random variables

Law of large numbers for Markov chains

Chapter-2 Relations and Functions. Miscellaneous

Chapter 3: Random Variables 1

Chapter 2. Random Variable. Define single random variables in terms of their PDF and CDF, and calculate moments such as the mean and variance.

2 Elementary algorithms for one-dimensional integrals

5. Conditional Distributions

X 1 ((, a]) = {ω Ω : X(ω) a} F, which leads us to the following definition:

Random numbers and random number generators

1 Review of The Learning Setting

Transcription:

Using the Pseudo-Random Number generator Generating random numbers is a useful technique in many numerical applications in Physics. This is because many phenomena in physics are random, and algorithms that use random numbers have applications in scientific problems. Most compilers come with a pseudo-random number generator. These generators use a numerical algorithm to produce a sequence of numbers that have many properties of truly random numbers. Although the sequence of pseudo-random numbers is not truly random, a good generator will produce numbers that give essentially the same results as truly random numbers. Most generators determine the pseudorandom number from previous ones via a formula. Once an initial number(s) (or seed(s)) is chosen, then the algorithm can generate the pseudo-random series. A Simple Pseudo Random Number algorithm A simple algorithm that will generate a sequence of integers between 0 and m is: x n+1 = (ax n + b) mod(m) (1) where a and b are constant integers. A sequence of integers x i is produced by this algorithm. Since all the integers, x i, generated are less than m, the sequence will eventually repeat. To have the period for repeating to be as large as possible, we want to chose m to be as large as possible. If m is very large, there is no guarantee that all integers less than m will be included in the sequence, nor is there a guarantee that the integers in the sequence will be uniformly distributed between 0 and m. However, for large m both these two properties are nearly satisfied and the algorithm works fairly well as a pseudo-random number generator. For a 32-bit machine, a good choice of values are a = 7 5, b = 0, and m = 2 31 1, which is a Mersenne prime number. Usually, one does not need to make up one s own pseudo-random number generator. Most C compilers have one built in. Pseudo Random Numbers in C There are various commands in C for generating random numbers. A common one is random(32767) This command returns a number with the properties of a random number with equal probability to lie between 0 and 32767 = 2 16 1. That is, a 16 bit random number 1

with uniform probability. To obtain a pseudo-random number between 0 and 1, the line of code: does the trick. One can also include a line of code that sets the initial seed, or have the program pick a random seed. To generate a random number with uniform probability between a and b, the following code should work: x = a + r*(b-a); Generating a non-uniform probability distribution Sometimes it is useful to generate random numbers that do not have a uniform distribution. This is fairly easy for the case of discrete outcomes. In this case, each discrete outcome i has a probability P i of occuring, where the index i is countable (say from 1 to integer i max ). Note that P i are unitless. For example suppose that you want to simulate an unfair coin: the coin is heads 40% of the time and tails 60% of the time. The following code does the task: if (r 0.4) then heads; if (r > 0.4) then tails; In this example, one throws r with uniform probability distribution between zero and one. One then divides the interval between zero and one into the probability desired for the outcomes. Often the outcome can be continuous. In this case the probabilities are represented as a probability density. Suppose the outcome is the continuous variable x, and the probability density P(x). Then, P(x)dx is the probability that the outcome lies between x and x + dx. Note that P(x) has units of 1/(units of x), since P(x)dx is unitless. Also note that normalization requires that P(x)dx = 1. One can generate random numbers x with probabililty density P(x) in a similar way as the discrete case. Below we describe the method, which will be proven in lecture: 2

1. Obtain a random number r with uniform probability between 0 and 1. That is, r = random(32767)/32767.0; 2. Solve the following integral for x: r = x 0 P(u)du (2) x will be pseudo-random with a probability density P(x). We demonstrate this method with a simple example. Suppose you wanted to generate random numbers x between 0 and 1 with a probability density proportional Cxdx = C/2 = 1. So C = 2. Thus, the normalized probability density is P(x) = 2x. Now we need to relate x to r, which can be done by solving the integral above. to x: P(x) = Cx, where C is a constant. Normalization requires 1 0 r = x 0 r = x 2 2udu or x = r. A sample of code that would generate real numbers x with the probability density P(x) = 2x might look like: x = sqrt(r); An important continuous non-uniform probability density is the Gaussian or Normal distribution with standard deviation σ: P(x) = 1 2πσ e x2 /(2σ 2 ) (3) The following code, which will be explained in lecture, will generate random numbers x with a Gaussian probability distribution: r1 = random(32767)/32767.0; r2 = random(32767)/32767.0; r = s * sqrt(-2*ln(1-r1)) ; theta = 2*pi*r2; x = r * sin(theta); 3

where s is the standard deviation. Metropolis Algorithm (Monte Carlo) The pseudo-random number generator is useful in evaluating multi-dimensional integrals, particularly those found in statistical mechanics. In statistical mechanics one often has to add up, or integrate, functions over configuration space, which is quite large. Sums of the form: µ f(µ)w(µ) where f(µ) is some function of configuration µ of the system with probability w(µ) of occuring. The sum is over all possible configurations µ. For simplicity let s suppose that the number of possible configurations is finite and equal to N tot. We can map each of the N tot configurations to an integer. That is, µ will be an integer between 1 and N tot. One can evaluate this sum approximately by using a random walk method in configuration space. The random walk will produce a sequence of configurations. For example, suppose N tot = 3 and we label the thre possible configurations as 1, 2, or 3. That is µ equals 1, 2, or 3. A random walk sequence of configurations would be a random sequence of numbers that are 1, 2, or 3: 2, 3, 1, 1, 2, 1, 3,... We can label the j th configuration in the sequence as µ j. For example, in the sequence above, µ 1 = 2, µ 2 = 3, and µ 6 = 1. Let N steps be the number of steps in the random walk. If N steps >> N tot the random walk will visit each configuration many times. Let N(µ) be the number of times we visit the configuration µ in the random walk. If we carry out the random walk such a way that N(µ) = N steps w(µ), then the random walk will be useful in evaluating the sum over configurations. This can be seen as follows. The sum over configurations can be written as: f(µ)w(µ) = µ µ f(µ) N(µ) N steps (4) Note that the trick is to have the random walk visit the configuration µ a number of times N(µ) such that N(µ) = N steps w(µ). However, the sum µ f(µ)n(µ)/n steps is equal to the sum ( j f(µ j ))/N steps since N steps factors out of the sum. Thus, we have j f(µ j ) N steps = j f(µ j ) N(µ j) N steps = µ 4 f(µ)w(µ) (5)

which is what we wanted to calculate. This is a nice result. We just need to do the sum of f(µ j ) for a random walk through configuration space such that the sites µ are visited w(µ)n step times. The trick is to find a way to generate an appropriate random walk. The algorithm of Metropolis et. al. is a clever way to produce such a random walk. Next, we state and justify the algorithm. An example program is located on the sample program web page, which allows you to check that the Metropolis algorithm has the desired characteristics. Random Walk of Metropolis et. al. The Metropolis et. al. algorithm (or Monte Carlo algorithm) is a method to obtain the (j + 1)th element, µ j+1, in the series from the jth element in the series, µ j. The method is as follows: 1. Pick a trial configuration µ t randomly, which is close to µ j. 2. If w(µ t ) w(µ j ), then µ j+1 = µ t. 3. If w(µ t ) < w(µ j ), then µ j+1 = µ t with probability r = w(µ t )/w(µ j ), and µ j+1 = µ j with probability (1 r). One can understand why the random walk guided by these rules yields the correct probability as follows. Consider two configurations that could be in the random walk: µ and ν. Let s also suppose that w(ν) < w(µ). Let represent the probability that ν is the next element in the random walk after µ if ν is chosen as the trial step. According to the rules above, = w(ν)/w(µ). Let P(ν µ) represent the probabililty that µ is chosen after ν in the random walk if µ is the trial step. According to the rules of Metropolis, P(ν µ) = 1 since w(µ) < w(ν). Therefore, we have: P(ν µ) = w(ν) (6) w(µ) We will next show that this condition will lead to N(ν)/N(µ) = w(ν)/w(µ) for any two configurations µ and ν. Thus, the random walk will sample the configuration space with the number of visits per site proportional to the probability for the site. Consider a finite size configuration space with N tot sites. Each one is mapped to an integer µ as before. Suppose we have M tot random walkers where M tot >> N tot, and that the walkers all change sites, jump, at the same time. Let m µ be the number of walkers at site µ at a certain time step. Then, the change in m µ, m µ, after the next step is given by: 5

m µ = ν µ(+m ν P(ν µ) m µ ) (7) where as before is the probability that a random walker will jump from configuration (or site) µ to configuration (or site) ν. The term on the left represents the number of walkers that jump to site µ, and the term on the right is the number of walkers that leave site µ and jump to site ν. After many many steps, N large the system of random walkers will stabilize, and the average number of walkers at each site will not change. That is, eventually, m µ 0 for each configuration µ. A solution to the steady state condition is for each term to vanish: m ν P(ν µ) = m µ m ν = m µ P(ν µ) after N large steps. Suppose that there was one random walker that carried out N large M tot steps. Then the number of times the walker visited site µ would be N large m µ times. Multiplying the numerator and denominator of the left side of the above equation by N large yields: N(ν) N(µ) = P(ν µ) (8) The above argument demonstrates that for a random walk for which N steps >>> N tot the ratio of the number of visits to site ν, N(ν) divided by the number of visits to site µ, N(µ) equals the inverse ratio of the probabilities for jumping between the two sites. Therefore, if then P(ν µ) = w(ν) w(µ) (9) N(ν) N(µ) = P(ν µ) = w(ν) w(µ) and the random walk will visit the configurations the correct number of times to give a correct estimate of the sum µ f(µ)w(µ). The Metropolis Algorithm satisfies this requirement. (10) 6