# Binomial Distribution - Coin Toss

> This document is written in *R*.
>
> ***GitHub***: https://github.com/czs108

## Background

> Suppose you conduct a series of experiments, each of which consists of tossing a fair coin **6** times. Let $X$ be the number of *heads*.

## Question A

> What are the probabilities of each value of $X$?

\begin{equation}
P(X = r) =\, ^{6}C_{r} \cdot 0.5^{r} \cdot 0.5^{6 - r}
\end{equation}


```R
heads <- c(0:6)

probs <- choose(n=6, k=heads) * (0.5 ^ heads) * (0.5 ^ (6 - heads))
probs
```


<ol class=list-inline>
	<li>0.015625</li>
	<li>0.09375</li>
	<li>0.234375</li>
	<li>0.3125</li>
	<li>0.234375</li>
	<li>0.09375</li>
	<li>0.015625</li>
</ol>



Use the `dbinom` function directly.


```R
dbinom(x=heads, prob=0.5, size=6)
```


<ol class=list-inline>
	<li>0.015625</li>
	<li>0.09375</li>
	<li>0.234375</li>
	<li>0.3125</li>
	<li>0.234375</li>
	<li>0.09375</li>
	<li>0.015625</li>
</ol>




```R
barplot(probs ~ c(0:6), xlab="Number of heads", ylab="Probability")
```

## Question B

\begin{equation}
\begin{split}
P(1st = head \mid X = 4)
    &= \frac{P(1st = head \cap X = 4)}{P(X = 4)} \\
    &= \frac{^{5}C_{3}}{^{6}C_{4}} \\
    &= 0.6667
\end{split}
\end{equation}

## Question C

\begin{equation}
\begin{split}
P(X = 4 \mid 1st = head)
    &= \frac{P(1st = head \cap X = 4)}{P(1st = head)} \\
    &= \frac{^{5}C_{3}}{2^{5}} \\
    &= 0.3125
\end{split}
\end{equation}


```R
dbinom(x=3, prob=0.5, size=5)
```


0.3125


## Question D

> Suppose the event

\begin{equation}
A = \text{The 1st trial where X = 6 is 10th.}
\end{equation}

> Find out

\begin{equation}
P(A)
\end{equation}

Its's a *Geometric Distribution*.

\begin{equation}
p = P(X = 6) = \frac{1}{2^{6}}
\end{equation}

\begin{equation}
\begin{split}
P(A) &= (1 - p)^{9} \cdot p \\
    &= (1 - \frac{1}{2^{6}})^{9} \times \frac{1}{2^{6}} \\
    &= 0.0136
\end{split}
\end{equation}

## Question E

\begin{equation}
\begin{split}
P(X > 4) &= P(X = 5) + P(X = 6) \\
    &= \frac{^{6}C_{5} +\, ^{6}C_{6}}{2^6} \\
    &= 0.1094
\end{split}
\end{equation}

Use the `pbinom` function.


```R
1 - pbinom(q=4, prob=0.5, size=6)
```


0.109375


## Question F

> What is the *mean* of $X$?

Suppose

\begin{equation}
N = \text{The number of trials needed to get the first head.}
\end{equation}

The the *expectation* is

\begin{equation}
E(N) = \frac{1}{0.5} = 2
\end{equation}

There are **6** trials.

\begin{equation}
E(X) = \frac{6}{E(N)} = 3
\end{equation}


```R
mean <- sum(heads * probs)
mean
```


3


6d6fba31af5a836b8ef70a22efb1f0f2df11298f
ipynb
Jupyter Notebook
exercises/Binomial Distribution - Coin Toss.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Binomial Distribution - Coin Toss.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Binomial Distribution - Coin Toss.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
2022-03-21T05:04:07.000Z
2022-03-21T05:04:07.000Z
Qwen/Qwen-72B
1. YES
2. YES