def transition_loop(n_states, prob):
if not isinstance(n_states, int) or n_states <= <NUM_LIT:1>:<EOL><INDENT>raise ParameterError('<STR_LIT>')<EOL><DEDENT>transition = np.empty((n_states, n_states), dtype=np.float)<EOL>prob = np.asarray(prob, dtype=np.float)<EOL>if prob.ndim == <NUM_LIT:0>:<EOL><INDENT>prob = np.tile(prob, n_states)<EOL><DEDENT>if prob.shape != (n_states,):<EOL><INDENT>raise ParameterError('<STR_LIT>'.format(prob, n_states))<EOL><DEDENT>if np.any(prob < <NUM_LIT:0>) or np.any(prob > <NUM_LIT:1>):<EOL><INDENT>raise ParameterError('<STR_LIT>'.format(prob))<EOL><DEDENT>for i, prob_i in enumerate(prob):<EOL><INDENT>transition[i] = (<NUM_LIT:1.> - prob_i) / (n_states - <NUM_LIT:1>)<EOL>transition[i, i] = prob_i<EOL><DEDENT>return transition<EOL>
Construct a self-loop transition matrix over `n_states`.

    The transition matrix will have the following properties:

        - `transition[i, i] = p` for all i
        - `transition[i, j] = (1 - p) / (n_states - 1)` for all `j != i`

    This type of transition matrix is appropriate when states tend to be
    locally stable, and there is no additional structure between different
    states.  This is primarily useful for de-noising frame-wise predictions.

    Parameters
    ----------
    n_states : int > 1
        The number of states

    prob : float in [0, 1] or iterable, length=n_states
        If a scalar, this is the probability of a self-transition.

        If a vector of length `n_states`, `p[i]` is the probability of state `i`'s self-transition.

    Returns
    -------
    transition : np.ndarray [shape=(n_states, n_states)]
        The transition matrix

    Examples
    --------
    >>> librosa.sequence.transition_loop(3, 0.5)
    array([[0.5 , 0.25, 0.25],
           [0.25, 0.5 , 0.25],
           [0.25, 0.25, 0.5 ]])

    >>> librosa.sequence.transition_loop(3, [0.8, 0.5, 0.25])
    array([[0.8  , 0.1  , 0.1  ],
           [0.25 , 0.5  , 0.25 ],
           [0.375, 0.375, 0.25 ]])