19  Workshop Activities

This workshop consists of two parts:

Part 1: Inverse Transform Sampling

Exercise 1: Precision Method

A mobile game awards players with different types of rewards when a treasure chest is opened. The reward type follows the probability distribution below.

Reward Type \(x\) Coins Gems Energy Rare Item
\(P(X=x)\) 0.50 0.25 0.15 0.10

Tasks

  1. Construct the cumulative distribution function (CDF) for this random variable and present it in a table.

  2. Using the precision method, write down the interval of the unit interval \((0,1)\) corresponding to each outcome. That is, determine the intervals for \(U \sim \text{Uniform}(0,1)\) such that

\[ X = \begin{cases} \text{Coins} & \text{if } \dots \\ \text{Gems} & \text{if } \dots \\ \text{Energy} & \text{if } \dots \\ \text{Rare Item} & \text{if } \dots \end{cases} \]

  1. Suppose the following random numbers are generated:

\[ 0.12, 0.63, 0.44, 0.91, 0.27 \]

   Using the intervals from part (b), determine the reward obtained in each case.

  1. Write an R function that simulates one reward using the precision method.

  2. Simulate 10,000 rewards and compute the empirical probabilities of each outcome. Compare the simulated probabilities with the theoretical probabilities. Briefly comment on the result.

  3. Explain why the precision method may become inefficient when the number of possible outcomes \(k\) is very large.

Exercise 2: Load on a bridge (CD 2014)

Consider the pdf for the random variable \(X\) which is the magnitude (in newtons) of a dynamic load on a bridge, given as

\[ f(x)= \left(\frac{1}{8} + \frac{3}{8} x \right), \quad 0 \le x \le 2 \]

\[ f(x)=0, \quad \text{otherwise.}\]

  1. Calculate the exact mean and variance.

  2. Write a program to simulate values from this distribution using the inverse cdf method. Calculate the mean and variance and compare with the exact results.

  • Hints
    • Step 1. Firstly, we need to find the cdf \(F(x).\)
    • Step 2. Set \(u = F(x)\) and solve for \(x\)
    • The final answer is \[ x=\frac{-1 + \sqrt{1+48u} }{3} \]

Exercise 3: Simulating the distribution of waiting time - Transportation

The article “A Model of Pedestrians’ Waiting Times for Street Crossings at Signalised Intersections” (Transportation Research, 2013: 17–28) suggested that under some circumstances the distribution of waiting time \(X\) could be modeled with the following pdf:

\[ f(x)= \frac{\theta}{\tau}\left( 1- \frac{x}{\tau}\right)^{\theta - 1}, \quad 0 \le x < \tau \]

\[ f(x)=0, \quad \text{otherwise.} \]

  1. Write a function to simulate values from this distribution, implementing the inverse cdf method. Your function should have three inputs: the desired number of simulated values \(n\) and values for the two parameters for \(\theta\) and \(\tau.\)
  • Hints: The final answer is \[ F(x)= 1 - (1-x/\tau)^{\theta}. \]
  1. Use your function in part (a) to simulate 10,000 values from this wait time distribution with \(\theta=4\) and \(\tau=80.\) Estimate \(E(X)\) under these parameter settings.
  • Hints: The final answer is \[ x= \tau(1 - (1- u)^{1/\theta}) \]
  1. How close is your estimate to the correct value of the exact \(E(X)=16\)?

Part 2: Acceptance-Rejection Sampling

Exercise 4: Simulating Beta(2,2) distribution using Uniform distribution (Templ 2016)

  1. Describe the steps. Hints: The pdf of \(Beta(\alpha,\beta)\) is \[ f(x)= \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)} x^{\alpha-1}(1-x)^{\beta-1}, \quad 0 \le x \le 1 \]

  2. Check visually the plots of \(f(x)\) and \(Mg(x).\)

  3. Perform Accept-Reject method to simulate Beta(2,2) distribution from Uniform distribution. Calculate the acceptance rate. Plot the simulating values of a Beta(2,2) using the accep rejection approach.

Exercise 5: Simulating the half-normal distribution using Exponential

The half-normal distribution is defined as:

\[ f(x) = \sqrt{\frac{2}{\pi}} \exp (-\frac{x^2}{2}), \quad x \ge 0.\]

Consider simulating values from this distribution using the accept–reject method with a candidate distribution of an Exponential random variable with parameter \(\lambda = 1\)

\[g(x)= e^{-x}, \quad x \ge 0.\]

  1. Find the inverse cdf corresponding to \(g(x).\) (This will allow us to simulate values from the candidate distribution.) Hints: If \(X \sim Exp(\lambda=1)\) then the pdf and cdf are \[ (x)= e^{-x}, \quad G(x)= 1- e^{-x}. \]

  2. Find the smallest majorisation constant \(M\) so that \(f(x)/g(x)\le M\) for all \(x \ge 0.\) Hints: Use calculus to determine where the ratio \(f(x)/g(x)\) is maximised. The answer is \(M=\sqrt{\frac{2e}{\pi}}.\)

  3. On the average, how many candidate values will be required to generate 10,000 “accepted” values?

  4. Write a program to construct 10,000 values from a half-normal distribution.