Monte Carlo integration (naive Monte Carlo) Example: Calculate π by MC integration [xpi] 1/21 INTEGER n total # of points INTEGER i INTEGER nu # of points in a circle REAL x,y coordinates of a point in a sphere REAL rnd(-1,1) function returning a random number in interval [ 1, 1) nu := 0 FOR i := 1 TO n DO x := rnd(-1,1) y := rnd(-1,1) IF x*x+y*y < 1 THEN nu := nu + 1 PRINT "pi=", 4*nu/n area of square = 4 PRINT "std. error=", 4*sqrt((1-nu/n)*(nu/n)/(n-1)) Also random shooting. Generally Ω f(x 1,..., x D ) dx 1... dx D Ω K K k=1 f(x (k) 1,..., x(k) D ) where (x (k) 1,..., x(k) D ) is a random vector from region Ω ( Ω = area, volume,... ; calculation of π: Ω = ( 1, 1) 2, Ω = 4)
Exercise Buffon s needle Let a needle of length l be dropped randomly on a plane ruled with parallel lines d units apart, l d. The probability that the needle crosses a line is p = 2l/πd. 2/21 Proof: expression (a < b) gives 1, if the inequality holds true, 0 otherwise p = 1 d/2 d/2 0 dz 1 π/2 π/2 0 dθ (z < l2 ) cos θ = 1 1 d/2π/2 π/2 0 l 2 cos θdθ = 2l πd Usage (δp is the standard error of p) π 2l pd, where p = n crosses n total, grid: pic/buffon-grid.pdf and buffon.sh δp p(1 p) n 1, 2l δπ = pd δp p rel. error
Exercises + 3/21 Easy. Calculate by Monte Carlo integration: 1 r r 0 d r where r 0 = (1, 1, 1) x>0,y>0,z>0,x+y+z<1 0.12522728... Hard. Calculate by Monte Carlo integration the second virial coefficient of the Lennard-Jones diatomics (ɛ/k B T = 1, σ = 1) for bond length L = σ. B 2 = 1 [ ( exp u ) ] 1 d r dω 1 dω 2 2 k B T 4π 4π u = u LJ ( r 1A r 2A ) + u LJ ( r 1A r 2B ) + u LJ ( r 1B r 2A ) + u LJ ( r 1B r 2B ) Hints: d r 4πr 2 dr substitution r = 1/u 1 dω i = d cos θ i dφ i 1.24
Importance sampling 4/21 e βu( r N) f( r N ) 1 K K k=1 f( r N,(k) ) where r N,(k) is a random vector with a probability density e βu( rn). Metropolis algorithm: r N,(k+1) generated sequentially from r N,(k) naive MC importance sampling
Metropolis method (intuitively) 5/21 Choose a particle, i (e.g., randomly) Try to move it, e.g.: x tr i = x i + u ( d,d), y tr i = y i + u ( d,d), z tr i = z i + u ( d,d) so that the probability of the reversed move is the same Calculate the change in the potential energy, U = U tr U or in/on sphere, Gaussian,... If U 0, the change is accepted If U 0, the change is accepted with probability exp( β U) Why? Because then it holds for the probability ratio: new : old = p tr : p = exp( β U) (moves there and back are compared, always the probability of one move = 1, and of the other = Boltzmann probability)
A bit of theory: random variables 6/21 Random variable S gives values in {A i }, i = 1,... M, with probabilities π(a i ) = π i. Normalization: i π i = 1 Markov chain is a sequence S (k), k = 1,..., such that S (k+1) depends only on S (k), or mathematically π (k+1) j = Normalization: M i=1 π (k) i W i j vector notation: π (k+1) = π (k) W M W i j = 1 j=1 for all i
Example [xoctave../octave/markov.m MARKOV] 7/21 Computer network: { 1. in order 2. out of order If in order: will crash with 10% probability (the following day is out of order) If out of order: gets fixed with 30% probability (the following day is in order) ( ) 0.9 0.1 W = 0.3 0.7 lim k π (k) = (0.75, 0.25) Profit: { 2000 in order 500 out of order X = ( 2000 500 Averaged profit = π i X i = π X = 1625 )
Detailed balance and microreversibility 8/21 We are looking for W, so that π i = exp[ βu(a i)] i exp[ βu(a i)] Conditions: W i j 0 for all i, j = 1,..., M M W i j = 1 for all i = 1,..., M j=1 π W = π sometimes detailed balance π i W i j = π j W j i microscopic reversibility (detailed balance) If: all states are accessible from an arbitrary state in a finite number of steps with a nonzero probability and no state is periodic then the set of states is called ergodic and for any initial state probability distribution π (1) there exists a limit π = lim k π (k)
Metropolis again 9/21 One of solutions (Metropolis): α i j for i j a π j π i W i j = π j α i j π i 1 k, k i W i k for i j a π j < π i for i = j Equivalent form: W i j = α i j min { 1, π } j π i for i j where matrix α i j = α j i describes a trial change of a configuration... equivalent to the algorithm given above
Algorithm details 10/21 Choose a particle (lattice site,... ) to move A tr := A (k) + random move (spin) of the chosen particle U := U(A tr ) U(A (k) ) U tr U (k) The configuration is accepted (A (k+1) := A tr ) with probability min{1, e β U } otherwise rejected: Version 1 Version 2 Version 3 u := u (0,1) u := u (0,1) IF U < 0 IF u < min{1, e β U } IF u < e β U THEN A (k+1) := A tr THEN A (k+1) := A tr THEN A (k+1) := A tr ELSE ELSE A (k+1) := A (k) ELSE A (k+1) := A (k) u := u (0,1) IF u < e β U k := k + 1 and again and again THEN A (k+1) := A tr ELSE A (k+1) := A (k)
[start z-vitezneho-oblouku.mov] 11/21 How to choose a particle to move In a cycle check the reversibility! Deterring examples of microreversibility violation: Three species A, B, C in a ternary mixture moved sequentially in the order of A B C A B C Sequence: move molecule A move molecule B change volume Randomly Chaos is better than bad control
Heat-bath method + 12/21 good for lattice models: Usually: W i j = exp( βu j ) A k C part exp( βu k ) for A i, A j C part choose spin/lattice site choose a new spin value with a Boltzmann probability (it depends on the neighbourhood)
Acceptance ratio 13/21 χ = number of accepted configurations number of all configurations χ depends on the displament d. Optimal χ depends on the system, quantity, algorithm. Often 0.3 is a good choice. Exception: diluted systems... 0.020 0.020 δp 0.015 0.010 0.015 0.010 0.005 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 d 0.005 LJ (reduced units): T = 1.2, ρ = 0.8 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 χ
Exercise 14/21 Write a computer code for one molecule of nitrogen in a gravitational field. Use temperature T = 300 K. Determine the pressure at elevation of 8850 m and the acceptance ratio. Hint: a suitable displacement length is about 30 km. The potential of a molecule is u(z) = { for z < 0 mgz for z 0 0.37 bar If you get bored: Determine the optimum trial displacement z with respect to the averaged height z. Note: it is necessary to measure z including the statistical error and to set z to minimize the error. cca. 30 km, ξ = 0.3
(Pseudo)random numbers [simul/ibm.sh 10000] 15/21 r i = F(r i 1, r i 2,..., r i m ) Requirements: the period (smallest number p such that r i+p = r i ) is as long as possible; distribution r i is (in an interval given) uniform, particularly: also the lowest bits are random; (r i, r i+1 ), triplets (r i, r i+1, r i+2 ), etc., are uncorrelated; the same holds for all functions f i : pairs (f 0 (r i ), f 1 (r i 1 )), triplets (f 0 (r i ), f 1 (r i+1 ), f 2 (r i+2 )), etc., are uncorrelated; the code is fast. IBM: K(2 16 + 3, 2 31 )
Feedback shift-register generators 16/21 R(A, B, C,...) : r i = r i A r i B r i C..., = addition modulo 2 = XOR: 0 0 = 1 1 = 0, 1 0 = 0 1 = 1 Max. period is 2 max(a,b...) 1 A word (32 or 64 bits) at once E.g., R(108, 250), R(471, 1586, 6988, 9689) Example. R(5, 2): 1 step: 5 4 3 2 1 1 1 0 1 1 0 1 1 = 0 more steps: 110110001111100110100100001010111011... here period = 2 5 1 = 31 (maximum possible)
Feedback shift-register generators + 17/21 Algorithm: CONST A=103 CONST B=205 CONST M=255 where M is the smallest number of form 2 k 1 so that B M INTEGER n unsigned integer INTEGER r[0..m] array, filled in advance by random numbers of any origin one step generating a random number (all bits): n := n+1 r[n and M] := r[(n-a) and M] xor r[(n-b) and M] where and and xor work bitwise RETURN r[n and M] The code is especially simple as a C/C++ macro: #define rnd (++n, r[n&m] = r[(n-a)&m] ^ r[(n-b)&m]) Pros: fast, mathematical theory for the perion and correlations Cons: fails with some tests,, e.g., the random walk Remedy #1: combine two of them (still fast) Remedy #2: Mersenne twister (high quality, popular)
Congruence generators [simul/kongr.sh] 18/21 K(C, M) : r i = Cr i 1 mod M where A mod B is a reminder after division A/B K(5 7, 2 32 ): period 2 32 /8 K(7 5, 2 31 1): period 2 31 2 Example. K(5, 31): 1 7 18 2 14 5 4 28 10 8 25 20 16 19 9 1 7 18 2 14 5 4 28 10 8 25 20 16 19 9 1 7 18... Suppression of correlations combine 2 generators Declare a table and fill it by random numbers using generator #1 take randomly selected (index = random number using generator #2) item of the table replace the used number by a new random number using generator #1
Other distributions 19/21 Library functions usually give random number u (0,1), uniformly distributed in (0, 1) (or [0, 1) or [0, 1] be careful!), i.e., φ(x) = { 1, x (0, 1) 0, x (0, 1) A number uniformly distributed in interval (a, b) is u (a,b) = a + (b a)u (0,1). Generally: function f(u) applied to u (0,1) φ(y) = x,f(x)=y 1 f (x) Inverse problem: known distribution φ(x), φ(x)dx = 1, needed: the distribution function y φ(x)dx must be inverted. Example: x = ln u gives φ(x) = exp( x) (check for u = 0!)
Gauss normal distribution u Gauss = 2 ln u (0,1) cos(2πu (0,1) ) 20/21 where both random numbers u (0,1) are independent, function u (0,1) thus has to be called twice. Second indepent number: replace cos sin. Approximately: u Gauss 2(u (0,1) u (0,1) + u (0,1) u (0,1) + u (0,1) u (0,1) ) General distribution When φ (in interval (a, b)) is not known (or too complicated): 1. generate x = u (a,b), 2. generate u = u (0,m), where m is the maximum of φ(x) in interval (a, b), 3. if u < φ(x), accept x as the number, otherwise repeat by step 1.
Multidimensional distribution [cd simul; insphere.sh; onsphere.sh] 21/21 In a unit ball (ball = inside of a sphere) 1. generate x = u ( 1,1), y = u ( 1,1), z = u ( 1,1), 2. calculate r 2 = x 2 + y 2 + z 2, 3. if r 2 < 1, accept vector (x, y, z), otherwise repeat by step 1. On a unit sphere: divide r in ball r (check for r 0), or: 1. z = u ( 1,1), φ = u (0,1) 2. x = 1 z 2 sin(2πφ), y = 1 z 2 cos(2πφ) Uniform discrete distribution u N = int(nu (0,1) ) Better not like this (r is a random integer): u N = r mod N (very bad for congruence generators lower significant bits are not random)