Skip to content

Commit

Permalink
bayes exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderthclark committed Mar 7, 2024
1 parent 8ae9098 commit 8a2dc95
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 21 deletions.
Binary file modified .DS_Store
Binary file not shown.
24 changes: 12 additions & 12 deletions Data/SimulatedDataGroupProjectDynamics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -216,7 +216,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -225,10 +225,10 @@
"$\\hat{y} = 4.712 +0.227 x_{1}$"
],
"text/plain": [
"<statwrap.utils.RegressionLine at 0x13c6928d0>"
"<statwrap.utils.RegressionLine at 0x13a1d5c10>"
]
},
"execution_count": 15,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -246,7 +246,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -256,7 +256,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand All @@ -265,10 +265,10 @@
"$\\hat{y} = 0.047 +1.491 x_{1}$"
],
"text/plain": [
"<statwrap.utils.RegressionLine at 0x13c6696d0>"
"<statwrap.utils.RegressionLine at 0x138653690>"
]
},
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -279,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -288,10 +288,10 @@
"$\\hat{y} = 0.056 +0.659 x_{1}$"
],
"text/plain": [
"<statwrap.utils.RegressionLine at 0x13c576490>"
"<statwrap.utils.RegressionLine at 0x13a3db790>"
]
},
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -302,7 +302,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand Down
Binary file modified book/.DS_Store
Binary file not shown.
Binary file modified book/_build/.doctrees/bayes.doctree
Binary file not shown.
Binary file modified book/_build/.doctrees/environment.pickle
Binary file not shown.
Binary file modified book/_build/.doctrees/probability.doctree
Binary file not shown.
105 changes: 103 additions & 2 deletions book/_build/html/_sources/bayes.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,107 @@ name: bayesTest

$$\mathbb{P}(Q \mid \cdot U) = \frac{\mathbb{P}(QU)}{\mathbb{P}(\cdot U)}$$

$\mathbb{P}(\cdot U)$ is found by summing the probability for each of the paths that terminate in $U$. This is $\frac{1}{26} + \frac{25}{676}$.
$\mathbb{P}(\cdot U)$ is found by summing the probability for each of the paths that terminate in $U$. This is $\frac{1}{26} + \frac{25}{676}$,

$$\mathbb{P}(Q \mid \cdot U) = \frac{\frac{1}{26}}{\frac{1}{26} + \frac{25}{676}} = \frac{26}{51}$$
$$\mathbb{P}(Q \mid \cdot U) = \frac{\frac{1}{26}}{\frac{1}{26} + \frac{25}{676}} = \frac{26}{51}.$$


## Exercises

```{exercise-start}
:label: boxes
```

A box contains two tickets, labeled $H$ or $T$. There is a 25% chance the box contains two $H$s. There is a 25% chance the box contains two $T$s. There is a 50% chance the box contains one $H$ and one $T$.

1. What is the chance of drawing an $H$?

2. Suppose you draw an $H$. What is the chance that the box contained two $T$s?

3. Suppose you draw an $H$. What is the chance that the box contained two $H$s?

4. After replacing the $H$, what is the chance of selecting another $H$?

```{exercise-end}
```

```{exercise-start}
:label: bayesraredisease
```
Consider a rare disease that affects 1 in 10,000 people in a population. A medical test for the disease has a 99% chance of correctly identifying a diseased person (true positive) and a 99% chance of correctly identifying a non-diseased person (true negative).

If a person from this population tests positive for the disease, what is the probability that they actually have the disease?

Given:

$$\mathbb{P}(\text{Disease}) = \frac{1}{10,000}$$

$$\mathbb{P}(\text{No disease}) = 1 - \mathbb{P}(D)$$

$$\mathbb{P}(\text{Positive} | \text{Disease}) = 0.99$$

$$\mathbb{P}(\text{Negative} | \text{No Disease}) = 0.99$$
```{exercise-end}
```


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

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.

1. What is the chance an unknown makes their first shot?
2. What is the chance that the player makes their second shot if they made their first?
3. Are the first and second shot outcomes independent from *your* perspective?


```{exercise-end}
```


```{exercise-start}
:label: troll
```

A pilgrim, traveling home, is wandering through a strange land when a troll appears:

> *Woe, to pass, pilgrim choose of these doors two. <br> Which is which, I cannot reveal to you. <br> Home with chance 7 or 73. <br> It depends on your choice and fate's decree. <br> Independent but certainty you lack. <br> Take now two draws before I turn thee back.*
1. What is the probability the pilgrim opens a door that leads home on the first draw?

2. The troll, old in his years, has seen 200 million other pilgrims pass through. Each has chosen a door randomly to start and then the other door second if the first didn't take them home. Finish filling in the table below with the expected counts.

