BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Reinforcement learning2 of 36 in topic

583. Run an epsilon-greedy multi-armed bandit from fixed random draws

EasyReinforcement learning
Asked atMicrosoft

A bandit agent must balance trying arms it knows little about (explore) and pulling the arm that looks best so far (exploit). Epsilon-greedy is the simplest rule, and it is still used for A/B style online tests.

Write epsilon_greedy(rewards, eps, draws, explore_arms).
- rewards: (T, K) array. rewards[t][a] is the reward you would get by pulling arm a at step t.
- draws: length T list of floats in [0, 1). At step t, if draws[t] < eps you explore and pull explore_arms[t]. Otherwise you exploit and pull the arm with the highest current estimate Q (ties: lowest index).
- Start with Q = 0 and counts N = 0 for every arm. After pulling arm a, N[a] += 1 and Q[a] += (r - Q[a]) / N[a] (a running mean).
Return (Q, chosen) where Q is a NumPy float array of length K and chosen is the list of pulled arms.

Functiondef epsilon_greedy(rewards, eps, draws, explore_arms)

Example 1

rewards [[1, 2], [1, 2]], eps 0.5, draws [0.9, 0.1], explore_arms [0, 1]. Step 0 exploits arm 0 (tie), Q = [1, 0]. Step 1 explores arm 1, Q = [1, 2]. chosen = [0, 1].

Example 2

Input: rewards = [[0.2, 1.0, 0.5], [0.4, 0.0, 0.6], [0.3, 0.8, 0.9], [0.1, 1.2, 0.7], [0.5, 0.2, 0.4], [0.2, 0.9, 1.1]], eps = 0.1, draws = [0.05, 0.7, 0.02, 0.9, 0.3, 0.6], explore_arms = [1, 0, 2, 0, 1, 2]

Output: ([0.0, 0.5, 0.775], [1, 1, 2, 2, 2, 2])

Example 3

Input: epsilon_greedy(np.array([[0.2, 1.0, 0.5], [0.4, 0.0, 0.6], [0.3, 0.8, 0.9], [0.1, 1.2, 0.7], [0.5, 0.2, 0.4], [0.2, 0.9, 1.1]]), 0.0, [0.05, 0.7, 0.02, 0.9, 0.3, 0.6], [1, 0, 2, 0, 1, 2])[1]

Output: [0, 0, 0, 0, 0, 0]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

At each step you make one choice, explore or exploit, then update only the pulled arm's running mean.

Hint 2

Start Q and N at zero; for each step pick explore_arms[t] if draws[t] < eps, else the argmax of Q, read that arm's reward, increment its count, and update its mean.

Hint 3

np.argmax(Q) already returns the lowest index on ties. The incremental mean update is Q[a] += (R[t, a] - Q[a]) / N[a] after N[a] += 1, and you return Q as a float array plus the chosen list.

Common mistakes
  • The buggy version flips the comparison and explores almost all the time.
Understand the idea

A bandit agent repeatedly chooses one of K slot machines. It must balance trying arms it knows little about (explore) with pulling the arm that looks best so far (exploit). Epsilon-greedy explores with probability eps and otherwise exploits. Each arm's estimate Q is a running average of the rewards it gave.

Worked example

K = 2, eps = 0.2, rewards every step [0, 1], draws [0.5, 0.1, 0.5], explore_arms [0, 1, 0]. Step 0: 0.5 is not < 0.2, so exploit; Q = [0, 0] is a tie, pick arm 0, reward 0, Q[0] = 0. Step 1: 0.1 < 0.2, so explore arm 1, reward 1, Q[1] = 1. Step 2: exploit, arm 1 is best, reward 1, N[1] = 2, Q[1] = 1 + (1 - 1) / 2 = 1. chosen = [0, 1, 1], Q = [0, 1].

explore when draws[t] < eps, using explore_arms[t]; otherwise take argmax Q with lowest index on ties; then N[a] += 1 and Q[a] += (r - Q[a]) / N[a]

Flipping the comparison makes it explore most of the time. Epsilon-greedy is still used in online experiments and simple recommender exploration.

Topics
Reinforcement learning
Companies1
  • Microsoft the topic appears in interview reports Microsoft Senior Applied Scientist (L63) Interview Experience

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Compute discounted returns for an episodeEasy
  • Pick the next arm with the UCB1 ruleEasy
  • Compute n-step bootstrapped returnsEasy
  • Compute Double DQN targetsEasy
All Reinforcement learning problems
Report
583 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def epsilon_greedy(rewards, eps, draws, explore_arms):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS