class: center, middle, inverse, title-slide # The Beta-Binomial Model ### Dr. Dogucu --- layout: true <div class="my-header"></div> <div class="my-footer"> From Bayes Rules! book Copyright © Drs. Alicia Johnson, Miles Ott & Mine Dogucu. <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div> --- ## Posterior for the Beta-Binomial Model Let `\(\pi \sim \text{Beta}(\alpha, \beta)\)` and `\(X|n \sim \text{Bin}(n,\pi)\)`. -- `\(f(\pi|x) \propto \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}\pi^{\alpha-1} (1-\pi)^{\beta-1} {n \choose x}\pi^x(1-\pi)^{n-x}\)` -- `\(f(\pi|x) \propto \pi^{(\alpha+x)-1} (1-\pi)^{(\beta+n-x)-1}\)` -- `\(\pi|x \sim \text{Beta}(\alpha +x, \beta+n-x)\)` -- `\(f(\pi|x) = \frac{\Gamma(\alpha+\beta+n)}{\Gamma(\alpha+x)\Gamma(\beta+n-x)} \pi^{(\alpha+x)-1} (1-\pi)^{(\beta+n-x)-1}\)` --- ## Conjugate prior We say that `\(f(\pi)\)` is a conjugate prior for `\(L(\pi|x)\)` if the posterior, `\(f(\pi|x) \propto f(\pi)L(\pi|x)\)`, is from the same model family as the prior. Thus, Beta distribution is a conjugate prior for the Binomial likelihood model since the posterior also follows a Beta distribution. --- ## Bike ownership The transportation office at our school wants to know `\(\pi\)` the proportion of people who own bikes on campus so that they can build bike racks accordingly. The staff at the office think that the `\(\pi\)` is more likely to be somewhere 0.05 to 0.25. The plot below shows their prior distribution. Write out a reasonable `\(f(\pi)\)`. Calculate the prior expected value, mode, and variance. <img src="slide-2-beta-binomial_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> --- ## Plotting the prior ```r plot_beta(5, 35) ``` <img src="slide-2-beta-binomial_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- ## Summarizing the prior ```r summarize_beta(5, 35) ``` ``` ## mean mode var ## 1 0.125 0.1052632 0.002667683 ``` --- ## Bike ownership posterior The transportation office decides to collect some data and samples 50 people on campus and asks them whether they own a bike or not. It turns out that 25 of them do! What is the posterior distribution of `\(\pi\)` after having observed this data? -- `\(\pi|x \sim \text{Beta}(\alpha +x, \beta+n-x)\)` -- `\(\pi|x \sim \text{Beta}(5 +25, 35+50-25)\)` -- `\(\pi|x \sim \text{Beta}(30, 60)\)` --- ## Plotting the posterior ```r plot_beta(30, 60) ``` <img src="slide-2-beta-binomial_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- ## Summarizing the posterior ```r summarize_beta(30,60) ``` ``` ## mean mode var ## 1 0.3333333 0.3295455 0.002442002 ``` --- ## Plot summary ```r plot_beta(30, 60, mean = TRUE, mode = TRUE) ``` <img src="slide-2-beta-binomial_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- ## Bike ownership: balancing act ```r plot_beta_binomial(alpha = 5, beta = 35, x = 25, n = 50) ``` <img src="slide-2-beta-binomial_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- ## Posterior Descriptives `\(\pi|(X=x) \sim \text{Beta}(\alpha+x, \beta+n-x)\)` `$$E(\pi | (X=x)) = \frac{\alpha + x}{\alpha + \beta + n}$$` $$\text{Mode}(\pi | (X=x)) = \frac{\alpha + x - 1}{\alpha + \beta + n - 2} $$ `$$\text{Var}(\pi | (X=x)) = \frac{(\alpha + x)(\beta + n - x)}{(\alpha + \beta + n)^2(\alpha + \beta + n + 1)}\\$$` --- ## Bike ownership - descriptives of the posterior What is the descriptive measures (expected value, mode, and variance) of the posterior distribution for the bike ownership example? -- ```r summarize_beta_binomial(5, 35, x = 25, n = 50) ``` ``` ## model alpha beta mean mode var ## 1 prior 5 35 0.1250000 0.1052632 0.002667683 ## 2 posterior 30 60 0.3333333 0.3295455 0.002442002 ```