From Discrete-Event System Simulation

Chapter 8 Problem # 5

Given the following cdf for a continuous variable with range from -3 to 4, develop a generator for the variable, generate 1000 values, and plot a histogram.

\[ F(x) = \begin{cases} 0, x \le -3 \\ \frac{1}{2} + \frac{x}{6}, -3 \le x \le 0 \\ \frac{1}{2} + \frac{x^2}{32}, 0 \lt x \le 4 \\ 1, x \gt 4 \end{cases} \]

The range is defined from -3 to 4 so we’ll focus specifically on \(-3 \le x \le 4\).

For \(-3 \le x \le 0\):

\[F(x) = \frac{1}{2} + \frac{X}{6} = R\]

Solving for R:

\[ \frac{X}{6} = R - \frac{1}{2} \] \[ X = 6(R - \frac{1}{2}) \]

Using equation \(\frac{1}{2} + \frac{x}{6}\) and using \(-3 \le X \le 0\) we define the range to b:

\[ 0 \le R \le \frac{1}{2} \]

For \(0 \le x \le 4\):

\[F(x) = \frac{1}{2} + \frac{X^2}{32} = R\]

Solving for R:

\[ \frac{X^2}{32} = R - \frac{1}{2} \] \[ X^2 = 32(R - \frac{1}{2}) \] \[ X= \sqrt{32(R - \frac{1}{2})}\]

Using equation \(\frac{1}{2} + \frac{x^2}{32}\) and using \(0 \le X \le 4\) we define the range to be:

\[ \frac{1}{2} \le R \le 1 \]

So the generator will be:

\[ X = \begin{cases} 6(R - \frac{1}{2}), 0 \le R \le \frac{1}{2} \\ \sqrt{32(R - \frac{1}{2}}, \frac{1}{2} \le R \le 1 \\ \end{cases} \]


Generate 1000 samples using the generator:

generator <- function(R) {
  return( ifelse(R <= 1/2, 6*(R - 1/2), sqrt(32*(R - 1/2))))
}

# apply to 1000 samples between 0 and 1
result <- sapply(runif(1000), generator)

head(result, 10)
##  [1] -1.2745349  3.0373943 -0.5461385  3.5009366  3.7543246 -2.7266610
##  [7]  0.9483542  3.5436435  1.2829343 -0.2603116

Create a histogram of the results

hist(result)