Skip to content

Commit

Permalink
bayes sols
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderthclark committed Mar 18, 2024
1 parent 3e94ffe commit 3ecd2d4
Show file tree
Hide file tree
Showing 25 changed files with 921 additions and 50 deletions.
Binary file modified book/_build/.doctrees/bayes.doctree
Binary file not shown.
Binary file modified book/_build/.doctrees/chancevary.doctree
Binary file not shown.
Binary file modified book/_build/.doctrees/correlation.doctree
Binary file not shown.
Binary file modified book/_build/.doctrees/environment.pickle
Binary file not shown.
Binary file modified book/_build/.doctrees/normal.doctree
Binary file not shown.
Binary file modified book/_build/.doctrees/solutions.doctree
Binary file not shown.
332 changes: 332 additions & 0 deletions book/_build/html/_images/productreviewtree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion book/_build/html/_sources/bayes.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ $$\mathbb{P}(\text{Negative} | \text{No Disease}) = 0.99$$


```{exercise-start}
:label: hatcoins
:label: bballbayes
```

You're playing basketball at the park when your team picks up an unknown player. The unknown player is equally likely to be a scrub or a baller. A baller makes 90% of their shots and each shot is independent. A scrub makes 10% of their shots and each shot is independent.
Expand Down
96 changes: 89 additions & 7 deletions book/_build/html/_sources/solutions.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ $$= \frac{1\times 0.25}{0.5} = 0.25$$
The first draw reveals there is an $H$ in the box. By part 3, we know there is a 0.5 probability the box contains two $H$s. The probability of an $H$ on the second draw is



$$ \underbrace{\frac{1}{2}}_{a} \cdot \overbrace{1}^{b} + \underbrace{\frac{1}{2}}_{c} \cdot \overbrace{\frac{1}{2}}^{b} = \frac{3}{4}.$$


Expand All @@ -242,18 +241,18 @@ Each ticket is marked $H$ or $T$ independently and with equal chance, so $d$ is

**4 - Solution 2 (my favorite)**

This solution is remarkably similar to the previous, but with a different interpretation. The first draw reveals there is an $H$ in the box. Half of the time you will draw the same ticket on your second draw. The other half of the time you draw the other ticket.
This solution is remarkably similar to the previous, but with a different interpretation. The first draw reveals there is an $H$ in the box. Half of the time you will draw the same exact ticket on your second draw. This always gives an $H$, but you can also get an $H$ by drawing the other ticket. The other half of the time you draw the other ticket. The other ticket is an $H$ half the time.

The conditional probability of an $H$ on the second draw given an $H$ on the first draw can be expanded as the sum of the probability of *the same ticket and $H$* and the probability of *a different ticket and $H$*, all conditional on $H$ on the first draw. This is
The conditional probability of an $H$ on the second draw given an $H$ on the first draw can be expanded as the sum of the probability of *the same ticket and $H$* and the probability of *a different ticket and $H$*, all conditional on $H$ on the first draw. The answer is

$$ \underbrace{\frac{1}{2}}_{\alpha} \cdot \overbrace{1}^{\beta} + \underbrace{\frac{1}{2}}_{\gamma} \cdot \overbrace{\frac{1}{2}}^{\delta} = \frac{3}{4}.$$

| Term | Probability of ... given $H$ on first draw |
|----------|--------------------------------------------------------|
| $\alpha$ | the same ticket (an $H$). |
| $\beta$ | $H$ given the same ticket. |
| $\alpha$ | the same ticket is drawn again (an $H$). |
| $\beta$ | $H$ given the same ticket is drawn second |
| $\gamma$ | drawing the other ticket (which can be $H$ or $T$) |
| $\delta$ | $H$ given the other ticket |
| $\delta$ | $H$ given the other ticket is drawn second |


Each ticket is marked $H$ or $T$ independently and with equal chance, so $\delta$ is $\frac{1}{2}$.
Expand Down Expand Up @@ -288,4 +287,87 @@ It follows that $\mathbb{P}(H_1 \mid H_2)$ is also $\frac{3}{4}$ because the unc


