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

Similar documents
Uniform Random Number Generators

2008 Winton. Review of Statistical Terminology

How does the computer generate observations from various distributions specified after input analysis?

Review of Statistical Terminology

Generating Uniform Random Numbers

Generating Uniform Random Numbers

Random Number Generation. Stephen Booth David Henty

General Principles in Random Variates Generation

Generating Uniform Random Numbers

Simulation. Where real stuff starts

Simulation. Where real stuff starts

Generating pseudo- random numbers

Chapter 4: Monte Carlo Methods. Paisan Nakmahachalasint

How does the computer generate observations from various distributions specified after input analysis?

A Repetition Test for Pseudo-Random Number Generators

Computational statistics

Markov Chain Monte Carlo (MCMC)

Uniform random numbers generators

MATH5835 Statistical Computing. Jochen Voss

Pseudo-Random Numbers Generators. Anne GILLE-GENEST. March 1, Premia Introduction Definitions Good generators...

Generating the Sample

Markov Chain Monte Carlo Lecture 1

So we will instead use the Jacobian method for inferring the PDF of functionally related random variables; see Bertsekas & Tsitsiklis Sec. 4.1.

Answers and expectations

Bayesian Methods for Machine Learning

Stat 451 Lecture Notes Simulating Random Variables

Random Number Generators

Uniform Random Binary Floating Point Number Generation

16 : Markov Chain Monte Carlo (MCMC)

Monte Carlo Methods. Geoff Gordon February 9, 2006

Exploring Monte Carlo Methods

2 Random Variable Generation

2008 Winton. Statistical Testing of RNGs

Cheng Soon Ong & Christian Walder. Canberra February June 2018

Monte Carlo Techniques

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

STA 4273H: Statistical Machine Learning

S6880 #6. Random Number Generation #2: Testing RNGs

Modern Methods of Data Analysis - WS 07/08

Notation Precedence Diagram

Ch3. Generating Random Variates with Non-Uniform Distributions

Monte Carlo Simulations

Generating Random Variables

A Bayesian Approach to Phylogenetics

Simulating Random Variables

Random numbers and generators

Introduction to Machine Learning CMU-10701

B. Maddah ENMG 622 Simulation 11/11/08

Bayesian Inference in GLMs. Frequentists typically base inferences on MLEs, asymptotic confidence

F denotes cumulative density. denotes probability density function; (.)

Monte Carlo Methods. Jesús Fernández-Villaverde University of Pennsylvania

Lecture 15: MCMC Sanjeev Arora Elad Hazan. COS 402 Machine Learning and Artificial Intelligence Fall 2016

Monte Carlo integration

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

EEC 686/785 Modeling & Performance Evaluation of Computer Systems. Lecture 19

Nonlife Actuarial Models. Chapter 14 Basic Monte Carlo Methods

MCMC Methods: Gibbs and Metropolis

HMMT February 2018 February 10, 2018

CSC 2541: Bayesian Methods for Machine Learning

Physics 403 Monte Carlo Techniques

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

I. Bayesian econometrics

Preliminary statistics

Class 12. Random Numbers

Random Number Generators - a brief assessment of those available

S6880 #7. Generate Non-uniform Random Number #1

17 : Markov Chain Monte Carlo

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

14 Random Variables and Simulation

Reminder of some Markov Chain properties:

CPSC 531: Random Numbers. Jonathan Hudson Department of Computer Science University of Calgary

Mathathon Round 1 (2 points each)

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

ST 740: Markov Chain Monte Carlo

Chapter 4: Monte-Carlo Methods

CS145: Probability & Computing Lecture 24: Algorithms

(5) Multi-parameter models - Gibbs sampling. ST440/540: Applied Bayesian Analysis

Random variables. DS GA 1002 Probability and Statistics for Data Science.

CPSC 540: Machine Learning

Metropolis Hastings. Rebecca C. Steorts Bayesian Methods and Modern Statistics: STA 360/601. Module 9

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

Sources of randomness

Advances and Applications in Perfect Sampling

Bayesian Inference and MCMC

