Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical...

49
Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for Theoretical Astrophysics University of Oslo Fall 2005 Edition

Transcript of Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical...

Page 1: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

Introduction to Numerical Simulation

of Many-Particle SystemsMethods and Applications

Jan Trulsen

The Institute for Theoretical Astrophysics

University of Oslo

Fall 2005 Edition

Page 2: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2

Page 3: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

Contents

1 Introduction 5

2 Random Variables 7

2.1 General definitions and notation . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.1.1 Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.1.2 Probability densities and distribution functions . . . . . . . . . . . . . 8

2.1.3 Expectation value and variance . . . . . . . . . . . . . . . . . . . . . . 9

2.1.4 Estimates and estimators . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.1.5 Multi-dimensional random variables . . . . . . . . . . . . . . . . . . . 12

2.2 Generation of uniformly distributed numbers . . . . . . . . . . . . . . . . . . 13

2.2.1 Linear congruential number generators . . . . . . . . . . . . . . . . . . 14

2.2.2 Generalized feedback shift register number generator . . . . . . . . . . 14

2.2.3 Bit-reversed number generator . . . . . . . . . . . . . . . . . . . . . . 15

2.2.4 Prime root fraction number generator . . . . . . . . . . . . . . . . . . 15

2.2.5 Shuffling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2.3 Tests of randomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.3.1 The histogram test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.3.2 Uncorrelatedness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.3.3 The random walk test . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.3.4 The decay test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.4 Transformation of random variables . . . . . . . . . . . . . . . . . . . . . . . 17

2.4.1 The fundamental theorem . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.4.2 The inversion method . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2.4.3 The rejection method . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2.4.4 The mixing method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2.5 Special methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.5.1 Adding random numbers . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.5.2 The Box-Muller algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.5.3 The Metropolis algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.6 Stochastic processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.6.1 The ergodicity theorem . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3 Monte Carlo methods 29

3.1 Evaluation of integrals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3.1.1 Sample mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3

Page 4: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

4 CONTENTS

3.1.2 Importance sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.2 The canonical ensemble . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

3.2.1 The ideal gas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333.2.2 Error analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

3.3 The microcanonical ensemble . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

4 Dynamical Systems 394.1 Equations of motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394.2 Orbit integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

4.2.1 Leap-frog method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404.2.2 Leap-frog and velocity dependent forces . . . . . . . . . . . . . . . . . 424.2.3 The Verlet method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444.2.4 Runge-Kutta method . . . . . . . . . . . . . . . . . . . . . . . . . . . 454.2.5 Predictor-Corrector methods . . . . . . . . . . . . . . . . . . . . . . . 46

4.3 Periodic boundaries and the Ewald’s problem . . . . . . . . . . . . . . . . . . 48

Page 5: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

Chapter 1

Introduction

From an early start in the late 1950’s or early 1960’s numerical simulation has grown intobecoming “the third way”. The scientist of today has at her disposal not only the twotraditional ways of theory and experiment, but also the techniques and methodologies ofnumerical simulations. Science is no longer resting on two legs only, but can also find supportfrom the third leg. Numerical simulations is now a tool that can provide physical insight inthe properties of complex systems, allowing for numerical experiments to be performed, evenallowing for practical measurements in numerical experiments. Numerical experiments havebeen crucial for the interpretation of results from real experiment, numerical simulations havebeen necessary for the design of complex experiments.

Numerical simulations have been boosted by the parallel development of electronic com-puters and the dramatic increase in their computing power, but also by the development ofsuitable numerical methodologies and algorithms. The relative importance of numerical sim-ulations will continue to increase in the future. Numerical simulations will not make theoryand experiments obsolete, but numerical experiment will to an increasing degree representcost effective alternatives to the design and operation of advanced experiments, or allow forthe study of complex systems still outside the reach of theoretical or experimental treatments.

Numerical simulation methods have been applied to a large variety of different systems.Generally they are applied to many-particle systems, or alternatively systems with manydegrees of freedom. The particles can be simple particles with up to 6 translational degrees offreedom, as particles with mass in a self-gravitating system or charged particles in a plasma.But the particles can also be of a more complicated structure with internal degrees of freedom,as in polymer studies.

It is usually conceptually unthinkable that the initial state can be specified precisely atsome initial time. We shall therefore have to specify the initial state in a probabilistic way.This means that the results of the simulation shall also have to be treated in a statisticalsense.

The dynamics of the system may be deterministic as for Hamiltonian systems, but theymay also be of stochastic nature or of mixed type. For the deterministic system we arefacing the problem of integrating a large set of equations of motion to some desirable level ofaccuracy. What method to chose will then depend on the range of the inter-particle forcesinvolved. For the stochastic system the “motion” of the system may consist of a sequence ofpossible states of the system chosen according to some rule.

The systems may finally be close to thermal equilibrium allowing for the methods of sta-

5

Page 6: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

6 CHAPTER 1. INTRODUCTION

tistical mechanics to be incorporated. But the system may also be kept far from equilibrium.The numerical simulation will produce very large set of data about the system. To in-

terpret these large data sets we shall have to take resort to sometimes rather advanced dataanalysis and data display methods. In this respect the data analysis of a numerical simulationexperiment may resemble the problem of data analysis from real experiments, except for thefact that the numerical experiment allows for data of far greater detail and completeness thanexperimenters can normally hope to obtain from their experiments. The effort put into thedata analysis and display part of a numerical simulation code may often exceed the effort putinto the simulation part proper.

In the following we intend to survey some of the methods in use today for a large varietyof different systems. This will not allow for very detailed treatment of the individual system.It is, however, hoped that the following survey will be helpful as a starting point for a morethorough study of this extensive and interesting subject.

Page 7: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

Chapter 2

Random Variables

2.1 General definitions and notation

For the subsequent discussion it will be convenient to introduce some definitions and a sys-tematic notation.

2.1.1 Probabilities

A random variable x is an entity that holds all possible values of a certain experiment. Eachsuch possible value or outcome is called a realization x of x. If x denotes the output voltagefrom a noise generator, then x may denote the actual output voltage at a certain time.Random variables may takes discrete or continuous values. The throw of a dice producesdiscrete results, our noise generator generates continuous ones1. We will mostly be dealingwith random variables taking continuous values in the subsequent discussion.

Let M denote an event, for instance, the event that the output voltage from the noisegenerator at a given time takes a value within a certain limited range, M = x1 < x ≤ x2.The probability that the event M occurs will be denoted P(M). Probabilities are real numbersin the range (0, 1),

0 ≤ P(M) ≤ 1,

0 being the probability for the occurrence of the impossible event O, 1 being the probabilityfor the certain event I.

If M and A are two events then P(MA) is the probability for the joint occurrence of Mand A. The conditional probability P(M|A) for the occurrence of M given that A actuallyoccurred, is defined by

P(MA) ≡ P(M|A)P(A). (2.1)

Two events A1 and A2 are mutually exclusive if they cannot occur together. Thus, if A1

and A2 represent the events that the voltage of our noise generator fall within two disjunctoutput bins, then they cannot both occur in a measurement at a given time. The probabilityfor the occurrence of A1 or A2, where A1 and A2 are mutually exclusive events, equals thesum of the probabilities for A1 and A2,

P(A1 + A2) = P(A1) + P(A2).

1For sake of argument we assume that we have at our disposal an ideal analog voltage recording device

7

Page 8: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

8 CHAPTER 2. RANDOM VARIABLES

The certain event I may be written as a union of mutually exclusive events Ai, i =1, · · · , n,

A1 + A2 + · · · + An = I.

If the total output range of our noise generator is divided into a series of n disjoint outputbins and Ai is the event that the output voltage at the given time falls within bin i, then thecertain event I, that the noise generator actually produces an output voltage at the giventime, is the sum of n events associated with each of the output bins.

If Ai, i = 1, · · · , n are mutually exclusive events then also MAi, i = 1, · · · , n are mutuallyexclusive. But that means that

P(M) = P(MI) = P(Mn∑

i=1

Ai) =n∑

i=1

P(MAi) =n∑

i=1

P(M|Ai)P(Ai). (2.2)

Making use of this result we also note that

P(Ai|M) =P(MAi)

P(M)=

P(M|Ai)P(Ai)∑n

i=1 P(M|Ai)P(Ai). (2.3)

In both (2.2) and (2.3) we made use of the definition (2.1) of conditional probabilities. Theidentity (2.3) is known as Bayes’ theorem. Bayes’ theorem will be a useful tool for thefollowing discussion.

2.1.2 Probability densities and distribution functions

For any continuous random variable x two particular events will play a special role. Considerfirst the infinitesimal event M = x ≤ x < x + dx. The probability density function fx(x)for the random variable x is defined by

P(x ≤ x < x + dx) ≡ fx(x) dx. (2.4)

Since probabilities are non-negative numbers and furthermore since any realization of x nec-essarily falls within its total range (a, b), we have

fx(x) ≥ 0 and

∫ b

afx(x) dx = 1. (2.5)

Next consider the cumulative event M = x ≤ x. The probability that a realization ofx is to take a value less than or equal to x defines the cumulative distribution function Fx(x),

P(x ≤ x) ≡ Fx(x) =

∫ x

afx(x) dx. (2.6)

The cumulative distribution function, which we will often refer to in shorthand notation asthe distribution function, is a non-decreasing function of x. We note that Fx(a) = 0 andFx(b) = 1. We also note from (2.7) that the probability density and the distribution functionare related through

fx(x) =dFx(x)

dx. (2.7)

Page 9: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.1. GENERAL DEFINITIONS AND NOTATION 9

Some particular probability density functions will be dealt with frequently enough towarrant a special notation. A random variable x is uniform in (a, b) if

fx(x) = U(a, b; x) ≡

1/(b − a) for a ≤ x < b0 otherwise.

(2.8)

A random variable x is normal or Gaussian with mean µ and variance σ2 if

fx(x) = N(µ, σ; x) ≡ 1√2πσ2

exp(−(x − µ)2/2σ2). (2.9)

For convenience we shall often omit the argument x when referring to these densities. Weshall also indicate that x represents a realization of the random variable x with density fx(x)by the shorthand notation

x ∈ fx.

A frequency interpretation of the density function is readily available. Among a largenumber N of realizations of x, the number dN of these expected to fall within the interval(x, x + dx) is given by

dN

N= fx(x) dx.

2.1.3 Expectation value and variance

The expectation value (or the mean value or in short the mean) of the random variable x isfound by weighting each possible outcome x of the variable with the probability density fx(x)for that particular outcome, that is,

µx = 〈 x 〉 =

∫ b

axfx(x) dx.

The expectation value represents the average value of possible outcomes of the variable.The mean square deviation of the variable x from its expectation value µx is called the

variance of the variable

σ2x = 〈 (x − µx)

2 〉 =

∫ b

a(x − µx)

2fx(x) dx.

