10  Data 102 Discussion 10: Multi-Armed Bandits

10.0.1 Contact Information

Name Wesley Zheng
Pronouns He/him/his
Email wzheng0302@berkeley.edu
Discussion Wednesdays, 11–12 PM @ Wheeler 224
Office Hours Tuesdays, 9–10 AM & 11-12 PM @ Gateway B1040

Contact me by email at ease — I typically respond within a day or so!


10.1 Multi-Armed Bandits

ImportantMulti-Armed Bandits

A multi-armed bandit problem has \(K\) options (“arms”). Each arm \(a\) has an unknown reward distribution \(\mathbb{P}_a\) with mean \(\mu_a\). At each round you pick one arm, observe a reward from that arm’s distribution, and update your beliefs.

The core tension is exploration vs. exploitation: try arms you know little about, or keep pulling the arm that looks best so far. Success is often measured by regret: how much worse you do in expectation than an oracle that always pulls the arm with the highest mean.

To cast a real-world problem as a bandit, name two things:

  • the arms (the discrete choices you can repeat), and
  • the rewards (the numeric feedback you observe after each choice).

If you took CS 188, this is a stripped-down version of the reinforcement learning setup from my notes there. A bandit is like an MDP with a single recurring situation: the arms are the actions, each pull gives an immediate reward, and there is no interesting next-state dynamics to plan over. Naming arms and rewards is the same modeling move as naming actions and rewards when you formulate an MDP. The explore-vs-exploit tension is also why Q-learning needs enough exploration to learn optimal \(q\)-values, and the RL practice questions walk through learning from action–reward samples.

Recall the multi-armed bandits framework. We consider a decision-maker who is given \(K\) options to choose from, referred to as “arms”. Each arm is associated with an unknown probability distribution over rewards. At each iteration, the decision-maker chooses an arm (“pulls” the arm) and receives a reward sampled from its corresponding distribution.

For each of the following scenarios, formulate the problem as a multi-armed bandit problem by identifying the arms and the rewards.


10.1.1 (a)

Choosing a Restaurant

You’re trying to figure out which restaurant on Euclid is your favorite, so every day for a month you get lunch at one of the restaurants on Euclid.

What are the arms and the rewards in this scenario?

Answer
  • Arms: The different restaurants on Euclid.
  • Rewards: How much you like the food at each restaurant, which could be measured using a numerical satisfaction score.

10.1.2 (b)

Finding the Best Shower Time

You’re trying to figure out what time of day your shower is most likely to have warm water, so you take a shower at different times of day over a sequence of days and observe the temperature of the water.

What are the arms and the rewards in this scenario?

Answer
  • Arms: The different times of day at which you could take a shower.
  • Rewards: A binary reward indicating whether the water is warm: \(1\) if the water is warm and \(0\) otherwise.

10.2 Multi-Armed Bandits and the UCB Algorithm

ImportantUCB and Hoeffding’s Inequality

Notation. Arms \(\mathcal{A} = \{1,\ldots,K\}\), rewards \(X_a \sim \mathbb{P}_a\) with means \(\mu_a\), pull count \(T_a(t)\), and sample mean \(\hat\mu_{a,T_a(t)}\).

Upper confidence bound (UCB). At time \(t\), pick the arm that maximizes \[ \hat\mu_{a,T_a(t-1)} + C_a\!\bigl(T_a(t-1),\delta\bigr), \] i.e., estimated mean plus a bonus that shrinks as that arm is pulled more. Arms that look good or are under-sampled get priority.

Hoeffding’s Inequality. If \(X_1,\ldots,X_n\) are independent and valued in \([a,b]\), then \[ \mathbb{P}\left[\frac{1}{n}\sum_{i=1}^n (X_i - \mathbb{E}[X_i]) \geq \epsilon\right] \leq \exp\left(-\frac{2n\epsilon^2}{(b-a)^2}\right). \] For rewards in \([0,1]\), this specializes to \(e^{-2n\epsilon^2}\). The same bound holds for the lower tail. Setting the right-hand side equal to a confidence level \(\delta\) and solving for \(\epsilon\) produces the UCB width \(C_a(T_a(t),\delta)\).

In CS 188 terms, UCB is one concrete answer to the exploration question that shows up in the Q-learning review: you need to try suboptimal-looking arms often enough to learn. Instead of \(\varepsilon\)-greedy randomness, UCB adds a bonus \(\sqrt{\frac{-\log\delta}{2T_a}}\) that shrinks as arm \(a\) is pulled more, so under-sampled arms stay attractive. TD learning is the other common relative: both update from sampled rewards, but TD/Q-learning track long-horizon returns through states, while UCB only needs the average reward of each arm.