Workshop on Heterogeneous Computing, 16-20, July No Monte Carlo is safe Monte Carlo - more so parallel Monte Carlo

Advanced Statistical Modelling

Outline. Simulation of a Single-Server Queueing System. EEC 686/785 Modeling & Performance Evaluation of Computer Systems.

INTRODUCTION TO MARKOV CHAIN MONTE CARLO

Continuous Distributions

SAMPLING ALGORITHMS. In general. Inference in Bayesian models

Slides 3: Random Numbers

arxiv: v1 [stat.co] 18 Feb 2012

CS242: Probabilistic Graphical Models Lecture 7B: Markov Chain Monte Carlo & Gibbs Sampling

Stochastic Simulation of

Bayesian Estimation with Sparse Grids

8 Error analysis: jackknife & bootstrap

( x) ( ) F ( ) ( ) ( ) Prob( ) ( ) ( ) X x F x f s ds

27 : Distributed Monte Carlo Markov Chain. 1 Recap of MCMC and Naive Parallel Gibbs Sampling

Lecture 2 : CS6205 Advanced Modeling and Simulation

Systems Simulation Chapter 7: Random-Number Generation

Transcription:

Monte Carlo Methods Interested in Examples: µ E f X = f x d x Type I error rate of a hypothesis test Mean width of a confidence interval procedure Evaluating a likelihood Finding posterior mean and variance Often calculating these will be difficult. Approximate with where x 1,, xn 1 n f i n i = 1 ( x ) µ. is sampled from ( X ) Under certain regularity conditions, 1 n i i 1 f x E f X n = 1

Issues: probability measure being integrated over function being integrated sampling scheme form of convergence Focus: Sampling Schemes Independently and identically distributed (IID) Importance sampling Sequential importance samplers (SIS) Markov Chain Monte Carlo (MCMC) Gibbs sampling Metropolis Hastings (M-H) Reversible jump Bridge sampling etc and so on 2

Choice is often driven by µ ( X ), e.g., IID infeasible leads to use of SIS or MCMC SIS may work but Gibbs sampler has reducible chain M-W works when SIS has poorly behaved importance sampling weights. etc There is no one Monte Carlo approach that will solve every problem. Want to develop a set of tools that can be used, possibly in combination, for a wide range of problems. Need to recognize when each of them should work and when modifications are needed. 3

Basic Simulation Methodology Pseudo-random numbers: Random numbers generated on computers are not random but generated by deterministic algorithms. Basic problem: generate ui, 0 < ui < 1, that U 0,1 distribution. appear to be an iid sample from the Once you have these, you can simulate from any distribution. Uniform deviates Instead of generating U ( 0,1), most generators actually generate integers ( U ( 0, m 1) or U ( 1, m 1) ) and then convert these to the interval ( 0,1 ). Numerically more stable and faster. 4

Multiplicative Congruential Generators: Generate integer sequence { k i } by ki+ 1 = aki modm for suitably chosen positive integers a and m, where b mod m is the remainder from dividing b by m. m 1 l If a = 1modm and a 1modm for 0 < l < m 1 and if k 0 is a positive integer that isn t a multiple of m, then it can be shown that k1,, km 1 will be a permutation of {1, 2,, m 1} (a is said to be a primitive root of unity mod m). The period of this generator is m 1. In general the period is the number of values until the generator starts to repeat. Linear Congruential Generators ki+ 1 = aki + b modm for suitable integers a, b, and m. 5

Good generators should have long periods low (near 0) correlations give samples that look uniform This holds for any generator, not just congruential generators. Choices for m, a, and b 31 1) m = 2 1: largest prime integer that can be stored on most computers 5 a = 7 (IMSL, early versions of Matlab) a = 950,706,376 (IMSL option, shown to be good by Fishman & Moore) 32 2) m = 2 : number of integers that can be represented on most computers. Can t get full period if b = 0 since m is even. 30 Maximum period of 2 can be achieved if a = 5 + 8l for some l. Common choice is a = 69069 6

