Ch. 1 - What is Bayesian Data Analysis?

A first taste of Bayes

[Video]

Unknowns and ice creams

Bayesian inference is a method for figuring out unknown or unobservable quantities given known facts. In the case of the Enigma machine, Alan Turing wanted to figure out the unknown settings of the wheels and ultimately the meaning of the coded messages.

When analyzing data, we are also interested in learning about unknown quantities. For example, say that we are interested in how daily ice cream sales relate to the temperature, and we decide to use linear regression to investigate this.

  • The slope of the underlying regression line.
  • How much ice cream sales vary on days with a similar temperature.
  • How many ice creams we will sell tomorrow given a forecast of 80°F / 27°C .
  • [*] All of the above.

Let’s try some Bayesian data analysis

[Video]

Probability

  • A number between 0 and 1
  • A statement about certainty / uncertainty
  • 1 is complete certainty something is the case
  • 0 is complete certainty something is not the case
  • Not only about yes/no events

A Bayesian model for the proportion of success

  • prop_model(data)
  • The data is a vector of successes and failures represented by 1s and 0s.
  • There is an unknown underlying proportion of success
  • If data point is a success is only affected by this proportion.
  • Prior to seeing any data, any underlying proportion of success is equally likely.
  • The result is a probability distribution that represents what the model knows about the underlying proportion of success.

Trying out prop_model

data <- c() prop_model(data)

data <- c(0)
prop_model(data)
## Warning: `data_frame()` is deprecated, use `tibble()`.
## This warning is displayed once per session.

Coin flips with prop_model

data <- c(1, 0, 0, 1)
prop_model(data)

Looking at the final probability distribution at n=4, what information does the model have regarding the underlying proportion of heads?

  • [*] It’s most likely around 50%, but there is large uncertainty.
  • It could be anywhere from 0% to 100%.
  • It’s more likely to be above 50%.
  • It is close to 50% with high probability.

Zombie drugs with prop_model

# Update the data and rerun prop_model
data = c(1, 0, 0, 1)
prop_model(data)

# Update the data and rerun prop_model
data = c(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
prop_model(data)

The model implemented in prop_model makes more sense here as we had no clue how good the drug would be. The final probability distribution (at n=13) represents what the model now knows about the underlying proportion of cured zombies. What proportion of zombies would we expect to turn human if we administered this new drug to the whole zombie population?

  • Between 0% to 100%.
  • Between 25% to 75%.
  • [*] Between 5% to 40%.
  • Between 20% to 25%.

Samples and posterior summaries

[Video]

Looking at samples from prop_model

data = c(1, 0, 0, 1, 0, 0, 
         0, 0, 0, 0, 0, 0, 0)

# Extract and explore the posterior
posterior <- prop_model(data)

head(posterior)
## [1] 0.2490093 0.2043833 0.2509618 0.3857773 0.4716986 0.1224137
# Plot the histogram of the posterior
hist(posterior)

# Edit the histogram
hist(posterior, breaks = 30, xlim = c(0, 1), col = "palegreen4")

Summarizing the zombie drug experiment

data = c(1, 0, 0, 1, 0, 0,
         0, 0, 0, 0, 0, 0, 0)
posterior <- prop_model(data)

hist(posterior, breaks = 30, xlim = c(0, 1), col = "palegreen4")

# Calculate the median
median(posterior)
## [1] 0.1878043
# Calculate the credible interval
quantile(posterior, c(0.05, 0.95))
##         5%        95% 
## 0.06113353 0.38891029
# Calculate the probability
sum(posterior > 0.07) / length(posterior)
## [1] 0.931

You’ve done some Bayesian data analysis!

[Video]


Ch. 2 - How does Bayesian inference work?

The parts needed for Bayesian inference

Take a generative model for a spin

Take the binomial distribution for a spin

Using a generative model

How many visitors could your site get (1)?

Representing uncertainty with priors

Adding a prior to the model

Bayesian models and conditioning

Update a Bayesian model with data

How many visitors could your site get (3)?

What have we done?


Ch. 3 - Why use Bayesian Data Analysis?

Four good things with Bayes

Explore using the Beta distribution as a prior

Pick the prior that best captures the information

Change the model to use an informative prior

Contrasts and comparisons

Fit the model using another dataset

Calculating the posterior difference

Decision analysis

A small decision analysis 1

A small decision analysis 2

Change anything and everything

The Poisson distribution

Clicks per day instead of clicks per ad

Bayes is optimal, kind of…


Ch. 4 - Bayesian inference with Bayes’ theorem

Probability rules

Cards and the sum rule

Cards and the product rule

Calculating likelihoods

From rbinom to dbinom

Calculating probabilities with dbinom

Bayesian calculation

Calculating a joint distribution

Conditioning on the data (again)

A conditional shortcut

Bayes’ theorem

A Poisson model description


Ch. 5 - More parameters, more data, and more Bayes

The temperature in a Normal lake

rnorm, dnorm, and the weight of newborns

A Bayesian model of water temperature

A Bayesian model of Zombie IQ

Eyeballing the mean IQ of zombies?

Answering the question: Should I have a beach party?

Sampling from the zombie posterior

But how smart will the next zombie be?

A practical tool: BEST

The BEST models and zombies on a diet

BEST is robust

What have you learned? What did we miss?


About Michael Mallari

Michael is a hybrid thinker and doer—a byproduct of being a StrengthsFinder “Learner” over time. With nearly 20 years of engineering, design, and product experience, he helps organizations identify market needs, mobilize internal and external resources, and deliver delightful digital customer experiences that align with business goals. He has been entrusted with problem-solving for brands—ranging from Fortune 500 companies to early-stage startups to not-for-profit organizations.

Michael earned his BS in Computer Science from New York Institute of Technology and his MBA from the University of Maryland, College Park. He is also a candidate to receive his MS in Applied Analytics from Columbia University.

LinkedIn | Twitter | michaelmallari.com