Ejemplo n.º 1
0
def test_reversible_pi(dtype):
    C = np.array([[7048, 6, 0], [6, 2, 3], [0, 3, 2933]]).astype(dtype)
    pi = np.array([0.70532947, 0.00109989, 0.29357064])
    P_mle = tmatrix(C, reversible=True, mu=pi)

    N = 1000
    sampler = TransitionMatrixSampler(C, reversible=True, mu=pi, n_steps=10)

    sample = np.zeros((N, 3, 3))
    for i in range(N):
        s = sampler.sample()
        np.testing.assert_equal(s.dtype, dtype)
        sample[i, :, :] = s
    mean = np.mean(sample, axis=0)
    std = np.std(sample, axis=0)

    # Check if sample mean and MLE agree within the sample standard deviation
    np.testing.assert_(np.all(np.abs(mean - P_mle) <= std))
Ejemplo n.º 2
0
def test_reversible(dtype):
    C = 1.0 * np.array([[7048, 6, 0], [6, 2, 3], [0, 3, 2933]]).astype(dtype)
    P_mle = tmatrix(C, reversible=True)
    N = 1000

    sampler = TransitionMatrixSampler(C, reversible=True)

    sample = np.zeros((N, 3, 3))
    for i in range(N):
        s = sampler.sample()
        np.testing.assert_equal(s.dtype, dtype)
        sample[i, :, :] = s
    mean = np.mean(sample, axis=0)
    std = np.std(sample, axis=0)

    # Check if sample mean and MLE agree within the sample standard deviation
    diff = np.abs(mean - P_mle)
    np.testing.assert_(np.all(diff <= std))
Ejemplo n.º 3
0
    def __init__(self, C, P0=None, seed: int = -1):
        from deeptime.markov.tools.estimation import transition_matrix as tmatrix
        from deeptime.markov.tools.analysis import stationary_distribution
        from .._mle_bindings import RevSampler32, RevSampler64, RevSampler128

        if C.dtype not in (np.float32, np.float64, np.longdouble):
            dtype = np.float64
        else:
            dtype = C.dtype

        self.C = C.astype(dtype)
        """Set up initial state of the chain"""
        if P0 is None:
            # only do a few iterations to get close to the MLE and suppress not converged warning
            P0 = tmatrix(self.C,
                         reversible=True,
                         maxiter=100,
                         warn_not_converged=False)
            assert P0.dtype == self.C.dtype
        else:
            P0 = P0.astype(self.C.dtype)
        pi0 = stationary_distribution(P0).astype(self.C.dtype)
        V0 = pi0[:, np.newaxis] * P0

        self.V = V0
        self.c = self.C.sum(axis=1)
        """Check for valid input"""
        self.check_input()
        """Get nonzero indices"""
        self.I, self.J = np.where((self.C + self.C.T) > 0.0)
        """Init Vsampler"""
        if self.C.dtype == np.float32:
            self._sampler = RevSampler32(seed)
        elif self.C.dtype == np.float64:
            self._sampler = RevSampler64(seed)
        elif self.C.dtype == np.longdouble:
            self._sampler = RevSampler128(seed)
        else:
            raise ValueError(f"Unknown dtype {self.C.dtype}")