In Reinforcement Learning, the state value functionVπ(s) represents the expected return starting from state s and following policy π thereafter. The action value functionQπ(s,a) represents the expected return starting from state s, taking action a, and then following π. The relationship between them is fundamental: the state value is simply the expectation of the action value under the policy distribution.
Mathematically, if π(a∣s) is the probability of taking action a in state s, then:
Vπ(s)=a∑π(a∣s)Qπ(s,a)
This is a weighted average where the weights are the policy probabilities. Note that ∑aπ(a∣s)=1 for a valid probability distribution, so Vπ(s) lies within the range of the Qπ(s,a) values. This concept is central to policy evaluation and policy improvement algorithms like Policy Iteration and Q-Learning.
2. Algorithm Approach
The problem reduces to computing a dot product between two vectors: the policy vector π and the action value vector Q. Since both vectors are aligned by action index, the i-th element of π corresponds to the i-th element of Q.
The general pattern is:
Verify that the inputs are valid (same length, non-negative probabilities, probabilities sum to 1).
Compute the element-wise product of π and Q.
Sum the resulting products.
This is a standard expectation calculation for a discrete random variable, where Q is the variable and π is its probability mass function.
3. Step-by-Step Strategy
Input Validation:
Check that pi and q have the same length.
Optionally, verify that pi is a valid probability distribution (all elements ≥0 and ∑π=1). This helps catch bugs early but may not be required by the problem.
PRO
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
PRO
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.