The square root of the variance is the standard deviation σx of the variable x. The standarddeviation is a measure of the average width of the density distribution function. Two typicalsituations are illustrated in figure 2.1, in a) for x uniform in (-1,1), and in b) for x normalwith zero mean and standard deviation unity. The size of the standard deviation is indicatedby arrows in the two cases.

Any function g(x) of the random variable x is itself a random variable2 provided g(x) isdefined over the full range of x. The definitions of expectation value and variance are readilyextended to any such variable. The expectation value of g(x) is calculated as

µg = 〈g(x)〉 =

∫ b

ag(x)fx(x) dx.

2For this to be true it is also necessary that1) for every y the set x; g(x) ≤ y must consist of the union of a countable number of intervals2) the events g(x) = ±∞ must have zero probability.

Page 10: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

10 CHAPTER 2. RANDOM VARIABLES

Figure 2.1: Illustrating mean value and variance

The corresponding expression for the variance of g(x) is

σ2g = 〈g2(x)〉 − 〈g(x)〉2 = 〈(g(x) − µg)

2〉 =

∫ b

a(g(x) − µg)

2fx(x) dx.

Quiz 2.1 : Calculate the mean µx and variance σ2x for a random variable x

a) uniform in (-1,1)b) normal with zero mean and unity varianceIn each case, what is the probability P(µx − σx ≤ x < µx + σx)?

2.1.4 Estimates and estimators

If the analytical form of the density function fx(x) is not known, approximations to ex-pectation value may be estimated from a suitable sample of independent realizations of x,x(1), x(2), . . . , x(n). A commonly used estimate for the expectation value µx is the sample

mean

mx ≡ 1

n

n∑

i=1

x(i). (2.10)

A corresponding estimator mx is constructed by replacing the individual realizations x(i)

by the random variable itself. The estimator is thus a random variable. It is said to be anunbiased estimator if the expectation value of the estimator equals the expectation value ofthe variable itself. The sample mean is an example of an unbiased estimator, thus

〈mx〉 =1

n

n∑

i=1

〈x(i)〉 = µx.

A corresponding estimate for the variance σ2x is the sample variance

s2g ≡ 1

n − 1

n∑

i=1

(x(i) − mx)2. (2.11)

Page 11: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.1. GENERAL DEFINITIONS AND NOTATION 11

The corresponding estimator is an unbiased estimator. To show this we note that

〈s2x〉 =1

n − 1

n∑

i=1

〈(

(x(i) − µx) − (mx − µx))2

=1

n − 1

n∑

i=1

〈(x(i) − µx)2 − 2(x(i) − µx)(mx − µx) + (mx − µx)

2〉

=1

n − 1

n∑

i=1

〈(x(i) − µx)2〉 − 2

n

n∑

i,j=1

〈(x(i) − µx)(x(j) − µx)〉

+ n · 1

n2

n∑

i,j=1

〈(x(i) − µx)(x(j) − µx)〉

=1

n − 1

(

nσ2x − n · 2

nσ2

x + n · 1

nσ2

x

)

= σ2x.

In this derivation we made use of definition (2.10), and we assumed that subsequent appear-ances of the random variable x(i) can be considered independent of each other,

〈x(i)x(j)〉 = 〈x(i)〉〈x(j)〉 for i 6= j.

This means, in particular, that all contributions from the double sums in the expression for〈s2x〉 vanish for i 6= j.

If the sample mean mx were calculated for subsequent samples of size n, different val-ues would in principle be obtained each time. The expected spread in these values, or theuncertainty in mx, is represented by the variance of the sample mean

σ2mx = 〈m2

x〉 − 〈mx〉2 =1

n2

n∑

i,j=1

〈x(i)x(j)〉 − 〈x〉2

= n · 1

n2〈x2〉 + n(n − 1) · 1

n2〈x〉2 − 〈x〉2 =

1

n〈x2〉 − 1

n〈x〉2 =

1

nσ2

x. (2.12)

Not unexpectedly the uncertainty of the sample mean decreases with the sample size n, in factinversely with n. A practical approximation to the uncertainty in sample mean is achievedby estimating the variance σ2

x on the right hand side of (2.12) by the corresponding samplevariance s2

x, that is,

σ2mx ≈ 1

ns2x. (2.13)

In the ”physicists” way we write

µx ≈ mx ±sx√n

, (2.14)

indicating that there is approximately a 2/3 probability that the expectation value µx fallswithin the confidence interval (mx−sx/

√n, mx+sx/

√n). By increasing the confidence interval

by a factor 2 to mx ± 2sx/√

n, the corresponding probability increases to approximately 0.95.In the derivation of (2.12) we assumed repeated appearances of the random variable to

be statistically uncorrelated. If this condition is not satisfied, then (2.14) will underestimatethe inherent error. We will return to a discussion of this effect in the following.

Page 12: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

12 CHAPTER 2. RANDOM VARIABLES

2.1.5 Multi-dimensional random variables

In section 2.1.2 basic definitions for a single random variable were introduced. We shall alsoneed to deal with multi-dimensional random variables. Since the generalizations to higherdimensions are formally trivial, we restrict ourselves here, for notational and illustrativeconvenience, to the two-dimensional case.

Let x and y represent continuous random variables. Their joint probability density func-tion fxy(x, y) is defined by

P(x ≤ x < x + dx, y ≤ y < y + dy) ≡ fxy(x, y) dxdy. (2.15)

Allowable density functions must be non-negative and normalized, that is,

fx,y(x, y) ≥ 0 and

∫ ∫

fxy(x, y) dxdy = 1.

The corresponding two-dimensional distribution function Fxy(x, y) is defined by

P(x ≤ x, y ≤ y) ≡ Fxy(x, y) =

∫ x

−∞

∫ y

−∞fxy(x, y) dxdy.

The two-dimensional density and distribution functions are seen to be related by

fxy(x, y) =∂2

∂x∂yFxy(x, y). (2.16)

From the two-dimensional probability density function fxy(x, y) two marginal probability

density functions fx(x) and fy(y) may be derived by allowing for any outcome of y and x,respectively

fx(x) =

∫ ∞

−∞fxy(x, y) dy and fy(y) =

∫ ∞

−∞fxy(x, y) dx.

From the definition of conditional probability (2.1) we may construct different conditional

density or distribution functions. Thus, the cumulative probability distribution function of ygiven the event M = x ≤ x is given by

Fy(y|x ≤ x) ≡ P(y ≤ y|x ≤ x) =P(x ≤ x, y ≤ y)

P(x ≤ x)=

Fxy(x, y)

Fx(x). (2.17)

The corresponding conditional probability density function is found by differentiating withrespect to y

fy(y|x ≤ x) =∂Fxy(x, y)/∂y

Fx(x). (2.18)

If we replace the cumulative event M = x ≤ x in (2.18) by the event M = x ≤ x < x+∆xand take the limit ∆x → 0 then

fy(y|x) ≡ lim∆x→0

fy(y|x ≤ x < x + ∆x) =fxy(x, y)

fx(x). (2.19)

Finally, if we make use of the fact that correspondingly

fxy(x, y) = fx(x|y)fy(y)

Page 13: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.2. GENERATION OF UNIFORMLY DISTRIBUTED NUMBERS 13

and

fx(x) =

∫ ∞

−∞fx(x|y)fy(y) dy

then (2.19) also leads to Bayes’ theorem for probability densities

fy(y|x) =fxy(x, y)

fx(x)=

fx(x|y)fy(y)∫∞−∞ fx(x|y)fy(y) dy

. (2.20)

Two random variables x and y are statistically independent if the conditional densityfy(y|x) of y given x is independent of x, that is, fy(y|x) = fy(y). This means that their jointdensity function may be written as the product of the individual densities in the two variables

fxy(x, y) = fx(x)fy(y).

The joint density function fxy(x, y) is needed to calculate the mean value of any functiong(x, y) of the two random variables x and y

〈 g(x, y) 〉 =

∫ ∫

g(x, y)fxy(x, y) dxdy. (2.21)

Of particular interest in this respect is, besides the mean values µx and µy and the variancesσ2

x and σ2y of each of x and y, their covariance defined by

γxy ≡ 〈 (x − µx)(y − µy) 〉 = 〈 xy 〉 − 〈 x 〉〈 y 〉 (2.22)

with the corresponding correlation coefficient

r =γxy

σxσy. (2.23)

Two random variables x and y are uncorrelated if their covariance vanish, that is, if

〈 xy 〉 ≡ 〈 x 〉〈 y 〉.Two random variables x and y that are statistically independent are also uncorrelated, butthe inverse statement does not hold.

2.2 Generation of uniformly distributed numbers

A property of true random number generators is that it shall be impossible to predict withcertainty the next number to be generated. Such generators may be implemented for in-stance by sampling and digitizing the output voltage from a random noise generator. Froma numerical point of view this will be an unpractical device: because of speed, because ofthe requirement of special hardware, and not the least for diagnostic purposes. With such anumber generator it will be impossible to recreate a specific fault situation in a calculation.

Numerically we shall prefer software implemented random number generators that willproduce identical series of numbers when started from identical initialization. That is, weshall prefer “deterministic” random number generators. The important point is that the seriesof numbers generated shall for all practical purposes “appear” to have random properties.Numbers generated in this manner are often referred to as pseudo-random numbers. We shallreturn to a discussion of criteria for verifying such apparent randomness in the next section.Here we will describe some popular generators of uniformly distributed numbers.

Page 14: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

14 CHAPTER 2. RANDOM VARIABLES

2.2.1 Linear congruential number generators

The linear congruential (LC) number generator produces a series x(0), x(1), x(2), · · · of inte-gers via the algorithm

x(n+1) = (ax(n) + c)modm, (2.24)

where a, c and m are integer constants, and the integer x(0) is called the seed. One commonchoice for the constants a, c and m are listed in table 2.1.

a c m

214 − 1 = 16807 0 231 − 1 = 2147483647

Table 2.1: Linear congruential generator parameters

After being applied a certain number of times, the algorithm (2.24) will start repeatingitself. The number of times the algorithm may be applied before a previously drawn numberis reproduced is called the period of the sequence. A normal requirement on the generatorwill be that the period of the random number generator exceeds the number of times thatthe generator will be invoked in a given calculation. For a given m, this period depends onthe choice of a and c, but also on the seed x(0). Obviously, this period cannot exceed m − 1.The linear congruential number generator with values for a, c and m as given in table 2.1has the rather exceptional property that the period actually takes its maximum value. Afterbeing applied m − 1 times every integer number between 1 and m − 1 have been generatedonce and only once in a deterministic but “apparently random” order. For this to occur it isnecessary that m is prime. Thus, if d is a divisor of m and x(i) is a multiple of d, then allsuccessors are multiples of d. For the maximum possible period to occur it is also necessarythat an is not a multiple of m for any n < m. The value of a given in table 2.1 satisfies thisrequirement for the given m. Another such value is a = 65539, but this choice produces aninferior sequence regarding correlations between successive numbers in the sequence.