| | Door 7 second | Door 73 second | Home after first |
|-------------------|---------------|----------------|------------------|
| Door 7 first | 0 million | | |
| Door 73 first | | 0 million | 73 million |

3. If the first draw does not lead home, what is the probability the pilgrim opened the door that leads home with chance 7%? What is the probability the pilgrim opened the door that leads home with chance 73%?

4. If the first draw does not lead home, should the pilgrim open a different door on the next draw or try the same door again? Or does it not matter?

5. What is the probability that the pilgrim remains in exile?

6. The pilgrim, alarmed by the risk of remaining in exile, bargains with the troll to replace the two doors with one 40% chance door. This averages the chances. Is this wise?

```{exercise-end}
```

```{exercise-start}
:label: bayesReview
```

Suppose that a product is sold on Amazon and it has either high quality ($H$) or low quality ($L$). We observe a single product review, which can either be good or bad. Reviewers can be of two types: fake or truth-teller. A fake reviewer always leaves a positive review, regardless of the product quality. A truth-teller reviewer leaves a positive review when the product is high quality and leaves a negative review if the product is low quality.

Assume $\mathbb{P}(H) = \frac{1}{2}$ and that each type of reviewer is equally likely.

- a.) What is $\mathbb{P}(\text{good review})$?
- b.) What is $\mathbb{P}(\text{good review} \mid H)$?
- c.) What is $\mathbb{P}(H \mid \text{good review})$?
- d.) Draw a probability tree that summarizes the probabilities based on product quality, review type, and reviewer type.
- e.) Are the events of "high quality product" and "good review" independent or dependent? Explain.
- f.) Suppose the truth-teller is replaced by a joker who leaves a negative review if the product is high quality and a positive review if the product is low quality. What is $\mathbb{P}(H \mid \text{bad review})$?

```{exercise-end}
```
4 changes: 2 additions & 2 deletions book/_build/html/_sources/probability.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ $$ \frac{n!}{k!(n-k)!} p^k (1-p)^{n-k}.$$

Think of $p^k (1-p)^{n-k}$ as the probability of a sequence of $k$ heads followed by $n-k$ tails. The binomial coefficient in front then adjusts that probability to allow for all of the other ways to get $k$ heads–$n-k$ tails followed by $k$ heads for example. This only works for coin flips or similar processes where the individual trials are independent and the probability of a heads or some substitutable event of interest is the same from one trial to the next. These trial outcomes are said to be *independent and identically distributed*, or *iid*.

**Example**
A trick coin comes up heads with probability $p = \frac{2}{3}$. Out of four flips, what is the probability of two heads?
**Example**: A trick coin comes up heads with probability $p = \frac{2}{3}$. Out of four flips, what is the probability of two heads?

```{dropdown} Two heads
Expand All @@ -355,6 +354,7 @@ $$ 4\cdot \frac{8}{27}\cdot\frac{1}{3} + \frac{16}{81} = \frac{48}{81} = \frac{1
```



## Exercises

