Suppose X1, X2, . . . , X21 are i.i.d. Poisson random variables with rate parameter λ = 1/2. Estimate, using simulation, the probability that the sample mean is larger than the sample median.
How do you do this using R?
The R code is pasted below.
# DECLARING SAMPLE SIZE AND RATE PARAMETER
n = 21
lambda = 0.5
# DECLARING TWO VECTORS TO STORE SAMPLE MEANS AND SAMPLE MEDIANS
FOR 1000 SIMULATIONS
sample_mean=NULL
sample_median=NULL
# 1000 SIMULATIONS
for(i in 1:1000)
{
sample_mean[i]=mean(rpois(n,lambda))
sample_median[i]=median(rpois(n,lambda))
}
prob = ifelse(sample_mean > sample_median,1,0)
# THE PROBABILITY THAT THE SAMPLE MEAN IS GREATER THAN
THE SAMPLE MEDIAN
# (ESTIMATED PROBABILITY)
sum(prob)/1000
Get Answers For Free
Most questions answered within 1 hours.