KatInterview

Matrix Power and Empirical Transitions for a Markov Chain

Math & Bit ManipulationAuto-graded · 14 test cases
EasyAuto-gradedMath & Bit Manipulationmatrix-exponentiationmarkov-chainprobability

Last updated

Raise a row-stochastic matrix to a given power and build a row-normalized transition estimate from observed index pairs, leaving rows with no data as zeros.

A system moves between n states numbered 0 to n - 1. Its behavior is described by a one-step transition matrix, where transition_matrix[i][j] is the probability of moving from state i to state j in one step. Rows are "from" states and columns are "to" states.

Write a function that does two independent things and returns both results.

1. Multi-step probabilities. Compute the matrix M = transition_matrix raised to the power steps (ordinary matrix product, rows times columns). Entry M[i][j] is the probability of being in state j exactly steps steps after being in state i. When steps is 0, the result is the identity matrix.

2. Empirical estimate. observed_pairs is a list of [source, destination] pairs, each recording one observed move between states. Build an n x n count matrix where counts[s][d] is the number of times the pair [s, d] appears. Then divide each row by its own sum, so that the entry is the fraction of moves out of state s that went to state d. If a row has no observed moves (its sum is 0), the whole estimated row is 0.0 for every column. Do not apply smoothing, self-loops, or any other fill-in.

Return value: a dictionary with exactly two keys:

  • "multi_step": the n x n matrix from part 1, as a list of lists of numbers.
  • "estimated": the n x n matrix from part 2, as a list of lists of floats.

Small floating-point rounding differences are expected, since the values are real numbers produced by repeated multiplication.

Constraints

  • 1 <= n <= 20, where n = len(transition_matrix); the matrix is square.
  • Each entry of transition_matrix is between 0 and 1, and each row sums to 1.
  • 0 <= steps <= 10^9.
  • 0 <= len(observed_pairs) <= 10^5.
  • Every state index in observed_pairs is an integer in [0, n - 1].

Examples

Example 1

Input: markov_power_and_estimate([[0.8, 0.2], [0.1, 0.9]], 2, [[0, 0], [0, 1], [0, 1], [1, 1], [1, 0]])

Output: {'multi_step': [[0.6600000000000001, 0.3400000000000001], [0.17000000000000004, 0.8300000000000001]], 'estimated': [[0.3333333333333333, 0.6666666666666666], [0.5, 0.5]]}

Example 2

Input: markov_power_and_estimate([[1, 0], [0, 1]], 0, [])

Output: {'multi_step': [[1, 0], [0, 1]], 'estimated': [[0, 0], [0, 0]]}

Hints

Hint 1

Matrix multiplication is associative, so you can compute the power by exponentiation by squaring instead of multiplying `steps` times. Start from the identity matrix.

Hint 2

For the estimate, first tally a count matrix from the pairs, then divide each row by its own total. Rows are the source states, so never normalize columns.

Hint 3

A row whose total is zero would cause a division by zero. Check for that case and emit a row of `0.0` values instead.