Introduction to Bayesian Data Analysis

krz · July 6, 2023

Bayesian statistics is a powerful and elegant framework for data analysis that allows us to incorporate prior knowledge, uncertainty, and learning from data into our statistical reasoning. In this entry, I will introduce the conceptual foundations of Bayesian data analysis and show how it can help us solve challenging problems that classical statistics may struggle with.

One of the key concepts in Bayesian statistics is that of Bayesian inference, which is the process of updating our beliefs about unknown quantities based on new data and evidence. Bayesian inference can be seen as a reallocation of credibility across different possibilities, where each possibility is assigned a probability that reflects how plausible or likely it is given the available information.

To illustrate this idea, let us consider the famous Monty Hall Problem, which is a probability puzzle based on a game show scenario. Suppose you are on a game show where you have to choose one of three doors. Behind one door is a car, and behind the other two doors are goats. After you make your choice, the host, who knows what is behind each door, opens one of the other doors and reveals a goat. He then offers you the chance to switch your choice to the remaining unopened door. Should you switch or stay with your original choice?

If you are familiar with this problem, you may know that the correct answer is to switch, because it gives you a 2/3 chance of winning the car, while staying gives you only a 1/3 chance. But why is this the case? How can we use Bayesian inference to justify this answer?

To apply Bayesian inference, we need to define some terms:

  • Let H be the hypothesis that the door you initially picked has the car behind it.
  • Let E be the evidence that the host opened another door with a goat behind it.
  • Let \(P(H)\) be the prior probability of H, i.e., the probability that you picked the right door before seeing any evidence.
  • Let \(P(E\mid H)\) be the likelihood of E given H, i.e., the probability that the host opened another door with a goat behind it if you picked the right door.
  • Let \(P(H\mid E)\) be the posterior probability of H given E, i.e., the probability that you picked the right door after seeing the evidence.

Using Bayes’ theorem, we can write:

\[P(H|E) = \frac{P(E|H)P(H)}{P(E)}\]

We can calculate each term as follows:

  • \(P(H)\) = 1/3, since there are three doors and only one has the car.
  • \(P(E\mid H)\) = 1/2, since if you picked the right door, the host can open either of the remaining doors with equal probability.
  • \(P(E)\) = 1/2, since regardless of your initial choice, there are two doors with goats and one with a car, and the host will always open one with a goat.
  • \(P(H\mid E)\) = (1/2)(1/3)/(1/2) = 1/3.

Therefore, the posterior probability of your initial choice being correct is still 1/3, even after seeing the evidence. This means that switching to the other remaining door will increase your probability of winning to 2/3, since there are only two possibilities left and one of them is wrong.

What we have done here is to use Bayesian inference to reallocate our credibility across two possibilities: staying or switching. Before seeing any evidence, we had no reason to favor one door over another, so we assigned equal probabilities (or credibilities) to each door. After seeing the evidence, we updated our probabilities based on how likely each possibility was given the new information. We found that switching was more likely than staying, so we assigned more credibility to switching.

This example shows how Bayesian inference can help us make rational decisions based on data and prior knowledge. But what if we have more than two possibilities? What if we have a complex model with many parameters that describe some phenomenon of interest? How can we use Bayesian inference to learn about these parameters and make predictions or comparisons?

This leads us to another key concept in Bayesian statistics: that of Bayesian modeling, which is the process of constructing and evaluating mathematical models that represent our assumptions and hypotheses about some data-generating process. Bayesian modeling allows us to express our prior knowledge and uncertainty about unknown parameters using probability distributions, and then update these distributions using data and Bayes’ theorem. The resulting distributions are called posterior distributions, and they reflect our updated beliefs about the parameters after seeing the data.

Bayesian modeling has many advantages over classical modeling approaches, such as:

  • It can handle complex models with many parameters and hierarchical structures.
  • It can incorporate prior information from different sources and account for different levels of uncertainty.
  • It can provide a full picture of parameter uncertainty and variability using posterior distributions, rather than point estimates and confidence intervals.
  • It can compare and evaluate different models using Bayesian model selection or model averaging techniques.
  • It can make predictions and decisions based on expected utility or loss functions.

One of the advantages of Bayesian modeling is that it can handle complex models with many parameters and hierarchical structures. Hierarchical models are models that have parameters that vary across different levels or groups of data, and that are themselves influenced by other parameters. For example, suppose we want to model the test scores of students from different schools, and we assume that the scores depend on the students’ abilities, the schools’ effects, and some random noise. A hierarchical model for this problem would look something like this:

\[\text{score}_{ij} \sim N(\text{ability}_i + \text{school effect}_j, \sigma^2)\] \[\text{ability}_i \sim N(\mu_a, \tau_a^2)\] \[\text{school effect}_j \sim N(\mu_s, \tau_s^2)\]

where i indexes the students and j indexes the schools. The score of each student is modeled as a normal distribution with a mean that depends on their ability and their school effect, and a variance that captures the measurement error. The ability of each student is also modeled as a normal distribution with a mean and a variance that represent the overall ability level and variation across students. Similarly, the school effect of each school is modeled as a normal distribution with a mean and a variance that represent the overall school quality and variation across schools.

This model has many parameters: the score variance \(\sigma^2\), the ability mean and variance \(\mu_a\) and \(\tau_a^2\), and the school effect mean and variance \(\mu_s\) and \(\tau_s^2\). In addition, there are as many ability parameters as there are students, and as many school effect parameters as there are schools. How can we estimate these parameters from the data?

Using Bayesian modeling, we can assign prior distributions to each parameter, reflecting our prior beliefs or assumptions about their values. For example, we may use normal priors for the means, inverse-gamma priors for the variances, or other distributions that are appropriate for the problem. Then, we can use Bayes’ theorem to update these priors using the data and obtain posterior distributions for each parameter. These posterior distributions will tell us how likely each parameter value is given the data and the priors.

However, calculating these posterior distributions analytically may be very difficult or impossible for complex models like this. This is where Markov chain Monte Carlo (MCMC) algorithms come in handy. MCMC algorithms are methods for generating samples from posterior distributions using stochastic processes that converge to the target distributions in the long run. By generating many samples from the posterior distributions, we can approximate their properties, such as means, variances, quantiles, or probabilities. For example, we can use MCMC to estimate the posterior mean and credible interval of each parameter, which will give us an idea of their most likely values and their uncertainty.

There are many types of MCMC algorithms, such as Metropolis-Hastings, Gibbs sampling, Hamiltonian Monte Carlo, or No-U-Turn Sampler. Each algorithm has its own advantages and disadvantages, depending on the model structure, the data size, and the computational resources available. Fortunately, there are also many software packages and libraries that implement these algorithms and make Bayesian modeling easier and faster. Some examples are Stan, PyMC3, JAGS, or BUGS.

Using these tools, we can fit complex Bayesian models to various problems in data analysis, such as regression, classification, clustering, dimensionality reduction, or causal inference. We can also compare and evaluate different models using Bayesian model selection or model averaging techniques, which are based on comparing the posterior probabilities or predictive performances of different models. We can also make predictions and decisions based on expected utility or loss functions, which are functions that quantify how good or bad an outcome is based on our preferences or goals.

Twitter, Facebook