Stochastic Simulation APPM 7400 Lesson 5: Generating (Some) Continuous Random Variables September 12, 2018 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 1 / 1
Generating Continuous Random Variables Suppose that X is a continuous random variable with cdf F (x). esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 2 / 1
Generating Continuous Random Variables Suppose that X is a continuous random variable with cdf F (x). Furthermore, suppose that you can invert F. esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 2 / 1
Generating Continuous Random Variables Suppose that X is a continuous random variable with cdf F (x). Furthermore, suppose that you can invert F. If U unif (0, 1), the random variable F 1 (U) has the same distribution as X! esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 2 / 1
Generating Continuous Random Variables Proof: P(F 1 (U) x) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 3 / 1
Generating Continuous Random Variables Proof: P(F 1 (U) x) = P(U F (x)) F non-decreasing F 1 non-decreasing esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 3 / 1
Generating Continuous Random Variables Proof: P(F 1 (U) x) = P(U F (x)) = F (x) F non-decreasing cdf of unif(0,1) F 1 non-decreasing esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 3 / 1
Generating Continuous Random Variables Proof: P(F 1 (U) x) = P(U F (x)) = F (x) = P(X x) F non-decreasing F 1 non-decreasing cdf of unif(0,1) definition of F esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 3 / 1
Generating Continuous Random Variables Example: Generating exponential rate λ r.v. s: pdf: f (x) = λe λx, x 0 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 4 / 1
Generating Continuous Random Variables Example: Generating exponential rate λ r.v. s: pdf: f (x) = λe λx, x 0 cdf: F (x) = x 0 λe λu du = 1 e λx, x 0 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 4 / 1
Generating Continuous Random Variables Example: Generating exponential rate λ r.v. s: pdf: f (x) = λe λx, x 0 cdf: F (x) = x 0 λe λu du = 1 e λx, x 0 F 1 (x) = 1 λ ln(1 x) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 4 / 1
Generating Continuous Random Variables Example: Generating exponential rate λ r.v. s: pdf: f (x) = λe λx, x 0 cdf: F (x) = x 0 λe λu du = 1 e λx, x 0 F 1 (x) = 1 λ ln(1 x) Now plug in a uniform! esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 4 / 1
Generating Continuous Random Variables Results: (100,000 values, λ = 3.5) Density 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 sample Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 5 / 1
Generating Continuous Random Variables The Standard Normal Distribution: f (x) = 1 2π e 1 2 x2, < x < Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 6 / 1
Generating Continuous Random Variables The Standard Normal Distribution: f (x) = 1 2π e 1 2 x2, < x < There is no closed form expression for the cdf! Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 6 / 1
Generating Continuous Random Variables The Box-Muller Transformation: Let U 1 and U 2 be independent unif (0, 1) r.v. s. Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 7 / 1
Generating Continuous Random Variables The Box-Muller Transformation: Let U 1 and U 2 be independent unif (0, 1) r.v. s. Then X 1 = 2 ln U 1 cos(2πu 2 ) is normally distributed with mean 0 and variance 1. Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 7 / 1
Generating Continuous Random Variables The Box-Muller Transformation: Let U 1 and U 2 be independent unif (0, 1) r.v. s. Then X 1 = 2 ln U 1 cos(2πu 2 ) is normally distributed with mean 0 and variance 1. So is X 2 = 2 ln U 1 sin(2πu 2 ) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 7 / 1
Generating Continuous Random Variables The Box-Muller Transformation: Let U 1 and U 2 be independent unif (0, 1) r.v. s. Then X 1 = 2 ln U 1 cos(2πu 2 ) is normally distributed with mean 0 and variance 1. So is X 2 = 2 ln U 1 sin(2πu 2 ) and X 1 and X 2 are independent! Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 7 / 1
Generating Continuous Random Variables So far, we have ways to simulate normal random variables random variables for which we have a closed-form invertible cdf Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 8 / 1
Generating Continuous Random Variables So far, we have ways to simulate normal random variables random variables for which we have a closed-form invertible cdf If neither of these apply to your simulation problem, move on to the next algorithm. Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 8 / 1
Generating Continuous Random Variables So far, we have ways to simulate normal random variables random variables for which we have a closed-form invertible cdf If neither of these apply to your simulation problem, move on to the next algorithm. It requires that you be able to uniformly bound the target pdf by an integrable function. Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 8 / 1
Generating Continuous Random Variables So far, we have ways to simulate normal random variables random variables for which we have a closed-form invertible cdf If neither of these apply to your simulation problem, move on to the next algorithm. It requires that you be able to uniformly bound the target pdf by an integrable function. If you can t do that, move on to the next algorithm... there is no catch-all procedure so we d better build up a big arsenal of simulation techniques! Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 8 / 1
Generating Continuous Random Variables : (due to von Neumann) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 9 / 1
Generating Continuous Random Variables : want to simulate a r.v. X with pdf f (due to von Neumann) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 9 / 1
Generating Continuous Random Variables : (due to von Neumann) want to simulate a r.v. X with pdf f need to find another function g so that g(x) f (x) x Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 9 / 1
Generating Continuous Random Variables : (due to von Neumann) want to simulate a r.v. X with pdf f need to find another function g so that g(x) f (x) x normalize g to a pdf h(x) = 1 c g(x) where c = g(x) dx Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 9 / 1
The Algorithm: Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 10 / 1
The Algorithm: generate Y with pdf h Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 10 / 1
The Algorithm: generate Y with pdf h generate U unif (0, 1) (indep of Y ) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 10 / 1
The Algorithm: generate Y with pdf h generate U unif (0, 1) (indep of Y ) if U f (Y )/g(y ), accept Y, set X = Y (Done!) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 10 / 1
The Algorithm: generate Y with pdf h generate U unif (0, 1) (indep of Y ) if U f (Y )/g(y ), accept Y, set X = Y (Done!) otherwise, reject Y, return to Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 10 / 1
Proof: (discrete case) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 11 / 1
Proof: (discrete case) Let X be the value produced by this algorithm esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 11 / 1
Proof: (discrete case) Let X be the value produced by this algorithm We will show that P(X = x) = f (x) where f is the target density. esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 11 / 1
Proof: (discrete case) Let X be the value produced by this algorithm We will show that P(X = x) = f (x) where f is the target density. Let N be the number of trials until a success? Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 11 / 1
Proof: (discrete case) Let X be the value produced by this algorithm We will show that P(X = x) = f (x) where f is the target density. Let N be the number of trials until a success? Q: Is N a geometric random variable? Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 11 / 1
Proof: (discrete case) Let X be the value produced by this algorithm We will show that P(X = x) = f (x) where f is the target density. Let N be the number of trials until a success? Q: Is N a geometric random variable? A: Yes! Success probability? Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 11 / 1
Proof: (discrete case) Consider the joint probability P(X = x, N = n) We will write down an expression for this and then marginalize out the N. Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 12 / 1
P(X = x, N = n) = P (n 1 failures, ) U n 1 > f (y n 1) g(y n 1 ), Y n = x, U n f (x) g(x) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 13 / 1
P(X = x, N = n) = P (n 1 failures, ) U n 1 > f (y n 1) g(y n 1 ), Y n = x, U n f (x) g(x) ( ) indep = P(n 1 failures) P Y n = x, U n f (x) g(x) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 13 / 1
P(X = x, N = n) = P (n 1 failures, ) U n 1 > f (y n 1) g(y n 1 ), Y n = x, U n f (x) g(x) ( ) indep = P(n 1 failures) P Y n = x, U n f (x) g(x) ( ) ident = P(n 1 failures) P Y = x, U f (x) g(x) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 13 / 1
So, P(X = x) = n=1 P(X = x, N = n) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 14 / 1
So, P(X = x) = n=1 P(X = x, N = n) ( = P Y = x, U f (x) g(x) ) n=1 P(n 1 failures) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 14 / 1
So, P(X = x) = n=1 P(X = x, N = n) ( = P ( = P Y = x, U f (x) g(x) Y = x, U f (x) g(x) ) n=1 P(n 1 failures) ) [blah (no x s)] esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 14 / 1
So, P(X = x) = n=1 P(X = x, N = n) ( = P ( = P Y = x, U f (x) g(x) Y = x, U f (x) g(x) ( indep = P(Y = x) P U f (x) g(x) ) n=1 P(n 1 failures) ) [blah (no x s)] ) [blah (no x s)] esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 14 / 1
So, P(X = x) = n=1 P(X = x, N = n) ( = P ( = P Y = x, U f (x) g(x) Y = x, U f (x) g(x) ( indep = P(Y = x) P U f (x) g(x) = h(x) f (x) g(x) [blah (no x s)] ) n=1 P(n 1 failures) ) [blah (no x s)] ) [blah (no x s)] (since f (x) g(x)) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 14 / 1
So, P(X = x) = h(x) f (x) g(x) [blah (no x s)] esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 15 / 1
So, P(X = x) = h(x) f (x) g(x) [blah (no x s)] = 1 f (x) c g(x) g(x) [blah (no x s)] esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 15 / 1
So, P(X = x) = h(x) f (x) g(x) [blah (no x s)] = 1 f (x) c g(x) g(x) [blah (no x s)] = 1 c f (x) [blah (no x s)] esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 15 / 1
So, P(X = x) = h(x) f (x) g(x) [blah (no x s)] = 1 f (x) c g(x) g(x) [blah (no x s)] = 1 c f (x) [blah (no x s)] Sum both sides over x: P(X = x) = x x { } 1 f (x) [blah (no x s)] c esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 15 / 1
P(X = x) = x x { } 1 f (x) [blah (no x s)] c Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 16 / 1
P(X = x) = x x { } 1 f (x) [blah (no x s)] c { } 1 1 = 1 [blah (no x s)] c Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 16 / 1
P(X = x) = x x { } 1 f (x) [blah (no x s)] c { } 1 1 = 1 [blah (no x s)] c { 1 c } [blah (no x s)] = 1 Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 16 / 1
P(X = x) = x x { } 1 f (x) [blah (no x s)] c { } 1 1 = 1 [blah (no x s)] c { 1 c } [blah (no x s)] = 1 P(X = x) = f (x) Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 16 / 1
Example: Simulate X Γ(2, 3) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 17 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 17 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 Will try to bound it by a function of the form g(x) = k e 2x. esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 17 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 Will try to bound it by a function of the form g(x) = k e 2x. ke 2x 9xe 3x = e 3x (ke x 9x) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 17 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 Will try to bound it by a function of the form g(x) = k e 2x. ke 2x 9xe 3x = e 3x (ke x 9x) Hmmm... k = 9 will work... f1 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 1 2 3 4 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 17 / 1 x
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 Will try to bound it by a function of the form g(x) = k e 2x. ke 2x 9xe 3x = e 3x (ke x 9x) Hmmm... k = 9 will work...... but we might try to do better since the accept probabilities are of the form f /g. f1 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 1 2 3 4 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 17 / 1 x
Example: Simulate X Γ(2, 3) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 18 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 18 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 Will try to bound it by a function of the form g(x) = 9k e 2x. esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 18 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 Will try to bound it by a function of the form g(x) = 9k e 2x. 9ke 2x 9xe 3x = 9e 3x (ke x x) esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 18 / 1
Example: Simulate X Γ(2, 3) Target Density: f (x) = 9xe 3x, x > 0 Will try to bound it by a function of the form g(x) = 9k e 2x. 9ke 2x 9xe 3x = 9e 3x (ke x x) k = e 1 will work... 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 1 2 3 4 esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 18 / 1 x
Example: Simulate X Γ(2, 3) In fact, it will be best (in terms of maximizing the acceptance rate) if we can, with our choice of g, closely mimic the target density f. 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 1 2 3 4 x esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 19 / 1
Example: Simulate X Γ(2, 3) In fact, it will be best (in terms of maximizing the acceptance rate) if we can, with our choice of g, closely mimic the target density f. But, choosing something of the form g(x) = x c 1 e c 2x that will be 0 for x = 0 will result in us having to draw values (for possible acceptance) from a density that is probably at least as difficult to draw from as the original f! 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 1 2 3 4 x esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 19 / 1
Example: Simulate X Γ(2, 3) In fact, it will be best (in terms of maximizing the acceptance rate) if we can, with our choice of g, closely mimic the target density f. We could use a piecewise uniform/exponential... 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 1 2 3 4 x esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 20 / 1
Example: Simulate X Γ(2, 3) In fact, it will be best (in terms of maximizing the acceptance rate) if we can, with our choice of g, closely mimic the target density f. We could use a piecewise uniform/exponential...... but this is probably not worth the trouble. Let s see how fast the algorithm is with g(x) = 9e 1 e 2x. 0.0 0.5 1.0 1.5 2.0 2.5 3.0 0 1 2 3 4 x esson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 20 / 1
Results: (100,000 values) Density 0.0 0.2 0.4 0.6 0.8 1.0 mean accept time = 1.65627 s.d. = 1.038629 Density 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0 2 4 6 8 10 12 14 0 1 2 3 4 5 Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 21 / 1
Odds and Ends... Incidentally, how would one draw from this (unnormalized) density: 0.0 0.2 0.4 0.6 0.8 1.0 1.2 f (x) { 9e 2, 0 < x < 0.5 9e 1 e 2x, x 0.5 0 1 2 3 4 Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 22 / 1
Odds and Ends... Incidentally, how would one draw from this (unnormalized) density: 0.0 0.2 0.4 0.6 0.8 1.0 1.2 could normalize then compute and and invert the piecewise continuous cdf 0 1 2 3 4 Lesson 5: Generating (Some) Continuous Randomx Variables Stochastic Simulation September 12, 2018 23 / 1
Odds and Ends... Incidentally, how would one draw from this (unnormalized) density: 0.0 0.2 0.4 0.6 0.8 1.0 1.2 could normalize then compute and and invert the piecewise continuous cdf could normalize with c, then w.p. 9e 1 /c draw a unif (0, 1) w.p. 1 9e 1 /c draw an exp(rate = 2) and shift it by adding 0.5 0 1 2 3 4 Lesson 5: Generating (Some) Continuous Randomx Variables Stochastic Simulation September 12, 2018 23 / 1
Odds and Ends... Results: (second method candidate draws) Density 0.0 0.2 0.4 0.6 0.8 1.0 0 1 2 3 4 5 6 Lesson 5: Generating (Some) Continuous Random Variables Stochastic Simulation September 12, 2018 24 / 1