diff --git a/R/chapter-3-beta-binomial/ch3-beta-binomial.html b/R/chapter-3-beta-binomial/ch3-beta-binomial.html index d164419..77ce010 100644 --- a/R/chapter-3-beta-binomial/ch3-beta-binomial.html +++ b/R/chapter-3-beta-binomial/ch3-beta-binomial.html @@ -184,6 +184,104 @@ Tip

a quick note on (1) above. Note that it does not place a restriction on f(\pi) being less than 1. This means that we can’t interpret values of f as probabilities, we can however use to interpret plausability of two different events, the greater the value of f the more plausible. To calculate probabilities using f we must determine the area under the curve it defines, as shown in (3).

+
+
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)")))
+
+
+
ggplot(data=d, aes(x, beta)) + geom_point() +
+    geom_line() + 
+    facet_wrap(vars(beta_shape))
+
+
+
+

+

Figure 1: The basic shapes of beta based on the hyperparameters

+
+
+
+
+

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 Figure 1.

+
+
+
+ +
+
+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 Figure 2

+
+
+
+
std_unif <- tibble(
+    x, `beta(1, 1)`=dbeta(x, 1, 1)
+)
+
+ggplot(data=std_unif, aes(x, `beta(1, 1)`)) + 
+    geom_point() + 
+    geom_line()
+
+
+
+

+

Figure 2: The standard uniform is a special case of the beta distrubtion with a = b = 1

+
+
+
+
+
+

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 Figure 3 we can see the variability of \pi differ based on the values \alpha, \beta.

+
+
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))
+
+
+
+

+

Figure 3: Two symmetrical shapes of beta with different variance

+
+
+
+
+

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)}

+
diff --git a/R/chapter-3-beta-binomial/ch3-beta-binomial.qmd b/R/chapter-3-beta-binomial/ch3-beta-binomial.qmd index d8b73c0..56931fe 100644 --- a/R/chapter-3-beta-binomial/ch3-beta-binomial.qmd +++ b/R/chapter-3-beta-binomial/ch3-beta-binomial.qmd @@ -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)}$$ + diff --git a/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/beta-shapes-1.png b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/beta-shapes-1.png new file mode 100644 index 0000000..7e82ef9 Binary files /dev/null and b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/beta-shapes-1.png differ diff --git a/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-beta-shapes-1.png b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-beta-shapes-1.png new file mode 100644 index 0000000..7e82ef9 Binary files /dev/null and b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-beta-shapes-1.png differ diff --git a/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-beta-vars-1.png b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-beta-vars-1.png new file mode 100644 index 0000000..b029438 Binary files /dev/null and b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-beta-vars-1.png differ diff --git a/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-std-unif-as-beta-1.png b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-std-unif-as-beta-1.png new file mode 100644 index 0000000..baf5d1b Binary files /dev/null and b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/fig-std-unif-as-beta-1.png differ diff --git a/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/unnamed-chunk-2-1.png b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/unnamed-chunk-2-1.png new file mode 100644 index 0000000..7e82ef9 Binary files /dev/null and b/R/chapter-3-beta-binomial/ch3-beta-binomial_files/figure-html/unnamed-chunk-2-1.png differ