The generator (2.24) will produce integer numbers in the range (0, m). Real numbers inthe range (0, 1) are produce by dividing each of these integer numbers by m.

2.2.2 Generalized feedback shift register number generator

The generalized feedback shift register (GFSH) number generator also operates on integernumbers, and according to the algorithm

x(n) = x(n−p) ⊕ x(n−q) for n > p > q. (2.25)

Here p and q are integer constants and ⊕ represents the bit-wise exclusive-or operation. Thegenerator requires that the first p numbers in the series be generated by some other means,for instance by a linear congruential number generator. Not every choice of the constants pand q will produce good series. Some popular choices are listed in table 2.2.

p 31 250 521

q 3 103 168

Table 2.2: Generalized feedback shift register parameters

Page 15: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.2. GENERATION OF UNIFORMLY DISTRIBUTED NUMBERS 15

Real numbers in the range (0, 1) are again produced by dividing by the maximum integervalue.

2.2.3 Bit-reversed number generator

The bit-reversed (BR) number generator produces a series of real numbers in the range (0, 1)directly. The nth number in the series, x(n), is derived by writing the integer number n inbasis b, reversing the order of basis b digits and considering this reversed sequence of digitsas the basis b fraction of the number sought. The procedure is illustrated in table 2.3 for thecase of basis b = 2.

basis reversed

n 2 fraction x(n)

0 0 0.0 0.01 1 0.1 0.52 10 0.01 0.253 11 0.11 0.754 100 0.001 0.125

Table 2.3: Bit-reversed numbers basis 2

As will be seen, a sequence of bk bit-reversed numbers for any integer value of k will be beequidistantly distributed in the range (0, 1). Even if this may not appear particularly random,for special purposes this sequence may be quite useful. For reasons to be explained in thenext section we shall refer to the bit-reversed number series as having “quiet start” property.

2.2.4 Prime root fraction number generator

Our last example of number generators is the prime root fraction (PRF) number generator.This generator produces a series of real numbers in the range (0, 1) from the algorithm

x(n+1) = frac (x(n) +√

p), (2.26)

where p is a small prime number, p = 2, 3, 5, · · · and frac indicates that only the fractionalpart of the argument is to be retained. The seed x(0) may be any real number in the givenrange. The prime root fraction algorithm will also produce number series with “quiet start”property.

2.2.5 Shuffling

An additional element of ”randomness” may be introduced in each of the generators describedabove through a process usually referred to as shuffling. A certain number n of randomnumbers are initially placed in a ”bucket”. An additional random number generator is thenused to draw numbers at random out of the bucket while at the same time refilling the bucketwith the the next number from the original generator. In the long run the shuffled seriesof number will contain the members of the original series, but in a different order. Theimportance of this scheme is that relationships between subsequent numbers in the originalseries be broken up.

Page 16: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

16 CHAPTER 2. RANDOM VARIABLES

2.3 Tests of randomness

A necessary and sufficient test of ”randomness” in a number series x(0), x(1), x(2), · · · doesnot exist. In addition, depending on the actual application some properties of ”randomness”may be more important than others. In the following we will describe some useful tests ofparticular aspects.

One obvious test have already been mentioned. For the integer-based number generatorsthe period of the series should be long enough. Long enough would normally mean that theperiod exceeds the number of times the generator must be invoked in a given calculation.

2.3.1 The histogram test

The histogram distribution of the produced numbers should agree with the desired distribu-tion. This does not mean that an exact agreement should be expected. Statistical fluctuationsin finite size samples will always be present. A useful quantitative test that these fluctuationsremains within probable bounds is the χ2-test. For this test the total range of the generatoris divided into a certain number M of bins. If the observed number of hits in bin number iis ni while the expected or theoretical number of hits is ti, the test requires the calculationof the weighted square deviation

χ2 =M∑

i=1

(ni − ti)2

ti. (2.27)

It can now be shown that the χ2-values for finite size samples of an ideal random numbergenerator will be distributed according to

P(χ2 ≤ x) = P (x, ν) =1

2ν/2Γ(ν/2)

∫ x

0t(ν−2)/2 exp(−t/2) dt. (2.28)

Here ν = M − 1 is the number of degrees of freedom, the number of bins less one. The onestems from the constraint that the number of hits in the collection of all bins must equal thesample size N . Conversely, we may write this result in the form

P(χ2 > x) = Q(x, ν) = 1 − P (x, ν). (2.29)

Some corresponding values of x for different values of the confidence level Q = q for differentdegrees of freedom ν are listed in table 2.4.

ν\q .005 .01 .025 .05

3 12.84 11.34 9.35 7.8110 25.18 23.21 20.48 18.3130 53.67 50.89 46.98 43.7750 79.49 76.15 71.42 67.50

100 140.17 135.81 129.56 124.34

Table 2.4: χ2-deviations as function of degrees of freedom ν and significance level q

Page 17: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.4. TRANSFORMATION OF RANDOM VARIABLES 17

The table entries should be interpreted in the following way. When making use of an idealrandom number generator, there is, in the case that the number of bins is M = 101, a q = 5% chance that the χ2-deviation exceeds the value x ≈ 124.

In a practical application of the χ2-test we should make sure that the bins are chosen suchthat the theoretical number of hits in each bin are approximately equal. A bin with a smallnumber of theoretical hits ti would otherwise tend to dominate the test.

2.3.2 Uncorrelatedness

Another property that a random number series must satisfy is that there should not exist anycorrelation between subsequent members in the series. A qualitative check on this propertymay be to make a scatter plot of x(i+k) versus x(i) for different values of k > 0. For the idealrandom number series no apparent structure in the distribution of points (x(i), x(i+k)) shouldbe visible.

The unshuffled prime root fraction generator will fail this test fragrantly.

2.3.3 The random walk test

A simple qualitative test of uncorrelatedness is the two-dimensional random walk test. Inthis test Nw different random walks, each consisting of Ns steps and starting at the origin.The individual steps are determined from the random number series as (x(i) − .5, x(i+1) − .5).From an ideal random number series we would expect to see equal number of the randomwalks to end up in each of the four quadrants. Thus, considering each quadrant as a binfor the χ2-test, we have M = 4 and ti = Nw/4. The values of the χ2-deviation at differentsignificance levels can be read off from table 2.4.

2.3.4 The decay test

One final test related to uncorrelatedness will be mentioned. Divide the two-dimensional unitsquare (0, 1)× (0, 1) into a number L2 of equal sized cells. Mark each of these cells with ones.Next pick random points (xi, xi+1) from subsequent numbers in the series. If this point fallswithin a cell marked with one, change the marking to zero. The process described resemblesradioactive decay in a sample of radioactive atoms. A cell marked with one represents anon-decayed atom. After picking tL2 points the number of remaining cells marked with onesare expected to be decreased with the factor exp(−t).

2.4 Transformation of random variables

In the previous section we have seen how to construct generators for random variables uni-formly distributed in the unit interval. Our next task will be to extend our techniques tobe able to construct generators for random variables with arbitrary densities. We shall seethat starting from a random variable x with a continuous density fx we may generate a ran-dom variable y with any given density fy through a proper transformation y = g(x). Thetransformation is to be interpreted in the sense that for any realization xi of the randomvariable x, the corresponding realization yi of the random variable y is given by yi = g(xi).Before demonstrating that this is actually possible, we shall consider the simpler problem of

Page 18: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

18 CHAPTER 2. RANDOM VARIABLES

deriving an explicit expression for the density fy for the random variable y derived from agiven random variable x with density fx through a given transformation y = g(x). The resultwill be referred to as the fundamental theorem. In the subsequent subsection we return to theoriginal problem, the inverse problem. We follow up by considering some additional generalmethods, the rejection method and the mixing method, before finally discussing a selectionof important special methods, among them the Metropolis algorithm.

2.4.1 The fundamental theorem

Consider a continuous function y = g(x) with the property that for any value y within itsrange only a finite number of solutions xi, i = 1, 2, ..., n exist, that is,

y = g(x1) = g(x2) = ... = g(xn).

A possible situation is illustrated in figure 2.2. The single infinitesimal y-interval (y, y + dy)will then correspond to the union of a finite number of x-intervals of widths dxi = dy/ |g′(xi) |.The probability that the random variable y takes a value within the given y-interval mustnecessarily be equal to the accumulated probability that the random variable x is to take avalue within any of the corresponding x-intervals. That is, we may write

fy(y) dy =∑

i

fx(xi) dxi =∑

i

fx(xi)

|g′(xi) |dy. (2.30)

We will refer to this result as the fundamental theorem of probability calculus. We note thatfor every extremal point of y = g(x) there will be a singularity in fy(y). These singularitieswill, however, all be integrable. The density function fy(y) remains normalized.

Figure 2.2: Illustrating the fundamental theorem

Example 2.1 : Find the density fy for the random variable y defined throughy = x2 where the random variable x has uniform density in the interval(−1, 1)?

Solution: The allowed range of y is (0, 1). The equation y = g(x) = x2 hastwo solution x1,2 = ±√

y. At these points | g′(xi) |= 2√

y. Since fx(x) = 1/2

Page 19: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.4. TRANSFORMATION OF RANDOM VARIABLES 19

for x in the interval (−1, 1) we find from (2.30) that

fy(y) =fx(−

√y)

2√

y+

fx(√

y)

2√

y=

1

2√

y.

Quiz 2.2 : A particle moves with constant speed along the unit circle in the xy-plane.For an observer, sampling at random times, its angular position appears to be uni-formly distributed over the interval (−π, π). What is the corresponding apparentprobability density distribution of its x-coordinate?

2.4.2 The inversion method

The fundamental theorem (2.30) immediately provides a solution to our original problem:finding a transformation y = g(x) such that starting from the random variable x with densityfx a random variable y with prescribed density fy is produced. Let us now restrict our atten-tion to a strictly increasing or decreasing function g, that is, we only consider transformationsfor which only one solution x of y = g(x) exist. For corresponding values of x and y it followsfrom (2.30) that

Fy(y) = P(y < y) = P(x < x) = Fx(x). (2.31)

Fx and Fy are the cumulative distributions for the two random variables x and y. From (2.31)we may now solve for y,

y = g(x) = F−1y (Fx(x)) . (2.32)

For strictly increasing distribution functions Fy the inverse function F−1y always exist. That

is, solutions of the form (2.32) may always be found. Finding the inverse function F−1y may,

however, be an intractable problem analytically. In the following sections we shall thereforelook for alternative methods to generate one random variable y from another random variablex which do not depend on the inversion of Fy.

Example 2.2 : Starting from the random variable x distributed uniformly in theunit interval (0, 1), we want to find a transformation y = g(x) such thatfy(y) = 2y for y in the same interval.

Solution: Since Fx(x) = x and Fy(y) = y2 the solution of Fy(y) = Fx(x) isy =

√x.

Example 2.3 : Generate random points uniformly distributed over the unit sphere.

