def is_reversible(T, mu=None, tol=1e-15): r""" checks whether T is reversible in terms of given stationary distribution. If no distribution is given, it will be calculated out of T. performs follwing check: :math:`\pi_i P_{ij} = \pi_j P_{ji} Parameters ---------- T : scipy.sparse matrix Transition matrix mu : numpy.ndarray vector stationary distribution tol : float tolerance to check with Returns ------- Truth value : bool True, if T is a stochastic matrix False, otherwise """ if not is_transition_matrix(T, tol): raise ValueError("given matrix is not a valid transition matrix.") T = T.tocsr() if mu is None: from deeptime.markov.tools.analysis import stationary_distribution mu = stationary_distribution(T) Mu = diags(mu, 0) prod = Mu * T return allclose_sparse(prod, prod.transpose(), rtol=tol)
def is_reversible(T, mu=None, tol=1e-15): r""" checks whether T is reversible in terms of given stationary distribution. If no distribution is given, it will be calculated out of T. performs follwing check: :math:`\pi_i P_{ij} = \pi_j P_{ji} Parameters ---------- T : scipy.sparse matrix Transition matrix mu : numpy.ndarray vector stationary distribution tol : float tolerance to check with Returns ------- Truth value : bool True, if T is a stochastic matrix False, otherwise """ if not is_transition_matrix(T, tol): raise ValueError("given matrix is not a valid transition matrix.") if sparse.issparse(T): T = T.tocsr() if mu is None: mu = stationary_distribution(T) if sparse.issparse(T): prod = sparse.construct.diags(mu) * T else: prod = mu[:, None] * T if sparse.issparse(T): return allclose_sparse(prod, prod.transpose(), rtol=tol) else: return np.allclose(prod, prod.transpose(), rtol=tol)
def test_allclose_sparse(): A = sp.random(50, 50) B = sp.random(50, 51) assert allclose_sparse(A, A) assert not allclose_sparse(A, B)
def test_prior_neighbor(self): Bn = prior_neighbor(self.C) self.assertTrue(allclose_sparse(Bn, self.alpha_def * self.B_neighbor)) Bn = prior_neighbor(self.C, alpha=self.alpha) self.assertTrue(allclose_sparse(Bn, self.alpha * self.B_neighbor))