For this experiment, I was curious about the “random” number generators in R based on different distributions. If a number generator is truly random, then theoretically about 50% of the numbers generated will be greater than the median. Let’s consider \(X\), where \(X \sim {\sf Exponential}(\lambda = 1.5)\).
We must first show that the median of \(X\) is given by \(Median(X) = \frac{\ln(2)}{\lambda}\).
Start off with \[\int_{0}^{m} \lambda e^{-\lambda x} \; dx = \frac{1}{2}\].
Integrating, we have \[-e^{-\lambda x}|_{0}^{m} = \frac{1}{2}\].
Solving for \(m\), we will have \[m = \frac {\ln(2)}{\lambda}\].
It follows that for \(X \sim {\sf Exponential}(\lambda = 1.5)\), roughly 50% of the randomly numbers generated should be \(> \frac{\ln(2)}{1.5} \approx 0.4621\).
We can visualize the median of the distribution below:
If you’re familiar with R, you can also simply find the median with a single line of code. We will double check our answer using R:
qexp(0.5, rate = 1.5)
## [1] 0.4620981
To proceed with our simulation, let’s generate 10,000 random numbers from \(X \sim {\sf Exponential}(\lambda = 1.5)\).
r <- rexp(10000, rate = 1.5)
Now let’s find out how many of the randomly generated numbers are greater than our median.
r[r < log(2)/1.5] = NA
r<-r[!is.na(r)]
num <- length(r)
phat <- num/10000
phat
## [1] 0.498
Lastly, let’s perform a confidence interval at the 95% level of confidence to test for our p-hat of 0.4933. Recall the confidence interval for a test of a single proportion below:
\[\hat{p} \pm Z_\frac{\alpha}{2}\sqrt{\frac{\hat{p}(1-\hat{p})}{n}}\] Plugging in the numbers required for testing about 50%, we get
\[0.5 \pm 1.96\sqrt{\frac{0.25}{10000}} \approx 0.5 \pm 0.0098\]
Which will yield an interval of \(I =(0.4902,0.5098)\). Since we can see that \(\hat{p} \in I\), the R random number generator looks like it works pretty well (at least for \(n \le 10000\)).
That concludes this edition of my RMarkdown adventures. Please let me know if you have any requests and I’ll be sure to accomodate them. Take care! :)