Solution: In terms of the polar and azimuthal angles θ and φ the coordinatesof any point on the unit sphere are expressed as

x = sin θ cos φy = sin θ sinφz = cos θ.

To produce a uniform distribution of points, θ and φ must be drawn withdensity fθφ = fθfφ with fθ = 1

2 sin θ and fφ = U(0, 2π). According to theinversion method cos θ can be generated from u ∈ U(0, 1) through cos θ =1 − 2u. This leads to the following algorithm:1) draw u ∈ U(0, 1) and φ ∈ U(0, 2π),2) set z = 1 − 2u, x = q cos φ, and y = q sin φ with q =

√1 − z2.

Page 20: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

20 CHAPTER 2. RANDOM VARIABLES

Quiz 2.3 : A more efficient algorithm for producing random points uniformly distributedover the unit sphere than that given in example 2.3 is provided by the algorithm1) draw (v, w) uniformly within the unit circle, for instance,by drawing v ∈ U(−1, 1) and w ∈ U(−1, 1) until r2 = v2 + w2 ≤ 1,2) set z = 1 − 2r2, x = qv, and y = qw with q = 2

√1 − r2.

Compare the efficiency of this algorithm with that of example 2.3. Can you explainwhy this algorithm produces the correct result?Hint: Show that fr2(r

2) = U(0, 1).

2.4.3 The rejection method

We will now discuss a number of methods that can be used to generate a random variable ywith specified density fy from another random variable x with density fx and which do notdepend on our ability to find the inverse of the distribution function Fy. It will be seen thata common feature of these methods is that they all make use of an auxiliary random variableu uniform over the unit interval and independent of x.

The two random variables x and y need not take values in the same range. Let us thereforefirst introduce a stretching function y = h(x) that maps the range of x into the correspondingrange of y. We will assume that h is a strictly increasing or decreasing function, that is, weassume h′(x) 6= 0.

Let us furthermore define the function

r(x) = c |h′(x) | fy(h(x))

fx(x)(2.33)

where c is a constant taking a value in the range 0 < c ≤ 1. The value of c is to be chosensuch that

0 < r(x) ≤ 1 (2.34)

over the full range of x. A necessary condition for finding a suitable c is that fy(h(x)) vanishesfor every x for which fx(x) vanishes. As will be seen, the optimal choice for c is the maximumvalue in the given range for which the condition (2.34) is satisfied.

Let u be uniform in (0, 1) and consider the event M = u ≤ r(x). The conditionalprobability of the event M, given that x takes a particular value x, is given by P(M | x) =r(x). Making use of (2.34) we may now write

P(M, x ≤ x < x + dx) = P(M|x)P(x ≤ x < x + dx) = r(x)fx(x) dx = cfy(y) dy.

The total probability for the event M may be found by summing over the full range of possiblex-values

P(M) =

P(M, x ≤ x < x + dx) = c

fy(y) dy = c.

From Bayes’ theorem (2.3) it now follows that

P(x ≤ x < x + dx | M) =P(M, x ≤ x < x + dx)

P(M)= fy(y) dy. (2.35)

The result (2.35) allows for a useful algorithm.

Page 21: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.4. TRANSFORMATION OF RANDOM VARIABLES 21

Algorithm 2.1 (rejection):A random variable y with density fy(y) may be generated from another randomvariable x with density fx(x) and an auxiliary random variable u uniform in (0, 1)provided a constant c and a stretching function h may be specified such that afunction r(x) with properties as defined in (2.33) may be found, by1) draw x ∈ fx and u ∈ U(0, 1) until u ≤ r(x),2) let y = h(x).

Obviously, the rejection rate of this algorithm decreases, and the efficiency therefore in-creases, with a choice of c such that the maximum value of r(x) approaches unity.

Example 2.4 : Generate y with fy(y) = U(−1, 1) from x with fx = U(0, 1) usingthe rejection method.

Solution: With the linear stretching y = h(x) = 2x − 1 we have fx = 1,fy(y) = .5, | h′(x) |= 2 and therefore r(x) = c. We may thus set c = 1.With P(M) = 1 the auxiliary random variable u is no longer needed and therejection method reduces to the simple prescription:draw x ∈ U(0, 1) and stretch according to y = 2x − 1.

Example 2.5 : From the random variable x with density fx(x) = exp(−x)U(x)where U(x) is the unit step function, we want to generate a random variabley with density fy(y) =

2/π exp(−y2/2)U(y).

Solution: Since the ranges of x and y are identical we choose the identitystretching y = h(x) = x. Then with c =

π/2e we have 0 ≤ r(x) =cfy(x)/fx(x) = exp

(

−(x − 1)2/2)

≤ 1. Thus, to generate y ∈ fy:1) draw x ∈ fx and u ∈ U(0, 1).2) Let y = x if u < exp(−(x − 1)2/2), otherwise draw new samples of x andu.

Example 2.6 : Generate random points (x, y) uniformly distributed over the an-nulus 1 ≤

x2 + y2 < 2.

Solution: Switching to polar random coordinates (ρ, φ), a uniform distributionof points in the annulus will require fρφ = fρfφ with fφ = U(0, 2π) andfρ(ρ) = 2ρ/3 for 1 ≤ ρ < 2. The random variable ρ may be generated from zwith fz = U(0, 1). With the stretching ρ = 1 + z we have r(z) = 1

2(1 + z) forc = 3

4 . The rejection algorithm now reads:1) draw z ∈ U(0, 1) and u ∈ U(0, 1) until u ≤ 1

2(1 + z)2) let ρ = 1 + z.

Hint: Show that ρ may also be generated through the inversion algorithm:draw u ∈ U(0, 1) and let ρ =

√3u + 1.

Quiz 2.4 : Generate y with fy(y) = exp(−y)U(y) from x with fx(x) = U(0, 1) using therejection method with stretching function y = − lnx. How does your result comparewith the corresponding result using the inversion method?

Page 22: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

22 CHAPTER 2. RANDOM VARIABLES

2.4.4 The mixing method

With generators for independent random variable yk with densities fyk(y), k = 1, · · · , m the

random variable y with density

fy(y) = p1fy1(y) + · · · + pmfym

(y) (2.36)

with pk > 0 and∑

k pk = 1, may be generated with the help of an extra random variable uuniform over the unit interval. The events

Mk = p1 + · · · + pk−1 ≤ u < p1 + · · · + pk, k = 1, · · · , m

are mutually exclusive with P(Mk) = pk. With fy(y |Mk) = fyk(y) the relation (2.36) may

be rewritten in the form

fy(y) = fy(y |M1)P(M1) + · · · + fy(y |Mm)P(Mm). (2.37)

Algorithm 2.2 (mixing):A realization y of the random variable y with density fy as given by (2.36) isgenerated through the procedure1) draw u ∈ U(0, 1),2) if p1 + · · · + pk−1 ≤ u < p1 + · · · + pk let y = yk with yk ∈ fyk

.

Example 2.7 : The normal density fy(y) = exp(−y2/2)/√

2π may be written

fy(y) =1

2fy

1(y) +

1

2fy

2(y)

with

fy1(y) =

2/π exp(−y2/2)U(y)

and

fy2(y) =

2/π exp(−y2/2)U(−y)

An algorithm to generate the random variable y1 with density fy1(y) was given

in example 5. The identical algorithm may be used to generate y2 with densityfy

2(y) by just introducing a extra sign factor. The corresponding algorithm

for the full normal random variable y is therefore1) draw y1 ∈ fy

1and u ∈ U(0, 1)

2) if u < .5 let y = −y1 else let y = y1.

Quiz 2.5 : Generate random variables y with fy(y) = 3p1y2 + 4p2y

3 for y in (0, 1) andwhere p1 and p2 are positive numbers satisfying p1 +p2 = 1 using the mixing method.

Page 23: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.5. SPECIAL METHODS 23

2.5 Special methods

2.5.1 Adding random numbers

We have in section 2.4 studied properties of random numbers produced through transforma-tions of another random number. Here we consider properties of random numbers producedby adding several different random numbers. For instance, let x1 and x2 be uniformly dis-tributed in (0, 1). What are the properties of the random variable y = x1 + x2?

To answer this question we must determine

P(y ≤ y < y + dy) = fy(y) dy =

y≤x1+x2<y+dyfx1

(x1)fx2(x2) dx1dx2.

The geometry is illustrated in figure 2.3a. The integral is easily performed by replacing x1

and x2 by x1 and y = x1 + x2 as free variables, thus

P(y ≤ y < y + dy) = fy(y) dy =

fx1(x1)fx2

(y − x1) dx1dy

and therefore

fy(y) =

fx1(x1)fx2

(y − x1) dx1. (2.38)

The probability density function for a sum of two random variables equals the convolutionproduct of the two individual probability density functions. If x1 and x2 are both uniformlydistributed in (0, 1) then their sum y = x1 + x2 will be triangularly distributed in (0, 2) asillustrated in figure 2.3b. The maximum of fy occurs at y = 1 because the integration stripin figure 2.3a takes its maximum length for y = 1.

Figure 2.3: Summing of two uniform random variables

Adding of more than two random variables are easily done by successively adding newvariables to a previous sum. As the number of random variables added increases the resultquickly approaches a normal distribution, and does so regardless of the actual probabilitydistributions for the individual random variables. In fact, adding 12 uniformly distributedrandom numbers will for most practical purposes produce acceptable quality normally dis-tributed random numbers.

Page 24: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

24 CHAPTER 2. RANDOM VARIABLES

Algorithm 2.3 (Adding):The algorithm

y =1

12(x1 + · · · + x12 − 6) (2.39)

with x1, · · · , x12 all uniform in (0, 1) produces approximately normal randomnumbers y with zero mean and unity variance.

2.5.2 The Box-Muller algorithm

Let the random variable pair (x, y) be uniformly distributed within the unit circle. Thismeans that

fxy(x, y) =

1/π0

for

r2 = x2 + y2 ≤ 1r2 = x2 + y2 < 1

. (2.40)

Introduce new random variables (u, v) with realizations (u, v) related to (x, y) through thetransformations

φ = arctany

x= arctan

v

u(2.41)

r2 = x2 + y2 = exp(

−(u2 + v2)/2)

. (2.42)

Since the Jacobian for the coordinate change from (r2, φ) to (u, v) is given by

J =

∂(r2, φ)

∂(u, v)

= exp(−(u2 + v2)/2),

it follows that

1

πdxdy =

1

2πdr2dφ =

1

2πJ dudv =

1

2πexp(−(u2 + v2)/2) dudv. (2.43)

With random variables x and y specified through (2.40), it is seen from (2.43) that u andv represent realizations of two normal random variables u and v with zero mean and unityvariance. From (2.41) and (2.42) explicit expressions for u and v may be derived in terms ofx and y.

Algorithm 2.4 (Box-Muller):Realizations u and v of normal random variables u and v with zero mean andunity variance, can be found by:1) draw x ∈ U(−1, 1) and y ∈ U(−1, 1) until r2 = x2 + y2 < 1,2) let u = x

