def shuffle_transmat(transmat):
"""Shuffle transition probability matrix within each row, leaving self transitions in tact.
It is assumed that the transmat is stochastic-row-wise, meaning that A_{ij} = Pr(S_{t+1}=j|S_t=i).
Parameters
----------
transmat : array of size (n_states, n_states)
Transition probability matrix, where A_{ij} = Pr(S_{t+1}=j|S_t=i).
Returns
-------
shuffled : array of size (n_states, n_states)
Shuffled transition probability matrix.
"""
shuffled = transmat.copy()
nrows, ncols = transmat.shape
for rowidx in range(nrows):
all_but_diagonal = np.append(np.arange(rowidx), np.arange(rowidx+1, ncols))
shuffle_idx = np.random.permutation(all_but_diagonal)
shuffle_idx = np.insert(shuffle_idx, rowidx, rowidx)
shuffled[rowidx,:] = shuffled[rowidx, shuffle_idx]
return shuffled
评论列表
文章目录