Simulation, Semester II 2021-2022
ST3247: Assignment 11
Due date: March 9th (Wednesday of Week 8)
Please submit an answer sheet with an R file in the submission folder separately, and name
them with the same name. Please do NOT use compressed files.
The answer sheet should contain answers to ALL questions. Do not leave answers in your
code file. Marking mainly depends on your answer sheet.
The R file should contain your coding work as a supplementary file. Please make it ready
for reproductions.
For all the questions, you can only use standard uniform random variables unless
there are particular statements in the problem.
1. Consider the distribution that
P (X = j) = (1/2)j+1 + (1/2)2j?1/3j , j = 1, 2, · · ·
Generate 1000 samples and estimate E[X] and V ar(X) according to your samples.
(Hint: The algorithm is not unique for this question. In case you have to generate geomet-
ric random variables, please note that you can use the inversion by truncation algorithm.)
2. The frequency of heavy rains in one week follows a Poisson distribution with parameter λ.
According the past years, the rate λ ~ Unif{1, 2, 3, 4, 5, 6}.
In the past 4 weeks, we have observed 4, 6, 5, 3. We want to give a proper estimate of λ.
Let X denote the frequency of heavy rains in one week. Please answer the following
questions.
(a) Find the joint probability of the data points 4, 6, 5, 3 when λ = j.
1All rights reserved by NUS. Reproduction or distribution of lecture notes/tutorials/quiz/exam without the
written permission of the sponsor is prohibited.
1
(b) With the observed data points 4, 6, 5, 3, please find the conditional distribution of λ,
p(λ|data), subject to a constant.
(Hint: p(λ|data) ∝ p(data|λ)p(λ), where p(data|λ) is calculated in part (a) and p(λ)
is uniform on {1, 2, 3, 4, 5, 6}.)
(c) With the previous results, write a pseudo-code for p(λ|data).
(Hint: one way is to use the rejection algorithm for the kernel function. You can also
identify the exact probabilities and use inverse method or table method.)
(d) Generate 1000 samples according to the algorithm in Part (c). What is the empirical
distribution of λ based on the generated samples?
(Remark: Empirical distribution: p?(λ = j) =
number of j’s in the sample
sample size
.)
3. Suppose that g is an easy probability density function to generate from, and h is a non-
negative function. Take a close look at the following algorithm pseudo-code:
Step 1. Generate Y ~ g.
Step 2. Generate E ~ Exp(1) in the way that E = ? log(U), U ~ Unif(0, 1).
Step 3. If E ≥ h(Y ), set X = Y . Otherwise go to Step 1.
Step 4. Return X.
This is a rejection algorithm and we want to find the density function of the generated
samples.
(a) Note that E ~ Exp(1). What is the probability that P (E ≤ t) for any constant
t > 0?
(b) Given X = x, what is the probability that X can be accepted?
(c) What is the joint probability that X is accepted and X = x?
(d) Note that the density function f(x) in the samples is the conditional prob. f(x|accepted).
Find f for X, subject to a constant.
(e) With the results, write the pseudo-code for the density
f(x) =
c
x2
e?x
2/2, x > 1.
(Hint. Find g and h to generate f . For g, you may consider the inversion algorithm.)
(f) Generate 10000 samples according to the algorithm in Part (e), and draw the his-
togram plot with command
hist(X, breaks = 50, freq = FALSE)
2
4. Buses arrive at a sporting event according to a nonhomogeneous Poisson process, having
the rate function
λ(t) = t/2.
per time unit (half hour).
Each bus contain some fans. When t ≤ 5, the number of fans on each bus follows a uniform
distribution on {20, 21, · · · , 40}. When t > 5, the number of fans on each bus follows a
uniform distribution on {30, 31, 32, · · · , 50}. The number of passengers on every bus are
independent.
We are interested in the number of audience at time T = 10.
(a) Let {X(t), t ≥ 0} denote the number buses arrived until time t. Please write code to
simulate such a process for the first 5 hours, i.e., 0 < t < 10.
(b) Let Yi be the number of fans on Bus i. Please write a function to generate Yi at time
t. (Hint: it will depend on whether t ≤ 5 or t > 5.)
(c) Let S10 =
∑X(10)
i=1 Yi. Please write a code to simulate S10.
(d) Generate 1000 samples of S10. With your result, given a numerical estimation of the
mean and variance of S10.