PIXELBANKv9.1.0
Menu

Marginalize Next-State Distribution

Problem Statement

Given a policy pi over actions at a state, and a transition matrix P where P[a][s'] is the probability of reaching next state s' when taking action a, compute the marginal next-state distribution under the policy:

Pπ(s′∣s)=∑aπ(a∣s) P(s′∣s,a)P^\pi(s'\mid s) = \sum_a \pi(a\mid s)\, P(s'\mid s, a)

Implement marginal_next_state(pi, P) returning a distribution over next states.

Example:

Input:
marginal_next_state([0.5, 0.5], [[1.0, 0.0], [0.0, 1.0]])
Output:
[0.5, 0.5]
Reasoning:
  • Initialize the next-state distribution vector to zeros, representing that no probability mass has been accumulated yet for any state.
  • Process the first action with policy probability Ï€(0)=0.5\pi(0) = 0.5: multiply this weight by the transition row [1.0,0.0][1.0, 0.0] to get [0.5,0.0][0.5, 0.0], and add this to the accumulator, resulting in [0.5,0.0][0.5, 0.0].
  • Process the second action with policy probability Ï€(1)=0.5\pi(1) = 0.5: multiply this weight by the transition row [0.0,1.0][0.0, 1.0] to get [0.0,0.5][0.0, 0.5], and add this to the current accumulator [0.5,0.0][0.5, 0.0].
  • Sum the contributions from both actions to obtain the final marginal probabilities: 0.5+0.0=0.50.5 + 0.0 = 0.5 for the first state and 0.0+0.5=0.50.0 + 0.5 = 0.5 for the second state.
  • The final output is [0.5, 0.5]

Constraints:

  • len(pi) == len(P) (one row per action); all rows same length S.
  • Each P[a] sums to 1; pi sums to 1.
  • Output length S, sums to 1.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.
Marginalize Next-State Distribution - Medium | PixelBank