30 Can get full period of 2 when b 0, such as with a = 69069 and b = 23606797. Problems with congruential generators: Must have some autocorrelation n dimensional uniformity (how close do n tuples of consecutive draws achieve uniformity in the n-dimensional unit cube). congruential generators tend to give n- vectors that concentrate near hyperplanes in n-dimensional space for some n. Example: RANDU generator IBM SYSTEM/360 generator ki+ 1 = 2 + 3 k mod2 Has the property that i i+ 1 i+ 2 16 31 i 9k 6k + x = 0mod2 31 7

Proof: 2 2 9xi 6axi + a xi = a 3 xi = = 32 31 2 xi 2 2 Realizations of triples must fall on one of 15 31 planes, 2 apart x i from <http://www.unf.edu/ccec/cis/cishtml/randuinfo.html> 8

This generator is still around is system software. From HP documentation <http://h18009.www1.hp.com/fortran/docs/lrm/lrm0315.htm> GSL (Gnu Scientific Library) <http://www.gnu.org/software/gsl/manual/gslref_17.html#sec271> In GSL, its there for backward compatibility with old code that people use and historical completeness. With congruential generators, the leading bits tend to be more random than the low order bits, so one shouldn t treat subsets of the bits in the representation as separate random numbers Standard congruential generators have periods that are too short for some statistical applications. 9

Shuffling algorithm Initialize: s() i = ui ; i = 1,, N and set y = s N. Generate a new value u and set j = int ( yn) + 1, where int ( x ) is the largest integer x. Set y = s( j), s( j) uniform deviate. = u, and return y as the The idea behind this scheme is that a permutation of uniforms is still uniform. By combining generators with long, but not equal periods, a new generator with a much longer period can be created. Example: ran2 (Numerical recipies due to L Ecuyer) Generator 1 ( v i ): a = 40014, m = 2147483563. Uses shuffle algorithm with N = 32. Generator 2 ( w ): a = 40692, m = 2147483399. Returns i ( 1 ) u = v w I v w + v + w I v < w i i i i i i i i i 10

The period of this generator is the product of the periods of the two streams, divided by any common factors. The period is about 18 61 2.3 10 2. Recursive generators k a k ak m i+ 1 = 1 i + + l i+ 1 l mod Linear combination of the previous l values. l Maximum period: m 1 Fibonacci generators u = u u i i 17 i 5 u = u u i i 97 i 33 If lagged difference < 0, add 1 to result. 11

Shift / Tausworthe generators Based on binary expansion of integers 32 j = b 2 l l l = 1 The idea is to shift the sequence and then combine it with the original sequence by exclusive or. As part of the S-Plus generator, they use the following shift generator double ush(j) unsigned long *j; { double v = 4294967296; /* v = 2^32 */ *j = *j ^ (*j >> 15); *j = *j ^ (*j << 17); return(*j/v); } *j >> 15 shifts the bits right by 15 and replaces bits 1 to 15 with 0. ^ is exclusive or so *j = *j ^ (*j >> 15) replaces the vector j with ( b b b + b b + b ),,,,, mod2 32 18 17 32 1 16 1 12

The S-Plus generator combines this with a congruential generator with a = 69069, m = with an exclusive or operation. 32 2 R has 6 different uniform generators. The default is the Mersenne Twister, a generalized feedback shift register (GFSR) generator with a 19937 6000 period of 2 1 10. To see the others available in R, see help(rngkind). Bottom line: Creating a good generator is an art and a science. The constants used in congruential generators and the lags used in Fibonacci and Tausworthe generators are not arbitrary. Poor choices can lead to very nonrandom behaviour (such as with RANDU). Diehard tests A set of procedures for testing random number generators created by George Marsaglia. 13

Generating from non-uniform distributions For a cumulative distribution function (CDF) P X x = F x, the inverse CDF is defined by [ ] For continuous RVs, { } inf : 1 F u = x F x u 1 = 1 = F F ( u) = u P F X u P X F u so F ( X) ~ U ( 0,1). Conversely, if U ~ U ( 0,1) = 1 P F u x F x Thus, given an iid U ( 0,1) sample { u,, 1 un } iid sample { x,, 1 xn } 1 x = F ( u ). i Example: Cauchy i, an from F can be obtained by 1 1 x µ F ( x; µσ, ) = + arctan 2 π σ ( ; µσ, ) = µ + σtan π( 1 2) 1 F u u 14

