This commit is contained in:
2022-10-01 00:25:34 -07:00
parent 47caee3ddd
commit 8e18e3ddc1
24 changed files with 3225 additions and 5 deletions

View File

@@ -148,7 +148,8 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(bayesrules)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(patchwork)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The chapter is set up with an example of polling results. We are put into the scenario where we are managig the campaing for a candidate. We know that on average her support based on recent polls is around 45%. In the next few sections well work through our Bayesian framework and incorporate a new tool the <strong>Beta-Binomial</strong> model. This model will take develop a continuous prior, as opposed to the discrete ones weve been working with so far.</p>
<section id="the-beta-prior" class="level2">
@@ -229,7 +230,7 @@ The Standard Uniform
<div class="callout-body-container callout-body">
<p>When <span class="math inline">\pi</span> can take equally take on any value between 0 and 1, we can model <span class="math inline">\pi</span> using the standard uniform model.</p>
<p><span class="math display">\pi \sim Unif(0, 1)</span></p>
<p>the pdf of <span class="math inline">Unif(0, 1)</span> is <span class="math inline">\f(\pi) = 1</span></p>
<p>the pdf of <span class="math inline">Unif(0, 1)</span> is <span class="math inline">f(\pi) = 1</span></p>
<p>Note that <span class="math inline">Unif(0, 1)</span> is just a special case of the Beta with hyperparameters <span class="math inline">\alpha = \beta = 1</span>, see <a href="#fig-std-unif-as-beta">Figure&nbsp;2</a></p>
</div>
</div>
@@ -282,6 +283,136 @@ The Standard Uniform
<p>it follows that</p>
<p><span class="math display">SD = \sqrt{Var(\pi)}</span></p>
</section>
<section id="tuning-the-beta-prior" class="level3">
<h3 class="anchored" data-anchor-id="tuning-the-beta-prior">Tuning the beta prior</h3>
<p>Now that we know we can use the beta model to represent <span class="math inline">\pi</span> for <span class="math inline">\pi \in [0, 1]</span>, what we need to try and do is tune the model so that it best represents our prior. Our options of course are chaning values of <span class="math inline">\alpha</span> and <span class="math inline">\beta</span>. We can make use of the fact that we know that on average the candidate is polling at around 45%. That is we know that</p>
<p><span class="math display">E(\pi) \approx .45 \approx \frac{\alpha}{\alpha + \beta}</span></p>
<p><em>so I tried to work through the algebra myself, but was unable to do so, the book gets to the following conclusion</em></p>
<p><span class="math display">\alpha = \frac{9}{11}\beta</span></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>p <span class="ot">&lt;-</span> <span class="fu">plot_beta</span>(<span class="dv">9</span>, <span class="dv">11</span>) <span class="sc">+</span> </span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title=</span><span class="st">"Beta(9,11)"</span>) <span class="sc">+</span> </span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks=</span><span class="fu">seq</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="at">by=</span>.<span class="dv">1</span>))</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot">&lt;-</span> <span class="fu">plot_beta</span>(<span class="dv">27</span>, <span class="dv">33</span>) <span class="sc">+</span> </span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title=</span><span class="st">"Beta(27,33)"</span>) <span class="sc">+</span> </span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks=</span><span class="fu">seq</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="at">by=</span>.<span class="dv">1</span>))</span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>p3 <span class="ot">&lt;-</span> <span class="fu">plot_beta</span>(<span class="dv">45</span>, <span class="dv">55</span>) <span class="sc">+</span> </span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title=</span><span class="st">"Beta(45,55)"</span>) <span class="sc">+</span> </span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks=</span><span class="fu">seq</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="at">by=</span>.<span class="dv">1</span>))</span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a>p <span class="sc">/</span> p2 <span class="sc">/</span> p3</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>when compared to the original polling values, we can see that the closest of these is <span class="math inline">Beta(45, 55)</span></p>
<p>we can get the pdf, and then mean, mode and variance of the pdf.</p>
<p><span class="math display">f(\pi) = \frac{\Gamma(100)}{\Gamma(45)\Gamma(55)}\pi^{44}(1-\pi)^{54}</span></p>
<p><span class="math display">E(\pi) = \frac{45}{45 + 55} = .4500</span> <span class="math display">\text{Mode}(\pi) = \frac{45 - 1}{45 + 55 - 2} = .449</span> <span class="math display"> SD(\pi) = .05</span></p>
<div class="cell" data-fig.caption="the prior distrubtions with Y = 30 hihglighted">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>rb1 <span class="ot">&lt;-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">2</span>)</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>rb2 <span class="ot">&lt;-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">3</span>)</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>rb3 <span class="ot">&lt;-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">5</span>)</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>rb4 <span class="ot">&lt;-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">6</span>)</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a>rb5 <span class="ot">&lt;-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">7</span>)</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>rb6 <span class="ot">&lt;-</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="dv">50</span>, .<span class="dv">8</span>)</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a>results <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .2)</span><span class="st">`</span> <span class="ot">=</span> rb1,</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .3)</span><span class="st">`</span> <span class="ot">=</span> rb2,</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .5)</span><span class="st">`</span> <span class="ot">=</span> rb3,</span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .6)</span><span class="st">`</span> <span class="ot">=</span> rb4,</span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .7)</span><span class="st">`</span> <span class="ot">=</span> rb5,</span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .8)</span><span class="st">`</span> <span class="ot">=</span> rb6,</span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|&gt;</span></span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"bin_model"</span>, <span class="at">values_to =</span> <span class="st">"value"</span>, </span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">Bin(50, .2)</span><span class="st">`</span><span class="sc">:</span><span class="st">`</span><span class="at">Bin(50, .8)</span><span class="st">`</span>) <span class="sc">|&gt;</span></span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">is_30 =</span> value <span class="sc">==</span> <span class="dv">30</span>)</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a>results <span class="sc">|&gt;</span></span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(value, <span class="at">fill=</span>is_30)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"count"</span>) <span class="sc">+</span></span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"grey30"</span>, <span class="st">"darkblue"</span>), <span class="at">guide=</span><span class="st">"none"</span>) <span class="sc">+</span> </span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(bin_model))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div id="fig-betas-for-poll" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="ch3-beta-binomial_files/figure-html/fig-betas-for-poll-1.png" class="img-fluid figure-img" width="672"></p>
<p></p><figcaption class="figure-caption">Figure&nbsp;4: <strong>?(caption)</strong></figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>After the finish the poll, we see that the actual value of <span class="math inline">Y</span> is 30. The vertical line in <a href="#fig-betas-for-poll">Figure&nbsp;4</a></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>results <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="at">ys =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">50</span>, </span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .1)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">1</span>),</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .2)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">2</span>),</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .3)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">3</span>),</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .4)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">4</span>),</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .5)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">5</span>),</span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .6)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">6</span>),</span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .7)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">7</span>),</span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .8)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">8</span>),</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(50, .9)</span><span class="st">`</span> <span class="ot">=</span> <span class="fu">dbinom</span>(<span class="at">x =</span> ys, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> .<span class="dv">9</span>),</span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|&gt;</span></span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"beta_model"</span>, <span class="at">values_to=</span><span class="st">"value"</span>, <span class="sc">-</span>ys) <span class="sc">|&gt;</span></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a> <span class="at">is_30 =</span> ys <span class="sc">==</span> <span class="dv">30</span>, </span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a> <span class="at">pie =</span> <span class="fu">rep</span>(<span class="fu">c</span>(.<span class="dv">1</span>, .<span class="dv">2</span>, .<span class="dv">3</span>, .<span class="dv">4</span>, .<span class="dv">5</span>, .<span class="dv">6</span>, .<span class="dv">7</span>, .<span class="dv">8</span>, .<span class="dv">9</span>), <span class="dv">50</span>))</span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a>results <span class="sc">|&gt;</span> <span class="fu">ggplot</span>(<span class="fu">aes</span>(ys, value, <span class="at">fill =</span> is_30)) <span class="sc">+</span> </span>
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>() <span class="sc">+</span> </span>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"darkgray"</span>, <span class="st">"darkblue"</span>), <span class="at">guide=</span><span class="st">"none"</span>) <span class="sc">+</span> </span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(beta_model))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>results <span class="sc">|&gt;</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(is_30) <span class="sc">|&gt;</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(pie, value)) <span class="sc">+</span> <span class="fu">geom_col</span>() <span class="sc">+</span> </span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">seq</span>(.<span class="dv">1</span>, .<span class="dv">9</span>, <span class="at">by =</span> .<span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summarize_beta_binomial</span>(<span class="at">alpha=</span><span class="dv">45</span>, <span class="at">beta=</span><span class="dv">55</span>, <span class="at">n=</span><span class="dv">50</span>, <span class="at">y=</span><span class="dv">30</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> model alpha beta mean mode var sd
1 prior 45 55 0.45 0.4489796 0.002450495 0.04950248
2 posterior 75 75 0.50 0.5000000 0.001655629 0.04068942</code></pre>
</div>
</div>
</section>
</section>
<section id="simulation" class="level2">
<h2 class="anchored" data-anchor-id="simulation">Simulation</h2>
<p>In this simulation we first generate 10,000 random values from a beta distribution with parameter <span class="math inline">\alpha = 45, \beta = 55</span>. Recall, that this is what we believe to be support based on recent polls for the candidate, and therefore our prior. We use each one of these are the <code>prob</code> value (the <span class="math inline">\pi</span> value) into 10,000 more random this time from the binomial distribution with <span class="math inline">n = 50</span>. This binomial distribution is the model for <span class="math inline">Y</span> conditioned on <span class="math inline">\pi</span>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>sim <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="at">pi =</span> <span class="fu">rbeta</span>(<span class="dv">10000</span>, <span class="dv">45</span>, <span class="dv">55</span>)</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|&gt;</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">y =</span> <span class="fu">rbinom</span>(<span class="dv">10000</span>, <span class="at">size =</span> <span class="dv">50</span>, <span class="at">prob =</span> pi))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>sim <span class="sc">|&gt;</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(pi, y)) <span class="sc">+</span> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">color =</span> (y <span class="sc">==</span> <span class="dv">30</span>))) <span class="sc">+</span> </span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"darkgrey"</span>, <span class="st">"navyblue"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>sim <span class="sc">|&gt;</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(y <span class="sc">==</span> <span class="dv">30</span>) <span class="sc">|&gt;</span> <span class="co"># the posterior</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(pi)) <span class="sc">+</span> <span class="fu">geom_density</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="ch3-beta-binomial_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
</main>

