Bayesian Learning#

Bayesian Learning#

Exercise 4.1 (Reservation Wage)#

Prove that

\[\begin{align*} \int_{w_r}^{\infty}(w-w_r)dH(w)=\int_{w_r}^{\infty}[1-H(w)]dw. \end{align*}\]
Solution

We start by simplifying the left-hand side. Let \(F(w) = w - w_r\), the integration by parts formula implies that

\[\begin{align*} \int_{w_r}^{\infty} (w - w_r) dH(w) &= \int_{w_r}^{\infty} F(w) dH(w)\\ &=\left. F(w) H(w) \right|_{w_r}^{\infty} - \int_{w_r}^{\infty} H(w) dF(w)\\ &=\left. (w - w_r)H(w) \right|_{w_r}^{\infty} - \int_{w_r}^{\infty} H(w) dw. \end{align*}\]

Since \(\left. (w - w_r)H(w) \right|_{w_r}^{\infty}=\int_{w_r}^{\infty} dw\), the expression above is indeed equal to \(\int_{w_r}^{\infty} [1-H(w)] dw\).

Exercise 4.2 (Normal-Normal conjugacy)#

Let \(y\) be a random variable that is drawn from a normal distribution with mean \(\mu\) and standard deviation \(\sigma\).

  1. Consider a dataset \(Y=(y_1,y_2,...,y_n)\) containing \(n\) realizations of \(y\). Write down the likelihood of \(Y\) as a function of \(\mu\) and \(\sigma\)

  2. Assume that \(\mu\) itself is Normally distributed around some mean \(\theta\) with standard deviation \(\tau\). Show that upon observing \(Y\), the posterior belief about \(\mu\) remains normally distributed. Derive the posterior mean and standard deviation of \(\mu\). What do you notice about the standard deviation? Comment your finding.

Solution
  1. The likelihood of \(Y\) can be written as

\[\begin{align*} L\left(\mu, \sigma | Y \right) &= \prod_{i = 1}^{n} f\left(y_{i}|\mu, \sigma\right) \\ &= \prod_{i = 1}^{n} \frac{1}{\sqrt{2\pi \sigma^{2}}}\text{exp}\left(-\frac{\left(y_{i}-\mu\right)^{2}}{2\sigma^{2}}\right). \end{align*}\]
  1. By Bayes’ rule, we find the posterior distribution of \(\mu\) given the data \(Y\):

\[\begin{align*} p\left(\mu|Y,\theta,\tau,\sigma\right) &\propto p\left(Y|\mu,\sigma\right)p\left(\mu|\theta,\tau\right)\\ & \propto \text{exp}\left(-\frac{1}{2}\left[\frac{1}{\sigma^{2}}\sum_{i=1}^{n}\left(y_{i}-\mu\right)^{2} + \frac{\left(\mu-\theta\right)^{2}}{\tau^{2}}\right]\right). \end{align*}\]

Let \(\overline{y} = \frac{1}{n}\sum_{i=1}^{n}y_{i}\), by the Law of Total Variance, we have

\[\begin{equation*} \sum_{i=1}^{n}\left(y_{i}-\mu\right)^{2} = \sum_{i=1}^{n}\left(y_{i} - \overline{y}\right)^{2} + n\left(\overline{y}-\mu\right)^{2}. \end{equation*}\]

Thus the posterior simplifies to:

\[\begin{equation*} p\left(\mu|Y,\theta,\tau,\sigma\right) \propto \text{exp}\left(-\frac{1}{2}\left[\frac{n\left(\overline{y}-\mu\right)^{2}}{\sigma^{2}} + \frac{\left(\mu-\theta\right)^{2}}{\tau^2}\right]\right). \end{equation*}\]

The exponent can be written as:

\[\begin{equation*} \left(\mu - \frac{\frac{\overline{y}}{\sigma^{2}} + \frac{\theta}{\tau^{2}}}{\frac{n}{\sigma^{2}} + \frac{1}{\tau^{2}}}\right)^{2}*\left(\frac{n}{\sigma^{2}} + \frac{1}{\tau^{2}}\right). \end{equation*}\]

From this, we can conclude that the posterior mean \(\mu_{post}\) and the posterior variance \(\sigma^{2}_{post}\) read