−2 ln r2/r2 and v = y√

−2 ln r2/r2.

We note in passing that if u ∈ N(0, 1) then µ + σu ∈ N(µ, σ), that is, if u is normal withzero mean and unity variance, then µ + σu is normal with mean value µ and variance σ2.

2.5.3 The Metropolis algorithm

In this method the random variable x with density fx(x) is generated through a random walkprocess. Each realization of x is generated from the previous realization by adding a randomwalk step with length s uniformly distributed over the interval (−∆, ∆) for some suitablychosen maximum step length ∆. The random walk steps must satisfy certain conditions.

Page 25: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.6. STOCHASTIC PROCESSES 25

Thus, a random walk step from a value xi to a value xj is accepted with a transition probabilityT (xi → xj). The transition probability for the reverse step from the value xj to xi isdenoted T (xj → xi). The important point is to decide on the proper choice for the transitionprobabilities for the given density fx. A sufficient criterion for the sequence of random walksteps to converge to the desired density is to invoke the principle of detailed balance, that ischoose transition probabilities T (xi → xj) and T (xj → xi) satisfying

fx(xi)T (xi → xj) = fx(xj)T (xj → xi). (2.44)

This principle means that in the long run the ”number” of random walks from any givenvalue xi to any other value xj are equal to the ”number” of reverse steps. A simple solutionof (2.44) can readily be found,

T (xi → xj) = min1,fx(xj)

fx(xi). (2.45)

Algorithm 2.5 (Metropolis):The realization xi+1 of a random variable x with density fx(x) may be foundfrom the previous realization xi through the procedure:For some suitably chosen ∆1) draw u ∈ U(0, 1) and s ∈ U(−∆, ∆) until u ≤ fx(xi + s)/fx(xi),2) let xi+1 = xi + s.

An important point to note about this algorithm is that it only makes use of the ratio of thedensity function fx(x) evaluated at two different arguments. This means that the algorithmmay be applied also in cases where the proper normalization of the density function is notknown.

The rejection rate as well as the convergence rate of the sequence of realizations to thewanted density fx depend on the particular choice of the maximum step length ∆. A rule ofthumb sometimes quoted is to choose ∆ such that the rejection rate equals approximated 40%. Other subtleties of the method is related to what to do near the borders of the range forthe random variable x, that is what to do if a given test step will result in a value x outside theallowed range. One way to deal with this problem may be to introduce ”reflecting” boundaryconditions, that is, reflect any step leading to non-valid values of x back into the allowedrange.

2.6 Stochastic processes

A stochastic process x(t) is an infinite aggregate of random variables, one for each instance oftime t. A realization of a continuous stochastic process x(t) is a continuous function x(t) oftime. The actual output voltage of a noise generator is a realization of a stochastic process. Ifwe had at our disposal a family (an ensemble) of noise generators, each of these would outputa different voltage as a function of time.

The probability density function fx(x, t) for a stochastic process x(t) defined in the bynow familiar way

P(x ≤ x(t) < x + dx) ≡ fx(x, t)dx (2.46)

Page 26: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

26 CHAPTER 2. RANDOM VARIABLES

together with the corresponding distribution function

Fx(x, t) ≡ P(x(t) ≤ x) (2.47)

are both functions of time.

The same fact generally applies to the mean value of the stochastic process

µx(t) = 〈 x(t) 〉 =

xfx(x, t)dx, (2.48)

but not necessarily so. If each realization x(t) of the process is varying with time, the meanvalue may still be time independent. The ideal noise generator is such an example. In anensemble of noise generators there is no reason to expect that the output voltage averagedover the ensemble at any time shall increase or decrease. A stochastic process for which µx isindependent of time is a wide sense stationary process. For wide sense stationary processes apowerful theorem tells that mean values may be estimated as time-averages of one realizationinstead of sampling an ensemble of similar but independent processes.

2.6.1 The ergodicity theorem

Let x(t) represent a wide sense stationary stochastic process. According to our assumptionthe mean value µx(t) = 〈x(t)〉 is thus independent of time t. Let us furthermore define thetime averaged stochastic process

wT =1

2T

∫ T

−Tx(t) dt. (2.49)

For the time averaged process we derive

µwT= 〈wT 〉 = µx

and

σ2wT

= 〈w2T 〉 − µ2

wT

=1

4T 2

∫ T

−Tdα

∫ T

−Tdβ(

〈x(α)x(β)〉 − µ2x

)

=1

8T 2

∫ 2T

−2Tdτ

∫ 2T−|τ|

−(2T−|τ|)dδ γxx(τ)

=1

T

∫ 2T

0dτ(

1 − τ

2T

)

γxx(τ). (2.50)

For this result we introduced new integration variables τ = β − α and δ = β + α and madeuse of the fact that the auto-covariance function

γxx(β − α) = 〈x(α)x(β)〉 − µ2x

is an even function, γxx(−γ) = γxx(γ).

Page 27: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

2.6. STOCHASTIC PROCESSES 27

Under mild conditions on the auto-covariance function of the wide sense stationary pro-cess x(t), the resulting integral in (2.50) remains finite and therefore σ2

wT→ 0 as T → ∞.

Examples of such sufficient conditions are

∫ ∞

0γxx(τ) dτ < ∞, or

γxx(τ) → 0 as τ → ∞.

We shall refer to processes satisfying these or similar conditions as processes with finitecorrelation time.

From Tchebycheff’s inequality

P(|wT − µx |≥ ε) =

|w−µx|≥εfwT

(w) dw <1

ε2

∫ ∞

−∞(w − µx)

2fwT(w) dw =

σ2wT

ε2(2.51)

it follows for wide sense stationary processes x(t) with finite correlation time that the timeaveraged process (2.49) with probability close to unity will approach the expectation value µx.In other words, the time average of one single realization x(t) of the stochastic process x(t)will be sufficient to predict the expectation value µx of the process. This result is referred toas the ergodicity theorem. This theorem forms the basis for any time-dependent simulationof physical systems.

Page 28: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

28 CHAPTER 2. RANDOM VARIABLES

Page 29: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

Chapter 3

Monte Carlo methods

A standard numerical technique for the evaluation of the integral

I =

∫ b

ag(x) dx (3.1)

is to divide the integration range (a, b) into N intervals of size ∆ = (b − a)/N and make useof the ”mid-point” rule for each of them. With this rule the integral over each interval isevaluated by approximating the integrand with a constant equal to its value gm = g(xm) atthe mid-point xm.

Assuming the integrand to be continuous with continuous first derivative, the accuracy ofthe mid-point rule is easily analyzed. In the interval (xi, xi+1) with midpoint xm = 1

2(xi+xi+1)the function g(x) is approximated by the Taylor series

g(x) = gm + g′m(x − xm) + · · ·where g′m = g′(xm). Integrating over the interval and accumulating contributions from eachof the n intervals, the result is

∫ b

ag(x) dx = ∆

gm + nO(∆3). (3.2)

Since ∆ = O(n−1) the error term decreases as O(n−2) with increasing number n of integrandevaluations.

The above analysis is easily extended to multi-dimensional integrals. For a d-dimensionalintegral the integration volume V is divided into n cells of size ∆d. The Taylor series of theintegrand around the mid-point of each cell contain n first order terms. After integrating andsumming over all sub-volumes the result is

Vg(x) ddx = ∆d

gm + nO(∆2+d). (3.3)

This time ∆ = O(n−1/d). The error term thus decreases as O(n−2/d) with increasing numbern of integrand evaluations.

We see that as the dimension of the integral to be evaluated increases, the convergencerate with the number of integrand evaluations decreases. From the standpoint of thermalequilibrium studies where integrals of very high dimension need to be evaluated, this is badnews. However, as we shall see in this chapter a statistical approach to the problem of integralevaluations will change the situation considerably.

29

Page 30: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

30 CHAPTER 3. MONTE CARLO METHODS

3.1 Evaluation of integrals

3.1.1 Sample mean

The integral (3.1) may be rewritten in the form

I =

∫ b

ag(x) dx =

∫ b

a

g(x)

fx(x)fx(x) dx, (3.4)

where fx is any probability density function, non-vanishing in every point x for which g(x) 6= 0.With the choice fx = U(a, b) the integral reduces to

I = (b − a)

∫ b

ag(x)fx(x) dx = (b − a)〈g〉.

The expectation value 〈g〉 may be approximated by the standard sample mean and samplevariance estimators according to (2.14). That is, we may write

I ≈ (b − a)

(

mg ±sg√n

)

where

mg =1

n

n∑

i=1

g(xi),

s2g =

1

n − 1

n∑

i=1

(g(xi) − mg)2 ,

and where the n sample points xi are drawn from the uniform distribution U(a, b), that isxi ∈ U(a, b).

It is interesting to compare the statistical error estimate sg/√

n with the correspondingerror term for the mid-point rule O(n−2). Although the two expressions have slightly dif-ferent interpretation, it would seem that the mid-point rule is the more efficient one for theone-dimensional integral as a function of the number of integrand evaluations n. The situa-tion changes, however, if we consider multi-dimensional integrals. For d-dimensional integralsthe mid-point rule error term is O(n−2/d) while the statistical error term remains formallyindependent of the dimension d. For d ≥ 5 the sample mean estimator has the superior con-vergence rate with increasing n. Additional advantages of the statistical method for integralevaluations will be explored in the next section.

3.1.2 Importance sampling

Let us return to the integral (3.4). This time we do not require fx(x) to be the uniformdistribution, instead we would like to seek the optimal choice of fx(x) for a particular g(x).

The integral may again be evaluated using the sample mean and sample variance estima-tors according to (2.14), this time as

I ≈(

mg/fx ±sg/fx√

n

)

(3.5)

Page 31: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

3.1. EVALUATION OF INTEGRALS 31

with

mg/fx =1

n

n∑

i=1

g(xi)

fx(xi), (3.6)

s2g/fx

=1

n − 1

n∑

i=1

(

g(xi)

fx(xi)− mg/fx

)2

. (3.7)

The sample values xi are drawn from the density fx, that is, xi ∈ fx.The optimal fx is seen to be that particular density that minimizes the variance

σ2g/fx

=

∫ b

a

g2(x)

f2x (x)

fx(x) dx −(∫ b

a

g(x)

fx(x)fx(x) dx

)2

=

∫ b

a

g2(x)

fx(x)dx −

(∫ b

ag(x) dx

)2

under the constraint∫

fx(x) dx = 1.This problem can be solved by Lagrange undetermined multiplier method by requiring

δ

[

∫ b

a

g2(x)

fx(x)dx −

(∫ b

ag(x) dx

)2

− λ

∫ b

afx(x) dx

]

=

∫ b

a

(

g2(x)

f2x (x)

− λ

)

δfx(x) dx = 0

for any variation δfx(x) around the optimal fx(x). The solution is

fx(x) =|g(x) |√

λ=

|g(x) |∫ ba |g(x) | dx

