Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney committed Dec 28, 2023
1 parent 210a7d7 commit ae1390b
Show file tree
Hide file tree
Showing 34 changed files with 769 additions and 656 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
463 changes: 235 additions & 228 deletions _sources/chap17.ipynb

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion bayes_dice.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script>window.MathJax = {"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]]}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'bayes_dice';</script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
Expand Down Expand Up @@ -602,7 +604,7 @@ <h2>The Update<a class="headerlink" href="#the-update" title="Permalink to this
</div>
</div>
<p>As an aside, you might have noticed that the values in <code class="docutils literal notranslate"><span class="pre">eq</span></code> are all the same.
So when the value we roll is equal to $T$, we don’t get any new information about <em>T</em>.
So when the value we roll is equal to <span class="math notranslate nohighlight">\(T\)</span>, we don’t get any new information about <em>T</em>.
We could leave the instances of <code class="docutils literal notranslate"><span class="pre">eq</span></code> out of the data, and we would get the same answer.</p>
</section>
</section>
Expand Down
62 changes: 38 additions & 24 deletions chap01.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script>window.MathJax = {"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]]}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'chap01';</script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
Expand Down Expand Up @@ -912,9 +914,9 @@ <h2>Laws of Probability<a class="headerlink" href="#laws-of-probability" title="
<p>Theorem 3 is also known as Bayes’s Theorem.</p>
<p>I’ll write these theorems using mathematical notation for probability:</p>
<ul class="simple">
<li><p>$P(A)$ is the probability of proposition $A$.</p></li>
<li><p>$P(A~\mathrm{and}~B)$ is the probability of the conjunction of $A$ and $B$, that is, the probability that both are true.</p></li>
<li><p>$P(A | B)$ is the conditional probability of $A$ given that $B$ is true. The vertical line between $A$ and $B$ is pronounced “given”.</p></li>
<li><p><span class="math notranslate nohighlight">\(P(A)\)</span> is the probability of proposition <span class="math notranslate nohighlight">\(A\)</span>.</p></li>
<li><p><span class="math notranslate nohighlight">\(P(A~\mathrm{and}~B)\)</span> is the probability of the conjunction of <span class="math notranslate nohighlight">\(A\)</span> and <span class="math notranslate nohighlight">\(B\)</span>, that is, the probability that both are true.</p></li>
<li><p><span class="math notranslate nohighlight">\(P(A | B)\)</span> is the conditional probability of <span class="math notranslate nohighlight">\(A\)</span> given that <span class="math notranslate nohighlight">\(B\)</span> is true. The vertical line between <span class="math notranslate nohighlight">\(A\)</span> and <span class="math notranslate nohighlight">\(B\)</span> is pronounced “given”.</p></li>
</ul>
<p>With that, we are ready for Theorem 1.</p>
<section id="theorem-1">
Expand Down Expand Up @@ -970,13 +972,15 @@ <h3>Theorem 1<a class="headerlink" href="#theorem-1" title="Permalink to this he
</div>
</div>
<p>The result is the same. This example demonstrates a general rule that relates conditional probability and conjunction. Here’s what it looks like in math notation:</p>
<p>$$P(A|B) = \frac{P(A~\mathrm{and}~B)}{P(B)}$$</p>
<div class="math notranslate nohighlight">
\[P(A|B) = \frac{P(A~\mathrm{and}~B)}{P(B)}\]</div>
<p>And that’s Theorem 1.</p>
</section>
<section id="theorem-2">
<h3>Theorem 2<a class="headerlink" href="#theorem-2" title="Permalink to this heading">#</a></h3>
<p>If we start with Theorem 1 and multiply both sides by $P(B)$, we get Theorem 2.</p>
<p>$$P(A~\mathrm{and}~B) = P(B) ~ P(A|B)$$</p>
<p>If we start with Theorem 1 and multiply both sides by <span class="math notranslate nohighlight">\(P(B)\)</span>, we get Theorem 2.</p>
<div class="math notranslate nohighlight">
\[P(A~\mathrm{and}~B) = P(B) ~ P(A|B)\]</div>
<p>This formula suggests a second way to compute a conjunction: instead of using the <code class="docutils literal notranslate"><span class="pre">&amp;</span></code> operator, we can compute the product of two probabilities.</p>
<p>Let’s see if it works for <code class="docutils literal notranslate"><span class="pre">liberal</span></code> and <code class="docutils literal notranslate"><span class="pre">democrat</span></code>. Here’s the result using <code class="docutils literal notranslate"><span class="pre">&amp;</span></code>:</p>
<div class="cell docutils container">
Expand Down Expand Up @@ -1009,16 +1013,19 @@ <h3>Theorem 2<a class="headerlink" href="#theorem-2" title="Permalink to this he
<section id="theorem-3">
<h3>Theorem 3<a class="headerlink" href="#theorem-3" title="Permalink to this heading">#</a></h3>
<p>We have established that conjunction is commutative. In math notation, that means:</p>
<p>$$P(A~\mathrm{and}~B) = P(B~\mathrm{and}~A)$$</p>
<div class="math notranslate nohighlight">
\[P(A~\mathrm{and}~B) = P(B~\mathrm{and}~A)\]</div>
<p>If we apply Theorem 2 to both sides, we have</p>
<p>$$P(B) P(A|B) = P(A) P(B|A)$$</p>
<p>Here’s one way to interpret that: if you want to check $A$ and $B$, you can do it in either order:</p>
<div class="math notranslate nohighlight">
\[P(B) P(A|B) = P(A) P(B|A)\]</div>
<p>Here’s one way to interpret that: if you want to check <span class="math notranslate nohighlight">\(A\)</span> and <span class="math notranslate nohighlight">\(B\)</span>, you can do it in either order:</p>
<ol class="arabic simple">
<li><p>You can check $B$ first, then $A$ conditioned on $B$, or</p></li>
<li><p>You can check $A$ first, then $B$ conditioned on $A$.</p></li>
<li><p>You can check <span class="math notranslate nohighlight">\(B\)</span> first, then <span class="math notranslate nohighlight">\(A\)</span> conditioned on <span class="math notranslate nohighlight">\(B\)</span>, or</p></li>
<li><p>You can check <span class="math notranslate nohighlight">\(A\)</span> first, then <span class="math notranslate nohighlight">\(B\)</span> conditioned on <span class="math notranslate nohighlight">\(A\)</span>.</p></li>
</ol>
<p>If we divide through by $P(B)$, we get Theorem 3:</p>
<p>$$P(A|B) = \frac{P(A) P(B|A)}{P(B)}$$</p>
<p>If we divide through by <span class="math notranslate nohighlight">\(P(B)\)</span>, we get Theorem 3:</p>
<div class="math notranslate nohighlight">
\[P(A|B) = \frac{P(A) P(B|A)}{P(B)}\]</div>
<p>And that, my friends, is Bayes’s Theorem.</p>
<p>To see how it works, let’s compute the fraction of bankers who are liberal, first using <code class="docutils literal notranslate"><span class="pre">conditional</span></code>:</p>
<div class="cell docutils container">
Expand Down Expand Up @@ -1053,9 +1060,10 @@ <h3>Theorem 3<a class="headerlink" href="#theorem-3" title="Permalink to this he
<h2>The Law of Total Probability<a class="headerlink" href="#the-law-of-total-probability" title="Permalink to this heading">#</a></h2>
<p>In addition to these three theorems, there’s one more thing we’ll need to do Bayesian statistics: the law of total probability.
Here’s one form of the law, expressed in mathematical notation:</p>
<p>$$P(A) = P(B_1 \mathrm{and} A) + P(B_2 \mathrm{and} A)$$</p>
<p>In words, the total probability of $A$ is the sum of two possibilities: either $B_1$ and $A$ are true or $B_2$ and $A$ are true.
But this law applies only if $B_1$ and $B_2$ are:</p>
<div class="math notranslate nohighlight">
\[P(A) = P(B_1 \mathrm{and} A) + P(B_2 \mathrm{and} A)\]</div>
<p>In words, the total probability of <span class="math notranslate nohighlight">\(A\)</span> is the sum of two possibilities: either <span class="math notranslate nohighlight">\(B_1\)</span> and <span class="math notranslate nohighlight">\(A\)</span> are true or <span class="math notranslate nohighlight">\(B_2\)</span> and <span class="math notranslate nohighlight">\(A\)</span> are true.
But this law applies only if <span class="math notranslate nohighlight">\(B_1\)</span> and <span class="math notranslate nohighlight">\(B_2\)</span> are:</p>
<ul class="simple">
<li><p>Mutually exclusive, which means that only one of them can be true, and</p></li>
<li><p>Collectively exhaustive, which means that one of them must be true.</p></li>
Expand Down Expand Up @@ -1100,7 +1108,8 @@ <h2>The Law of Total Probability<a class="headerlink" href="#the-law-of-total-pr
</div>
<p>Because <code class="docutils literal notranslate"><span class="pre">male</span></code> and <code class="docutils literal notranslate"><span class="pre">female</span></code> are mutually exclusive and collectively exhaustive (MECE), we get the same result we got by computing the probability of <code class="docutils literal notranslate"><span class="pre">banker</span></code> directly.</p>
<p>Applying Theorem 2, we can also write the law of total probability like this:</p>
<p>$$P(A) = P(B_1) P(A|B_1) + P(B_2) P(A|B_2)$$</p>
<div class="math notranslate nohighlight">
\[P(A) = P(B_1) P(A|B_1) + P(B_2) P(A|B_2)\]</div>
<p>And we can test it with the same example:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
Expand All @@ -1116,8 +1125,9 @@ <h2>The Law of Total Probability<a class="headerlink" href="#the-law-of-total-pr
</div>
</div>
<p>When there are more than two conditions, it is more concise to write the law of total probability as a summation:</p>
<p>$$P(A) = \sum_i P(B_i) P(A|B_i)$$</p>
<p>Again, this holds as long as the conditions, $B_i$ are mutually exclusive and collectively exhaustive.
<div class="math notranslate nohighlight">
\[P(A) = \sum_i P(B_i) P(A|B_i)\]</div>
<p>Again, this holds as long as the conditions, <span class="math notranslate nohighlight">\(B_i\)</span> are mutually exclusive and collectively exhaustive.
As an example, let’s consider <code class="docutils literal notranslate"><span class="pre">polviews</span></code>, which has seven different values.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
Expand Down Expand Up @@ -1175,13 +1185,17 @@ <h2>The Law of Total Probability<a class="headerlink" href="#the-law-of-total-pr
<h2>Summary<a class="headerlink" href="#summary" title="Permalink to this heading">#</a></h2>
<p>Here’s what we have so far:</p>
<p><strong>Theorem 1</strong> gives us a way to compute a conditional probability using a conjunction:</p>
<p>$$P(A|B) = \frac{P(A~\mathrm{and}~B)}{P(B)}$$</p>
<div class="math notranslate nohighlight">
\[P(A|B) = \frac{P(A~\mathrm{and}~B)}{P(B)}\]</div>
<p><strong>Theorem 2</strong> gives us a way to compute a conjunction using a conditional probability:</p>
<p>$$P(A~\mathrm{and}~B) = P(B) P(A|B)$$</p>
<p><strong>Theorem 3</strong>, also known as Bayes’s Theorem, gives us a way to get from $P(A|B)$ to $P(B|A)$, or the other way around:</p>
<p>$$P(A|B) = \frac{P(A) P(B|A)}{P(B)}$$</p>
<div class="math notranslate nohighlight">
\[P(A~\mathrm{and}~B) = P(B) P(A|B)\]</div>
<p><strong>Theorem 3</strong>, also known as Bayes’s Theorem, gives us a way to get from <span class="math notranslate nohighlight">\(P(A|B)\)</span> to <span class="math notranslate nohighlight">\(P(B|A)\)</span>, or the other way around:</p>
<div class="math notranslate nohighlight">
\[P(A|B) = \frac{P(A) P(B|A)}{P(B)}\]</div>
<p><strong>The Law of Total Probability</strong> provides a way to compute probabilities by adding up the pieces:</p>
<p>$$P(A) = \sum_i P(B_i) P(A|B_i)$$</p>
<div class="math notranslate nohighlight">
\[P(A) = \sum_i P(B_i) P(A|B_i)\]</div>
<p>At this point you might ask, “So what?” If we have all of the data, we can compute any probability we want, any conjunction, or any conditional probability, just by counting. We don’t have to use these formulas.</p>
<p>And you are right, <em>if</em> we have all of the data. But often we don’t, and in that case, these formulas can be pretty useful – especially Bayes’s Theorem.
In the next chapter, we’ll see how.</p>
Expand Down
Loading

0 comments on commit ae1390b

Please sign in to comment.