Skip to content

Commit 8efd128

Browse files
committed
Deploying to main from @ resampling-stats/resampling-with@8ba4a0e 🚀
1 parent 52a9bd8 commit 8efd128

File tree

179 files changed

+1732
-1740
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+1732
-1740
lines changed

bayes_simulation.html

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -753,12 +753,10 @@ <h3 data-number="31.2.2" class="anchored" data-anchor-id="probability-relationsh
753753
\]</span></p>
754754
<p>We read this as “the probability that the car is broken given that the car is red”. Such a probability is known as a <em>conditional probability</em>. We discuss these in more detail in <a href="probability_theory_1a.html#sec-cond-uncond" class="quarto-xref"><span>Section 8.13</span></a>.</p>
755755
<p>In our original problem, we ask what the chance is of a car being broken given that a mechanic approved it. As discussed under “Ratios of proportions”, it can be calculated with:</p>
756-
<p><span class="math display">\[
757-
\begin{align*}
756+
<p><span class="math display">\[\begin{align*}
758757
P(\text{car broken | mechanic approved}) = \\
759758
P(\text{car broken and mechanic approved}) / P(\text{mechanic approved})
760-
\end{align*}
761-
\]</span></p>
759+
\end{align*}\]</span></p>
762760
<p>We have already used <span class="math inline">\(B\)</span> to mean “broken” (above), so let us use <span class="math inline">\(A\)</span> to mean “mechanic approved”. Then we can write the statement above in a more compact way:</p>
763761
<p><span class="math display">\[
764762
P(B | A) = P(B \text{ and } A) / P(A)
@@ -769,12 +767,12 @@ <h3 data-number="31.2.2" class="anchored" data-anchor-id="probability-relationsh
769767
</section>
770768
<section id="example-conditional-probability" class="level3" data-number="31.2.3">
771769
<h3 data-number="31.2.3" class="anchored" data-anchor-id="example-conditional-probability"><span class="header-section-number">31.2.3</span> Example: conditional probability</h3>
772-
<p>Let’s discuss a very relevant example. You get a COVID test, and the test is negative. Now, you would like to know what the chance is of you having COVID.</p>
770+
<p>Let’s discuss a very relevant example. You get a Covid test, and the test is negative. Now, you would like to know what the chance is of you having Covid.</p>
773771
<p>We have the following information:</p>
774772
<ul>
775-
<li>1.5% of people in your area have COVID</li>
776-
<li>The false positive rate of the tests (i.e., that they detect COVID when it is absent) is very low at 0.5%</li>
777-
<li>The false negative rate (i.e., that they fail to detect COVID when it is present) is quite high at 40%</li>
773+
<li>1.5% of people in your area have Covid</li>
774+
<li>The false positive rate of the tests (i.e., that they detect Covid when it is absent) is very low at 0.5%</li>
775+
<li>The false negative rate (i.e., that they fail to detect Covid when it is present) is quite high at 40%</li>
778776
</ul>
779777
<p><img src="diagrams/covid-tree.png" class="img-fluid"></p>
780778
<p>Again, we start with our simulation.</p>
@@ -794,40 +792,40 @@ <h3 data-number="31.2.3" class="anchored" data-anchor-id="example-conditional-pr
794792
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a>n_trials <span class="op">=</span> <span class="dv">1_000_000</span></span>
795793
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a></span>
796794
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a><span class="co"># For each person, generate a True or False label,</span></span>
797-
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a><span class="co"># indicating that they have / don't have COVID.</span></span>
795+
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a><span class="co"># indicating that they have / don't have Covid.</span></span>
798796
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a>person_has_covid <span class="op">=</span> rnd.choice(</span>
799797
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a> [<span class="va">True</span>, <span class="va">False</span>], p<span class="op">=</span>[<span class="fl">0.015</span>, <span class="fl">0.985</span>],</span>
800798
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a> size<span class="op">=</span>n_trials</span>
801799
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a>)</span>
802800
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a></span>
803-
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate the numbers of people with and without COVID.</span></span>
801+
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate the numbers of people with and without Covid.</span></span>
804802
<span id="cb11-16"><a href="#cb11-16" aria-hidden="true" tabindex="-1"></a>n_with_covid <span class="op">=</span> np.<span class="bu">sum</span>(person_has_covid)</span>
805803
<span id="cb11-17"><a href="#cb11-17" aria-hidden="true" tabindex="-1"></a>n_without_covid <span class="op">=</span> n_trials <span class="op">-</span> n_with_covid</span>
806804
<span id="cb11-18"><a href="#cb11-18" aria-hidden="true" tabindex="-1"></a></span>
807805
<span id="cb11-19"><a href="#cb11-19" aria-hidden="true" tabindex="-1"></a><span class="co"># In this array, we will store, for each person, whether they</span></span>
808806
<span id="cb11-20"><a href="#cb11-20" aria-hidden="true" tabindex="-1"></a><span class="co"># had a positive or a negative test.</span></span>
809807
<span id="cb11-21"><a href="#cb11-21" aria-hidden="true" tabindex="-1"></a>test_result <span class="op">=</span> np.zeros(n_trials, dtype<span class="op">=</span><span class="bu">bool</span>)</span>
810808
<span id="cb11-22"><a href="#cb11-22" aria-hidden="true" tabindex="-1"></a></span>
811-
<span id="cb11-23"><a href="#cb11-23" aria-hidden="true" tabindex="-1"></a><span class="co"># Draw test results for people with COVID.</span></span>
809+
<span id="cb11-23"><a href="#cb11-23" aria-hidden="true" tabindex="-1"></a><span class="co"># Draw test results for people with Covid.</span></span>
812810
<span id="cb11-24"><a href="#cb11-24" aria-hidden="true" tabindex="-1"></a>test_result[person_has_covid] <span class="op">=</span> rnd.choice(</span>
813811
<span id="cb11-25"><a href="#cb11-25" aria-hidden="true" tabindex="-1"></a> [<span class="va">True</span>, <span class="va">False</span>], p<span class="op">=</span>[<span class="fl">0.6</span>, <span class="fl">0.4</span>],</span>
814812
<span id="cb11-26"><a href="#cb11-26" aria-hidden="true" tabindex="-1"></a> size<span class="op">=</span>n_with_covid</span>
815813
<span id="cb11-27"><a href="#cb11-27" aria-hidden="true" tabindex="-1"></a>)</span>
816814
<span id="cb11-28"><a href="#cb11-28" aria-hidden="true" tabindex="-1"></a></span>
817-
<span id="cb11-29"><a href="#cb11-29" aria-hidden="true" tabindex="-1"></a><span class="co"># Draw test results for people without COVID.</span></span>
815+
<span id="cb11-29"><a href="#cb11-29" aria-hidden="true" tabindex="-1"></a><span class="co"># Draw test results for people without Covid.</span></span>
818816
<span id="cb11-30"><a href="#cb11-30" aria-hidden="true" tabindex="-1"></a><span class="co"># ~person_has_covid` flips all Boolean values from FALSE to TRUE</span></span>
819817
<span id="cb11-31"><a href="#cb11-31" aria-hidden="true" tabindex="-1"></a><span class="co"># and from TRUE to FALSE.</span></span>
820818
<span id="cb11-32"><a href="#cb11-32" aria-hidden="true" tabindex="-1"></a>test_result[<span class="op">~</span>person_has_covid] <span class="op">=</span> rnd.choice(</span>
821819
<span id="cb11-33"><a href="#cb11-33" aria-hidden="true" tabindex="-1"></a> [<span class="va">True</span>, <span class="va">False</span>], p<span class="op">=</span>[<span class="fl">0.005</span>, <span class="fl">0.995</span>],</span>
822820
<span id="cb11-34"><a href="#cb11-34" aria-hidden="true" tabindex="-1"></a> size<span class="op">=</span>n_without_covid</span>
823821
<span id="cb11-35"><a href="#cb11-35" aria-hidden="true" tabindex="-1"></a>)</span>
824822
<span id="cb11-36"><a href="#cb11-36" aria-hidden="true" tabindex="-1"></a></span>
825-
<span id="cb11-37"><a href="#cb11-37" aria-hidden="true" tabindex="-1"></a><span class="co"># Get the COVID statuses of all those with negative tests</span></span>
823+
<span id="cb11-37"><a href="#cb11-37" aria-hidden="true" tabindex="-1"></a><span class="co"># Get the Covid statuses of all those with negative tests</span></span>
826824
<span id="cb11-38"><a href="#cb11-38" aria-hidden="true" tabindex="-1"></a><span class="co"># (`test_result` is a Boolean mask, like `[True, False, False, True, ...]`,</span></span>
827825
<span id="cb11-39"><a href="#cb11-39" aria-hidden="true" tabindex="-1"></a><span class="co"># and `~test_result` flips all Boolean values to `[False, True, True, False, ...]`.</span></span>
828826
<span id="cb11-40"><a href="#cb11-40" aria-hidden="true" tabindex="-1"></a>covid_status_negative_test <span class="op">=</span> person_has_covid[<span class="op">~</span>test_result]</span>
829827
<span id="cb11-41"><a href="#cb11-41" aria-hidden="true" tabindex="-1"></a></span>
830-
<span id="cb11-42"><a href="#cb11-42" aria-hidden="true" tabindex="-1"></a><span class="co"># Now, count how many with COVID had a negative test results.</span></span>
828+
<span id="cb11-42"><a href="#cb11-42" aria-hidden="true" tabindex="-1"></a><span class="co"># Now, count how many with Covid had a negative test results.</span></span>
831829
<span id="cb11-43"><a href="#cb11-43" aria-hidden="true" tabindex="-1"></a>n_with_covid_and_negative_test <span class="op">=</span> np.<span class="bu">sum</span>(covid_status_negative_test)</span>
832830
<span id="cb11-44"><a href="#cb11-44" aria-hidden="true" tabindex="-1"></a></span>
833831
<span id="cb11-45"><a href="#cb11-45" aria-hidden="true" tabindex="-1"></a><span class="co"># And how many people, overall, had negative test results.</span></span>
@@ -855,15 +853,15 @@ <h3 data-number="31.2.3" class="anchored" data-anchor-id="example-conditional-pr
855853
<li><span class="math inline">\(T^-\)</span> means the Covid <em>test</em> was negative.</li>
856854
</ul>
857855
<p>For example <span class="math inline">\(P(C^+ | T^-)\)</span> is the probability (<span class="math inline">\(P\)</span>) that you do actually have Covid (<span class="math inline">\(C^+\)</span>) <em>given that</em> (<span class="math inline">\(|\)</span>) the test was negative (<span class="math inline">\(T^-\)</span>).</p>
858-
<p>We would like to know the probability of having COVID <em>given that</em> your test was negative (<span class="math inline">\(P(C^+ | T^-)\)</span>). Using the conditional probability relationship from above, we can write:</p>
856+
<p>We would like to know the probability of having Covid <em>given that</em> your test was negative (<span class="math inline">\(P(C^+ | T^-)\)</span>). Using the conditional probability relationship from above, we can write:</p>
859857
<p><span class="math display">\[
860858
P(C^+ | T^-) = P(C^+ \text{ and } T^-) / P(T^-)
861859
\]</span></p>
862860
<p>We see from the tree diagram that <span class="math inline">\(P(C^+ \text{ and } T^-) = P(T^- | C^+) * P(C^+) = .4 * .015 = 0.006\)</span>.</p>
863861
<!---
864862
**TODO: ADD REFERENCE TO SUMMATION OF MUTUALLY EXCLUSIVE PROBABILITIES**
865863
-->
866-
<p>We observe that <span class="math inline">\(P(T^-) = P(T^- \text{ and } C^-) + P(T^- \text{ and } C^+)\)</span>, i.e.&nbsp;that we can obtain a negative test result through two paths, having COVID or not having COVID. We expand these further as conditional probabilities:</p>
864+
<p>We observe that <span class="math inline">\(P(T^-) = P(T^- \text{ and } C^-) + P(T^- \text{ and } C^+)\)</span>, i.e.&nbsp;that we can obtain a negative test result through two paths, having Covid or not having Covid. We expand these further as conditional probabilities:</p>
867865
<p><span class="math inline">\(P(T^- \text{ and } C^-) = P(T^- | C^-) * P(C^-)\)</span></p>
868866
<p>and</p>
869867
<p><span class="math inline">\(P(T^- \text{ and } C^+) = P(T^- | C^+) * P(C^+)\)</span>.</p>
-2.24 KB
Loading

0 commit comments

Comments
 (0)