MLCrack

    REINFORCEMENT LEARNING Β· INTERVIEW QUESTION

    Explain the Bellman equation. How is it used in Q-learning?

    🟑 intermediate · Fundamentals & MDPs

    Hint: Think about the recursive relationship between values of successive states.

    Answer

    Bellman equation expresses value of a state as immediate reward plus discounted future value:

    V(s)=max⁑a[R(s,a)+Ξ³βˆ‘sβ€²P(sβ€²βˆ£s,a)V(sβ€²)]V(s) = \max_a \left[ R(s,a) + \gamma \sum_{s'} P(s'|s,a) V(s') \right]

    For Q-values (state-action values):

    Q(s,a)=R(s,a)+Ξ³βˆ‘sβ€²P(sβ€²βˆ£s,a)max⁑aβ€²Q(sβ€²,aβ€²)Q(s,a) = R(s,a) + \gamma \sum_{s'} P(s'|s,a) \max_{a'} Q(s',a')

    Q-learning is model-free β€” learns QQ without knowing transition probabilities PP:

    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]

    where Ξ±\alpha is the learning rate and Ξ³\gamma is the discount factor.

    python
    import numpy as np # Tabular Q-learning Q = np.zeros((n_states, n_actions)) for episode in range(1000): s, _ = env.reset() done = False while not done: a = epsilon_greedy(Q, s, epsilon=0.1) s_next, r, terminated, truncated, _ = env.step(a) done = terminated or truncated bootstrap = 0.0 if terminated else Q[s_next].max() Q[s, a] += alpha * (r + gamma * bootstrap - Q[s, a]) s = s_next

    Deep Q-Networks (DQN) replace the Q-table with a neural network for large state spaces.