\[\begin{align*} & \mu_{post} = \frac{\frac{\overline{y}}{\sigma^{2}} + \frac{\theta}{\tau^{2}}}{\frac{n}{\sigma^{2}} + \frac{1}{\tau^{2}}}, \\ & \sigma^{2}_{post} = \frac{1}{\frac{n}{\sigma^{2}} + \frac{1}{\tau^{2}}}. \end{align*}\]

Note that the posterior variance is smaller than the prior variance, reflecting the fact that, after observing the data, we have more precise information about \(\mu\).

Exercise 4.3 (Bernouilli)#

Let \(y\) be a random variable that is drawn from a Bernouilli distribution with probability of success \(p\).

  1. Consider a dataset \(Y=(y_1,y_2,...,y_n)\) containing \(n\) realizations of \(y\). Write down the likelihood of \(Y\) as a function of \(p\).

  2. Assume that \(p\) itself is drawn from the following distribution

\[\begin{equation*} p=\left\{ \begin{array}{l} p_{h}\text{ with probability } \mu _{0}, \\ p_{l}\text{ with probability } 1-\mu _{0}. \end{array}% \right. \end{equation*}\]

The variable \(\mu_0\) denotes the prior in period \(0\). Use Bayes rule to express the agent’s posterior \(μ_1(0)\) at the beginning of period \(2\) after having observed a failure in period \(1\), i.e. \(y_1=0\). Iterate the computation to derive the posterior after \(n\) failures in a row.

Solution
  1. The likelihood of \(Y\) reads

\[\begin{equation*} L\left(p | Y \right) = \prod_{i = 1}^{n} p^{y_{i}}(1-p)^{1-y_{i}}. \end{equation*}\]
  1. By Baye’s rule, we can express \(μ_1(0)\) as:

\[\begin{align*} μ_1(0) = p(p_{h}|y_{1}=0) & = \frac{p(y_{1}=0|p_{h})\mu_{0}}{p(y_{1}=0|p_{h})\mu_{0} + p(y_{1}=0|p_{l})(1-\mu_{0})}\\ & = \frac{(1-p_{h})\mu_{0}}{(1-p_{h})\mu_{0} + (1-p_{l})(1-\mu_{0})}. \end{align*}\]

The posterior after \(n\) failures could be expressed as:

\[\begin{align*} μ_n(0^n) = p(p_{h}|y_{1}=0, y_{2}=0,...,y_{n}=0) & = \frac{p(y_{1}=0, y_{2}=0,...,y_{n}=0|p_{h})\mu_{0}}{p(y_{1}=0, y_{2}=0,...,y_{n}=0|p_{h})\mu_{0} + p(y_{1}=0, y_{2}=0,...,y_{n}=0|p_{l})(1-\mu_{0})}\\ & = \frac{(1-p_{h})^{n}\mu_{0}}{(1-p_{h})^{n}\mu_{0} + (1-p_{l})^{n}(1-\mu_{0})}. \end{align*}\]

Note that \(μ_n\) can be expressed recursively

\[\begin{align*} μ_n(0) = \frac{(1-p_{h})\mu_{n-1}}{(1-p_{h})^{n}\mu_{n-1} + (1-p_{l})^{n}(1-\mu_{n-1})}. \end{align*}\]

Exercise 4.5 (The Exact Gittins Index)#

We consider a two-armed bandit. Arm 2 is known: it pays \(1\) with probability \(p_2 = 0.7\). Arm 1 is unknown: it pays \(1\) with probability \(p_1 \in \{0.9, 0.1\}\), with prior \(\Pr(p_1 = 0.9) = 1/2\). The discount factor is \(\beta = 0.9\). We write \(R_t \in \{0, 1\}\) for the reward of the \(t\)-th pull of arm 1 (\(t = 0, 1, \dots\)).

The Gittins index of arm 1 in belief state \(s\) (here \(s\) is the prior, before any pull) is

\[ \Lambda(s) = \sup_{\tau \ge 0} \frac{\mathbb{E}\left[\sum_{t=0}^{\tau}\beta^t R_t \,\middle|\, s\right]}{\mathbb{E}\left[\sum_{t=0}^{\tau}\beta^t \,\middle|\, s\right]}. \]

In the slides, the rule “stop after a loss, never stop after a win” gave the lower bound \(\Lambda(s) \ge 0.762\). We now compute \(\Lambda(s)\) exactly. For a fee \(\lambda\), define

\[ G(\lambda) = \sup_{\tau \ge 0}\, \mathbb{E}\left[\sum_{t=0}^{\tau}\beta^t (R_t - \lambda) \,\middle|\, s\right]. \]

\(G(\lambda)\) is the best expected profit of a gambler who pays \(\lambda \in [0, 1]\) for each pull of arm 1, must pull at least once, and may quit after any pull.

  1. Show that \(\Lambda(s) > \lambda\) if and only if \(G(\lambda) > 0\). Deduce that \(\Lambda(s)\) is the unique root of \(G(\Lambda(s)) = 0\).

  2. Write down a Bellman equation that computes \(G(\lambda)\) by dynamic programming. (Hint: show that the posterior only depends on \(k = \text{wins} - \text{losses}\).) Use it to compute \(\Lambda(s)\) numerically.

  3. Characterize the optimal stopping rule \(\tau^*\) and compare it with the rule used in the slides.

Solution

1. \(\Lambda(s) > \lambda\) if and only if \(G(\lambda) > 0\)

Fix a stopping time \(\tau\) and write

\[ N(\tau) = \mathbb{E}\left[\sum_{t=0}^{\tau}\beta^t R_t \,\middle|\, s\right], \qquad D(\tau) = \mathbb{E}\left[\sum_{t=0}^{\tau}\beta^t \,\middle|\, s\right] . \]

Step 1: \(1 \le D(\tau) \le 1/(1-\beta)\). The sum \(\sum_{t=0}^{\tau}\beta^t\) always contains the term \(\beta^0 = 1\), all other terms are nonnegative, and the full geometric series sums to \(1/(1-\beta)\).

Step 2: fixed \(\tau\). Since \(D(\tau) > 0\), multiplying by \(D(\tau)\) preserves the inequality:

\[ \frac{N(\tau)}{D(\tau)} > \lambda \iff N(\tau) - \lambda D(\tau) > 0 , \]

and by linearity \(N(\tau) - \lambda D(\tau) = \mathbb{E}\left[\sum_{t=0}^{\tau}\beta^t (R_t - \lambda) \,\middle|\, s\right]\) is the profit of \(\tau\) at fee \(\lambda\).

Step 3: suprema. For any set \(A\), \(\sup A > \lambda\) iff some element of \(A\) exceeds \(\lambda\) (this needs the strict inequality). Applying this to the ratios and to the profits:

\[ \Lambda(s) > \lambda \iff \exists\, \tau:\ \frac{N(\tau)}{D(\tau)} > \lambda \overset{\text{Step 2}}{\iff} \exists\, \tau:\ N(\tau) - \lambda D(\tau) > 0 \iff G(\lambda) > 0 . \qquad \square \]

Consequence. Raising the fee by \(\delta > 0\) lowers the profit of every rule by \(\delta\, D(\tau)\), which by Step 1 lies between \(\delta\) and \(\delta/(1-\beta)\). Taking suprema,

\[ G(\lambda) - \frac{\delta}{1-\beta} \;\le\; G(\lambda + \delta) \;\le\; G(\lambda) - \delta , \]

so \(G\) is continuous and strictly decreasing. By the equivalence above, \(G(\lambda) > 0\) for \(\lambda < \Lambda(s)\) and \(G(\lambda) \le 0\) for \(\lambda \ge \Lambda(s)\). By continuity, \(\Lambda(s)\) is the unique root of

\[ G(\Lambda(s)) = 0 . \]

Interpretation. \(\Lambda(s)\) is the fair charge of arm 1: the largest per-pull fee at which a gambler is still willing to play. This “calibration” view of the index goes back to Gittins and is the basis of Weber’s (1992) proof of the index theorem.

Why this helps. The ratio is hard to maximize directly. The profit is a sum of rewards, so maximizing it over \(\tau\) is a standard optimal stopping problem, which dynamic programming solves.

Link with the slides. Under our simple rule, we pull once. After a loss (probability \(1/2\)) we stop. After a win (probability \(1/2\)) the win probability becomes \(0.82\) (see part 2), and we pull forever, which adds \(\beta/(1-\beta) = 9\) discounted pulls. Hence

\[ N = 0.5 + 0.5 \times 9 \times 0.82 = 4.19, \qquad D = 1 + 0.5 \times 9 = 5.5 . \]

With \(\lambda = 0.7\) its profit is \(4.19 - 0.7 \times 5.5 = 0.34 > 0\), so \(G(0.7) \ge 0.34 > 0\) and \(\Lambda(s) > 0.7\), as found in the slides.


2. Computing \(G(\lambda)\) by dynamic programming

The state

Each win multiplies the odds of “good” by \(0.9/0.1 = 9\), and each loss divides them by \(9\). With a prior of \(1/2\), the posterior depends only on

\[ k = \text{wins} - \text{losses}, \qquad q_k = \Pr(p_1 = 0.9 \mid k) = \frac{9^k}{1 + 9^k}. \]

The next pull wins with probability

\[ r_k = 0.9\, q_k + 0.1\,(1 - q_k) = 0.1 + 0.8\, q_k . \]

\(k\)

\(-2\)

\(-1\)

\(0\)

\(1\)

\(2\)

\(3\)

\(q_k\)

0.012

0.100

0.500

0.900

0.988

0.999

\(r_k\)

0.110

0.180

0.500

0.820

0.890

0.899

Let \(k_t\) denote the state before pull \(t\), so \(k_0 = 0\) (the prior \(s\)). After a pull in state \(k\), the state moves to \(k+1\) with probability \(r_k\) (win) and to \(k-1\) with probability \(1 - r_k\) (loss).

The Bellman equation

A stopping rule is a choice, in every state, between continuing and stopping. Fix the fee \(\lambda\) and let \(V_\lambda(k)\) be the best expected profit when we pull in state \(k\) and then behave optimally. Then

\[ V_\lambda(k) = r_k - \lambda + \beta\Big[\, r_k \max\big(0,\, V_\lambda(k+1)\big) + (1 - r_k)\max\big(0,\, V_\lambda(k-1)\big) \Big]. \]

The term \(\max(0, \cdot)\) is the stopping decision: after this pull we continue only if continuing is worth more than stopping, which is worth \(0\). Since we start at \(k_0 = 0\), the answer to the question is \(G(\lambda) = V_\lambda(0)\).

Why this finds the supremum over all stopping times

Solve the equation by value iteration, starting from \(V_1(k) = r_k - \lambda\):

\[ V_{n+1}(k) = r_k - \lambda + \beta\Big[\, r_k \max\big(0,\, V_n(k+1)\big) + (1 - r_k)\max\big(0,\, V_n(k-1)\big) \Big]. \]

By backward induction, \(V_n(k)\) is exactly the best profit over all stopping rules that pull at most \(n\) times. We never enumerate stopping rules: the optimal one is assembled from the best decision in each state.

Any stopping rule can be truncated after \(n\) pulls. Since \(R_t \in \{0,1\}\) and \(\lambda \in [0,1]\), we have \(|R_t - \lambda| \le 1\), so truncation costs at most \(\sum_{t \ge n} \beta^t = \beta^n/(1-\beta)\). Hence \(V_n(0) \to G(\lambda)\), the supremum over all stopping times.

Worked example: \(\lambda = 0.7\)

\(n = 1\) (pull once, then stop). \(V_1(k) = r_k - 0.7\):

\[ V_1(-1) = -0.52, \quad V_1(0) = -0.20, \quad V_1(1) = 0.12, \quad V_1(2) = 0.19 . \]

\(n = 2\). A second pull is only worth taking after a win (\(V_1(1) > 0 > V_1(-1)\)):

\[ V_2(0) = -0.20 + 0.9\,\big[\,0.5 \times 0.12 + 0.5 \times 0\,\big] = -0.146 . \]

We also need \(V_2(1)\) for the next step. From \(k = 1\), a win leads to \(k = 2\) and a loss to \(k = 0\):

\[ V_2(1) = 0.12 + 0.9\,\big[\,0.82 \times 0.19 + 0.18 \times \max(0, -0.20)\,\big] = 0.260 . \]

\(n = 3\).

\[ V_3(0) = -0.20 + 0.9\,\big[\,0.5 \times 0.260 + 0.5 \times 0\,\big] = -0.083 . \]

Further iterations.

\(n\)

1

2

3

4

5

\(\infty\)

\(V_n(0)\)

\(-0.200\)

\(-0.146\)

\(-0.083\)

\(-0.026\)

\(+0.026\)

\(0.541\)

A rule with at most 5 pulls already makes a profit at fee \(0.7\). With no limit, the best profit is \(G(0.7) = 0.541\), larger than the \(0.34\) of our simple rule, as it must be.

Note how slowly \(V_n(0)\) converges: most of the profit comes from pulls far in the future. With \(\beta = 0.9\) the effective horizon is about \(1/(1-\beta) = 10\) pulls, and once arm 1 looks good it is worth exploiting for a long time. This is why the slides’ rule, which commits to arm 1 forever after a first win, already does quite well.

Finding \(\Lambda(s)\)

Repeat the computation for several fees:

\(\lambda\)

0.70

0.75

0.80

0.81

0.85

\(G(\lambda)\)

0.541

0.286

0.030

\(-0.020\)

\(-0.208\)

\(G\) changes sign between \(0.80\) and \(0.81\). Bisection on \(\lambda\) converges to

\[ \boxed{\Lambda(s) \approx 0.806} \]

The index of the known arm 2 is simply \(p_2 = 0.7\). By the Gittins index theorem, it is optimal to pull the arm with the highest index. Since \(0.806 > 0.7\), we should start by pulling arm 1, even though its expected winning probability is only \(0.5\). More generally, arm 1 should be tried first against any known arm with \(p_2 < 0.806\). The gap between \(0.806\) and \(0.5\) is the value of information: a win reveals that arm 1 is probably good, and that knowledge can be exploited for many periods.


3. The optimal stopping rule

At \(\lambda = \Lambda(s) \approx 0.806\), the solution of the Bellman equation gives

\(k\)

\(-2\)

\(-1\)

\(0\)

\(1\)

\(2\)

\(V_\lambda(k)\)

\(-0.696\)

\(-0.626\)

\(0.000\)

\(0.680\)

\(0.902\)

It is optimal to continue where \(V_\lambda(k) > 0\) and to stop where \(V_\lambda(k) < 0\). At \(k = 0\) the gambler is indifferent, which is exactly what defines the index. An optimal rule is therefore

\[ \tau^* = \min\{\, t : k_{t+1} < 0 \,\}: \]

keep pulling arm 1 as long as it has won at least as often as it has lost. Its ratio equals the index:

\[ \frac{\mathbb{E}\left[\sum_{t=0}^{\tau^*}\beta^t R_t\right]}{\mathbb{E}\left[\sum_{t=0}^{\tau^*}\beta^t\right]} \approx 0.806 . \]

Because of the indifference at \(k = 0\), the optimal rule is not unique. The rule “stop as soon as wins no longer exceed losses”, \(\tau^{**} = \min\{\, t : k_{t+1} \le 0 \,\}\), reaches the same ratio \(0.806\). Both rules make the same decisions in every state except \(k = 0\), where stopping and continuing are worth the same.

Comparison of rules.

Stopping rule

Ratio

\(\tau = 0\) (pull once)

0.500

\(\tau = \infty\) (pull forever)

0.500

Stop after a first loss, never stop after a first win (slides)

0.762

Keep pulling while wins \(\ge\) losses (optimal)

0.806

The rule in the slides was close, but too stubborn: after a first win it never quits, even when later losses suggest that arm 1 is bad.


Appendix: Python code

import numpy as np

beta, K = 0.9, 200                      # states k = -K, ..., K
k = np.arange(-K, K + 1)
q = 1 / (1 + 9.0 ** (-k))               # P(good | k)
r = 0.1 + 0.8 * q                       # P(win | k)

def V(lam, iters=500):
    """Value iteration for V_lam(k); returns the vector over k = -K..K."""
    v = np.zeros_like(r)
    for _ in range(iters):
        c = np.maximum(v, 0)            # continue only if profitable
        # Edges: at k = +-K the state is assumed to stay put. This truncation
        # is harmless since |k| = K = 200 is far from the start k = 0.
        up = np.append(c[1:], c[-1])    # value after a win  (k+1)
        dn = np.insert(c[:-1], 0, c[0]) # value after a loss (k-1)
        v = r - lam + beta * (r * up + (1 - r) * dn)
    return v

lo, hi = 0.5, 0.9                       # bisection on the fee: G(lam) = V(lam)[K]
for _ in range(30):
    mid = (lo + hi) / 2
    lo, hi = (mid, hi) if V(mid)[K] > 0 else (lo, mid)

print(f"Gittins index: {lo:.4f}")       # 0.8058
print("V(k), k=-2..2:", np.round(V(lo)[K - 2:K + 3], 3))