Sutton & Barto show that linear function approximation can make even dynamic programming unstable, when each update is projected back into the representable functions. Take a fixed policy with transition matrix over the non-terminal states (rows may sum to less than 1; the missing mass goes to a terminal state of value 0), expected rewards , features (one row per state) and state weights . One least-squares DP iteration solves
an expected DP backup followed by the best fit to it.
Write least_squares_dp(X, P, r, gamma, w0, n_iters, mu=None):
X has shape (n_states, d), P shape (n_states, n_states), r length n_states, and mu the per-state weights (all ones when None, as in Example 11.1).np.linalg.lstsq on the rows scaled by (this also handles rank-deficient features).Return a 2-D array of shape (n_iters + 1, d) with .
In Example 11.1 two states have features 1 and 2, the first moves to the second, the second terminates with probability and otherwise stays, and every reward is 0. The true values are 0, which represents exactly.