(3.8)

with the corresponding minimum variance

minσ2g/fx

=

(∫ b

a|g(x) | dx

)2

−(∫ b

ag(x) dx

)2

.

For non-negative integrands g(x) and with the optimal choice (3.8)) of density fx(x) theminimum variance vanishes. This means that an exact evaluation of the integral can be madeeven with a single sampling point.

In practice it may not always be possible to aim for this ideal situation. Still vastlyimproved accuracy may be obtained by a properly chosen density function fx.

Several variance reducing techniques are available. For instance, if the integrand g(x)takes alternative signs within the integration range, improved accuracy may be obtained byevaluating the integral in the form

I =

∫ b

a(g(x) + c) dx − c

∫ b

adx (3.9)

with the constant c chosen such that g + c takes only one sign in the given range.If g(x) has discontinuities within the integration range, improved accuracy may be ob-

tained by evaluating the integral as a sum over several sub-ranges for which g(x) is continuous.

Quiz 3.6 : Evaluate the simple integral I =∫ 1−1 xdx numerically using

a) the sample mean method,b) importance sampling, andc) importance sampling with a shifted integrand, see (3.9).In each case study the evolution of the numerical error as a function of the numberof integrand evaluations n.

Page 32: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

32 CHAPTER 3. MONTE CARLO METHODS

3.2 The canonical ensemble

For a system consisting of N particles in a volume V in thermal equilibrium with a heatbath at temperature T (in energy units), the ensemble average of an arbitrary observable Adepending on the microstate X of the system is given by

〈A 〉NV T =1

Z

ΩA(X) exp(−H(X)/T ) dX. (3.10)

Here H(X) is the Hamiltonian of the system and the partition function

Z =

Ωexp(−H(X)/T ) dX. (3.11)

With 6 degrees of freedom per particle, the integrals in (3.10) and (3.11) are to be taken overthe accessible part of the 6N -dimensional phase space Ω of the system. For a Hamiltonian ofthe form

H(X) =N∑

i=1

p2i

2mi+ U(r1, r2, · · · , rN )

where ri, pi are the position and canonical momentum of particle i and U is the potentialenergy of the system, the 3N integrals over the canonical momenta can often be performedanalytically. This is trivially the case if the observable A does not depend on the canonicalmomenta of the individual particles.

The awesome task of evaluating the remaining multi-dimensional integrals may be handledsuccessfully by an importance sampling Monte Carlo integration technique. This can beaccomplished by writing (3.10) in the form

〈A 〉NV T =

Ω [A(X) exp(−H(X)/T )/fX(X)] fX(X) dX∫

Ω [exp(−H(X)/T )/fX(X)] fX(X) dX. (3.12)

A suitable choice for the auxiliary density function fX is

fX(X) ∼ exp(−H(X)/T ). (3.13)

With this choice the variance of the integral in the denominator vanishes. This meansthat for a sample size n we have the approximation

〈A 〉NV T ≈ mA =1

n

n∑

i=1

A(Xi) with Xi ∈ fX. (3.14)

The problem is then reduced to the problem of drawing microstates Xi of the system inaccordance with the chosen density function fX. The Metropolis algorithm is ideally suitedfor this task, in particular since this algorithm does not require the normalization factor ofthe density function to be known.

Page 33: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

3.2. THE CANONICAL ENSEMBLE 33

Algorithm 3.6 (Canonical ensemble):Microstate Xi of the system are generated from the previous one X = Xi−1

by visiting N particles in a systematic or random manner, and for each of thesevisits1) choose a random but allowed perturbation of the phase space configuration forthe given particle, denote the perturbed configuration by X ′

2) compute ∆H associated with the change in microstate from X to X ′

3) draw u ∈ U(0, 1)4) if u < exp(−∆H/T ) accept the new configuration, X = X ′

5) otherwise retain the old configuration X.After visiting N particles in this way set Xi = X and compute the contributionA(Xi) to the sample average.

A simple example will demonstrate the strength of this algorithm.

3.2.1 The ideal gas

The one-dimensional ideal gas represents the simplest possible example. With N non-interactingparticles in a volume V at temperature T and a Hamiltonian

H(X) =N∑

i=1

p2i

2mi.

We are interested in evaluating the mean energy per particle 〈H 〉/N at thermal equilibrium.The two N -dimensional integrals of (3.10) can all be performed analytically with the result

〈H 〉/N =1

2T .

In figure 3.1 one result of a numerical Monte Carlo evaluation of the mean energy perparticle mH/N for a system of N = 100 particles of mass m = 1 at temperature T = 1. Inalgorithm 3.2 the individual perturbations where chosen as ∆pi ∈ U(−∆pmax, ∆pmax) with∆pmax = 1. In the figure the mean energy per particle (solid line) is plotted as a functionof sample size n. We note that already for a sample size of n ≈ 4000 the numerical resultfalls within 1 % of the analytical one, that is, with less than 1.1 sample points per integraldimension!

In the figure 3.1 we have also plotted the experimental error bars (dashed lines) accordingto (2.14), that is as mH ± sH/

√n with

s2H =

1

n − 1

n∑

i=1

(H(Xi) − mH)2 with Xi ∈ fX.

This formula assumes the individual samples to be statistically uncorrelated. If this conditionis not satisfied, the sample mean mH will still be an un-biased estimator, but the samplevariance s2

H will underestimate the size of the real error bar. For an improved error barestimate we therefore have to embark on a more elaborate analysis.

Page 34: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

34 CHAPTER 3. MONTE CARLO METHODS

Figure 3.1: Mean energy per particle

3.2.2 Error analysis

Let x1, x2, . . . be a sequence of random variables. These could represent a random processx(t) at given times t1, t2, . . .. The sequence could also be the sequence of the underlyingrandom variables giving rise to the series of samples in a Monte Carlo evaluation of a multi-dimensional integral. We allow for the existence of correlations between different randomvariables, that is,

〈 xixj 〉 6= 〈 xi 〉〈 xj 〉,but we assume the process to be wide-sense stationary,

〈 x1 〉 = 〈 x2 〉 = . . . = 〈 x 〉.

For the random variable

mx ≡ 1

n

n∑

i=1

xi

we note thatµmx ≡ 〈mx 〉 = 〈 x 〉

and

σ2mx = 〈m2

x 〉 − 〈mx 〉2 =1

n2

n∑

i,j=1

(〈 xixj 〉 − 〈 xi 〉〈 xj 〉) ≡1

n2

n∑

i,j=1

γi,j .

We here defined the correlation functions γi,j . For stationary processes the invariance undertime translation means that we may define

γ|i−j| ≡ γi,j

and write

σ2mx =

1

n2

n∑

i,j=1

γ|i−j| =1

n

(

γ0 + 2n−1∑

i=1

(

1 − i

n

)

γi

)

. (3.15)

Page 35: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

3.2. THE CANONICAL ENSEMBLE 35

If γi = 0 for i > 0 then (3.15) reduces to

σ2mx =

1

nγ0 =

1

nσ2

x,

in accordance with (2.12). Intuitively we expect the simplified formula (2.12) to underestimatethe variance σ2

mx in the presence of correlations, that is, we expect

σ2mx ≥ γ0

n. (3.16)

By estimating the n correlation functions γi, the formula (3.15) could be used to estimate thevariance in the case of correlated random variables. This is, however, a computational taskof complexity n2 that tends to be costly for large sample sizes. Fortunately, there exist analternative, less costly method.

For this method we introduce the notion of a blocking operation, that is, we define areduced sequence of random variables x′1, x′2, . . . through the transformation

x′i =1

2(x2i−1 + x2i) for i = 0, . . . , n′ (3.17)

n′ =1

2n. (3.18)

For the primed sequence the following results apply

µmx′= µmx (3.19)

γ′i =

1

4γ|2i−1| +

1

2γ2i +

1

4γ2i+1 (3.20)

σ2mx′

=1

n′2

n′

i,j=1

γ′|i−j| = σ2

mx . (3.21)

This means that µmx and σ2mx are both invariant under repeated blocking operations. In

particular, (3.15) must apply also for the primed sequence. Thus we expect

σ2mx′

≥ γ′0

n′=

γ0 + γ1

n. (3.22)

If we could assume γ1 ≥ 0, then, comparing with (3.16), successive blocking operations lead tosuccessively higher lower bounds for the variance σ2

mx . This cannot generally be guarantied.However, we know that γ0 > 0 and that γi tends toward zero with increasing i for stationaryprocesses. Furthermore, according to the results of section 2.5.1, with repeated blockingoperations the blocked variables approach Gaussian distributions, and for Gaussian Markovprocesses it is known that γi ≥ 0 for all i.

To apply this result we need to estimate γ0 for subsequent applications of the blockingoperation. To this end we consider the estimate

c0 =1

n

n∑

i=1

(xi − mx)2. (3.23)

Page 36: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

36 CHAPTER 3. MONTE CARLO METHODS

The expectation value of the corresponding estimator is

〈 c0 〉 =1

n〈

n∑

i=1

(xi − mx)2 〉 =

1

n

n∑

i=1

〈 x2i 〉 − 〈m2

x 〉 = γ0 − σ2mx .

Thus we see that c0 is not an unbiased estimator for γ0. We note, however, that since weexpect σ2

mx ≥ γ0/n we may write 〈 c0 〉 ≤ γ0 − γ0/n, or

γ0

n≥ 〈 c0 〉

n − 1. (3.24)

These considerations lead to a practical procedure for estimating the error bar associatedwith the evaluation of sample means in a sequence of correlated data. For each of subsequentblocking operations we evaluate c0/(n − 1). If a saturated plateau in this value is achievedbefore n′ = 2, then this level is a realistic estimate for the variance of the sample mean. Inthis process it must be remembered that as n′ approaches the value 2, increased uncertaintyin the estimate for c0 is to be expected.

Figure 3.2: Error lower bonds

As an example of this procedure we seek estimates of the error associated with the eval-uation of the mean energy of the one-dimensional ideal gas studied in section 3.2.1. For asample size of 8192 the blocking operation can be repeated 12 times before n′ = 2. Thecorresponding values of

c0/(n − 1) for these levels for the data of figure 3.1 are shown infigure 3.2. We see a saturated plateau at

c0/(n − 1) ≈ .0025. This value thus representsthe error level sought for. The corresponding error bars have been plotted in figure 3.1 andare seen to give reasonable results.

3.3 The microcanonical ensemble

For the microcanonical ensemble the energy of the system remains constant and identical apriori probabilities for every microstate satisfying this constraint. If the Hamiltonian and the

Page 37: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

3.3. THE MICROCANONICAL ENSEMBLE 37

allowed energy of the system are denoted by H(X) and E respectively, the ensemble averageof the observable A(X) is is given by

〈A 〉NV E =1

Z

ΩA(X)δ(H(X) − E) dX (3.25)

with partition function

Z =

Ωδ(H(X) − E) dX. (3.26)