Recall the multi-armed bandits framework. We consider a decision-maker who is given \(K\) options to choose from. We refer to these options as “arms”. Associated with each arm is a probability distribution over rewards. Initially, this distribution is unknown to the decision-maker. The decision-maker chooses an arm, usually referred to as pulling an arm, and receives a reward sampled from the corresponding reward distribution. This process is repeated over and over again.

Let’s begin by setting up some mathematical notation. Suppose you have a set of \(K\) “arms”, \(\mathcal{A}=\{1,2,...,K\}\). Each arm \(a \in \mathcal{A}\) has its own reward distribution \(X_a \sim \mathbb{P}_a\) with mean \(\mu_a=\mathbb{E}[X_a]\). Define the number of times arm \(a\) has been pulled up to and including time \(t\) as \(T_a(t)\). In these problems we do not know \(\mu_a\) but we would like to efficiently find the arm with the maximum mean by creating an algorithm that balances exploration of the arms with exploitation of the best possible arm. The efficiency of the algorithm is measured by a theoretical quantity known as regret, which measures how well the algorithm performs in expectation against an “oracle” that knows the means of all the arms and always pulls the arm with highest mean. In this discussion, we will study the derivation of the UCB algorithm for the multi-armed bandit problem, which uses the following bound: \[ \mathbb{P}\left[ \mu_a <\hat \mu_{a,T_a(t)} + C_a(T_a(t),\delta)\right]> 1-\delta \] where \(\hat \mu_{a,T_a(t)}\) is the average reward of arm \(a\) up to time \(t\) and \(C_a(T_a(t),\delta)\) is the upper confidence bound, a function of the desired confidence level \(\delta\).

To derive this bound, we will make use of Hoeffding’s Inequality, which tells us that if random variables \(X_1, X_2, ..., X_n\) are independent and bounded between \([a, b]\), then \[ \mathbb{P}\left[ \frac{1}{n}\sum_{i=1}^n (X_i - \mathbb{E}[X_i]) \geq \epsilon \right] \leq \exp\left(-\frac{2n\epsilon^2}{(b-a)^2}\right) \]


10.2.1 (a)

Applying Hoeffding’s Inequality for Bounded Random Variables

Suppose that you know that the reward of any arm is between 0 and 1: \(X_a \in [0,1]\). Find a bound on the difference between \(\hat \mu_{a,T_a(t)}\) and \(\mu_a\) using Hoeffding’s Inequality.

Answer

\[ \begin{align*} \mathbb{P}\left[ \hat{\mu}_{a,T_a(t)} - \mu_a \geq \epsilon \right] &\leq e^{-2T_a(t)\epsilon^2} \end{align*} \]

Before applying Hoeffding’s Inequality, rewrite the event so it matches the form in the statement. Here the random variables are the rewards \(X_{a,i}\) from arm \(a\), and we want a bound on \(\mathbb{P}\left[ \hat{\mu}_{a,T_a(t)} - \mu_a \geq \epsilon \right]\): \[ \begin{align*} \mathbb{P}\left[ \hat{\mu}_{a,T_a(t)} - \mu_a \geq \epsilon \right] &= \mathbb{P}\left[ \frac{1}{T_a(t)}\sum_{i=1}^{T_a(t)} X_{a,i} - \mu_a \geq \epsilon \right] \\ &= \mathbb{P}\left[ \frac{1}{T_a(t)}\sum_{i=1}^{T_a(t)} \left(X_{a,i} - \mathbb{E}\left[ X_{a,i} \right] \right) \geq \epsilon \right] \\ &\leq \exp\left(-\frac{2T_a(t)\epsilon^2}{(1-0)^2}\right) \\ &\leq e^{-2T_a(t)\epsilon^2}. \end{align*} \] So \(\mathbb{P}\left[ \hat{\mu}_{a,T_a(t)} - \mu_a \geq \epsilon \right] \leq e^{-2T_a(t)\epsilon^2}\). Hoeffding’s Inequality also applies to the lower tail, giving \(\mathbb{P}\left[ \hat{\mu}_{a,T_a(t)} - \mu_a \leq -\epsilon \right] \leq e^{-2T_a(t)\epsilon^2}\).


10.2.2 (b)

Connecting Hoeffding’s Inequality to UCB

Use the inequality derived in Part (a) to show that \[ \mathbb{P}\left[ \hat \mu_{a,T_a(t)}-\mu_a \leq - C_a(T_a(t), \delta)\right] \leq e^{-2T_a(t)C_a(T_a(t), \delta)^2} \]