View File

@@ -18,6 +18,7 @@ format:
```{r}
library(bayesrules)
library(tidyverse)
library(patchwork)
```
The chapter is set up with an example of polling results. We are put into
@@ -93,7 +94,7 @@ we can model $\pi$ using the standard uniform model.
$$\pi \sim Unif(0, 1)$$
the pdf of $Unif(0, 1)$ is $\f(\pi) = 1$
the pdf of $Unif(0, 1)$ is $f(\pi) = 1$
Note that $Unif(0, 1)$ is just a special case of the Beta with
hyperparameters $\alpha = \beta = 1$, see @fig-std-unif-as-beta
@@ -151,3 +152,151 @@ it follows that
$$SD = \sqrt{Var(\pi)}$$
### Tuning the beta prior
Now that we know we can use the beta model to represent
$\pi$ for $\pi \in [0, 1]$, what we need to try and do is
tune the model so that it best represents our prior. Our
options of course are chaning values of $\alpha$ and $\beta$.
We can make use of the fact that we know that on average
the candidate is polling at around 45%. That is we know
that
$$E(\pi) \approx .45 \approx \frac{\alpha}{\alpha + \beta}$$
*so I tried to work through the algebra myself, but was
unable to do so, the book gets to the following conclusion*
$$\alpha = \frac{9}{11}\beta$$
```{r}
p <- plot_beta(9, 11) +
labs(title="Beta(9,11)") +
scale_x_continuous(breaks=seq(0, 1, by=.1))
p2 <- plot_beta(27, 33) +
labs(title="Beta(27,33)") +
scale_x_continuous(breaks=seq(0, 1, by=.1))
p3 <- plot_beta(45, 55) +
labs(title="Beta(45,55)") +
scale_x_continuous(breaks=seq(0, 1, by=.1))
p / p2 / p3
```
when compared to the original polling values, we can see that the closest
of these is $Beta(45, 55)$
we can get the pdf, and then mean, mode and variance of the pdf.
$$f(\pi) = \frac{\Gamma(100)}{\Gamma(45)\Gamma(55)}\pi^{44}(1-\pi)^{54}$$
$$E(\pi) = \frac{45}{45 + 55} = .4500$$
$$\text{Mode}(\pi) = \frac{45 - 1}{45 + 55 - 2} = .449$$
$$ SD(\pi) = .05$$
```{r}
#| label: fig-betas-for-poll
#| fig-caption: "the prior distrubtions with Y = 30 hihglighted"
rb1 <- rbinom(10000, 50, .2)
rb2 <- rbinom(10000, 50, .3)
rb3 <- rbinom(10000, 50, .5)
rb4 <- rbinom(10000, 50, .6)
rb5 <- rbinom(10000, 50, .7)
rb6 <- rbinom(10000, 50, .8)
results <- tibble(
`Bin(50, .2)` = rb1,
`Bin(50, .3)` = rb2,
`Bin(50, .5)` = rb3,
`Bin(50, .6)` = rb4,
`Bin(50, .7)` = rb5,
`Bin(50, .8)` = rb6,
) |>
pivot_longer(names_to = "bin_model", values_to = "value",
`Bin(50, .2)`:`Bin(50, .8)`) |>
mutate(is_30 = value == 30)
results |>
ggplot(aes(value, fill=is_30)) + geom_bar(stat = "count") +
scale_fill_manual(values = c("grey30", "darkblue"), guide="none") +
facet_wrap(vars(bin_model))
```
After the finish the poll, we see that the actual value of
$Y$ is 30. The vertical line in @fig-betas-for-poll
```{r}
results <- tibble(
ys = 1:50,
`beta(50, .1)` = dbinom(x = ys, size = 50, prob = .1),
`beta(50, .2)` = dbinom(x = ys, size = 50, prob = .2),
`beta(50, .3)` = dbinom(x = ys, size = 50, prob = .3),
`beta(50, .4)` = dbinom(x = ys, size = 50, prob = .4),
`beta(50, .5)` = dbinom(x = ys, size = 50, prob = .5),
`beta(50, .6)` = dbinom(x = ys, size = 50, prob = .6),
`beta(50, .7)` = dbinom(x = ys, size = 50, prob = .7),
`beta(50, .8)` = dbinom(x = ys, size = 50, prob = .8),
`beta(50, .9)` = dbinom(x = ys, size = 50, prob = .9),
) |>
pivot_longer(names_to = "beta_model", values_to="value", -ys) |>
mutate(
is_30 = ys == 30,
pie = rep(c(.1, .2, .3, .4, .5, .6, .7, .8, .9), 50))
results |> ggplot(aes(ys, value, fill = is_30)) +
geom_col() +
scale_fill_manual(values = c("darkgray", "darkblue"), guide="none") +
facet_wrap(vars(beta_model))
```
```{r}
results |>
filter(is_30) |>
ggplot(aes(pie, value)) + geom_col() +
scale_x_continuous(breaks = seq(.1, .9, by = .1))
```
```{r}
summarize_beta_binomial(alpha=45, beta=55, n=50, y=30)
```
## Simulation
In this simulation we first generate 10,000 random values from
a beta distribution with parameter $\alpha = 45, \beta = 55$. Recall,
that this is what we believe to be support based on recent polls for the
candidate, and therefore our prior. We
use each one of these as the `prob` value (the $\pi$ value) into
10,000 more random samples from `rbinom` with
$n = 50$. This binomial distribution is how we model the likelihood. Our
simualtion will
```{r}
sim <- tibble(
pi = rbeta(10000, 45, 55)
) |>
mutate(y = rbinom(10000, size = 50, prob = pi))
```
```{r}
sim |>
ggplot(aes(pi, y)) + geom_point(aes(color = (y == 30))) +
scale_color_manual(values = c("darkgrey", "navyblue"))
```
```{r}
sim |>
filter(y == 30) |> # the posterior
ggplot(aes(pi)) + geom_density()
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -1,6 +1,5 @@
@import url('https://fonts.googleapis.com/css?family=Lora&display=swap');
@import url('https://fonts.googleapis.com/css?family=Source+Code+Pro&display=swap');
body {
font-family: 'Lora';
font-family: 'Lora'
}