Here δ(x) is the Dirac delta function.An efficient Monte Carlo procedure for the microcanonical ensemble have been developed

by Creutz and coworkers, in which an extra degree of freedom, a demon, is added to thesystem. The demon is allowed to have positive energy Ed only. The demon travels throughthe system and exchanges energies locally with the system, but is such a way that the to-tal energy constraint for the system is satisfied. Visiting the particles of the system in asystematic or random manner a random perturbation of the phase space coordinated of theparticle is selected. The new microstate is accepted if the energy change associated with thisperturbation is positive or can be compensated by the instantaneous energy of the demon.

The estimate for the observable A is calculated by summing individual contributions fora series of n microstates Xi,

〈A 〉NV E ≈ 1

n

n∑

i=1

A(Xi). (3.27)

with the microstates Xi generated by the following algorithm:

Algorithm 3.7 (Microcanonical ensemble):Microstate Xi of the system are generated from the previous one X = Xi−1 byvisiting N particles in a systematic or random manner, and for each for each ofthese visits1) choose a perturbation in the phase space configuration of the particle, denotethe perturbed configuration by X ′

2) compute ∆H associated with the change in microstate from X to X ′

3) if ∆H − Ed > 0 set Ed = Ed − ∆H and accept the new microstate, X = X ′

4) otherwise retain the old configuration X.After visiting N particles in this way set Xi = X and compute the contributionA(Xi) to the sample average.

The algorithm guaranties that the system relaxes toward thermal equilibrium. The demonmay be viewed as a thermometer in thermal contact with the system. The system acts asa reservoir and thermalizes the demon. The demon energy on its travel through the systemwill acquire a Boltzmann distribution,

fEd(Ed) ∼ exp(−Ed/Td), (3.28)

allowing the temperature Td to be calculated. The mean energy of the demon turns out toacquire a value twice the mean energy of the individual particle in the system.

Page 38: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

38 CHAPTER 3. MONTE CARLO METHODS

Page 39: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

Chapter 4

Dynamical Systems

4.1 Equations of motion

Consider a classical N -particle system. At time t let the particles have positions and velocitiesri(t), vi(t), i = 1, · · · , N . The particles may be subject to external forces F ext

i and they mayinteract via pair forces, particle j acting on particle i with the force F ij . The pair forcesmay depend on the relative position of the two particles involved, their velocities and mayeven depend on additional internal parameters of the particles. The total force on particle iis found by summing the different contributions,

F i = F exti +

j 6=i

F ij .

The time evolution of the system is determined through the Newtonian equations of motion

ri = vi (4.1)

vi = ai =1

miF i (4.2)

where mi is the mass of particle i. With the initial state of the system and all forces perfectlyknown, the subsequent evolution of the system is in principle determined. For conveniencewe will sometimes suppress particle indices in these equations.

The force F on a given particle is a function of its position r and possibly its velocityv, and in principle the coordinates of all other particles in the system. The force may alsodepend on particle velocity. For particles with charge q in electric and magnetic fields E andB we have

a =q

m(E + v × B) = e + v × Ω. (4.3)

The equations of motion as given above are correct only as long as the particle velocitiesremain non-relativistic. For relativistic charged particles in electric and magnetic fields theequations (4.1) and (4.2) may be written in the form

r =u

γ(4.4)

u =q

m0

(

E +u

γ× B

)

, (4.5)

39

Page 40: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

40 CHAPTER 4. DYNAMICAL SYSTEMS

where m0 is now the rest mass and where we introduced u = γv with γ2 = 1 + u2/c2.In important applications it is necessary to consider particles with a more complicated

internal structure. This could be molecules or polymers with the individual atoms bound inparticular configurations. In such situations the equations of motion may take more compli-cated forms. In such situations it may prove advantageous to formulate equations of motionin Lagrange formalism. We will give specific examples in the following chapter.

4.2 Orbit integration

At the basis of any time dependent simulation lies the need for integrating the equations ofmotion for each particle in the system. The equations of motion are ordinary differentialequations. Through the force expression the equations of motion for one particle are coupledto the corresponding equations for the other particles in the system. This means that theequations of motion for all particles must be treated as one large system of equations.

The literature abounds in different numerical methods for the solution of ordinary dif-ferential equations. Some methods exploit special features of the differential equations to besolved, others are intended for general purpose use. Some methods aim at the highest possiblenumerical accuracy, others sacrify accuracy for speed of execution and economy of storage.For the numerical simulation of many-particle systems, the latter qualities are often of primeimportance. In this section we review some relevant methods, pointing out strengths andweaknesses of different alternatives.

4.2.1 Leap-frog method

In the leap-frog method staggered time frames for positions and velocities are employed. Theposition vector is evaluated at integer time steps tn = n∆t, the velocity at half-integer stepstn± 1

2

= (n ± 12)∆t. The equations of motion (4.1) and (4.2) are discretized as

vn+ 1

2

− vn− 1

2

∆t= an (4.6)

rn+1 − rn

∆t= vn+ 1

2

. (4.7)

We will refer to these equations as the leap-frog algorithm. With this algorithm the particlevelocity vn− 1

2

at time tn− 1

2

and position rn at time tn may be advanced to the corresponding

values at the times tn+ 1

2

and tn+1 provided the acceleration an at time tn may be evaluated

on the basis of positions only.The leap-frog algorithm is accurate to order O(∆t2). This means that the discretized

equations reproduce the differential equations of motion up to terms of order ∆t2. To see this(4.7) may be used to eliminate the velocity terms of (4.6) to give

rn+1 − 2rn + rn−1

∆t2= an. (4.8)

Expressing the rn±1 terms as Taylor series centered at time tn, the equation (4.8) reduces to

the equations of motion (4.1)-(4.2) except for the lowest order error term r(iv)n ∆t2/12. This

means that the numerical error introduced by the leap-frog algorithm over one time step

Page 41: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

4.2. ORBIT INTEGRATION 41

compared to the exact solution of the equations of motion is reduced by a factor 4 as the timestep is reduced by a factor 2.

Numerical accuracy is only one factor of relevance when deciding on the length of thetimestep ∆t. Another factor is numerical stability. This effect can be illustrated by thesimple one-dimensional harmonic oscillator. In this case an = −ω2

0xn where ω0 is a constant.(4.8) thus reduces to

xn+1 − 2xn + xn−1 = −ω20∆t2xn, (4.9)

a linear difference equation with constant coefficients and with a general solution of the formxn ∼ exp(ιωtn). The constant ω can be determined by substituting this solution into (4.9).The result is a characteristic equation for ω in the form

sin2 ω∆t

2=

(

ω0∆t

2

)2

.

Figure 4.1: Harmonic oscillator

Real solutions for ω require ω0∆t < 2. If this condition is not satisfied ω takes complexvalues and the solution of the difference equation starts to exhibit exponentially growingoscillations, which indicates numerical instability. In figure 4.1a the real (solid) and imaginary(dashed) parts of ω∆t as a function ω0∆t are given. For ω0∆t less than the instabilitythreshold, ω∆t as a function of ω0∆t deviates from the straight line ω∆t = ω0∆t. Thismeans that the numerical solution will experience a phase error, the numerical solution willindicate a decreasing period of the oscillation with increasing ω0∆t.

In figure 4.1b the time evolution of the solution of the difference equation for three differentvalues of ω0∆t are given together with the analytical solution (dashed). The numericalsolution shows a gradually increasing deviation from the analytic solution as the time stepincreases. The amplitude of oscillation is correctly described but the period shortened aspredicted. As the stability threshold is passed the numerical solution literally explodes.

The leap-frog algorithm is convenient in that it requires only one force evaluation per timestep and needs storage space for only one copy of r and v for each particle. A disadvantageof the algorithm is that it requires a separate starting algorithm, advancing rn and vn given

Page 42: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

42 CHAPTER 4. DYNAMICAL SYSTEMS

at the same time tn to the staggered time instance rn+1 and vn+ 1

2

. This can be done as

rn+1 = rn + ∆tvn +∆t2

2an (4.10)

vn+ 1

2

= vn +∆t

2an. (4.11)

The corresponding synchronization algorithm is

vn+1 = vn+ 1

2

+∆t

2an+1. (4.12)

These algorithms have the same accuracy O(∆t2) as the leap-frog algorithm and are relatedto the Verlet algorithm to be discussed in the next section.

4.2.2 Leap-frog and velocity dependent forces

With velocity dependent forces the leap-frog method must be modified. With an accelerationterm of the form (4.3), equations (4.6) and (4.7) are replaced with

vn+ 1

2

− vn− 1

2

∆t= en +

vn+ 1

2

+ vn− 1

2

2× Ωn. (4.13)

rn+1 − rn

∆t= vn+ 1

2

(4.14)

We note that also this equation makes use of staggered time frames for positions and velocities.Again both sides of the equation are centered on the same time tn. Again it can be shownthat the numerical scheme consisting of (4.13) and (4.14) will be accurate of order O(∆t2).The implicit equation (4.13) for vn+ 1

2

may be solved conveniently by a split step method. For

this purpose we introduce auxiliary velocities v− and v+

v− = vn− 1

2

+1

2en∆t (4.15)

vn+ 1

2

= v+ +1

2en∆t. (4.16)

In term of these quantities (4.13) reduces to

v+ − v−

∆t=

v+ + v−

2× Ωn. (4.17)

Equation (4.17) represents a pure rotation of v− into v+ with Ω as the axis of rotation andwith the angle of rotation θ determined by

tanθ

2=

|v+⊥ − v−

⊥ ||v+

⊥ + v−⊥ |

= Ωn∆t

2. (4.18)

The geometry of this transformation is illustrated in figure 4.2a where the vectors v− and v+

are seen projected into the plane perpendicular to Ωn.The solution (4.18) reveals that the rotational angle θ will deviate from the true rotational

angle of the particle in the given magnetic field. This means that the apparent gyro-frequency

Page 43: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

4.2. ORBIT INTEGRATION 43

Figure 4.2: Illustrating the Boris algorithm

of the solution of (4.17) will be different from the true physical value Ω. For the simulationof physical systems it is often important that characteristic frequencies remain correct. Thiscan be achieved in the numerical solution by introducing a correctional multiplicative factor

λ =tan Ω∆t

2Ω∆t

2

on the right hand side of (4.17).

An efficient implementation of (4.17) with the correctional factor λ included was suggestedby Boris and consists of the following steps

w = v− + v− × p (4.19)

v+ = v− + w × q (4.20)

with

p = Ωn tanΩn∆t

2(4.21)

q =2p

1 + p2. (4.22)

The implementation is easily understood with reference to figure 4.2b. With the given p, therequirement on q ‖ p to produce the corrected angle of rotation θ = Ω∆t is

qv−⊥

1 + tan2 θ

2=|w × q |=|v+ − v− |= 2v−⊥ sin

θ

2

with solution

q = sin θ =2 tan θ

2

1 + tan2 θ2

,