Example: Exponential ( ; µ ) = 1 exp( x µ ) ( ; µ ) = µ log( 1 ) F x 1 F u u Sometimes its easier to work with the survivor function S( x) = 1 F ( x). Since U and 1 U 1 both have uniform distributions, S ( u) will also be a draw from F. So ( ; µ ) = µ log 1 S u u will also give a draw from an exponential distribution. Not all distributions (e.g. normal or gamma) 1 have nice closed form expressions for F. The density is usually of a nice form, but often the CDF, and thus its inverse aren t. However there are often good analytical approximations 1 to F, so these can be used instead. For example with the standard normal, a rational function approximation could be used (R and Matlab definitely do, S-Plus probably). 15

Note that the Inverse CDF method isn t commonly used for most distributions as it tends to be slow. Functions like log, sin, cos, etc tend to be somewhat expensive to calculate. Though surprisingly in R, it is the default for normals. However there are 4 other methods available (see help(rngkind)). In S-Plus and Matlab, I don t know what they are doing. Discrete Distributions: Suppose that the distribution has support points s 1, s 2,, s k (k possibly infinite) and set j [ ] p = P X = s = P X s j i j i = 1 Then independent observations x i can be generated by setting xi = sj if p 1 < u p (where p 0 = 0). j i j Essentially this is inverting the CDF. 16

If k is small, then only a few comparisons need to be made. However if k is big, many comparisons may be needed (if u i is close to 1). In this case, other methods are needed. Relationships with other distributions: Examples: 2 ~ (, ) X N µ σ then X ~ N ( 0,1) then Y Y X = e is lognormal 2 = X is 2 χ 1 Xα ~ Gamma( 1, α ), Xβ ~ Gamma( 1, ) X Y = ~ Beta( α, β ) X α α X + β β, then Polar Coordinates and the Normal Distribution Suppose that X, Y are independent N ( 0,1) variables and consider the polar coordinates transformation given by X = Rcos θ, Y = Rsin θ; R 0,0 θ < 2π 17

It is easily shown that ~ U ( 0,2 ) θ π, and they are independent. Also 2 2 2 [ > ] = > = exp ( 2) P R r R r r Box-Muller Method Generate u, u ~ U ( 0,1) 1 2 R ~ χ, 2 2 2 Set R = 2logu1 and θ = 2π u2 Set x1 = Rcosθ and x2 = Rsinθ Marsaglia Polar Method The sin and cos functions (which are slow) can be avoided. Underlying the Box-Muller method is to pick uniform angle independently of a radius. This can be recast in terms of picking points in the unit circle 18

Let, ~ ( 1,1) v v U such that v 2 + v 2 1 2 1 2 1 Let θ be the counterclockwise angle from the positive v 1 axis to the point ( v1, v 2). By θ ~ U 0,2π and symmetry, cosθ = u u 1 + u 2 2 1 2 and sinθ = u u 2 + u 2 2 1 2 Also P v + v u = πu π = u 2 2 1 2, so 2 2 v + v ~ U 0,1 1 2 19

Finally θ and v 2 2 1 + v2 are independent (again by symmetry). Thus, given ( v, v ) given by 1 2 2 2 1 2, two normal deviates are u = v + v, w = 2log u/ u, x = v w, x2 = v2w 1 1 This is an example of the Acceptance-Rejection Method. For this example, the fraction of pairs ( v, v ) 1 2 that are accepted = π 4 = 0.785, the ratio of the area of the circle to the square. An old generator N ( 0,1) and z = u 6 Let u,, u ~ U ( 0,1) 1 12 1 1 = i = 2 12 [ ] ;Var( u ) E u i i ( i) E u = 6; Var u = 1 i 20

So z has mean 0 and standard deviation 1 and is approximately normal (very approximately) by the Central Limit Theorem sum(runif(12)-6) -4-2 0 2 4-4 -2 0 2 4 rnorm (R) This generator has tails which are too light. An example of a consequence of this would be to incorrectly estimate the coverage rate of a confidence interval procedure. 21

