8
TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th , 2012 CECAM school on Numerical simulations F. Becca & G. Lattanzi SISSA

TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Embed Size (px)

Citation preview

Page 1: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

TUTORIAL no. 1Random numbers

G. LattanziUniversità degli studi di Bari “Aldo Moro”

Trieste, July 9th, 2012

CECAM school on Numerical simulationsF. Becca & G. Lattanzi

SISSA

Page 2: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Random numbers generators

The programming language of your choice contains a built-in function that generates random numbers:

• Fortran: RAND• C++: rand• Perl: rand()• Python: random

There are also alternatives, based on different and more sophisticated algorithms, e.g. those proposed by Numerical Recipes

http://www.nr.com/

Page 3: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Exercises with random numbers (1)1. Casuality test: consider the function, being r a random variable uniformlydistributed between 0 and 1:

In the large N limit:

ψ r1,r2,K ,rN( ) =1

Nri

i=1

N

ψ =0.5

σ 2 = ψ 2 − ψ2=1

12N

Verify that your favourite built-in random number generator passes the confidence test at 5%, i.e. that 95% of the realizations fall in the interval (μ-2σ,μ+2σ). Plot the obtained values of ψ for N=10, 20, 50, 100.

Page 4: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Modules

To see the list of available modules: module availTo load a specific module: module load name_module

LOCATED AT id001719CALLED random.c

Real(8) drand1

Iseed=142527Call rand_init(iseed)

R=drand1()

Page 5: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Exercises with random numbers (2)2. Random integration. Consider the following integral:

I = f (x)dx = 1− x 2dx0

1

∫0

1

We will evaluate the integral exploiting random numbers: we generate Nnumbers xi in the interval [0,1] and calculate:

F =1

Nf (x i) =

1

N1− x i

2

i=1

N

∑i=1

N

Verify that the average value is, as expected, π/4. What about the variance? What is its expected value?

Page 6: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Exercises with random numbers (3)3. Rejection method. We want to obtain a sequence of random numbers

distributed according to a probability distribution ρ defined in [a,b] with a maximum value ρΜΑΧ.

Method:I. We extract two series of random numbers uniformly distributed between

0 and 1: we call them ηi and θi.II. We use the ηi’s to obtain a sequence of random numbers xi uniformly

distributed between a and b:

III. We use the θi’s to generate random numbers yi uniformly distributed between 0 and ρΜΑΧ:

IV. We accept the (xi,yi) if and only if: €

x i = (b− a)η i + a

y i = ρMAXθ i

y i ≤ ρ(x i)

Page 7: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Exercises with random numbersAlgorithm:1. Extract ηi

2. Obtain xi=(b-a)ηi+a3. Extract θi 4. Obtain yi=θi*ρΜΑΧ

5. Check: yi ≤ ρ(xi)? If yes, accept (xi, yi) . If not, reject it.

Exercise 3. Use the above specified algorithm to generate random numbersdistributed between -1 and 1 with probability density:

ρ(x) =2

π1− x 2

Having divided the interval [-1,1] in bins, plot the histogram of the valuesgenerated with this procedure. Plot also in the plane (x,y) the points obtained.

Page 8: TUTORIAL no. 1 Random numbers G. Lattanzi Università degli studi di Bari “Aldo Moro” Trieste, July 9 th, 2012 CECAM school on Numerical simulations F

Exercises with random numbersJustification of the algorithm

The probability to obtain the sequence of numbers yi is the product of the probabilityto extract xi with the given uniform probability distribution:

And the acceptance probability

u(x)dx =1

b− adx

α(x) =f (x)

fMAXThis function is exactly ρ(x), apart from a normalization factor:

α(x)u(x)dx =1

b− a

1

ρMAXρ(x)