State Value from Action Values
Problem Statement
The state value under a policy is the policy-weighted average of action values:
Vπ(s)=∑a​π(a∣s)Qπ(s,a)
Given the policy distribution pi and action values q (same length, aligned by action), implement state_value(pi, q).
Example:
state_value([0.5, 0.5], [2.0, 4.0])
3.0
- Identify the policy probabilities π and action values Q from the input: π=[0.5,0.5] and Q=[2.0,4.0].
- Calculate the weighted contribution for the first action by multiplying its probability by its value: 0.5×2.0=1.0.
- Calculate the weighted contribution for the second action similarly: 0.5×4.0=2.0.
- Sum these individual contributions to compute the policy-weighted average state value: 1.0+2.0=3.0.
- The final output is 3.0
Constraints:
len(pi) == len(q),1 <= len <= 1000piis a valid distribution (sums to 1).- Return a float.
1. Background Knowledge
In Reinforcement Learning, the state value function Vπ(s) represents the expected return starting from state s and following policy π thereafter. The action value function Qπ(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.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.