Monte Carlo integration (naive Monte Carlo)

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

Cheng Soon Ong & Christian Walder. Canberra February June 2018

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

Physics 403. Segev BenZvi. Monte Carlo Techniques. Department of Physics and Astronomy University of Rochester

Today: Fundamentals of Monte Carlo

Physics 403 Monte Carlo Techniques

Today: Fundamentals of Monte Carlo

Statistical Thermodynamics and Monte-Carlo Evgenii B. Rudnyi and Jan G. Korvink IMTEK Albert Ludwig University Freiburg, Germany

Monte Carlo (MC) Simulation Methods. Elisa Fadda

Uniform Random Number Generators

Advanced Monte Carlo Methods Problems

Markov Processes. Stochastic process. Markov process

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

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

Week 7: Integration: Special Coordinates

The curious properties of spin

Today: Fundamentals of Monte Carlo

( ) ( ) Monte Carlo Methods Interested in. E f X = f x d x. Examples:

The XY-Model. David-Alexander Robinson Sch th January 2012

Monte Carlo Methods for Computation and Optimization (048715)

3.320 Lecture 18 (4/12/05)

The Markov Chain Monte Carlo Method

Note: Each problem is worth 14 points except numbers 5 and 6 which are 15 points. = 3 2

1. (3) Write Gauss Law in differential form. Explain the physical meaning.

Sampling Random Variables

Understanding Molecular Simulation 2009 Monte Carlo and Molecular Dynamics in different ensembles. Srikanth Sastry

JUST THE MATHS UNIT NUMBER INTEGRATION APPLICATIONS 13 (Second moments of a volume (A)) A.J.Hobson

The Monte Carlo Algorithm

2 Random Variable Generation

Monte Carlo importance sampling and Markov chain

André Schleife Department of Materials Science and Engineering

Mean Intensity. Same units as I ν : J/m 2 /s/hz/sr (ergs/cm 2 /s/hz/sr) Function of position (and time), but not direction

Jim Lambers MAT 280 Summer Semester Practice Final Exam Solution. dy + xz dz = x(t)y(t) dt. t 3 (4t 3 ) + e t2 (2t) + t 7 (3t 2 ) dt

Computer Intensive Methods in Mathematical Statistics

Metropolis/Variational Monte Carlo. Microscopic Theories of Nuclear Structure, Dynamics and Electroweak Currents June 12-30, 2017, ECT*, Trento, Italy

Modern Methods of Data Analysis - WS 07/08

Random numbers and generators

SAMPLING SPECIAL DISTRIBUTIONS

PHYS 7411 Spring 2015 Computational Physics Homework 3

MTH739U/P: Topics in Scientific Computing Autumn 2016 Week 6

Power Series. Part 2 Differentiation & Integration; Multiplication of Power Series. J. Gonzalez-Zugasti, University of Massachusetts - Lowell

Markov Chain Monte Carlo Simulations and Their Statistical Analysis An Overview

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

1 Stat 605. Homework I. Due Feb. 1, 2011

Markov Chain Monte Carlo (MCMC)

Monte Carlo methods for kinetic equations

Markov chain Monte Carlo

Math Stochastic Processes & Simulation. Davar Khoshnevisan University of Utah

SOUTH AFRICAN TERTIARY MATHEMATICS OLYMPIAD

Riemann integral and volume are generalized to unbounded functions and sets. is an admissible set, and its volume is a Riemann integral, 1l E,

Mathematical Methods for Neurosciences. ENS - Master MVA Paris 6 - Master Maths-Bio ( )

Monte Carlo and cold gases. Lode Pollet.

CE 530 Molecular Simulation

Perfect simulation for image analysis

Monte Carlo Radiation Transfer I

Random Number Generation. Stephen Booth David Henty

Physics 9 Spring 2012 Midterm 1 Solutions

Computer simulation methods (1) Dr. Vania Calandrini

Random Walks A&T and F&S 3.1.2

1 Maintaining a Dictionary

You may not start to read the questions printed on the subsequent pages until instructed to do so by the Invigilator.

Answers and expectations

Preliminary Exam 2018 Solutions to Morning Exam

Math 350 Solutions for Final Exam Page 1. Problem 1. (10 points) (a) Compute the line integral. F ds C. z dx + y dy + x dz C

14 Higher order forms; divergence theorem

Numerical integration and importance sampling

Homework 10 Solution

Preliminary Exam 2016 Solutions to Morning Exam

Monte Carlo Methods in Statistical Mechanics

MSc Mas6002, Introductory Material Mathematical Methods Exercises

Monte Carlo Sampling

ICCP Project 2 - Advanced Monte Carlo Methods Choose one of the three options below

Pattern Recognition and Machine Learning. Bishop Chapter 11: Sampling Methods

Instructions: No books. No notes. Non-graphing calculators only. You are encouraged, although not required, to show your work.

1. Find and classify the extrema of h(x, y) = sin(x) sin(y) sin(x + y) on the square[0, π] [0, π]. (Keep in mind there is a boundary to check out).

Classical Monte-Carlo simulations

Introduction. Microscopic Theories of Nuclear Structure, Dynamics and Electroweak Currents June 12-30, 2017, ECT*, Trento, Italy

CE 530 Molecular Simulation

In this approach, the ground state of the system is found by modeling a diffusion process.

Monte Carlo Molecular Simulation with Isobaric-Isothermal and Gibbs-NPT Ensembles. Thesis by Shouhong Du

Question: My computer only knows how to generate a uniform random variable. How do I generate others? f X (x)dx. f X (s)ds.

Final exam (practice 1) UCLA: Math 32B, Spring 2018

xy 2 e 2z dx dy dz = 8 3 (1 e 4 ) = 2.62 mc. 12 x2 y 3 e 2z 2 m 2 m 2 m Figure P4.1: Cube of Problem 4.1.

Lecture 10 - Moment of Inertia

Stat 516, Homework 1

Data Analysis I. Dr Martin Hendry, Dept of Physics and Astronomy University of Glasgow, UK. 10 lectures, beginning October 2006

A Repetition Test for Pseudo-Random Number Generators

MATH5835 Statistical Computing. Jochen Voss

Solutions Ph 236b Week 1

Multimodal Nested Sampling

Problem Set 4. f(a + h) = P k (h) + o( h k ). (3)

Quantum Monte Carlo. Matthias Troyer, ETH Zürich

Fault Tolerance & Reliability CDA Chapter 2 Cyclic Polynomial Codes

Contents. MATH 32B-2 (18W) (L) G. Liu / (TA) A. Zhou Calculus of Several Variables. 1 Multiple Integrals 3. 2 Vector Fields 9

Physics 115/242 Monte Carlo simulations in Statistical Physics

Math Final Exam.

Bayesian Methods and Uncertainty Quantification for Nonlinear Inverse Problems

Contents. 1 Probability review Introduction Random variables and distributions Convergence of random variables...

an introduction to bayesian inference

Simulation. Where real stuff starts

Transcription:

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)