solution
The variable *num_heads* is defined as the number of heads observed from the vector of outputs obtained when the coin is tossed *n times*
```{r}
toss = function(n){
outcome = sample(c("Head", "Tail"), size = n, replace = T, prob = c(0.6,0.4))
success = ifelse (outcome == "Head", 1,0)
num_heads = sum(success)
}
```
Assume an experiment where the coin is tossed 5 times and the number of heads recorded. The distribution of number of heads given 100,000 simulations of the experiment is obtained as follows:
```{r}
set.seed(9999)
num_heads_vector = replicate(100000, toss(5))
mean = mean(num_heads_vector)
variance = var(num_heads_vector)
mean
variance
```
The Mean number of heads when a coin is tossed 5 times is obtained as 2.9986 with Variance 1.1955. This is consistent with the theoretical number of getting a Head which is;
Comments
Leave a comment