Logo Xingxin on Bug

How to Approximate Value Functions with Linear Regression?

October 18, 2026
7 min read

These are the notes I took while learning how to approximate a value function in section 8.6 of Algorithms for Decision Making.

📝Overview

Tip

Using linear regression to approximate a value function is a global approximation approach.

From a linear regression perspective, approximating the state value function Uθ(s)U_{\theta}(s) or an action-value function Qθ(s,a)Q_{\theta}(s, a) can be defined as follows:

Uθ(s)=θ⊤β(s),Qθ(s,a)=θ⊤β(s,a),U_{\theta}(s) = \boldsymbol{\theta}^\top \boldsymbol{\beta}(s), \qquad Q_{\theta}(s, a) = \boldsymbol{\theta}^\top \boldsymbol{\beta}(s, a),

where Uθ(s)U_{\theta}(s) is a linear combination of basis functions (commonly called features), and θ\boldsymbol{\theta} is the vector of learnable parameters.

Remark

These basis functions are generally nonlinear functions of the state ss, combined into a vector function β(s)\boldsymbol{\beta}(s) or β(s,a)\boldsymbol{\beta}(s, a). Even though the model is linear with respect to the basis functions, the resulting approximation can be nonlinear with respect to the underlying state ss.

This is a great example of lifting features into a higher dimension (“升维”). In the left plot, the value is nonlinear with respect to the state ss. However, in a higher-dimensional space formed by basis functions (ss and s2s^2), we can fit a flat plane to the data.

📁Example

Suppose the ground truth value function is U(s)=s2U(s) = s^2.

ssGround Truth U(s)U(s)
00
11
24
39

From the table above, we see that the value function is nonlinear with respect to the state ss. However, if we tailor our feature representation properly, say

β(s)=[1, s, s2]⊤.\boldsymbol\beta(s) = [1,\ s,\ s^2]^\top.

Then the learnable parameter θ\boldsymbol{\theta} could be θ=[0, 0, 1]⊤\boldsymbol\theta = [0,\ 0,\ 1]^\top. In this case, our model UθU_\theta perfectly captures the ground truth U(s)U(s). For example, when s=2s=2, we have

Uθ(2)=[0, 0, 1]⏟θ⊤⋅[124]⏟β(2)=0×1+0×2+1×4=4.U_\theta(2) = \underbrace{[0,\ 0,\ 1]}_{\boldsymbol\theta^\top}\cdot\underbrace{\begin{bmatrix}1\\2\\4\end{bmatrix}}_{\boldsymbol\beta(2)} = 0\times1 + 0\times2 + 1\times4 = 4.

This exact fit can only be achieved when 2 conditions are met:

  • ✅ we choose an appropriate feature representation. i.e., β(s)=[1, s, s2]⊤.\boldsymbol\beta(s) = [1,\ s,\ s^2]^\top.
  • ✅ we identify the right parameters. i.e., θ=[0, 0, 1]⊤\boldsymbol\theta = [0,\ 0,\ 1]^\top

But what if we don’t choose an appropriate representation? Suppose our feature vector only includes linear terms:

β(s)=[1, s]⊤.\boldsymbol\beta(s) = [1,\ s]^\top.

If we use the least squares method to fit the parameter, we get θ=[−1, 3]⊤\boldsymbol\theta = [-1,\ 3]^\top (meaning Uθ(s)=−1+3sU_\theta(s) = -1 + 3s).. In this case, the true value can not be perfectly approximated.

ssground truth U(s)U(s)approximated Uθ(s)U_\theta(s)error
00−1−1
112+1
245+1
398−1

This is the most common case we encounter in real life.

⛳Objective

Now we know what θ\boldsymbol\theta and β\boldsymbol\beta are, let’s see how to use the least squares method to solve for θ\boldsymbol\theta. Given mm states s1:ms_{1:m} and their target utilities u1:mu_{1:m}, our objective is to minimize the sum of squared errors between the predicted value U^θ(si)\hat{U}_{\theta}(s_i) and the target value uiu_i:

J(θ)=∑i=1m(U^θ(si)−ui)2=∑i=1m(θ⊤β(si)−ui)2.J(\boldsymbol{\theta}) = \sum_{i=1}^{m} (\hat{U}_{\theta}(s_i) - u_i)^2 = \sum_{i=1}^{m} (\boldsymbol{\theta}^\top \boldsymbol{\beta}(s_i) - u_i)^2.

We construct a feature matrix X\mathbf{X} where each row ii is the transposed feature vector for state sis_i:

Xi=β(si)⊤,X_i = \boldsymbol{\beta}(s_i)^\top,

where β(si)\boldsymbol{\beta}(s_i) is a column vector and β(si)⊤\boldsymbol{\beta}(s_i)^\top is a row vector.

Since the dot product of two vectors is symmetric (θ⊤β(si)=β(si)⊤θ\boldsymbol{\theta}^\top \boldsymbol{\beta}(s_i) = \boldsymbol{\beta}(s_i)^\top \boldsymbol{\theta}), each prediction θ⊤β(si)\boldsymbol{\theta}^\top \boldsymbol{\beta}(s_i) is simply the ii-th row (entry) of the matrix-vector product:

Xθ.\mathbf{X} \boldsymbol{\theta}.


Let’s use a toy example to illustrate this:

  • we have m=3m = 3 samples of state
  • feature vector is β(s)=[1s]\boldsymbol{\beta}(s) = \begin{bmatrix} 1 \\ s \end{bmatrix}
  • weight vector is θ=[θ1θ2]\boldsymbol{\theta} = \begin{bmatrix} \theta_1 \\ \theta_2 \end{bmatrix}
