library(ggplot2)
library(dplyr)
library(kableExtra)
library(knitr)
#setwd("/Users/kevinpiger/Desktop/碩一下/統模/Homework02/")
options(scipen = 100)

Uniform (0,1) transformation

For uniform (0,1) random variables \(U_1,U_2, ... ,\) define N = min{n :\(\sum_{i=1}^{n}{U_i>1}\) }. That is, N is the number of random numbers that must be summed to exceed 1.

(a)

Estimate E(N) with standard errors by generating 1,000, 2,000, 5,000, 10,000, and 100,000 values of N, and check if there are any patterns in the estimate and its s.e.

# T 欲模擬次數 #
Q1 <- function(T){
N = NULL
S = NULL
for( i in 1:T){
  n = 0; i = 0
  while(n < 1){
    n = sum(n ,runif(1))
    i = i + 1}
    N <- c(N,i)
    }
  return(N)
}

Calculation and output

output <- NULL
output2 <- NULL
sim <- c(1000, 2000, 5000, 10000, 100000)
for(i in 1:5){
  output[i] <- mean(Q1(sim[i]));
  output2[i] <- var(Q1(sim[i]))
}
output <- t(output)
output2 <- t(output2)
output <- rbind(output,output2)
rownames(output) <- c("Output","var")
colnames(output) <- sim
kable(output,"html") %>% 
  kable_styling(bootstrap_options = 'striped', full_width = F) %>% 
  add_header_above(c(" " = 1, "# of Simulation" = 5))
# of Simulation
1000 2000 5000 10000 100000
Output 2.7610000 2.706000 2.7112000 2.7153000 2.7177600
var 0.7283123 0.736496 0.7714232 0.7737012 0.7694329

(b)

Compute the density function of N, E(N), and Var(N).

E(N) = e

Var(N) = 3e-e^2