Generating Random Deviates Often there are no direct ways of sampling from a desired distribution (e.g. inverse cdf or relationship with other distributions). So we need other approaches to generation for other distributions. Acceptance-Rejection (von Neumann, 1951) Want to simulate from a distribution with density f ( x ). Need to find a dominating or majorizing distribution g( x ) where g is easy to sample from and f ( x) cg( x) = h ( x) for all x and some constant c > 1. 22

Sampling scheme 1) Sample x from g( x ) and compute the ratio r ( x) cg ( x ) 2) Sample u ~ U ( 0,1) = f x = f x 1 h x If u r ( x) accept and return x If u > r ( x ) reject and go back to 1) Note that step 2) is equivalent to flipping a biased coin with success probability r. The resultant sample is a draw from f ( x ). Proof: Let I be the indicator of whether a sample x is accepted. Then Next, [ = 1] = = 1 = f ( x) 1 = g ( x ) dx = cg ( x ) c P I P I X x g x dx 23

( x) ( x ) f p( x I = 1) = g x P I = 1 cg x cf = = c [ ] f ( x) See Flury (1990) for a more geometrical proof. Its based on the idea of drawing uniform points, h x and only accepting ( xy ) under the curve the points that also lie under the curve f x. Generating Half Normal 0.0 0.2 0.4 0.6 0.8 1.0 1.2 Half normal c * Exponential(1) 0 1 2 3 4 x 24

The number of draws needed until an acceptance occurs is Geometric ( 1 c ) and thus the expected number of draws until a sample is accepted is c. The acceptance probability satisfies 1 c f x dx Area under f x = = cg x dx Area under h x One consequence of this is that c should be made as small as possible to minimize the number rejections. The optimal c is given by c ( x ) = sup f g x Note that the best c need not be determined, just one that satisfies for all x. f ( x) cg( x) = h ( x) 25

Example: Generating from the half normal distribution. = 2φ ( 0) f x x I x = I x π 2 2 2 x exp 0 Lets use an Exp(1) as the dominating density x = ( 0) g x e I x Generating Half Normal 0.0 0.2 0.4 0.6 0.8 1.0 1.2 Half normal c * Exponential(1) 0 1 2 3 4 The optimal c is x c 2 = exp ( 1 2 ) 1.315489 π 26

Thus the acceptance rejection scheme is 1) Draw x ~Exp( 1) ( 2 ) = exp 0.5 ( x 1) r x 2) Draw u ~ U ( 0,1) If u r ( x) accept and return x If u > r ( x ) reject and go back to 1) Note that this scheme isn t needed for this example as the half normal distribution is the N 0,1 distribution of the absolute value of 5 QQ Plot with 100,000 samples 4.5 4 Acceptance - Rejection 3.5 3 2.5 2 1.5 1 0.5 0 0 1 2 3 4 5 N(0,1) 27

In the above is was assumed that f ( x ) was a density function. In fact f ( x ) only needs to be known up to a multiplicative constant l( x) = bf ( x) where b may be unknown. One place where this is useful is with posterior distributions as π p x y x f y x The normalizing constant may be difficult to determine exactly. However it is not necessary to do so. Modify the procedure as follows. Find c such that l( x) cg( x) = h ( x) for all x and some constant c > 1. Sampling scheme 1) Sample x from g( x ) and compute the ratio r x = l x l x 1 cg x = h x 28

2) Sample u ~ U ( 0,1) If u r ( x) accept and return x If u > r ( x ) reject and go back to 1) Do everything the same except use l( x ) instead of f ( x ) The acceptance probability for this scheme is bc. In addition to the constant c chosen, the g x will also affect the acceptance distribution rate. (c is chosen conditional on g( x )) A good choice g( x ) will normally be close to f ( x ). You want to minimize the separation between the two densities. Often will look for much member of a parametric family will minimize c. 29

