forked from Open-Deep-ML/DML-OpenProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path158.json
More file actions
33 lines (33 loc) · 2.31 KB
/
Copy path158.json
File metadata and controls
33 lines (33 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
"id": "158",
"title": "Incremental Mean for Online Reward Estimation",
"difficulty": "easy",
"category": "Reinforcement Learning",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [],
"description": "Implement an efficient method to update the mean reward for a k-armed bandit action after receiving each new reward, **without storing the full history of rewards**. Given the previous mean estimate (Q_prev), the number of times the action has been selected (k), and a new reward (R), compute the updated mean using the incremental formula.\n\n**Note:** Using a regular mean that stores all past rewards will eventually run out of memory. Your solution should use only the previous mean, the count, and the new reward.",
"learn_section": "### Incremental Mean Update Rule\n\nThe incremental mean formula lets you update your estimate of the mean after each new observation, **without keeping all previous rewards in memory**. For the k-th reward $R_k$ and previous estimate $Q_{k}$:\n\n$$\nQ_{k+1} = Q_k + \\frac{1}{k} (R_k - Q_k)\n$$\n\nThis saves memory compared to the regular mean, which requires storing all past rewards and recalculating each time. The incremental rule is crucial for online learning and large-scale problems where storing all data is impractical.",
"starter_code": "def incremental_mean(Q_prev, k, R):\n \"\"\"\n Q_prev: previous mean estimate (float)\n k: number of times the action has been selected (int)\n R: new observed reward (float)\n Returns: new mean estimate (float)\n \"\"\"\n # Your code here\n pass",
"solution": "def incremental_mean(Q_prev, k, R):\n return Q_prev + (1 / k) * (R - Q_prev)",
"example": {
"input": "Q_prev = 2.0\nk = 2\nR = 6.0\nnew_Q = incremental_mean(Q_prev, k, R)\nprint(round(new_Q, 2))",
"output": "4.0",
"reasoning": "The updated mean is Q_prev + (1/k) * (R - Q_prev) = 2.0 + (1/2)*(6.0 - 2.0) = 2.0 + 2.0 = 4.0"
},
"test_cases": [
{
"test": "Q = 0.0\nk = 1\nR = 5.0\nprint(round(incremental_mean(Q, k, R), 4))",
"expected_output": "5.0"
},
{
"test": "Q = 5.0\nk = 2\nR = 7.0\nprint(round(incremental_mean(Q, k, R), 4))",
"expected_output": "6.0"
},
{
"test": "Q = 6.0\nk = 3\nR = 4.0\nprint(round(incremental_mean(Q, k, R), 4))",
"expected_output": "5.3333"
}
]
}