Sample iiState sis_iTarget Utility ui​u_i​Feature Row Xi,:=β(si)⊤\mathbf{X}_{i,:} = \boldsymbol{\beta}(s_i)^\top
10011[1,0][1, 0]
21122[1,1][1, 1]
32255[?,?][?, ?]

Clearly, the last row is [1,2][1, 2], so our feature matrix X\mathbf{X} is:

X=[101112].\mathbf{X}=\begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{bmatrix}.

Multiplying X\mathbf{X} by θ\boldsymbol{\theta} gives all predictions at once:

Xθ=[101112][θ1θ2]=[θ1θ1+θ2θ1+2θ2].\mathbf{X}\boldsymbol{\theta} = \begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{bmatrix} \begin{bmatrix} \theta_1 \\ \theta_2 \end{bmatrix} = \begin{bmatrix} \theta_1 \\ \theta_1+\theta_2 \\ \theta_1 + 2\theta_2 \end{bmatrix}.

Let’s verify with i=3i=3. The prediction θ⊤β(si)\boldsymbol{\theta}^\top \boldsymbol{\beta}(s_i) becomes θ⊤β(s3)\boldsymbol{\theta}^\top \boldsymbol{\beta}(s_3):

θ⊤β(s3)=[θ1θ2][12]=θ1+2θ2.\boldsymbol{\theta}^\top \boldsymbol{\beta}(s_3) = \begin{bmatrix} \theta_1 & \theta_2 \end{bmatrix}\begin{bmatrix} 1 \\ 2 \end{bmatrix} = \theta_1 + 2\theta_2.

This is the exact last entry of the product Xθ\mathbf{X}\boldsymbol{\theta}.


Look back at our objective function:

∑i=1m(θ⊤β(si)−ui)2.\sum_{i=1}^{m} (\boldsymbol{\theta}^\top \boldsymbol{\beta}(s_i) - u_i)^2.

If we define the error vector as e=Xθ−u\mathbf{e} = \mathbf{X}\boldsymbol{\theta} - \mathbf{u}, summing the squared entries of vector e\mathbf{e} is the same as taking its dot product with itself, e⊤e\mathbf{e}^\top \mathbf{e}. Therefore, the equation above becomes:

J(θ)=(Xθ−u)⊤(Xθ−u).J(\boldsymbol{\theta}) = (\mathbf{X}\boldsymbol{\theta} - \mathbf{u})^\top (\mathbf{X}\boldsymbol{\theta} - \mathbf{u}).

✏Solution

Because this is a least squares problem, the minimum occurs where the derivative with respect to θ\boldsymbol{\theta} is 0. Too see this intuitively, let’s first look at the 1D scalar version:

J(θ)=(xθ−u)2,J(\theta) = (x\theta - u)^2, assuming x≠0x\neq0. We have

ddθ(xθ−u)2=2x(xθ−u)=0x(xθ−u)=0(x⋅x)θ=xuθ=(x⋅x)−1xu.\begin{align} \frac{d}{d\theta}(x\theta - u)^2 &= 2x(x\theta - u) = 0 \\ x(x\theta - u) &= 0 \\ (x \cdot x)\theta &= xu \\ \theta &= (x \cdot x)^{-1}xu . \end{align}

The matrix calculus works the same way. Taking the gradient with respect to θ\boldsymbol{\theta} and setting it to zero gives:

∇θJ(θ)=2X⊤(Xθ−u)=0X⊤Xθ=X⊤uθ=(X⊤X)−1X⊤u\begin{align} \nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) = 2\mathbf{X}^\top(\mathbf{X}\boldsymbol{\theta} - \mathbf{u}) &= \mathbf{0} \\ \mathbf{X}^\top \mathbf{X}\boldsymbol{\theta} &= \mathbf{X}^\top \mathbf{u} \\ \boldsymbol{\theta} &= (\mathbf{X}^\top \mathbf{X})^{-1}\mathbf{X}^\top \mathbf{u} \end{align}

This gives Equation 8.16 in ADM

θ=(X⊤X)−1X⊤u1:m=X+u1:m.\boldsymbol{\theta} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{u}_{1:m} = \mathbf{X}^+ \mathbf{u}_{1:m}.

Depending on X\mathbf{X}, we have 2 cases to consider:

  • 1️⃣ when X⊤X\mathbf{X}^\top \mathbf{X} is invertible (i.e., X\mathbf{X} has full column rank): the (X⊤X)−1X⊤(\mathbf{X}^\top \mathbf{X})^{-1}\mathbf{X}^\top and the pseudoinverse X+\mathbf{X}^+ are mathematically identical. Case closed.
  • 2️⃣ when X⊤X\mathbf{X}^\top \mathbf{X} (e.g., we have fewer states than features, resulting in a wide matrix): (X⊤X)−1(\mathbf{X}^\top \mathbf{X})^{-1} does not exist. Instead, we find X+\mathbf{X}^+ directly using SVD, which gives the minimum norm least squares solution.
Tip

In Python, numpy.linalg.pinv uses SVD behind the scene.

Therefore, whether X⊤X\mathbf{X}^\top \mathbf{X} is invertible or not, the safest and most general formula is:

θ=X+u,\boldsymbol{\theta} = \mathbf{X}^+ \mathbf{u},

where X+\mathbf{X}^+ is the pseudoinverse of matrix X\mathbf{X}. The pseudoinverse is often computed by first finding the singular value decomposition,

X=UΣV∗.\mathbf{X} = \mathbf{U}\mathbf{\Sigma}\mathbf{V}^*.

We then have

X+=VΣ+U∗.(8.17)\mathbf{X}^+ = \mathbf{V}\mathbf{\Sigma}^+\mathbf{U}^*. \qquad (8.17)

That wraps up how linear regression can be used to approximate a value function!

See also...