in accordance with (4.22). The algorithm does not require extra storage space for v− and v+,v− overwrites vn− 1

2

, v+ overwrites v− and vn+ 1

2

overwrites v+. The auxiliary vector v′ may

be reused for all particles. Considerable speed-up can be achieved for the case of varying Ωif the correctional factor λ can be left out. In this case the expression (4.21) reduces to

p =Ωn∆t

2. (4.23)

Page 44: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

44 CHAPTER 4. DYNAMICAL SYSTEMS

The above method easily extends to relativistic motions. The analogues to (4.13) and(4.14) now read

un+ 1

2

− un− 1

2

∆t= en +

un+ 1

2

+ un− 1

2

2γn× Ωn. (4.24)

rn+1 − rn

∆t=

un+ 1

2

γn+ 1

2

. (4.25)

We again introduce new variables u− and u+ defined by

u− = un− 1

2

+1

2en∆t (4.26)

un+ 1

2

= u+ +1

2en∆t. (4.27)

and findu+ − u−

∆t=

u+ + u−

2γn× Ωn. (4.28)

The form of the equation shows that |u− |=|u+ | irrespective of the specific expression forγn. If we therefore substitute γ2

n = 1 + u2n− 1

2

/c2, the Boris algorithm may again be applied

w = u− + u− × p (4.29)

u+ = u− + w × q (4.30)

this time, however, with

p = Ωn tanΩn∆t

2γn. (4.31)

4.2.3 The Verlet method

The Verlet algorithm is an alternative to the leap-frog algorithm that avoids the use of stag-gered time frames. The Verlet algorithm reads

rn+1 = rn + ∆tvn +∆t2

2an (4.32)

vn+1 = vn +∆t

2(an + an+1). (4.33)

Eliminating v’s between (4.32) and (4.33) again leads to equation (4.8). That means thatthe accuracy of the Verlet algorithm is identical to that of the leap-frog algorithm (4.6) and(4.7). Also the Verlet method requires only one force evaluation per time step and only onecopy of r and v for each particle. For the latter property to hold it is necessary that thevelocity updating is performed in two steps, separated the the force evaluation for the updatedparticle positions rn+1. The updated force evaluation will then be used for the first part ofthe subsequent time step. The algorithm is self-starting, but requires a slightly increasednumber of operations per time step relative to the leap-frog algorithm.

Page 45: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

4.2. ORBIT INTEGRATION 45

4.2.4 Runge-Kutta method

A whole class of non-staggered, self-starting integration methods are known under the name ofRunge-Kutta algorithms. They come with different order accuracy and have simple geometricinterpretations. We present two examples here, an O(∆t2) algorithm and the more commonlyused O(∆t4) algorithm.

For our purpose it is useful to write the equations of motion in the form of a set of firstorder differential equation in position-velocity space

z =

(

r

v

)

.

With

G(z, t) =

(

v

a

)

the equation of motion can be written as

z = G(z, t).

The Runge-Kutta algorithm of order 2 now takes the form

G1 = G(zn, tn) (4.34)

G2 = G(zn +∆t

2G1, tn+ 1

2

) (4.35)

zn+1 = zn + ∆tG2. (4.36)

The graphical interpretation of the second order Runge-Kutta algorithm is demonstrated infigure 4.3a. We note that the derivative at the mid-point t = tn+1/2 is used as the averagederivative for the interval (tn, tn+1).

Figure 4.3: Illustrating the Runge-Kutta algorithm

Two forcing function evaluations are necessary per time step for the second order Runge-Kutta method. The storage requirement is 3d per particle, 2d for two z-vectors and d for oneG-vector.

Page 46: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

46 CHAPTER 4. DYNAMICAL SYSTEMS

The Runge-Kutta algorithm of order four is constructed in a similar way. Here fourdifferent values of the derivative over the interval (tn, tn+1) is used

G1 = G(zn, tn) (4.37)

G2 = G(zn +∆t

2G1, tn+ 1

2

) (4.38)

G3 = G(zn +∆t

2G2, tn+ 1

2

) (4.39)

G4 = G(zn + ∆tG3, tn+1) (4.40)

zn+1 = zn +∆t

6(G1 + 2G2 + 2G3 + G4). (4.41)

This time the average derivative over the interval (tn, tn+1) is made up of an average of thederivative at the starting point, one approximation for the derivative at the end point andtwo approximations for the mid-point derivative. The two mid-point derivatives have beengiven double weight for the average. The fourth order algorithm is also illustrated graphicallyin figure 4.3b.

The fourth order Runge-Kutta method needs four forcing function evaluations per timestep. The storage requirement is 4d per particle, 2d for two z-vectors and 2d for two G-vectors. For this result the average derivative must be accumulated in several steps, G1+2G2

overwrites G1 and so on.

4.2.5 Predictor-Corrector methods

Predictor-corrector methods make use of system information stored from earlier time steps,tn−1, tn−2, · · · to advance the solution from time tn to tn+1. For this reason these methodsare also known as multi-step methods. If the stored information is limited to the values ofthe ”forcing” function G, then the multi-step method is said to be of Adams-Bashford type.We restrict our discussion to this class of methods.

As the name indicates predictor-corrector methods split into two steps. The predictorstep is an extrapolation based on present and previous values of the forcing function topredict the solution at the future time. The corrector step then improved on this result byan interpolation based on future, present and previous values of the forcing function. Thealgorithm is generally given in the form

z′n+1 = zn + ∆t

p−1∑

i=0

PiGn−i (4.42)

for the predictor and

zn+1 = zn + ∆t

p−2∑

i=−1

CiGn−i (4.43)

for the corrector. The parameter p denotes the order of the predictor-corrector method. Fromthe formula it is seen that p− 1 of the previous values of the forcing function are used for thepredictor step to predict the future solution z′

n+1. The predicted value of the forcing functionG(z′

n+1, tn+1), calculated on the basis of the predicted solution z′n+1, together with p − 2 of

the previous values of the forcing function are then used to produce the corrected solution.

Page 47: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

4.2. ORBIT INTEGRATION 47

The coefficients Pi and Ci in (4.42) and (4.43) are determined by requiring that thealgorithm is to produce exact results for forcing function G ∼ tq for q = 0, 1, · · · , p− 1. Thisleads to the following conditions

1

q + 1=

p∑

i=0

(−i)qPi (4.44)

1

q + 1=

p−1∑

i=−1

(−i)qCi (4.45)

for q = 0, 1, · · · , p−1. For a predictor-corrector scheme of order p, the numerical error term isO(∆tp). In table 4.1 coefficients for predictor-corrector methods of different orders are givenfor first order systems.

-1 0 1 2 3

p = 3 P 23 -16 5×1/12 C 5 8 -1

p = 4 P 55 -59 37 -9×1/24 C 9 19 -5 1

Table 4.1: PC coefficients for first order system

The predictor-corrector method require two forcing function evaluations per time step, theforcing function at time tn and the forcing function corresponding to the predicted solutionat tn+1. For a predictor-corrector method of order p the storage requirement is (p + 3)d perparticle when Gn+1 overwrites Gn−p+1

A predictor-corrector method may also be constructed for the equations of motion in theform (4.1)-(4.2). For this case the order p predictor step is

r′n+1 = rn + ∆tvn + ∆t2

p−1∑

i=0

PiF n−i (4.46)

v′n+1 = vn + ∆t

p−1∑

i=0

P ′iF n−i (4.47)

while the corresponding corrector step is

rn+1 = rn + ∆tvn + ∆t2p−2∑

i=−1

CiF n−i (4.48)

vn+1 = vn + ∆t

p−2∑

i=−1

C ′iF n−i (4.49)

Again requiring the algorithm to produce exact results for forcing functions F ∼ tq for

Page 48: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

48 CHAPTER 4. DYNAMICAL SYSTEMS

q = 0, 1, · · · , p − 1, the coefficients are this time determined by

1

(q + 1)(q + 2)=

p−1∑

i=0

(−i)qPi (4.50)

1

q + 1=

p−1∑

i=0

(−i)qP ′i (4.51)

and

1

(q + 1)(q + 2)=

p−2∑

i=−1

(−i)qCi (4.52)

1

q + 1=

p−2∑

i=−1

(−i)qC ′i (4.53)

for q = 0, 1, · · · , p − 1. Coefficients Pi, P ′i , Ci and C ′

i for second order systems are given intable 4.2.

-1 0 1 2 3

p = 4 P 19 -10 3×1/24 P ′ 46 -32 10

C 3 10 -1C ′ 10 16 -2

p = 5 P 323 -264 159 -38×1/360 P ′ 825 -885 555 -137

C 38 171 -36 7C ′ 137 285 -75 15

Table 4.2: PC coefficients for second order system

Two forcing function evaluations are required per time step and the storage requirementis this time (p+5)d/2 per particle. We note that in the particular case that F is independentof v, then only the first part (4.46) of the predictor step need be performed. The storagerequirement then also reduces to (p + 4)d/2.

Common for all multi-step methods is that these are not self-starting. That is, anotherintegration method is needed to advance the system through the first p time steps until therequired amount of previous information of the system have been accumulated. For the start-up procedure a Runge-Kutta method is often used. For the multi-step method to proceedit is also necessary that the time step is not altered. If for some reasons a time step changeis required, steps must again be taken to provide the required previous information for thesystem.

4.3 Periodic boundaries and the Ewald’s problem

With N particles in a volume V each particle may be considered occupying an elementarycubical volume V/N . The area of one side surface of such an elementary volume is (V/N)2/3.

Page 49: Introduction to Numerical Simulation of Many-Particle Systems · Introduction to Numerical Simulation of Many-Particle Systems Methods and Applications Jan Trulsen The Institute for

4.3. PERIODIC BOUNDARIES AND THE EWALD’S PROBLEM 49

The number of particles located adjacent to the boundary of the system at any given timeis approximately 6V 2/3/(V/N)2/3 = 6N2/3. The ratio of wall adjacent to the total particlenumber is thus 6/N1/3. For almost any physical system conceivable this will be a very smallnumber.

In a numerical simulation we are for practical reasons restricted to work with simulationsystems with a much reduced particle number. This means that wall adjacent particles andconditions at the wall to a much larger degree will dominate the system while we may bemostly interested in the typical behavior of interior particles.

This may be achieved by introducing periodic boundary conditions in the system. Thatis, we consider the system divided into a large number of identical volumes and assumethe physical state in each of these volumes to be identical to each other. We may thenconsider only the particles in one volume and their motions. The total force acting n anygiven particle is determined by the other particles in the given volume in addition to theperiodically located particles in all the other volumes. With this trick the wall particlesare effectively pushed away to infinity, but we are instead faced with the so called Ewald’sproblem of evaluating the contribution to the force from periodically located particles inadjacent volumes. Mathematically, the force on particle number j may be expressed as

F j =∑

k 6=j

f(rj , rk) +∑

cells

j

f(rj , rk + Dcells)

where Dcells are shift vectors from the calculation volume to the different periodic volumes.