Sidechair

matrix.is_stochastic()

Browse the reference

Returns true when a matrix has no negative items and every row adds up to 1.

Try it in the editor

Every item must be 0 or more and every row must total 1: [[0.75, 0.25], [0.5, 0.5]] passes, and [[0.5, 0.6], [0.4, 0.6]] fails because its first row totals 1.1.

These are the properties of a transition matrix, where each row gives the probabilities of moving from one state to every other. Rows built from inexact decimals can miss 1 by a rounding error, so use exact fractions such as 0.5 when a strict result matters.

Syntax

Pine Script
matrix.is_stochastic(id: matrix<int/float>) -> series bool
matrix.is_stochastic() -> series bool

Arguments

NameTypeDefaultDescription
idmatrix<int/float>requiredThe matrix checked for non-negative items and rows that each total 1, like a table of transition probabilities.

Returns

  • series bool: true when no item is negative and every row totals 1, otherwise false.
  • series bool: true when the receiving matrix is stochastic, otherwise false.

Example

Pine ScriptTwo-state transition matrixOpen in editor
//@version=6
indicator("matrix.is_stochastic: transitions", overlay = false)
matrix<float> p = matrix.new<float>(2, 2, 0.5)
plot(matrix.is_stochastic(p) ? 1 : 0, "Stochastic")

See also

Runs on Sidechair