In: Computer Science
USE R-studio TO WRITE THE CODES!
# 2. More Coin Tosses
Experiment: A coin toss has outcomes {H, T}, with P(H) = .6.
We do independent tosses of the coin until we get a head.
Recall that we computed the sample space for this experiment in class, it has infinite number of outcomes.
Define a random variable "tosses_till_heads" that counts the
number of tosses until we get a heads.
```{r}
```
Use the replicate function, to run 100000 simulations of this
random variable.
```{r}
```
Use these simulations to estimate the probability of getting a head
after 15 tosses. Compare this with the theoretical value computed
in the lectures.
```{r}
```
Compute the probability of getting a head after 50 tosses. What do
you notice?
```{r}
Answer:
Defining and running 100000 simulation of the random variable "tosses_till_heads" using replicate() function
> set.seed(666)
> tosses_till_heads=replicate(100000,(rgeom(1,0.6)+1))
> # 1st 10 values
> head(tosses_till_heads,10)
[1] 1 2 1 1 1 3 1 3 1 1
Using the simulation to estimate the probability of getting a head after 15 tosses i.e P[ tosses_till_heads>15] and comparing this with the theoretical value i.e (1-0.6)15 .
> #Probability of getting a head after 15 tosses
>
> sum(tosses_till_heads>15)/100000
[1] 0
>
> # Theoretical value of the probability
>
> (1-0.6)^15
[1] 1.073742e-06
Clearly the theoretical value is slightly higher than the empirical/ estimated probability.
> #Probability of getting a head after 50 tosses
> sum(tosses_till_heads>50)/100000
[1] 0
>
> # Theoretical value of the probability
>
> (1-0.6)^50
[1] 1.267651e-20
The estimated probability is 0 for this case as well but the theoretical probability is almost equal to zero and almost equal to the estimated value of the probability.
Conclusion:Therefore as the no of tosses to get 1st head increase the estimated value of the probability comes closure to the theoretical value.