Bayesian Learning#
Bayesian Learning#
Exercise 4.1 (Reservation Wage)#
Prove that
Solution
We start by simplifying the left-hand side. Let \(F(w) = w - w_r\), the integration by parts formula implies that
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\).
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\)
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
The likelihood of \(Y\) can be written as
By Bayes’ rule, we find the posterior distribution of \(\mu\) given the data \(Y\):
Let \(\overline{y} = \frac{1}{n}\sum_{i=1}^{n}y_{i}\), by the Law of Total Variance, we have
Thus the posterior simplifies to:
The exponent can be written as:
From this, we can conclude that the posterior mean \(\mu_{post}\) and the posterior variance \(\sigma^{2}_{post}\) read
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\).
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\).
Assume that \(p\) itself is drawn from the following distribution
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
The likelihood of \(Y\) reads
By Baye’s rule, we can express \(μ_1(0)\) as:
The posterior after \(n\) failures could be expressed as:
Note that \(μ_n\) can be expressed recursively
Exercise 4.4 (Job Search)#
We consider a labor market where workers are either employed or searching for a job. Time is discrete and agents discount the future at rate \(r\), so that their discount factor \(\beta =1/(1+r).\) When a firm meets a job seeker, it offers her a constant wage contract and the firm-employee relationship lasts forever.
At the end of each period:
(i). Employed workers receive their wages.
(ii). Unemployed workers receive their benefits \(z=-1\) along with a job offer whose wage is sampled from the following distribution
Write down the Bellman equation for an unemployed worker and for a worker employed at wage \(w\). Reinsert the value function \(E(w)\) of employed workers into that of unemployed workers \(U\) to express the latter as a function of \(w\).
Consider the following two job searching strategies. One where the worker accepts all job offers, which we denote by \(U^{A}\); and one where the worker rejects low-wage offers (\(w=0\)), which we denote by \(U^{R}.\) Compute the values of \(U^{A}\) and \(U^{R}.\)
Under which condition on \(p\) is it optimal for workers to reject low-wage offers? Provide an economic intuition for your result. (Hint: Remember that \(z=-1.\))
We now assume that the worker search for only two periods. What is the value function \(U_{2}\) of an unemployed worker in the second and last search period?
Consider now the value of being unemployed in the first search period \(U_{1}\). Again, compare two job searching strategies: one where the worker accepts all job offers (\(U_{1}^{A}\)); and one where the worker rejects low-wage offers (\(U_{1}^{R}\)). Compute the two value functions.
Under which condition is it optimal to reject low-wage offers in the first period? Compare your answer to the solution in 3. Provide an interpretation for your finding.
We now assume that workers are uncertain about the probability \(p\) at which they receive high-wage offers. Their initial belief at the beginning of period 1 is given by
where \(p_{h}>p_{l}.\) Use Bayes’ rule to express the agent’s posterior \(\mu _{2}(0)\) at the beginning of period \(2\) after she has received a low wage-offer. Show that \(\mu _{2}(0)<\mu _{1}.\) In particular, what is the value of \(\mu_{2}(0)\) when \(p_{h}=1?\)
We assume that \(p_{h}=1\). Use Bayes’ rule to compute \(U_{1}^{R}\) and \(U_{1}^{A}\). Under which condition is it optimal to reject a low-wage offer in period 1?
Solution
The Bellman equation for an unemployed worker can be written as:
where \(\overline{w} = 1\) and \(\underline{w} = 0\). Reinserting the value function \(E(w) = \frac{w}{r}\) into \(U\), we find that
The value of the job search srategy that accepts all job offer is given by:
where the equality follows reinserting \(\overline{w}=1\), \(\underline{w}=0\), and \(z=-1\). Similarly, the value of the job search strategy that rejects low-wage offers is given by:
Since the denominator of \(U^R\) is smaller than the denominator of \(U^A\), \(U^R\) is greater than \(UÂ\) whenever their numerators are positive, that is when \(p>r\). Intuitively, the probability of receiving a high-wage offer must be high enough to compensate for the opportunity cost \(r\) of remaining unemployed.
In the second period, if the worker rejects the offer, she will stay unemployed forever and receive \(\frac{z}{r}<0=\frac{\underline{w}}{r}\). Therefore, workers will accept all offers in the second period so that \(U_2 = U^A = \frac{p-r}{(1 +r)r}\).
\(U_1^A\) and \(U_1^R\) can be expressed as:
For \(U_1^R\) to be greater than \(U_1^A\), the value of searching in the second and last period \(U_2>0\) must be positive, which holds true whenever \(p>r\). This is the same condition as in the infinite horizon problem derived in 3.
According to Bayes’ rule, \(\mu _{2}(0)\) can be expressed as:
The condition \(\mu _{2}(0)<\mu _{1}\) is equivalent to \(1/\mu _1<1/\mu _{2}(0)\). The second condition holds true whenever
which holds true by definition. As expected, workers put less weight on the optimistic prior \(p=p_h\) after having received a low-wage offer. In particular, when \(p_h=1\), \(\mu _{2}(0) = 0\) because workers can exclude the belief that they will always receive a high-wage offer.
With \(p_h=1\), \( U_1^A\) and \(U_1^R\) can be expressed as:
where, according to the answer to question 4, \(U_2(0) = U^A(0)=\frac{1}{1+r}(z + \frac{\mu_2(0)}{r} + (1-\mu_2(0))\frac{p_l}{r})\). Since, \(\mu_2(0)=0\) when \(p_h=1\), we have \(U_2(0)=\frac{p_l-r}{(1+r)r}\). Therefore, we find that
The worker find it profitable to reject all low-wage offers in the first period when \(p_l>r\). This condition is similar to the one in the environment without learning except that now the pessimistic belief has replaced the average one.
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
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)\) 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.
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\).
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.
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
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:
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:
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,
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
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
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
The next pull wins with probability
\(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
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\):
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\):
\(n = 2\). A second pull is only worth taking after a win (\(V_1(1) > 0 > V_1(-1)\)):
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\):
\(n = 3\).
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
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
keep pulling arm 1 as long as it has won at least as often as it has lost. Its ratio equals the index:
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))