Formula Bro

Math and physics formulas, explained like you are new.


Q-Learning Update

In one sentence: Q-learning lets an agent improve its guess about how good an action is, a little at a time, using the reward it got on its latest step.

Q(s,a)←Q(s,a)+α[r+γmaxa′Q(s′,a′)−Q(s,a)]Q(s,a) \leftarrow Q(s,a) + \alpha\left[r + \gamma \max_{a'} Q(s',a') - Q(s,a)\right]

Read it out loud: “Q of s, a becomes Q of s, a plus alpha times a bracket. Inside the bracket: the reward r, plus gamma times the largest Q of s prime and a prime, minus Q of s, a. The arrow means is replaced by. Q is the agent's guess of how good an action is. The letter s is the state, the situation, and a is the action. The letter r is the reward, s prime is the next state, and the small prime mark means next. Alpha, said AL-fuh, is the learning rate, and gamma, said GAM-uh, is the discount factor.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Agent, state, action and reward

An agent is a program that learns by trying actions and watching what happens. Its state, written ss, is the situation it is in now, such as one square of a game board. An action, written aa, is a choice it can make, such as moving right. After each action it receives a reward, written rr: a number of points that says how good the result was.

The Q table and the value estimate

The agent keeps a table with one number for every pair of a state and an action. The number Q(s,a)Q(s,a) is its estimate of the total reward it can expect from taking action aa in state ss and playing well afterward. The table starts with guesses, often all zeros, and learning means improving those numbers.

The Greek letters alpha and gamma

The Greek letter α\alpha, called alpha and said AL-fuh, is the learning rate. It is a number between 0 and 1 that sets how big a step each update takes. The Greek letter γ\gamma, called gamma and said GAM-uh, is the discount factor, also between 0 and 1. It sets how much future reward counts compared with reward now.

The arrow and the brackets

The arrow ←\leftarrow means replace the value on the left with the value on the right. So x←x+1x \leftarrow x + 1 means add 1 to xx and store the result in xx. Square brackets, [][\,], group a calculation. Inside the brackets is the gap between a target and the old estimate, and the whole bracket is multiplied by α\alpha before it is added.

The maximum and the prime mark

The maximum, written max\max, is the largest value in a list. In max⁡a′Q(s′,a′)\max_{a'} Q(s',a'), the small label a′a' says to look over every action a′a', and the answer is the biggest QQ among them. A prime mark, such as s′s' or a′a', means the next one. So s′s' is the next state, the place the agent ends up after its action.

What each symbol means

SymbolMeaning

Q(s,a)Q(s,a)

The agent’s current estimate of how good it is to take action aa in state ss. It is the total future reward the agent expects, measured in reward points, with no unit.

ss

The state, the situation the agent is in right now, such as one square of a maze. It is a label for a situation, so it has no unit.

aa

The action taken in state ss, such as moving right. It is a choice from a list of possible moves, so it has no unit.

rr

The reward received right after taking action aa, measured in reward points. It can be zero, positive or negative.

s′s'

The next state, where the agent ends up after taking the action. The prime mark means next. It is a label, so it has no unit.

max⁡a′Q(s′,a′)\max_{a'} Q(s',a')

The best value available from the next state: the largest QQ among all the actions a′a' the agent could take from there. It is measured in reward points, with no unit.

a′a'

One of the possible actions in the next state s′s'. The prime mark marks it as an action taken from the next state, and the maximum looks over all of them.

α\alpha

The learning rate, a number between 0 and 1 with no unit. It sets how big a step each update takes. A value of 0 changes nothing, and a value of 1 replaces the old estimate completely.

γ\gamma

The discount factor, a number between 0 and 1 with no unit. It sets how much a future reward counts compared with a reward now. A value of 0 counts only the next reward.

←\leftarrow

The replace arrow. The value on the left is set to the result on the right. It is not an equals sign, because the two sides are not equal before the update happens.

The formula piece by piece

rr

The reward received for taking action aa in state ss, in points. It comes from the world the agent acts in, and it is the only new real information in this update.

max⁡a′Q(s′,a′)\max_{a'} Q(s',a')

The best value the agent expects from the next state. It looks at every action available there and takes the largest estimate. This assumes the agent plays well from then on.

γmax⁡a′Q(s′,a′)\gamma \max_{a'} Q(s',a')

The discounted future. Multiplying the best next value by γ\gamma shrinks it, so the future counts for less than the reward now. A discount of 0.9 counts each future point as 90 percent of a point.

r+γmax⁡a′Q(s′,a′)r + \gamma \max_{a'} Q(s',a')

The target. It adds the reward we got to the discounted best future value. This is the agent’s new, better guess of how good the action was, based on one real step.

r+γmax⁡a′Q(s′,a′)−Q(s,a)r + \gamma \max_{a'} Q(s',a') - Q(s,a)

The TD error, the gap between the target and the old estimate. A positive gap means the old guess was too low, and a negative gap means it was too high.

α[r+γmax⁡a′Q(s′,a′)−Q(s,a)]\alpha\left[r + \gamma \max_{a'} Q(s',a') - Q(s,a)\right]

The step. The gap is multiplied by the learning rate, so the update moves the old estimate only a fraction of the way toward the target.

Q(s,a)←Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)]Q(s,a) \leftarrow Q(s,a) + \alpha\left[r + \gamma \max_{a'} Q(s',a') - Q(s,a)\right]

The new estimate equals the old estimate, plus the learning rate times the gap between the target and the old estimate.

Q(s,a)←(1−α)Q(s,a)+α(r+γmax⁡a′Q(s′,a′))Q(s,a) \leftarrow (1 - \alpha)\, Q(s,a) + \alpha\left(r + \gamma \max_{a'} Q(s',a')\right)

The same update written as a weighted mix. The old estimate keeps the share 1−α1 - \alpha, and the new target gets the share α\alpha. Both forms give the same number.

What the formula is doing, and why

The agent keeps a table of Q(s,a)Q(s,a) numbers, one for each pair of state and action. At the start the numbers are guesses. Each time the agent acts, it gets a reward and lands in a new state, and it uses that one step to improve the number for the action it took. Over many visits, the table comes closer to the true value of each action.

Why a target with the best next value? After one step we know two things. One is the reward rr we got, which is real. The other is the best the agent can expect from the next state, which is the max\max term. Adding them gives a target that uses one real reward, so it carries more real information than the old guess. The max\max assumes the agent will act well from then on, which is why the update describes the best play.

Why move only part of the way? The gap between the target and the guess is the TD error, short for temporal difference error. One step of experience is noisy, so moving all the way to the target can make the estimate jump around. The learning rate α\alpha moves the estimate only a fraction of the way. A small α\alpha changes things slowly and smooths out noise, while a large α\alpha reacts quickly but can jump around.

Why discount with γ\gamma? Gamma decides how far ahead the agent looks. With γ=0\gamma = 0, only the immediate reward counts. As γ\gamma nears 1, rewards far in the future count almost as much as rewards now. A discount below 1 also keeps the total finite when rewards are bounded, even if the task never ends.

When it applies. A table works when there are few enough states and actions to store one number for each pair, such as a small grid. Big problems use a neural network instead, which keeps the same kind of target. The update uses the best action in the next state even if the agent takes a different action there, which is why Q-learning is called off-policy.

An everyday picture

Think of rating a move in a board game. You know what the move gained right away, and you know the best you can expect from the position it leads to. Each time you play, you move your old rating a bit toward the sum of those two. Over many games the ratings settle near the true value of each move.

Worked example: One update in a grid world

The agent stands in state ss and takes action aa. Its current estimate is Q(s,a)=2.0Q(s,a) = 2.0 points. The action gives reward r=1.0r = 1.0 point and leads to state s′s', where the best action has value max⁡a′Q(s′,a′)=4.0\max_{a'} Q(s',a') = 4.0 points. Use the learning rate α=0.5\alpha = 0.5 and the discount factor γ=0.9\gamma = 0.9.

  1. Start with the old estimate. The agent currently thinks this action in this square is worth 2.0 points. We will adjust that guess using the one step it has taken.
  2. Discount the best value that follows. Multiply the best next value by the discount factor: 0.9×4.0=3.60.9 \times 4.0 = 3.6. Multiplying by 0.9 counts each future point as 90 percent of a point now, so the future is worth a little less than its face value.
  3. Form the target. Add the reward we got: r+γmax⁡a′Q(s′,a′)=1.0+3.6=4.6r + \gamma \max_{a'} Q(s',a') = 1.0 + 3.6 = 4.6. The target is the agent’s new best guess of this action’s value, using the real reward and the discounted future.
  4. Find the gap. The TD error is the target minus the old estimate: 4.6−2.0=2.64.6 - 2.0 = 2.6. The gap is positive, which means the old estimate was too low.
  5. Take a step of the learning rate. Multiply the gap by α=0.5\alpha = 0.5: 0.5×2.6=1.30.5 \times 2.6 = 1.3. With α=0.5\alpha = 0.5 the agent moves halfway toward the target, so it does not trust one step completely.
  6. Add the step to the old estimate. The new estimate is 2.0+1.3=3.32.0 + 1.3 = 3.3 points. Written as the update rule, Q(s,a)←2.0+0.5×2.6=3.3Q(s,a) \leftarrow 2.0 + 0.5 \times 2.6 = 3.3.
  7. Check the halfway idea. The point halfway between 2.0 and 4.6 is 2.0+4.62=3.3\frac{2.0 + 4.6}{2} = 3.3. The two ways of looking at the update give the same new estimate.

Answer: The new estimate is Q(s,a)=3.3Q(s,a) = 3.3. It moved halfway from 2.0 toward the target of 4.6, because α=0.5\alpha = 0.5. The next time the agent visits this pair, the estimate will move a little closer to the target again.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

What does the learning rate α\alpha do?

It sets how big a step each update takes. With α=0\alpha = 0, nothing is learned. With α=1\alpha = 1, the old estimate is replaced by the target completely.

Why use the max over the next actions?

The estimate should describe the best play from here on. Using the largest next value gives that, so the table can learn the best policy even while the agent sometimes explores at random. A policy is the agent’s rule for choosing actions.

What happens when an episode ends?

An episode is one run from a start to an end, such as one game. When it ends, there is no next state to value, so the future term is set to zero. The update then uses the reward alone: Q(s,a)←Q(s,a)+α[r−Q(s,a)]Q(s,a) \leftarrow Q(s,a) + \alpha\,[r - Q(s,a)].

Who first described Q-learning?

Chris Watkins introduced it in 1989, in his thesis Learning from Delayed Rewards.

Sources

  1. Q-learning, Wikipedia
  2. Reinforcement learning, Wikipedia

| All AI & ML formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: ai-ml/q-learning-update.md.