```{exercise-start}
Expand Down
108 changes: 106 additions & 2 deletions book/_build/html/bayes.html
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ <h2> Contents </h2>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercises">Exercises</a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -527,12 +528,114 @@ <h4>Bayes Theorem with Trees<a class="headerlink" href="#bayes-theorem-with-tree
<p><a class="reference internal" href="#qwordtree"><span class="std std-numref">Fig. 42</span></a> can help us find the probability that a word starts with <span class="math notranslate nohighlight">\(Q\)</span>, given that the second letter is <span class="math notranslate nohighlight">\(U\)</span>. Maybe that’s helpful if you’re on Wheel of Fortune.</p>
<div class="math notranslate nohighlight">
\[\mathbb{P}(Q \mid \cdot U) = \frac{\mathbb{P}(QU)}{\mathbb{P}(\cdot U)}\]</div>
<p><span class="math notranslate nohighlight">\(\mathbb{P}(\cdot U)\)</span> is found by summing the probability for each of the paths that terminate in <span class="math notranslate nohighlight">\(U\)</span>. This is <span class="math notranslate nohighlight">\(\frac{1}{26} + \frac{25}{676}\)</span>.</p>
<p><span class="math notranslate nohighlight">\(\mathbb{P}(\cdot U)\)</span> is found by summing the probability for each of the paths that terminate in <span class="math notranslate nohighlight">\(U\)</span>. This is <span class="math notranslate nohighlight">\(\frac{1}{26} + \frac{25}{676}\)</span>,</p>
<div class="math notranslate nohighlight">
\[\mathbb{P}(Q \mid \cdot U) = \frac{\frac{1}{26}}{\frac{1}{26} + \frac{25}{676}} = \frac{26}{51}\]</div>
\[\mathbb{P}(Q \mid \cdot U) = \frac{\frac{1}{26}}{\frac{1}{26} + \frac{25}{676}} = \frac{26}{51}.\]</div>
</section>
</section>
</section>
<section id="exercises">
<h2>Exercises<a class="headerlink" href="#exercises" title="Permalink to this heading">#</a></h2>
<div class="exercise admonition" id="boxes">

<p class="admonition-title"><span class="caption-number">Exercise 32 </span></p>
<section id="exercise-content">
<p>A box contains two tickets, labeled <span class="math notranslate nohighlight">\(H\)</span> or <span class="math notranslate nohighlight">\(T\)</span>. There is a 25% chance the box contains two <span class="math notranslate nohighlight">\(H\)</span>s. There is a 25% chance the box contains two <span class="math notranslate nohighlight">\(T\)</span>s. There is a 50% chance the box contains one <span class="math notranslate nohighlight">\(H\)</span> and one <span class="math notranslate nohighlight">\(T\)</span>.</p>
<ol class="arabic simple">
<li><p>What is the chance of drawing an <span class="math notranslate nohighlight">\(H\)</span>?</p></li>
<li><p>Suppose you draw an <span class="math notranslate nohighlight">\(H\)</span>. What is the chance that the box contained two <span class="math notranslate nohighlight">\(T\)</span>s?</p></li>
<li><p>Suppose you draw an <span class="math notranslate nohighlight">\(H\)</span>. What is the chance that the box contained two <span class="math notranslate nohighlight">\(H\)</span>s?</p></li>
<li><p>After replacing the <span class="math notranslate nohighlight">\(H\)</span>, what is the chance of selecting another <span class="math notranslate nohighlight">\(H\)</span>?</p></li>
</ol>
</section>
</div>
<div class="exercise admonition" id="bayesraredisease">

<p class="admonition-title"><span class="caption-number">Exercise 33 </span></p>
<section id="exercise-content">
<p>Consider a rare disease that affects 1 in 10,000 people in a population. A medical test for the disease has a 99% chance of correctly identifying a diseased person (true positive) and a 99% chance of correctly identifying a non-diseased person (true negative).</p>
<p>If a person from this population tests positive for the disease, what is the probability that they actually have the disease?</p>
<p>Given:</p>
<div class="math notranslate nohighlight">
\[\mathbb{P}(\text{Disease}) = \frac{1}{10,000}\]</div>
<div class="math notranslate nohighlight">
\[\mathbb{P}(\text{No disease}) = 1 - \mathbb{P}(D)\]</div>
<div class="math notranslate nohighlight">
\[\mathbb{P}(\text{Positive} | \text{Disease}) = 0.99\]</div>
<div class="math notranslate nohighlight">
\[\mathbb{P}(\text{Negative} | \text{No Disease}) = 0.99\]</div>
</section>
</div>
<div class="exercise admonition" id="hatcoins">

<p class="admonition-title"><span class="caption-number">Exercise 34 </span></p>
<section id="exercise-content">
<p>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.</p>
<ol class="arabic simple">
<li><p>What is the chance an unknown makes their first shot?</p></li>
<li><p>What is the chance that the player makes their second shot if they made their first?</p></li>
<li><p>Are the first and second shot outcomes independent from <em>your</em> perspective?</p></li>
</ol>
</section>
</div>
<div class="exercise admonition" id="troll">

<p class="admonition-title"><span class="caption-number">Exercise 35 </span></p>
<section id="exercise-content">
<p>A pilgrim, traveling home, is wandering through a strange land when a troll appears:</p>
<blockquote>
<div><p><em>Woe, to pass, pilgrim choose of these doors two. <br> Which is which, I cannot reveal to you. <br> Home with chance 7 or 73. <br> It depends on your choice and fate’s decree. <br> Independent but certainty you lack. <br> Take now two draws before I turn thee back.</em></p>
</div></blockquote>
<ol class="arabic simple">
<li><p>What is the probability the pilgrim opens a door that leads home on the first draw?</p></li>
<li><p>The troll, old in his years, has seen 200 million other pilgrims pass through. Each has chosen a door randomly to start and then the other door second if the first didn’t take them home. Finish filling in the table below with the expected counts.</p></li>
</ol>
<table class="table">
<thead>
<tr class="row-odd"><th class="head"><p></p></th>
<th class="head"><p>Door 7 second</p></th>
<th class="head"><p>Door 73 second</p></th>
<th class="head"><p>Home after first</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Door 7 first</p></td>
<td><p>0 million</p></td>
<td><p></p></td>
<td><p></p></td>
</tr>
<tr class="row-odd"><td><p>Door 73 first</p></td>
<td><p></p></td>
<td><p>0 million</p></td>
<td><p>73 million</p></td>
</tr>
</tbody>
</table>
<ol class="arabic simple" start="3">
<li><p>If the first draw does not lead home, what is the probability the pilgrim opened the door that leads home with chance 7%? What is the probability the pilgrim opened the door that leads home with chance 73%?</p></li>
<li><p>If the first draw does not lead home, should the pilgrim open a different door on the next draw or try the same door again? Or does it not matter?</p></li>
<li><p>What is the probability that the pilgrim remains in exile?</p></li>
<li><p>The pilgrim, alarmed by the risk of remaining in exile, bargains with the troll to replace the two doors with one 40% chance door. This averages the chances. Is this wise?</p></li>
</ol>
</section>
</div>
<div class="exercise admonition" id="bayesReview">

<p class="admonition-title"><span class="caption-number">Exercise 36 </span></p>
<section id="exercise-content">
<p>Suppose that a product is sold on Amazon and it has either high quality (<span class="math notranslate nohighlight">\(H\)</span>) or low quality (<span class="math notranslate nohighlight">\(L\)</span>). We observe a single product review, which can either be good or bad. Reviewers can be of two types: fake or truth-teller. A fake reviewer always leaves a positive review, regardless of the product quality. A truth-teller reviewer leaves a positive review when the product is high quality and leaves a negative review if the product is low quality.</p>
<p>Assume <span class="math notranslate nohighlight">\(\mathbb{P}(H) = \frac{1}{2}\)</span> and that each type of reviewer is equally likely.</p>
<ul class="simple">
<li><p>a.) What is <span class="math notranslate nohighlight">\(\mathbb{P}(\text{good review})\)</span>?</p></li>
<li><p>b.) What is <span class="math notranslate nohighlight">\(\mathbb{P}(\text{good review} \mid H)\)</span>?</p></li>
<li><p>c.) What is <span class="math notranslate nohighlight">\(\mathbb{P}(H \mid \text{good review})\)</span>?</p></li>
<li><p>d.) Draw a probability tree that summarizes the probabilities based on product quality, review type, and reviewer type.</p></li>
<li><p>e.) Are the events of “high quality product” and “good review” independent or dependent? Explain.</p></li>
<li><p>f.) Suppose the truth-teller is replaced by a joker who leaves a negative review if the product is high quality and a positive review if the product is low quality. What is <span class="math notranslate nohighlight">\(\mathbb{P}(H \mid \text{bad review})\)</span>?</p></li>
</ul>
</section>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -607,6 +710,7 @@ <h4>Bayes Theorem with Trees<a class="headerlink" href="#bayes-theorem-with-tree
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercises">Exercises</a></li>
</ul>
</nav></div>

Expand Down
Binary file modified book/_build/html/objects.inv
Binary file not shown.
3 changes: 1 addition & 2 deletions book/_build/html/probability.html
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,7 @@ <h3>Binomial Formula<a class="headerlink" href="#id5" title="Permalink to this h
<div class="math notranslate nohighlight">
\[ \frac{n!}{k!(n-k)!} p^k (1-p)^{n-k}.\]</div>
<p>Think of <span class="math notranslate nohighlight">\(p^k (1-p)^{n-k}\)</span> as the probability of a sequence of <span class="math notranslate nohighlight">\(k\)</span> heads followed by <span class="math notranslate nohighlight">\(n-k\)</span> tails. The binomial coefficient in front then adjusts that probability to allow for all of the other ways to get <span class="math notranslate nohighlight">\(k\)</span> heads–<span class="math notranslate nohighlight">\(n-k\)</span> tails followed by <span class="math notranslate nohighlight">\(k\)</span> heads for example. This only works for coin flips or similar processes where the individual trials are independent and the probability of a heads or some substitutable event of interest is the same from one trial to the next. These trial outcomes are said to be <em>independent and identically distributed</em>, or <em>iid</em>.</p>
<p><strong>Example</strong>
A trick coin comes up heads with probability <span class="math notranslate nohighlight">\(p = \frac{2}{3}\)</span>. Out of four flips, what is the probability of two heads?</p>
<p><strong>Example</strong>: A trick coin comes up heads with probability <span class="math notranslate nohighlight">\(p = \frac{2}{3}\)</span>. Out of four flips, what is the probability of two heads?</p>
<details class="sd-sphinx-override sd-dropdown sd-card sd-mb-3">
<summary class="sd-summary-title sd-card-header">
Two heads<div class="sd-summary-down docutils">
Expand Down
2 changes: 1 addition & 1 deletion book/_build/html/searchindex.js

Large diffs are not rendered by default.

Loading

0 comments on commit 8a2dc95

Please sign in to comment.