Physics 433: Computational Physics Lecture 6 Random number distributions Generation of random numbers of various distribuition functions Normal distribution Poisson distribution Arbitrary functions
Random Number Generation Algorithms Uniform deviate Linear congruential algorithm I new = (ai old + b)mod M Random integers I range in value from 0 M - Scale by M to get real random numbers, R, in [0,1] - M large, Choice of a < M, b < M critical Other functional forms of distributions Triangular distribution y = R 1 + R 2 y P y (y) = 2 y 0 y 1 1 y 2 Uniform deviates can be used to generate random deviates that fit other probability distribution functions, p.d.f.
Sums of Uniform Deviates double uniformdistribution() { return ((double) random()) / ((double) LONG_MAX); } double sum2r() // TRIANGULAR { return (uniformdistribution()+uniformdistribution()); } double sum3r() { return (uniformdistribution()+uniformdistribution()+uniformdistribution()); } double sum4r() { return (uniformdistribution()+uniformdistribution()+ uniformdistribution()+uniformdistribution()); }... for large n becomes gaussian or normal distribution - CLT
Central Limit Theorem Central Limit Theorem Convolutions of any distribution normal (gaussian) distribution
Probability Distributions Probability Density Function (p.d.f.) A functional form that describes the probability density Uniform density - all values in the interval are equally probable P R (R) = 1 0 0 R 1 otherwise Exponential density - probability falls of exponentially as the random variable value increases P u (u) = 1 λ e u λ, 0 u Integral of probability density over the complete range is unity Cumulative Distribution Function Integral of probability between minimum and a point in the range x F x (x) = P x (y)dy, where P x (x) is p.d.f. for a x < b a i.e., F x (a) = 0 and F x (b) =1
Inversion Technique pdf - cdf mapping Map interval [a,b) to [0,1) Where slope of F is small, the interval dr maps to large interval dx Uniform deviate interval yields correct F(x) One needs to evaluate and invert it F x (x) or F x -1 (R)
Inversion Technique - II Applicable for simple distribution functions Use normalized distribution function (p.d.f.) Integrate p.d.f. P(x) analytically to obtain c.d.f. F(x), i.e., probability of chosing a value less than x Equate this to a uniform random variable and solve for x. The result is distributed according to the original p.d.f. The method is fully efficient since each uniformly distributed random number R gives an x value
Exponential Distribution Invertible distribution Exponential distribution P u (u) = 1 λ e u/ λ where 0 u mean u = λ, variance u u 2 = λ 2 New random variable, u = λln(1 R) R =1 e u/λ, dr = 1 λ e u/λ du P u (u)du =1.dR = P R (R)dR i.e., R is distributed uniformly Exponential distribution with mean and standard deviation λ is obtained uniformly distributed random variables R simply by computing, u=-λ ln(1-r) double exponentialdistribution(double lambda = 1.) { return (-lambda * log(1. - uniformdistribution())); }
Central Limit Theorem - II Central limit theorem Works with exponential distribution also!
Normal Distribution Normal or Gaussian Distribution P x (x)dx = 1 2 2πσ e (x µ) /2σ 2 dx, mean µ, standard deviation σ Convolutions of other distributions lead to this - CLT But, this distribtution is invertible using a simple trick P x (x)p y (y)dxdy = 1 [ ( x µ ) 2 + ( y µ ) 2 ] 2σ 2 2πσ 2 e dxdy Changing to polar coordinates, x = ρ cosθ + µ, y = ρ sinθ + µ, 1 2 P ρ (ρ)p θ (θ)ρdρdθ = 2πσ 2 e ρ 2σ 2 ρdρdθ Another change of variables, u = ρ 2 2σ 2, R = 1 2π θ P u (u)p R (R)dudR = (e u du)(1 dr) Normal distribution obtained by using exponential and uniform distributions
Normal Distribution Generation of Normal Distribution Uses exponential and uniformly distributed random numbers Inversion formula double normaldistribution(double mean = 0., double sd = 1.) { return (sqrt(2. * sd * exponentialdistribution()) * cos(2. * pi * uniformdistribution()) + mean); }
Discrete Distributions Coin flip - two outcomes head or tail P(head) = P(tail) = 0.5 Given uniform deviate R in [0,1), if R<0.5, head otherwise tail. Die roll - six outcomes 1-6 P(1) = P(2) = P(3) = P(4) = P(5) = P(6) = 1/6 Given uniform deviate R in [0,1), if R<1/6, x=1, 1/6<R<1/3, x=2,... Binomial Distribution (with two equal probability outcomes) Probability of getting k heads when flipping N coins N! 1 P k (k) = N, where k = 0,1,...,N. k!(n k)! 2 Algorithm: simply use coin flip algorithm for N trials Any Discrete Distribution Algorithm for selecting a random variable i with distribution P i (i) is to find the value of i that satisfies: i 1 P i ( j) R < P i ( j), with P i (a 1) 0 j= a i j= a
Discrete Distributions Discrete cumulative distribution i F i (i) = P i ( j) j= a Compute F i (i) just once for all i, and then for each random number requested pick the value based on uniform deviate [0,1) F i (i 1) R < F i (i) More efficient - even better if search algorithm is O(ln N)
Poisson Distribution Example: Radioactive decay Probability to observe 1 decay in time t, p=β t, where β=αn, N is total number of parent nuclei and t is small compared to time T in which observations are made, i.e., β t<<1. Probability of observing n decays in time T is: P = m! m! (m n)!n! pn (1 p) m n = (m n)!n! In the limit of t 0, (i.e., m ), 1 βt m e βt, 1 βt m m with µ = βt, n 1, βt m n 1 βt m m! (m n)!n! mn m n P = µn e µ, Poisson distribution, mean = µ, variance = µ n! If n events are observed the standard deviation is n Examples: Number of observed events when efficiency is constant Number of entries in a histogram bin
Poisson vs Gaussian The Gaussian (Normal) distribution is a reasonable approximation of the Poisson distribution even for µ as small as 5. Asymmetric for small µ.
Acceptance-Rejection Technique Generation of random numbers from arbitrary distribution function, P x (x) Use a trial number x try at random Accept it with the probability proportional to f(x try ) 1. Determine P x max Px (x) 2. Select x try = a + (b a)r 1 3. Compute P x (x try ) 4. Accept x try as the generated random number if P (x ) x try max R P 2 x Also yields the integral! b I = P(x)dx n accept max P x (b a) a n try - But, the method is inefficient in generating random number series if there are sharp peaks in P x (x), because many trial events will be not accepted
Slide Title