Use R to answer the following question II. The purpose of this problem is to sim
ID: 3374530 • Letter: U
Question
Use R to answer the following question
II. The purpose of this problem is to simulate a fair coin flip, and to see how many flips it takes for the probability of ahead to be approximately 0.50.
(a) Use the function sample to flip a fair coin 20 times, and find the probability that you flipped a “head” based on the 20 flips.
(b) Use an sapply to repeat (a) for the following values of n: 10, 100, 1000, 10000, 100000. Show the probabilities for all 5 values of n.
(c) The error of a coin flip is the absolute value of the estimated probability minus the true probability, i.e error =|0.50?ˆP(head)|
Find the error for your simulations from (c).
(d) What happens to the error as n increases, and why? Explain your answer.
II. The purpose of this problem is to simulate a fair coin flip, and to see how many flips it takes for the probability of a head to be approximately 0.50. (a) Use the function sample to flip a fair coin 20 times, and find the probability that you flipped a "head" based on the 20 flips all 5 values of n. error- 0.50 - P(head) (b) Use an sapply to repeat (a) for the following values of n: 10, 100, 1000, 10000, 100000. Show the probabilities for (c) The error of a coin flip is the absolute value of the estimated probability minus the true probability, i.e Find the error for your simulations from (c). (d) What happens to the error as n increases, and why? Explain your answer.Explanation / Answer
The code and output is given below:
set.seed(1)
##a.) Function for finding prob of "H" in 20 fair tosses
f<-function(n){
+ s<-sample(c("H","T"),n,replace = T)
+ p<-length(which(s=="H"))/length(s)
+ return(p)
+ }
> f(20)
[1] 0.45
###b.) Using sapply for finding the prob for 10,100,1000,10000,100000 tosses
a<-sapply(c(10,100,1000,10000,100000),f)
a
[1] 0.70000 0.53000 0.51700 0.50150 0.49997
####c.) Finding the abs error
abs(.5-a)
[1] 0.20000 0.03000 0.01700 0.00150 0.00003
d.) As the sample size increases, the error goes on decreasing. As the sample size increases the sample approaches to the population and the expected value of an estimator approaches to the true value of the parameter.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.