# Conditional Probability - Dice Toss

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

## Background

> If you throw **2** dice, let the *total* score be $X$.


```R
scores <- matrix(nrow=6, ncol=6)
for (i in c(1:6)) {
    for (j in c(1:6)) {
        scores[i, j] <- i + j
    }
}

scores
```


<table>
<tbody>
	<tr><td>2 </td><td>3 </td><td>4 </td><td> 5</td><td> 6</td><td> 7</td></tr>
	<tr><td>3 </td><td>4 </td><td>5 </td><td> 6</td><td> 7</td><td> 8</td></tr>
	<tr><td>4 </td><td>5 </td><td>6 </td><td> 7</td><td> 8</td><td> 9</td></tr>
	<tr><td>5 </td><td>6 </td><td>7 </td><td> 8</td><td> 9</td><td>10</td></tr>
	<tr><td>6 </td><td>7 </td><td>8 </td><td> 9</td><td>10</td><td>11</td></tr>
	<tr><td>7 </td><td>8 </td><td>9 </td><td>10</td><td>11</td><td>12</td></tr>
</tbody>
</table>




```R
hist(scores, xlab="Score", ylab="Count", breaks=c(1.5:12.5))
```

## Question A

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


```R
probs <- round(table(scores) / (6 * 6), 2)

probs
```


    scores
       2    3    4    5    6    7    8    9   10   11   12 
    0.03 0.06 0.08 0.11 0.14 0.17 0.14 0.11 0.08 0.06 0.03 


## Question B

\begin{equation}
\begin{split}
P(1\ of\ the\ scores = 4\, \mid\, X = 9)
    &= \frac{P(\{4, 5\}) + P(\{5, 4\})}{\sum_{sum(i, j) = 9} P(\{i, j\})} \\
    &= \frac{2}{4}
\end{split}
\end{equation}

## Question C

\begin{equation}
\begin{split}
P(X = 9\, \mid\, only\ 1\ score = 4)
    &= \frac{\sum_{sum(i, j) = 9} P(\{i, j\})}{\sum_{i = 1, i \neq 4}^{6} P(\{4, i\}) + \sum_{i = 1, i \neq 4} P(\{i, 4\})} \\
    &= \frac{4}{10}
\end{split}
\end{equation}

## Question D

\begin{equation}
\begin{split}
P(at\ least\ 1\ score = 4)
    &= \sum_{i = 1}^{6} P(\{4, i\}) + \sum_{i = 1}^{6} P(\{i, 4\}) - P({4, 4}) \\
    &= \frac{11}{36}
\end{split}
\end{equation}

## Question E

\begin{equation}
\begin{split}
P(2\ scores\ are\ the\ same)
    &= \sum_{i = 1}^{6} P(\{i, i\}) \\
    &= \frac{6}{36}
\end{split}
\end{equation}

## Question F

> What are the *mean* and *variance* of $X$?


```R
probs <- list("score"=as.numeric(names(probs)), "prob"=as.numeric(probs))

mean <- with(probs, sum(score * prob))
var <- with(probs, sum(((score - mean) ^ 2) * prob))

print(sprintf("Mean = %.2f", mean), quote=FALSE)
print(sprintf("Variance = %.2f", var), quote=FALSE)
```

    [1] Mean = 7.07
    [1] Variance = 6.02


952cf23f292160141b85d2f93635aea6f3a18dd9
ipynb
Jupyter Notebook
exercises/Conditional Probability - Dice Toss.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Conditional Probability - Dice Toss.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Conditional Probability - Dice 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