Answer

\[ \begin{align*} \mathbb{P}\left[ \mu_a <\hat \mu_{a,T_a(t)} + C_a(T_a(t),\delta) \right] &> 1-\delta \\ \mathbb{P}\left[ \mu_a - \hat \mu_{a,T_a(t)} < C_a(T_a(t),\delta) \right] &> 1-\delta \\ 1 - \mathbb{P}\left[ \mu_a - \hat \mu_{a,T_a(t)} < C_a(T_a(t),\delta) \right] &\leq 1 - ( 1-\delta ) \\ \mathbb{P}\left[ \mu_a - \hat \mu_{a,T_a(t)} \geq C_a(T_a(t),\delta) \right] &\leq \delta \\ \mathbb{P}\left[ \hat \mu_{a,T_a(t)} - \mu_a \leq -C_a(T_a(t),\delta) \right] &\leq \delta \end{align*} \]

Start from the UCB guarantee and rearrange it into a lower-tail event. From the second line to the third we take the complement, and from the fourth line to the fifth we multiply both sides of the inequality inside the probability by \(-1\) (which flips the inequality). Then set \(\epsilon = C_a(T_a(t), \delta)\) in the Part (a) bound to get \[ \mathbb{P}\left[ \hat \mu_{a,T_a(t)}-\mu_a \leq - C_a(T_a(t), \delta)\right] \leq e^{-2T_a(t)C_a(T_a(t), \delta)^2}. \]


10.2.3 (c)

Solving for the Upper Confidence Bound

Find the value of \(C_a(T_a(t), \delta)\) in terms of \(\delta\) so that \(\mathbb{P}\left[\mu_a <\hat \mu_{a,T_a(t)} + C_a(T_a(t),\delta)\right]> 1-\delta\) holds. Plug this result into the inequality to derive the upper confidence bound.

Answer

\[ \begin{align*} C_a(T_a(t), \delta) &= \sqrt{\frac{-\log{\delta}}{2T_a(t)}} \end{align*} \]

Choose \(C_a(T_a(t), \delta)\) so the Part (b) exponential equals the target failure probability \(\delta\): \[ \begin{align*} e^{-2T_a(t)C_a(T_a(t), \delta)^2} &= \delta \\ \implies -2T_a(t)C_a(T_a(t), \delta)^2 &= \log{\delta} \\ \implies C_a(T_a(t), \delta) &= \sqrt{\frac{-\log{\delta}}{2T_a(t)}}. \end{align*} \] Plugging this width back into the UCB inequality gives \[ \mathbb{P}\left[\mu_a <\hat \mu_{a,T_a(t)} + \sqrt{\frac{-\log{\delta}}{2T_a(t)}}\right]> 1-\delta. \]


10.2.4 (d)

Arm Selection with UCB

Suppose we set \(\delta = \frac{1}{t^3}\). This controls the probability that the true mean \(\mu_a\) is greater than our upper confidence bound \(C_a(T_a(t), \delta)\) on the estimated mean \(\hat\mu_{a,T_a(t)}\). What rule does the UCB algorithm use to choose an arm \(A_t\) at each iteration \(t\)?

Answer

\[ \begin{align*} A_t &=\operatorname*{argmax}_{a \in \mathcal{A}} \ \ \hat \mu_{a, T_a(t-1)} + \sqrt{\frac{3\log{t}}{2T_a(t-1)}} \end{align*} \]

UCB picks the arm with the largest estimated mean plus upper confidence bound. At time \(t\) we only have estimates through time \(t-1\), so \[ \begin{align*} A_t &=\operatorname*{argmax}_{a \in \mathcal{A}} \ \ \hat \mu_{a, T_a(t-1)} + C_a(T_a(t-1), \delta). \end{align*} \] Using the width from Part (c): \[ \begin{align*} A_t &=\operatorname*{argmax}_{a \in \mathcal{A}} \ \ \hat \mu_{a, T_a(t-1)} + \sqrt{\frac{-\log{\delta}}{2T_a(t-1)}}. \end{align*} \] With \(\delta = \frac{1}{t^3}\), we get \(-\log\delta = -\log(1/t^3) = 3\log t\), and therefore \[ \begin{align*} A_t &=\operatorname*{argmax}_{a \in \mathcal{A}} \ \ \hat \mu_{a, T_a(t-1)} + \sqrt{\frac{3\log{t}}{2T_a(t-1)}}. \end{align*} \]