---
title: "Bayesian Basics"
author:
  - name: "Fan Wang"
    affiliation: "Department of Economics, University of Houston"
    url: "https://fanwangecon.github.io/"
date: "2026-11-30"
---

**Part of [Math Camp: An Introduction to Mathematics and Statistics with Applications in Economics](https://fanwangecon.github.io/Stam4Econ/).**  
[Main Stam4Econ site](https://fanwangecon.github.io/Stam4Econ/) | [View this section in the book](https://fanwangecon.github.io/Stam4Econ/stats_conditional/overview.html#bayesian-basics) | [Author: Fan Wang](https://fanwangecon.github.io/) | [DOI: 10.5281/zenodo.24696926](https://doi.org/10.5281/zenodo.24696926)  
Date: 2026-11-30 | [HTML](https://fanwangecon.github.io/Stam4Econ/htmlpdfq/stats_conditional/bayesian.html) | [PDF](https://fanwangecon.github.io/Stam4Econ/htmlpdfq/stats_conditional/bayesian.pdf) | [QMD](https://fanwangecon.github.io/Stam4Econ/htmlpdfq/stats_conditional/bayesian.qmd)

## Bayesian Basics

### Bayes rule 

The bayes rule states that if $P(A)>0$ and $P(B)>0$, then:

$$
P(A \mid B) = \frac{P(B \mid A) P(A)}{P(B)}
$$

This formula allows us to "reverse" conditional probabilities: it expresses the probability of $A$ given $B$ in terms of the probability of $B$ given $A$, the probability of $A$, and the probability of $B$.

We can further expand the denominator $P(B)$ using the law of total probability. Alternatively, we can justify the equality below using the third property of probability function. If $\{A, A^c\}$ is a partition of the sample space, then:

$$
P(B) = P(B \mid A) P(A) + P(B \mid A^c) P(A^c)
$$

So Bayes' rule can be rewritten as:

$$
P(A \mid B) = \frac{P(B \mid A) P(A)}{P(B \mid A) P(A) + P(B \mid A^c) P(A^c)}
$$

This is the general form of Bayes' rule for partitions. Note that when we write things out, we don't need to know $P(B)$

### Bayes rule mixed discrete-continuous version 

If $A$ is a continuous random variable and $B=b$ is observed, then the continuous Bayes rule is:

$$
f(a\mid b)
=
\frac{f(b\mid a)\,f(a)}{f(b)}.
$$

Here $f(a\mid b)$ is the posterior density of $A$ given $B=b$, $f(b\mid a)$ is the likelihood, and $f(a)$ is the prior density.

The denominator is the marginal density of $B$:

$$
f(b)=\int_{-\infty}^{\infty} f(b\mid a)\,f(a)\,da.
$$

So equivalently,

$$
f(a\mid b)
=
\frac{f(b\mid a)\,f(a)}{\int_{-\infty}^{\infty} f(b\mid a')\,f(a')\,da'}
\propto f(b\mid a)\,f(a).
$$


### Bayes rule mixed discrete + continuous version 

If $A$ is discrete and $B$ is continuous, then for any value $a$ in the support of $A$ and observed $B=b$,

$$
P(A=a\mid B=b)
=
\frac{f(b\mid a)\,P(A=a)}{\sum_{a'} f(b\mid a')\,P(A=a')}.
$$

Here $f(b\mid a)$ is the likelihood density of $B$ given $A=a$, and the denominator is the marginal density of $B$ at $b$.




### Bayes rule application

Suppose there is a firm with the following production function:

$$
\widetilde{Y} = f(K) = A\cdot L^{\alpha}
$$

Suppose I already know the value of $\alpha$ = 0.5, and furthermore that we even know that labor demand for the firm is inelastic, the firm owner just hires herself, so $L$ is fixed at 1, but I don't know what the value of $A$ is. Suppose I know, however that $A \in \left\{1, 2\right\}$. So we are trying to figure out whether $A=1$ or $A=2$, in another word, whether we are dealing with a low type ($A=1$) or a high type ($A=2$) firm. 

If we know $\widetilde{Y}$, we would have our answer immediately. If $A=1$, $Y=1$, and if $A=2$, $Y=2$.

But suppose now that $\widetilde{Y}$ is measured with additive normal error, which means that:

$$
Y = \widetilde{Y} + \epsilon\,,
$$

where $\epsilon$ is normal with mean $\mu_{\epsilon}$ and standard deviation $\sigma_{\epsilon}$.

We will observe a sequence of observed revenue levels, $\left\{Y\right\}_{i=1}^n$, where each $Y_i$ is an independent measurement of the firm's revenue. And we want to update our belief on whether $A=1$ or $A=2$ based on these observations.

Note that $P(A=1) = 1 - P(A=2)$, I believe $A$ is either 1 or 2, so the probability that it is either must sum up to 1. 

Also note that we have the exact density function for $f(Y_i \mid A)$:

$$
f(Y_i \mid A=a) = \phi(Y_i - a\cdot L^{\alpha})
$$

Since $A$ is discrete here, we do **not** use a posterior density $f(A\mid Y_i)$. Instead, we use posterior probabilities:

$$
P(A=a\mid Y_i=y_i).
$$

### Prior distribution, likelihood, and posterior distribution

The **prior distribution** is what we believe about $A$ before seeing the data. Since $A\in\{1,2\}$, the prior is just two numbers:

$$
P(A=1)=\pi_1,\qquad P(A=2)=\pi_2,
$$

with $\pi_1+\pi_2=1$. If we have no reason to favor one value over the other, we might choose $\pi_1=\pi_2=1/2$.

The **likelihood** is the data model: for each possible value of $A$, it tells us how likely it is to observe $Y_i$.

$$
f(Y_i\mid A=1),\qquad f(Y_i\mid A=2).
$$

The **posterior distribution** is the updated belief after seeing the data. Bayes' rule combines the prior and the likelihood:

$$
P(A=a\mid Y_i=y_i)
=
\frac{f(y_i\mid A=a)\,P(A=a)}{\sum_{a'\in\{1,2\}} f(y_i\mid A=a')\,P(A=a')}.
$$

So the posterior is just the prior reweighted by how well each candidate value of $A$ explains the observed data.

Why does the numerator matter so much? Because the denominator does **not** depend on the candidate value $a$. For a fixed observation $y_i$, the term

$$
\sum_{a'\in\{1,2\}} f(y_i\mid A=a')\,P(A=a')
$$

is the same no matter whether we are computing the posterior for $A=1$ or for $A=2$. It is only there to make the posterior probabilities add up to 1. So the denominator is the **normalizing constant**.

That means the relative size of the posterior probabilities is determined entirely by the numerator:

$$
f(y_i\mid A=a)\,P(A=a).
$$

So when we compare two possible values, say $A=1$ and $A=2$, we can write

$$
P(A=1\mid Y_i=y_i)\propto f(y_i\mid A=1)P(A=1),
$$

and

$$
P(A=2\mid Y_i=y_i)\propto f(y_i\mid A=2)P(A=2).
$$

This is why the proportional form is useful: it tells us which candidate value of $A$ gets more posterior weight before we normalize.

This is the key updating idea: after seeing one observation, the posterior becomes the new prior for the next observation. So if we first observe $Y_1=y_1$, then we use

$$
P(A=a\mid Y_1=y_1)
$$

as the starting belief for updating again when $Y_2=y_2$ arrives.

This is why the formula looks a little different from the one-shot Bayes rule. The basic Bayes rule uses the original prior $P(A=a)$ on the right-hand side because it describes one update from scratch. In the sequential version, we simply apply the same rule again, but now the prior has already been updated by the earlier data. So the posterior from the first observation is the prior for the second observation, the posterior from the second observation is the prior for the third observation, and so on.

In sequential form, the update after the second observation is

$$
P(A=a\mid Y_1=y_1,Y_2=y_2)
=
\frac{f(y_2\mid A=a)\,P(A=a\mid Y_1=y_1)}{\sum_{a'\in\{1,2\}} f(y_2\mid A=a')\,P(A=a'\mid Y_1=y_1)}.
$$

To see where the product form comes from, start with the first observation:

$$
P(A=a\mid Y_1=y_1)
=
\frac{f(y_1\mid A=a)\,P(A=a)}{\sum_{a'\in\{1,2\}} f(y_1\mid A=a')\,P(A=a')}.
$$

Now plug this posterior into the second-step formula. The numerator becomes

$$
f(y_2\mid A=a)\,P(A=a\mid Y_1=y_1)
=
f(y_2\mid A=a)\,
\frac{f(y_1\mid A=a)\,P(A=a)}{\sum_{a'\in\{1,2\}} f(y_1\mid A=a')\,P(A=a')}.
$$

This shows that the likelihood terms multiply: one factor for $y_1$ and one factor for $y_2$. The denominator only serves to normalize the probabilities, so after simplifying we get

$$
P(A=a\mid Y_1=y_1,Y_2=y_2)
=
\frac{P(A=a)\,f(y_1\mid A=a)\,f(y_2\mid A=a)}{\sum_{a'\in\{1,2\}} P(A=a')\,f(y_1\mid A=a')\,f(y_2\mid A=a')}.
$$

If the observations are conditionally independent given $A$, then the same logic continues for $n$ observations. The joint likelihood is the product of the individual likelihoods:

$$
f(y_1,\dots,y_n\mid A=a)=\prod_{i=1}^n f(y_i\mid A=a).
$$

That is why the product symbol appears in the multi-data Bayes formula: each new observation contributes one more likelihood factor, and conditional independence lets us multiply them together.

### How the posterior updates the prior

After we observe one data point $Y_i=y_i$, the posterior becomes our new belief about $A$. In other words, the posterior from one step is the prior for the next step.

If the observations $Y_1,\dots,Y_n$ are independent conditional on $A$, then after all $n$ observations we have

$$
P(A=a\mid Y_1=y_1,\dots,Y_n=y_n)
=
\frac{P(A=a)\prod_{i=1}^n f(y_i\mid A=a)}{\sum_{a'\in\{1,2\}} P(A=a')\prod_{i=1}^n f(y_i\mid A=a')}.
$$

This formula shows the updating rule clearly: start with the prior, multiply by the likelihood for each observation, and then normalize so the posterior probabilities add up to 1.

### Numerical illustration with three observed values

To make the updating process concrete, let us choose a very simple numerical example.

- Prior belief: $P(A=1)=0.5$ and $P(A=2)=0.5$.
- Let $L=1$, $\alpha=0.5$, and assume $\epsilon\sim N(0,1)$.
- Then the conditional density is

$$
f(y_i\mid A=a)=\phi(y_i-a),
$$

because the mean under $A=a$ is just $a\cdot L^{\alpha}=a$.

Suppose we observe three data points:

$$
y_1=1.2,\qquad y_2=0.4,\qquad y_3=1.6.
$$

We now update step by step.

**Step 1: update after observing $y_1=1.2$.**

$$
P(A=1\mid y_1)
=
\frac{0.5\,\phi(1.2-1)}{0.5\,\phi(1.2-1)+0.5\,\phi(1.2-2)}
=
\frac{\phi(0.2)}{\phi(0.2)+\phi(-0.8)}
\approx 0.574.
$$

So after the first observation, the posterior puts about $57.4\%$ probability on $A=1$ and $42.6\%$ on $A=2$.

**Step 2: use that posterior as the new prior and update with $y_2=0.4$.**

Before seeing $y_2$, our current belief is the posterior from Step 1:

$$
P(A=1\mid y_1)\approx 0.574,\qquad P(A=2\mid y_1)\approx 0.426.
$$

Now update again:

$$
P(A=1\mid y_1,y_2)
=
\frac{P(A=1\mid y_1)\,\phi(0.4-1)}{P(A=1\mid y_1)\,\phi(0.4-1)+P(A=2\mid y_1)\,\phi(0.4-2)}
\approx 0.802.
$$

After the second observation, the evidence shifts more strongly toward $A=1$.

**Step 3: update once more with $y_3=1.6$.**

Using the Step 2 posterior as the new prior,

$$
P(A=1\mid y_1,y_2,y_3)
=
\frac{P(A=1\mid y_1,y_2)\,\phi(1.6-1)}{P(A=1\mid y_1,y_2)\,\phi(1.6-1)+P(A=2\mid y_1,y_2)\,\phi(1.6-2)}
\approx 0.786.
$$

So after all three observations, the final posterior belief is about $78.6\%$ on $A=1$ and $21.4\%$ on $A=2$.

This illustrates the key idea of Bayesian updating: the prior is the starting belief, each observation updates that belief through the likelihood, and the posterior from one round becomes the prior for the next round.

So the answer to $P(A\mid Y_i)$ is the posterior probability above, and it is the object we use to update our prior belief about $A$.




