# Marginal Probability - Districts & Ages

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

## Background

> The following table gives the *percentage of pupils* in a certain school from different *districts* and at different *ages*.
>
> | *District \ Age* |  14  |  15  |  16  |  17  |
> | :--------------: | :--: | :--: | :--: | :--: |
> |    **North**     |  5%  | 10%  |  5%  | 10%  |
> |    **Center**    | 10%  | 15%  |  5%  |  5%  |
> |    **South**     | 15%  | 10%  |  5%  |  5%  |

## Question A

> Give values for the following probabilities.

### Question 1

\begin{align}
P(age = 15)
    &= \sum_{i = North,Center,South} P(age = 15\, \cap\, district = i) \\
    &= 10\% + 15\% + 10\% \\
    &= 35\%
\end{split}
\end{equation}

### Question 2

\begin{equation}
\begin{split}
P(district = Center)
    &= \sum_{i = 14}^{17} P(age = i\, \cap\, district = Center) \\
    &= 10\% + 15\% + 5\% + 5\% \\
    &= 35\%
\end{split}
\end{equation}

### Question 3

\begin{equation}
\begin{split}
P(age = 15\, \mid\, district = Center)
    &= \frac{P(age = 15\, \cap\, district = Center)}{P(district = Center)} \\
    &= \frac{15\%}{35\%}
\end{split}
\end{equation}

### Question 4

\begin{equation}
\begin{split}
P(age = 15\, \cap\, district = Center)
    &= P(district = Center) \cdot P(age = 15\, \mid\, district = Center) \\
    &= 35\% \times \frac{15\%}{35\%} \\
    &= 15\%
\end{split}
\end{equation}

### Question 5

\begin{equation}
\begin{split}
P(age = 15\, \cup\, district = Center)
    &= P(age = 15) + P(district = Center) - P(age = 15\, \cap\, district = Center) \\
    &= 35\% + 35\% - 15\% \\
    &= 55\%
\end{split}
\end{equation}

## Question B

> What are the *mean* and *variance* of *age*?

\begin{equation}
Mean = \sum_{i = 14}^{17} i \cdot P(age = i)
\end{equation}

\begin{equation}
Variance = \sum_{i = 14}^{17} (i - \overline{i})^2 \cdot P(age = i)
\end{equation}


```R
ages <- c(14:17)
probs <- c(0.30, 0.35, 0.15, 0.20)
mean <- sum(ages * probs)
var <- sum(((ages - mean) ^ 2) * probs)

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

    [1] Mean = 15.2
    [1] Variance = 1.2


## Question C

> Use the `barplot` to visualize the table.

Store the data in a matrix.


```R
prcnt <- c(5, 10, 5, 10, 10, 15, 5, 5, 15, 10, 5, 5)
prcnt <- matrix(prcnt, nrow=3, byrow=TRUE)

prcnt
```


<table>
<tbody>
	<tr><td> 5</td><td>10</td><td>5 </td><td>10</td></tr>
	<tr><td>10</td><td>15</td><td>5 </td><td> 5</td></tr>
	<tr><td>15</td><td>10</td><td>5 </td><td> 5</td></tr>
</tbody>
</table>



Add row names and column names.


```R
colnames(prcnt) <- c(14:17)
rownames(prcnt) <- c("North", "Center", "South")

prcnt
```


<table>
<thead><tr><th></th><th scope=col>14</th><th scope=col>15</th><th scope=col>16</th><th scope=col>17</th></tr></thead>
<tbody>
	<tr><th scope=row>North</th><td> 5</td><td>10</td><td>5 </td><td>10</td></tr>
	<tr><th scope=row>Center</th><td>10</td><td>15</td><td>5 </td><td> 5</td></tr>
	<tr><th scope=row>South</th><td>15</td><td>10</td><td>5 </td><td> 5</td></tr>
</tbody>
</table>




```R
barplot(prcnt, xlab="Age", ylab="Percentage", legend.text=TRUE)
```

98c24fdc2e31f2a555cab396c90cbec8062dbf89
ipynb
Jupyter Notebook
exercises/Marginal Probability - Districts & Ages.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Marginal Probability - Districts & Ages.ipynb
czs108/Probability-Theory-Exercises
60c6546db1e7f075b311d1e59b0afc3a13d93229
MIT
exercises/Marginal Probability - Districts & Ages.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