EXERCISE 4.17 Part A: The Prior Belief
The employees are starting with 4 “imaginary clicks” (\(\alpha\)) and 3 “imaginary ignores” (\(\beta\)). Their prior understanding is that the expected click rate is \(4 / (4+3) \approx 0.57\). They initially believe about 57% of users will click the ad.
Part B: Posteriors “From Scratch”
To construct the posterior we must multiply the prior probability
density function (PDF) by the likelihood function:Prior: Proportional to
\(\pi^{\alpha-1}(1-\pi)^{\beta-1}\)Likelihood:
Proportional to \(\pi^y(1-\pi)^{n-y}\)Posterior: Combining
these gives \(\pi^{\alpha+y-1}(1-\pi)^{\beta+n-y-1}\),
which proves the new Beta parameters are \(\alpha + y\) and \(\beta + n - y\).
Applying this addition logic to each employee’s unique data:Employee 1 (\(n=1, y=0\)): \(Beta(4+0, 3+1-0) \rightarrow Beta(4, 4)\)
Employee 2 (\(n=10, y=3\)): \(Beta(4+3, 3+10-3) \rightarrow Beta(7, 10)\)
Employee 3 (\(n=100, y=20\)): \(Beta(4+20, 3+100-20) \rightarrow Beta(24,
83)\)
Part C: Plotting the Models
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'readr' was built under R version 4.5.2
## Warning: package 'forcats' was built under R version 4.5.2
## Warning: package 'lubridate' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.5.2
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.0
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(bayesrules)
## Warning: package 'bayesrules' was built under R version 4.5.3
Sketching the Prior
To sketch the prior probability density function (pdf) and see the employees’ initial understanding, we use the plot_beta() function.
# Plotting the shared Beta(4,3) prior
plot_beta(alpha = 4, beta = 3, mean = TRUE, mode = TRUE)
## Warning in geom_segment(aes(x = mean, y = 0, xend = mean, yend = dbeta(mean, : All aesthetics have length 1, but the data has 2 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_segment(aes(x = mode, y = 0, xend = mode, yend = stats::dbeta(mode, : All aesthetics have length 1, but the data has 2 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
# Optional: obtaining numerical summaries of the prior
summarize_beta(alpha = 4, beta = 3)
## mean mode var sd
## 1 0.5714286 0.6 0.03061224 0.1749636
Plotting and Summarizing the Posteriors
Employee 1 (Tested 1, Clicked 0)
# Plot prior, likelihood, and posterior for Employee 1
plot_beta_binomial(alpha = 4, beta = 3, y = 0, n = 1)
# Numerical summary for Employee 1
summarize_beta_binomial(alpha = 4, beta = 3, y = 0, n = 1)
## model alpha beta mean mode var sd
## 1 prior 4 3 0.5714286 0.6 0.03061224 0.1749636
## 2 posterior 4 4 0.5000000 0.5 0.02777778 0.1666667
Employee 2 (Tested 10, Clicked 3)
# Plot prior, likelihood, and posterior for Employee 2
plot_beta_binomial(alpha = 4, beta = 3, y = 3, n = 10)
# Numerical summary for Employee 2
summarize_beta_binomial(alpha = 4, beta = 3, y = 3, n = 10)
## model alpha beta mean mode var sd
## 1 prior 4 3 0.5714286 0.6 0.03061224 0.1749636
## 2 posterior 7 10 0.4117647 0.4 0.01345636 0.1160016
Employee 3 (Tested 100, Clicked 20)
# Plot prior, likelihood, and posterior for Employee 3
plot_beta_binomial(alpha = 4, beta = 3, y = 20, n = 100)
# Numerical summary for Employee 3
summarize_beta_binomial(alpha = 4, beta = 3, y = 20, n = 100)
## model alpha beta mean mode var sd
## 1 prior 4 3 0.5714286 0.6000000 0.030612245 0.17496355
## 2 posterior 24 83 0.2242991 0.2190476 0.001611009 0.04013738
Summarize and compare the employees’ posterior models of π
The employees’ posterior models become more influenced by the observed data as the sample size increases. Employee 1 had a posterior of Beta(4,4) with a mean of 0.5000 and sd of 0.1667. Since there was only one observation, the posterior remained close to the prior. Employee 2 had a posterior of Beta(7,10) with a mean of 0.4118 and sd of 0.1160, showing more influence from the observed success rate of 0.30.
Employee 3 had a posterior of Beta(24,83) with a mean of 0.2243 and sd of 0.0401. Because this employee had the largest sample size, the posterior was much closer to the observed success rate of 0.20 and had the least uncertainty. Overall, as the sample size increased, the posterior estimates became more precise and the standard deviations decreased.
EXERCISE 4.18
Part A: The Sequential Posteriors (Day-by-Day) Using our running scoreboard logic where today’s posterior becomes tomorrow’s prior:
Day 1 (Inheriting Employee 1’s data: n = 1, y = 0):
Starting Prior: {Beta}(4, 3)
New Data: 0 clicks, 1 ignore (1 - 0 = 1)
The Math:= 4 + 0 = 4 = 3 + 1 = 4
End of Day 1 Posterior: {Beta}(4, 4)
Day 2 (Inheriting Employee 2’s data: n = 10, y = 3):
Starting Prior: {Beta}(4, 4)
New Data: 3 clicks, 7 ignores (10 - 3 = 7)
The Math:= 4 + 3 = 7 = 4 + 7 = 11
End of Day 2 Posterior: {Beta}(7, 11)
Day 3 (Inheriting Employee 3’s data: n = 100, y = 20):
Starting Prior: {Beta}(7, 11) New Data: 20 clicks, 80 ignores (100 - 20 = 80) The Math:= 7 + 20 = 27 = 11 + 80 = 91 End of Day 3 Posterior: {Beta}(27, 91)
Part (b) - Sketching and Describing the Evolution
# Day 1: Starting from initial prior
plot_beta_binomial(alpha = 4, beta = 3, y = 0, n = 1)
# Day 2: Starting from Day 1's posterior parameters
plot_beta_binomial(alpha = 4, beta = 4, y = 3, n = 10)
# Day 3: Starting from Day 2's posterior parameters
plot_beta_binomial(alpha = 7, beta = 11, y = 20, n = 100)
Describing the Evolution (In Words)
Day 1: Starting with a {Beta}(4, 3) prior, the employee sees a single failure (n=1, y=0). This slight negative evidence pulls the belief down to a {Beta}(4, 4) posterior, shifting the mean from ~0.57 down to 0.50.
Day 2: Carrying forward the {Beta}(4, 4) belief into day two, the employee adds moderate data (n=10, y=3). This updates the model to {Beta}(7, 11), pulling the mean down further to ~0.39 and increasing certainty.
Day 3: Carrying the {Beta}(7, 11) belief into day three, the large dataset arrives (n=100, y=20). This substantial evidence dominates the model, resulting in a final {Beta}(27, 91) posterior with a mean of ~0.23 and a much narrower spread, indicating high confidence.
Part (c) - Waiting Until the End (Batch Updating)
Calculate the Total Data (n and y):
Total Clicks (y): Add up all the successes from the three employees:Employee 1: 0 Employee 2: 3 Employee 3: 20 Total y = 0 + 3 + 20 = 23 Total Trials (n): Add up all the people tested:Employee 1: 1 Employee 2: 10 Employee 3: 100 Total n = 1 + 10 + 100 = 111
Calculate the Combined Posterior: Starting Prior: {Beta}(4, 3) The Math:= {Prior } + {Total } y = 4 + 23 = 27 = {Prior } + ({Total } n - {Total } y) = 3 + (111 - 23) = 91 Batch Posterior: {Beta}(27, 91)
The Comparison: How it compares to part a: It is completely identical to the final day-three posterior calculated in Part (a) ({Beta}(27, 91))
What it proves: This proves the rule of sequentiality (order invariance): it doesn’t matter whether you process data piece-by-piece each day or wait and crunch it all at once in a single batch—the mathematical endpoint is always the same.
EXERCISE 5.18
Part a We can use Poisson model becasuse:A Poisson model is specifically designed for count data (discrete, whole numbers representing how many times an event occurs within a fixed space or time). Here, we are counting the number of insects per m^2 area. A Normal model is meant for continuous measurements (like weight, height, or time), whereas Poisson naturally handles non-negative integer counts and rates.
Part (b): Finding the Prior Parameters, Plotting, and Commenting
For a Gamma model: {Mean} = = 0.5 {Standard Deviation} = = 0.25 {Variance} = 0.25^2 = 0.0625 = By solving these two equations: = = = 0.125 r = = 8 Since = 0.5, then $= 0.5 r = 0.5 = 4. Prior Model: {Gamma}(s = 4, r = 8)
Inspected n = 20 separate areas (1m^2 each). Counts were: 3, 2, 5, 1, 2 for the first five, and 0 for the next 15. Total Sum of Counts (y_i): 3 + 2 + 5 + 1 + 2 + 0 { (fifteen times)} = 13 insects total.
Plots
# Plot prior, likelihood, and posterior
plot_gamma_poisson(shape = 4, rate = 8, sum_y = 13, n = 20)
# Get numerical summary (posterior mean, sd, etc.)
summarize_gamma_poisson(shape = 4, rate = 8, sum_y = 13, n = 20)
## model shape rate mean mode var sd
## 1 prior 4 8 0.5000000 0.3750000 0.06250000 0.2500000
## 2 posterior 17 28 0.6071429 0.5714286 0.02168367 0.1472538
Commenting on the Posterior:
The posterior will lean heavily toward the data because we collected a substantial amount of evidence (n = 20 areas inspected), which gives the data enough weight to heavily influence the final model compared to the smaller prior sample size equivalence.
Part (c): Posterior Mean and Standard Deviation
From the output table, Posterior Mean: 0.6071 insects per m^2 Posterior Standard Deviation: 0.1473 (or variance of 0.0217)
Part (d): Describing the Conclusions in Context (for the Biologist)
Based on our field inspection of 20 square meters (which yielded a total of 13 insects), combined with our initial prior assumptions, our updated model estimates that the true insect density is approximately 0.607 insects per m^2 (posterior mean). Furthermore, our standard deviation has shrunk significantly from the prior (down to 0.147), indicating that we have gained much more precision and certainty about the local insect population density after collecting the sample data.