```{solution-end}
```
```

```{solution-start} bayesraredisease
:class: dropdown
```

What are trying to find the probability that someone has the rare disease given a positive test. We'll use a natural frequencies approach to find

$$\mathbb{P}(\text{disease} mid \text{positive}) = \mathbb{P}(\text{positive and disease}) / \mathbb{P}(\text{positive}).$$

In a population of 990,000 people (99$\times$10000), we expect 99 to have the diease. Of those, 99% will test positive. This is 99-.99 = 98.01. We expect 989,901 not to have the disease. Of those, 1% will get a positive result, or 9899.01. Together, 9899.01 + 98.01 = 9997.02 get a positive test result

Thus, the conditional probability is about 98/9997, which is less than 1%.

```{solution-end}
```


```{solution-start} bballbayes
:class: dropdown
```

An unknown player makes their first shot 50% of the time. This is calculated as

$$0.5\times 0.9 + 0.5 \times 0.1.$$

Simplifying yields $0.5(0.9 + 0.1) = 0.5$.


Suppose there were 200 players. We can expect 100 scrubs and 100 ballers. The ballers make 90 shots and the scrubs make 10. There are 100 makes total.

The probability of a scrub is now $\frac{10}{100}$ and the probability of a baller is now $\frac{90}{100}$. The second shot goes in with probability
$$0.9\times 0.9 + 0.1 \times 0.1 = 0.81 + 0.01 = 0.82.$$

From the observer's perspective, the first and second shots are not independent. With more shots observed, they are beginning to learn the player's type. In other words, you should be more willing to pass to someone who makes their first shot because you should have greater confidence that the second shot will go in.
```{solution-end}
```


```{solution-start} bayesReview
:class: dropdown
```

$\mathbb{P}(\text{good review}) = 0.75$

$\mathbb{P}(\text{good review} \mid H) = 1$

$$\mathbb{P}(H \mid \text{good review}) = \frac{\mathbb{P}(\text{good review}\mid H)\mathbb{P}(H)}{\mathbb{P}(\text{good review})}$$

$$ = \frac{1 \times 0.5}{.75} = \frac{2}{3}.$$

The probability tree is shown below.

```{figure} images/tikz/productreviewtree.svg
:width: 70%
:name: productreviewtree
```

These events are dependent because $\mathbb{P}(H \mid \text{good review}) \neq \mathbb{P}(H)$.

If the truth-teller is replaced by a joker, then $\mathbb{P}(H \mid \text{bad review}) = 1$. The fake reviewer always leaves a positive reviewer, so the probability is the same as $\mathbb{P}(H \mid \text{joker and bad review})$ and the joker only leaves a bad review if the product is of good quality.

```{solution-end}
```


```{solution-start} bayesCountry
:class: dropdown
```

4/63 songs mention tractor and 14/63 mention truck.

2 of the 14 mentioning truck mention tractor.

2 of the 4 mentioning tractor mention truck. This can be calculated by simplying inspecting the data or by solve for the first three and then applying Bayes' Theorem.


$$\mathbb{P}(\text{truck}\mid \text{tractor}) = \frac{ \mathbb{P}(\text{tractor}\mid \text{truck}) \mathbb{P}(\text{truck})} {\mathbb{P}(\text{tractor}) } = \frac{ \frac{2}{14} \times \frac{14}{63} }{\frac{4}{63}} = \frac{2}{4}.$$

```{solution-end}
```



2 changes: 1 addition & 1 deletion book/_build/html/bayes.html
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ <h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this he
\[\mathbb{P}(\text{Negative} | \text{No Disease}) = 0.99\]</div>
</section>
</div>
<div class="exercise admonition" id="hatcoins">
<div class="exercise admonition" id="bballbayes">

<p class="admonition-title"><span class="caption-number">Exercise 34 </span></p>
<section id="exercise-content">
Expand Down
6 changes: 3 additions & 3 deletions book/_build/html/chancevary.html

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions book/_build/html/correlation.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions book/_build/html/normal.html

Large diffs are not rendered by default.

Binary file modified book/_build/html/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion book/_build/html/searchindex.js

Large diffs are not rendered by default.

64 changes: 59 additions & 5 deletions book/_build/html/solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ <h2><a class="reference internal" href="bayes.html#bayes"><span class="std std-r
</table>
<p>Each ticket is marked <span class="math notranslate nohighlight">\(H\)</span> or <span class="math notranslate nohighlight">\(T\)</span> independently and with equal chance, so <span class="math notranslate nohighlight">\(d\)</span> is <span class="math notranslate nohighlight">\(\frac{1}{2}\)</span>.</p>
<p><strong>4 - Solution 2 (my favorite)</strong></p>
<p>This solution is remarkably similar to the previous, but with a different interpretation. The first draw reveals there is an <span class="math notranslate nohighlight">\(H\)</span> in the box. Half of the time you will draw the same ticket on your second draw. The other half of the time you draw the other ticket.</p>
<p>The conditional probability of an <span class="math notranslate nohighlight">\(H\)</span> on the second draw given an <span class="math notranslate nohighlight">\(H\)</span> on the first draw can be expanded as the sum of the probability of <em>the same ticket and <span class="math notranslate nohighlight">\(H\)</span></em> and the probability of <em>a different ticket and <span class="math notranslate nohighlight">\(H\)</span></em>, all conditional on <span class="math notranslate nohighlight">\(H\)</span> on the first draw. This is</p>
<p>This solution is remarkably similar to the previous, but with a different interpretation. The first draw reveals there is an <span class="math notranslate nohighlight">\(H\)</span> in the box. Half of the time you will draw the same exact ticket on your second draw. This always gives an <span class="math notranslate nohighlight">\(H\)</span>, but you can also get an <span class="math notranslate nohighlight">\(H\)</span> by drawing the other ticket. The other half of the time you draw the other ticket. The other ticket is an <span class="math notranslate nohighlight">\(H\)</span> half the time.</p>
<p>The conditional probability of an <span class="math notranslate nohighlight">\(H\)</span> on the second draw given an <span class="math notranslate nohighlight">\(H\)</span> on the first draw can be expanded as the sum of the probability of <em>the same ticket and <span class="math notranslate nohighlight">\(H\)</span></em> and the probability of <em>a different ticket and <span class="math notranslate nohighlight">\(H\)</span></em>, all conditional on <span class="math notranslate nohighlight">\(H\)</span> on the first draw. The answer is</p>
<div class="math notranslate nohighlight">
\[ \underbrace{\frac{1}{2}}_{\alpha} \cdot \overbrace{1}^{\beta} + \underbrace{\frac{1}{2}}_{\gamma} \cdot \overbrace{\frac{1}{2}}^{\delta} = \frac{3}{4}.\]</div>
<table class="table">
Expand All @@ -659,16 +659,16 @@ <h2><a class="reference internal" href="bayes.html#bayes"><span class="std std-r
</thead>
<tbody>
<tr class="row-even"><td><p><span class="math notranslate nohighlight">\(\alpha\)</span></p></td>
<td><p>the same ticket (an <span class="math notranslate nohighlight">\(H\)</span>).</p></td>
<td><p>the same ticket is drawn again (an <span class="math notranslate nohighlight">\(H\)</span>).</p></td>
</tr>
<tr class="row-odd"><td><p><span class="math notranslate nohighlight">\(\beta\)</span></p></td>
<td><p><span class="math notranslate nohighlight">\(H\)</span> given the same ticket.</p></td>
<td><p><span class="math notranslate nohighlight">\(H\)</span> given the same ticket is drawn second</p></td>
</tr>
<tr class="row-even"><td><p><span class="math notranslate nohighlight">\(\gamma\)</span></p></td>
<td><p>drawing the other ticket (which can be <span class="math notranslate nohighlight">\(H\)</span> or <span class="math notranslate nohighlight">\(T\)</span>)</p></td>
</tr>
<tr class="row-odd"><td><p><span class="math notranslate nohighlight">\(\delta\)</span></p></td>
<td><p><span class="math notranslate nohighlight">\(H\)</span> given the other ticket</p></td>
<td><p><span class="math notranslate nohighlight">\(H\)</span> given the other ticket is drawn second</p></td>
</tr>
</tbody>
</table>
Expand All @@ -693,6 +693,60 @@ <h2><a class="reference internal" href="bayes.html#bayes"><span class="std std-r
<p><a class="reference external" href="https://docs.google.com/spreadsheets/d/1xlryzoPWZ05K4SeHVzZHiCC4yQ-XhLVaZChy-gYoWRc/edit?usp=sharing">Here is a Google Sheets simulation</a>.</p>
</section>
</div>
<div class="solution dropdown admonition" id="solutions-solution-14">

<p class="admonition-title">Solution to<a class="reference internal" href="bayes.html#bayesraredisease"> Exercise 33</a></p>
<section id="solution-content">
<p>What are trying to find the probability that someone has the rare disease given a positive test. We’ll use a natural frequencies approach to find</p>
<div class="math notranslate nohighlight">
\[\mathbb{P}(\text{disease} mid \text{positive}) = \mathbb{P}(\text{positive and disease}) / \mathbb{P}(\text{positive}).\]</div>
<p>In a population of 990,000 people (99<span class="math notranslate nohighlight">\(\times\)</span>10000), we expect 99 to have the diease. Of those, 99% will test positive. This is 99-.99 = 98.01. We expect 989,901 not to have the disease. Of those, 1% will get a positive result, or 9899.01. Together, 9899.01 + 98.01 = 9997.02 get a positive test result</p>
<p>Thus, the conditional probability is about 98/9997, which is less than 1%.</p>
</section>
</div>
<div class="solution dropdown admonition" id="solutions-solution-15">

<p class="admonition-title">Solution to<a class="reference internal" href="bayes.html#bballbayes"> Exercise 34</a></p>
<section id="solution-content">
<p>An unknown player makes their first shot 50% of the time. This is calculated as</p>
<div class="math notranslate nohighlight">
\[0.5\times 0.9 + 0.5 \times 0.1.\]</div>
<p>Simplifying yields <span class="math notranslate nohighlight">\(0.5(0.9 + 0.1) = 0.5\)</span>.</p>
<p>Suppose there were 200 players. We can expect 100 scrubs and 100 ballers. The ballers make 90 shots and the scrubs make 10. There are 100 makes total.</p>
<p>The probability of a scrub is now <span class="math notranslate nohighlight">\(\frac{10}{100}\)</span> and the probability of a baller is now <span class="math notranslate nohighlight">\(\frac{90}{100}\)</span>. The second shot goes in with probability
$<span class="math notranslate nohighlight">\(0.9\times 0.9 + 0.1 \times 0.1 = 0.81 + 0.01 = 0.82.\)</span>$</p>
<p>From the observer’s perspective, the first and second shots are not independent. With more shots observed, they are beginning to learn the player’s type. In other words, you should be more willing to pass to someone who makes their first shot because you should have greater confidence that the second shot will go in.</p>
</section>
</div>
<div class="solution dropdown admonition" id="solutions-solution-16">

<p class="admonition-title">Solution to<a class="reference internal" href="bayes.html#bayesReview"> Exercise 36</a></p>
<section id="solution-content">
<p><span class="math notranslate nohighlight">\(\mathbb{P}(\text{good review}) = 0.75\)</span></p>
<p><span class="math notranslate nohighlight">\(\mathbb{P}(\text{good review} \mid H) = 1\)</span></p>
<div class="math notranslate nohighlight">
\[\mathbb{P}(H \mid \text{good review}) = \frac{\mathbb{P}(\text{good review}\mid H)\mathbb{P}(H)}{\mathbb{P}(\text{good review})}\]</div>
<div class="math notranslate nohighlight">
\[ = \frac{1 \times 0.5}{.75} = \frac{2}{3}.\]</div>
<p>The probability tree is shown below.</p>
<figure class="align-default" id="productreviewtree">
<a class="reference internal image-reference" href="_images/productreviewtree.svg"><img alt="_images/productreviewtree.svg" src="_images/productreviewtree.svg" width="70%" /></a>
</figure>
<p>These events are dependent because <span class="math notranslate nohighlight">\(\mathbb{P}(H \mid \text{good review}) \neq \mathbb{P}(H)\)</span>.</p>
<p>If the truth-teller is replaced by a joker, then <span class="math notranslate nohighlight">\(\mathbb{P}(H \mid \text{bad review}) = 1\)</span>. The fake reviewer always leaves a positive reviewer, so the probability is the same as <span class="math notranslate nohighlight">\(\mathbb{P}(H \mid \text{joker and bad review})\)</span> and the joker only leaves a bad review if the product is of good quality.</p>
</section>
</div>
<div class="solution dropdown admonition" id="solutions-solution-17">

<p class="admonition-title">Solution to<a class="reference internal" href="bayes.html#bayesCountry"> Exercise 37</a></p>
<section id="solution-content">
<p>4/63 songs mention tractor and 14/63 mention truck.</p>
<p>2 of the 14 mentioning truck mention tractor.</p>
<p>2 of the 4 mentioning tractor mention truck. This can be calculated by simplying inspecting the data or by solve for the first three and then applying Bayes’ Theorem.</p>
<div class="math notranslate nohighlight">
\[\mathbb{P}(\text{truck}\mid \text{tractor}) = \frac{ \mathbb{P}(\text{tractor}\mid \text{truck}) \mathbb{P}(\text{truck})} {\mathbb{P}(\text{tractor}) } = \frac{ \frac{2}{14} \times \frac{14}{63} }{\frac{4}{63}} = \frac{2}{4}.\]</div>
</section>
</div>
</section>
</section>

Expand Down
6 changes: 3 additions & 3 deletions book/_build/jupyter_execute/chancevary.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 3ecd2d4

Please sign in to comment.