Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-175

Stochastic Matrix Check

Problem

Return True if A is a row-stochastic matrix: it must be square, every entry must be non-negative (within 1e-9), and every row must sum to 1 (within 1e-9).

Otherwise return False.

Hint

A Markov transition matrix is row-stochastic; check non-negativity and row sums.

Test cases

case 1
in: [[[0.5,0.5],[1,0]]]
expected: true
case 2
in: [[[0.5,0.5],[0.5,0.5]]]
expected: true
case 3
in: [[[1.2,-0.2],[0.5,0.5]]]
expected: false
case 4
in: [[[0.6,0.6],[0,1]]]
expected: false
Starter codePython
def is_stochastic(A):
    # Your code here
    pass

4 test cases · run your code against them in the editor