CS代考 2022/10/20 11:54 (5) IEOR E4404 001 – Ed Lessons

2022/10/20 11:54 (5) IEOR E4404 001 – Ed Lessons
1: Inverse Transform
Inverse CDF for Continuous Distributions
If we recall, random variable X has continuous distribution if it has an associated density function f(x) such that for any A ⊂ R, we have:

Copyright By PowCoder代写 加微信 powcoder

P(X ∈ A) = ∫ f(x)dx A
This means that the cdf F(x) = ∫ x f(y)dy, and by extension F′(x) = f(x). −∞
We can continue to use our same denition of an inverse cdf:
F−1(u) = inf{x : F(x) ≥ u}
but since the cdf will be a continuous function, this will specically be equal to:
F−1(u) = inf{x : F(x) = u}
https://edstem.org/us/courses/25500/lessons/42351/preview

2022/10/20 11:54 (5) IEOR E4404 001 – Ed Lessons
Inverse Transform
Proposition Let F be the cdf of a continuous random variable, and let F −1 be the inverse as
described earlier. Let U ∼ Unif(0,1). Then P(F−1(U) ≤ x) = F(x), that is, it has cdf F(x). Proof
P(F−1(U) ≤ x) = P(F(F−1(U) ≤ F(x)) = P(U ≤ F(x))
https://edstem.org/us/courses/25500/lessons/42351/preview

2022/10/20 11:54 (5) IEOR E4404 001 – Ed Lessons
Some basic examples
We let u = xn, which gives us the inverse cdf, x = u1/n. Example 2
Let X ∼ exp(λ), then F (x) = 1 − exp(−λx) for x > 0.
f(x) = {x, 0 < x ≤ 1 (2−x), 1 0.5.
When U < 0.5, we set u = x2 , and nd X = 2U . Conversely, when U > 0.5, we set u = 1 −
This give us the following algorithm:
1. Let U ∼ Unif(0, 1)
2. If U ≤ 0.5, return X = 2U, else return X = 2 − 2(1 − U).
https://edstem.org/us/courses/25500/lessons/42351/preview

2022/10/20 11:54 (5) IEOR E4404 001 – Ed Lessons
Sampling From Gamma Distributions Note that if X ∼ Gamma(n, λ), then we have the cdf:
x λexp(−λy)(λy)n−1 F(x) = ∫0 Γ(n) dy
Which we cannot solve in a closed form (except in trivial cases like n = 1), which will almost certainly will preclude us from inverting this directly.
However, in the case that n is an integer, we know that X can be represented as the sum of n independent exp(λ) random variables.
Therefore, we can generate X as follows:
X = − λ ∑ log(Ui )
= − λ log(∏ Ui )
Let’s compare the eciency of this to numerically evaluating the cdf and numerically inverting:
import numpy as np
import time
from scipy.optimize import root_scalar
from scipy.stats import gamma, uniform
def inv_num(n, lmbda):
u = uniform.rvs()
# scipy uses mean parametrization
f = lambda x: gamma.cdf(x, n , scale=1 / lmbda) – u
f_prime = lambda x: gamma.pdf(x, n, scale=1 / lmbda)
sol = root_scalar(f, x0=1, fprime=f_prime)
return sol.root
def inv(n, lmbda):
u = uniform.rvs(size=n)
x = (-1 / lmbda) * np.log(np.product(u))
nsims = 100
t1 = time.time()
X = [inv_num(n, lmbda) for i in range(0, nsims)]
t2 = time.time()
ps://edstem.org/us/courses/25500/lessons/42351/preview 4

https://edstem.org/us/courses/25500/lessons/42351/preview 5/5
22/10/20 11:54 (5) IEOR E4404 001 – Ed Lessons
t3 = time.time()
X = [inv(n, lmbda) for i in range(0, nsims)]
t4 = time.time()
print(t2 – t1)
print(t4 – t3)

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com