more work on chapter 3 work

This commit is contained in:
Emanuel Rodriguez 2022-09-17 01:28:47 -07:00
parent 231da48b00
commit 47caee3ddd
7 changed files with 198 additions and 0 deletions

View File

@ -184,6 +184,104 @@ Tip
<p>a quick note on (1) above. Note that it does not place a restriction on <span class="math inline">f(\pi)</span> being less than 1. This means that we cant interpret values of <span class="math inline">f</span> as probabilities, we can however use to interpret plausability of two different events, the greater the value of <span class="math inline">f</span> the more plausible. To calculate probabilities using <span class="math inline">f</span> we must determine the area under the curve it defines, as shown in (3).</p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">seq</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="at">by =</span> .<span class="dv">05</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>y1 <span class="ot">&lt;-</span> <span class="fu">dbeta</span>(<span class="at">x=</span>x, <span class="dv">5</span>, <span class="dv">5</span>)</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>y2 <span class="ot">&lt;-</span> <span class="fu">dbeta</span>(<span class="at">x=</span>x, <span class="dv">5</span>, <span class="dv">1</span>)</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>y3 <span class="ot">&lt;-</span> <span class="fu">dbeta</span>(<span class="at">x=</span>x, <span class="dv">1</span>, <span class="dv">5</span>)</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>d <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> x, </span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(5, 1)</span><span class="st">`</span><span class="ot">=</span>y2, </span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(5, 5)</span><span class="st">`</span><span class="ot">=</span>y1, </span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(1, 5)</span><span class="st">`</span><span class="ot">=</span>y3</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|&gt;</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"beta_shape"</span>, <span class="at">values_to=</span><span class="st">"beta"</span>, </span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a> <span class="sc">-</span>x) <span class="sc">|&gt;</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">beta_shape=</span><span class="fu">factor</span>(beta_shape, </span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a> <span class="at">levels=</span><span class="fu">c</span>(<span class="st">"beta(5, 1)"</span>,</span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a> <span class="st">"beta(5, 5)"</span>, </span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a> <span class="st">"beta(1, 5)"</span>)))</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="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data=</span>d, <span class="fu">aes</span>(x, beta)) <span class="sc">+</span> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>() <span class="sc">+</span> </span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(beta_shape))</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-beta-shapes" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="ch3-beta-binomial_files/figure-html/fig-beta-shapes-1.png" class="img-fluid figure-img" width="672"></p>
<p></p><figcaption class="figure-caption">Figure&nbsp;1: The basic shapes of beta based on the hyperparameters</figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>In general the shape of the beta distribution is skewed-left when <span class="math inline">\alpha &gt; \beta</span>, symmetrical when <span class="math inline">\alpha = \beta</span> and skewed-right when <span class="math inline">\alpha &lt; \beta</span>, see <a href="#fig-beta-shapes">Figure&nbsp;1</a>.</p>
<div class="callout-note callout callout-style-default no-icon callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
The Standard Uniform
</div>
</div>
<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>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>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>std_unif <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> x, <span class="st">`</span><span class="at">beta(1, 1)</span><span class="st">`</span><span class="ot">=</span><span class="fu">dbeta</span>(x, <span class="dv">1</span>, <span class="dv">1</span>)</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data=</span>std_unif, <span class="fu">aes</span>(x, <span class="st">`</span><span class="at">beta(1, 1)</span><span class="st">`</span>)) <span class="sc">+</span> </span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span> </span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>()</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-std-unif-as-beta" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="ch3-beta-binomial_files/figure-html/fig-std-unif-as-beta-1.png" class="img-fluid figure-img" width="672"></p>
<p></p><figcaption class="figure-caption">Figure&nbsp;2: The standard uniform is a special case of the beta distrubtion with a = b = 1</figcaption><p></p>
</figure>
</div>
</div>
</div>
<section id="mean-and-mode-of-the-beta" class="level3">
<h3 class="anchored" data-anchor-id="mean-and-mode-of-the-beta">Mean and Mode of the Beta</h3>
<p>The mean and mode are both measures of centrality. The mean is average value the mode is the most “common”, in the case of pmf this is just the value that occurs the most in the pdf its the max value.</p>
<p>The formulations of these for the beta are:</p>
<p><span class="math display">E(\pi) = \frac{\alpha}{\alpha + \beta}</span> <span class="math display">\text{Mode}(\pi) = \frac{\alpha - 1}{\alpha + \beta -2}\;\; \text{when} \;\;\alpha,\beta &gt; 1</span></p>
<p>When can also measure the variability of <span class="math inline">\pi</span>. Take <a href="#fig-beta-vars">Figure&nbsp;3</a> we can see the variability of <span class="math inline">\pi</span> differ based on the values <span class="math inline">\alpha, \beta</span>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>beta_variances <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> x, </span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(5, 5)</span><span class="st">`</span><span class="ot">=</span><span class="fu">dbeta</span>(x, <span class="dv">5</span>, <span class="dv">5</span>), </span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="st">`</span><span class="at">beta(20, 20)</span><span class="st">`</span><span class="ot">=</span><span class="fu">dbeta</span>(x, <span class="dv">20</span>, <span class="dv">20</span>)</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|&gt;</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">names_to =</span> <span class="st">"beta_shape"</span>, <span class="at">values_to =</span> <span class="st">"beta"</span>, <span class="sc">-</span>x)</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data=</span>beta_variances, <span class="fu">aes</span>(x, beta)) <span class="sc">+</span> </span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span> </span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>() <span class="sc">+</span> </span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="fu">vars</span>(beta_shape))</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-beta-vars" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="ch3-beta-binomial_files/figure-html/fig-beta-vars-1.png" class="img-fluid figure-img" width="672"></p>
<p></p><figcaption class="figure-caption">Figure&nbsp;3: Two symmetrical shapes of beta with different variance</figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>We can formulate the variance of <span class="math inline">Beta(\alpha, \beta)</span> with</p>
<p><span class="math display">Var(\pi) = \frac{\alpha\beta}{(\alpha + \beta)^2(\alpha+\beta+1)}</span></p>
<p>it follows that</p>
<p><span class="math display">SD = \sqrt{Var(\pi)}</span></p>
</section>
</section>
</main>

View File

@ -51,3 +51,103 @@ To calculate probabilities using $f$ we must determine the area under the
curve it defines, as shown in (3).
:::
```{r}
x <- seq(0, 1, by = .05)
y1 <- dbeta(x=x, 5, 5)
y2 <- dbeta(x=x, 5, 1)
y3 <- dbeta(x=x, 1, 5)
d <- tibble(
x,
`beta(5, 1)`=y2,
`beta(5, 5)`=y1,
`beta(1, 5)`=y3
) |>
pivot_longer(names_to = "beta_shape", values_to="beta",
-x) |>
mutate(beta_shape=factor(beta_shape,
levels=c("beta(5, 1)",
"beta(5, 5)",
"beta(1, 5)")))
```
```{r}
#| label: fig-beta-shapes
#| fig-cap: The basic shapes of beta based on the hyperparameters
ggplot(data=d, aes(x, beta)) + geom_point() +
geom_line() +
facet_wrap(vars(beta_shape))
```
In general the shape of the beta distribution is skewed-left when
$\alpha > \beta$, symmetrical when $\alpha = \beta$ and skewed-right
when $\alpha < \beta$, see @fig-beta-shapes.
:::{.callout-note}
## The Standard Uniform
When $\pi$ can take equally take on any value between 0 and 1,
we can model $\pi$ using the standard uniform model.
$$\pi \sim Unif(0, 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
:::
```{r}
#| label: fig-std-unif-as-beta
#| fig-cap: "The standard uniform is a special case of the beta distrubtion with a = b = 1"
std_unif <- tibble(
x, `beta(1, 1)`=dbeta(x, 1, 1)
)
ggplot(data=std_unif, aes(x, `beta(1, 1)`)) +
geom_point() +
geom_line()
```
### Mean and Mode of the Beta
The mean and mode are both measures of centrality. The mean is average
value the mode is the most "common", in the case of pmf this is just
the value that occurs the most in the pdf its the max value.
The formulations of these for the beta are:
$$E(\pi) = \frac{\alpha}{\alpha + \beta}$$
$$\text{Mode}(\pi) = \frac{\alpha - 1}{\alpha + \beta -2}\;\; \text{when} \;\;\alpha,\beta > 1$$
When can also measure the variability of $\pi$. Take @fig-beta-vars
we can see the variability of $\pi$ differ based on the values
$\alpha, \beta$.
```{r}
#| label: fig-beta-vars
#| fig-cap: "Two symmetrical shapes of beta with different variance"
beta_variances <- tibble(
x,
`beta(5, 5)`=dbeta(x, 5, 5),
`beta(20, 20)`=dbeta(x, 20, 20)
) |>
pivot_longer(names_to = "beta_shape", values_to = "beta", -x)
ggplot(data=beta_variances, aes(x, beta)) +
geom_point() +
geom_line() +
facet_wrap(vars(beta_shape))
```
We can formulate the variance of $Beta(\alpha, \beta)$ with
$$Var(\pi) = \frac{\alpha\beta}{(\alpha + \beta)^2(\alpha+\beta+1)}$$
it follows that
$$SD = \sqrt{Var(\pi)}$$

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 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: 20 KiB