For example, for the Half normal problem, c µ. which Exp ( µ ), will minimize 0.0 0.2 0.4 0.6 0.8 1.0 1.2 Half Normal mu = 1 mu = 2 0 1 2 3 4 x In fact 1 problem. µ = will minimize c µ for this Note that so far I m been appeared to have been focusing on continuous random variables. In fact acceptance-rejection works fine with discrete random variables and with variables over more than one dimension. 30

The proof goes through in this more general place by replacing integration over a density to integration over a more general measure. For discrete random variables, you get a sum over the probability mass function. With higher dimensional problems, the majorization constants tend to be higher, implying the procedure is less efficient. Log-concave densities There is a class of densities where it is easy to set up an acceptance-rejection scheme. It the case when the log of the density is concave on the support of the distributions If f ( x ) is log concave, any tangent line to log f ( x ) will lie above log f ( x ) (call it l( x) = a + bx). Thus l ( x ) = = a bx lies above h x e e e f x. h ( x ) looks like a scaled exponential density. 31

This suggests that exponential distributions can be used as the majorizing distributions. A strictly log concave density is unimodal. The mode may occur at either endpoint or in the middle. If the mode occurs at an endpoint, a single exponential can be used (as with the half normal example) If the mode occurs in the middle of the range, two exponential envelopes are needed (one for left of the mode, the other for the right of the mode) Example: Gamma ( k = 2, λ = 1) The mode for a Gamma is ( k 1) example) Left side: λ (so 1 for this 1 gl x = exp x 1 I x < 1 µ l ( µ l) 1 gr x = exp x 1 I x 1 µ Right side: r ( µ r ) 32

Gamma(k=2,lambda=1) 0.0 0.1 0.2 0.3 0.4 0 2 4 6 8 x The choice of µ l and r µ depend on where you g x want the majorized densities l to be tangent to f ( x ) g x and In the above the tangent points are x l = 0.5 and x = 2. r The total area under h ( x) c g ( x) c g ( x) c = c + c l r = + is l l r r For the example, c l = 0.5 and c r = 0.8925206, so the rejection rate for this sampler is just under 30%. r 33

To determine which exponentials to use, involves solving the systems (for given x l and x ) r Solving gives ( l ) = l l ( l ) ( r ) = r r ( r ) = = f x c g x f x c g x f ' x c g ' x f ' x c g ' x l l l l r r r r ( l ) ( l ) ' ( r ) f ' x f ' x λl = λr = f x f x l l ( r ) ' 2 2 f x f x cl = e c = e f x f x where λ = 1 µ i i r λl xl m λr xr m r r The optimal choices for x l and x r can be found by minimizing c l and c r separately. 34

Discrete log concave distributions Random variable defined on non-negative integers Log concave defined as 1 log f x log f x 1 log f x 1 2 ( ) + ( + ) which is equivalent to for all integers x. 2 ( 1) ( + 1) f x f x f x A possible majorizing distribution in the discrete case is the geometric distribution [ ] P X = x = p 1 p ; x = 0,1,2, See Lange for choices of pl, pr, xl, x r x 35

Ratio Method This is another method that is useful when the distribution you are interested in f ( x ), is only known up to an unknown constant, h x = cf x Define h {(, ):0 } S = u v < u h v u If this set is bounded, we can draw uniform points from this set to generate X. Proposition 20.5.1 Suppose k u = sup x h x and k = sup x h x v x are both finite. Then the rectangle [ 0, k ] [ k, k ] encloses S h. u v v If h ( x ) = 0 for x < 0, then the rectangle [ 0, k ] [ 0, k ] S. u encloses h v 36

If the point ( UV, ) sampled uniformly from the enclosing set falls with in S h, then the ratio X V U f x. = is distributed according to Example: Standard normal h x h x k k u v 1 = exp x 2 1 = exp x 4 = h 0 = 1 2 2 = 2exp 0.5 S_h v -0.5 0.0 0.5 0.0 0.2 0.4 0.6 0.8 1.0 u 37

Generate U Accept X ~ U 0,1 ( ) V ~ U 2 exp 0.5, 2 exp 0.5 = V U if 2 2 U exp 0.25V U The acceptance rate for this procedure is about 62.6% 38