def simulate(self, N, start=None, stop=None, dt=1): """ generates a realization of the Hidden Markov Model :param N: int trajectory length in steps of the lag time :param start: int (default=None) - starting hidden state. If not given, will sample from the stationary distribution of the hidden transition matrix :param stop: int or int-array-like (default=None) - stopping hidden set. If given, the trajectory will be stopped before N steps once a hidden state of the stop set is reached :param dt: int - trajectory will be saved every dt time steps. Internally, the dt'th power of P is taken to ensure a more efficient simulation :return: ndarray, ndarray - tuple of (hidden state trajectory with length N/dt, observable state discrete trajectory with length N/dt) """ from scipy import stats import msmtools.generation as msmgen # generate output distributions output_distributions = [stats.rv_discrete(values=(np.arange(self.pobs.shape[1]), pobs_i)) for pobs_i in self.pobs] # sample hidden trajectory htraj = msmgen.generate_traj(self.transition_matrix, N, start=start, stop=stop, dt=dt) otraj = np.zeros(htraj.size, dtype=int) # for each time step, sample microstate for t, h in enumerate(htraj): otraj[t] = output_distributions[h].rvs() # current cluster return htraj, otraj
def setUp(self): """Store state of the rng""" self.state = np.random.mtrand.get_state() """Reseed the rng to enforce 'deterministic' behavior""" np.random.mtrand.seed(42) """Meta-stable birth-death chain""" b = 2 q = np.zeros(7) p = np.zeros(7) q[1:] = 0.5 p[0:-1] = 0.5 q[2] = 1.0 - 10**(-b) q[4] = 10**(-b) p[2] = 10**(-b) p[4] = 1.0 - 10**(-b) bdc = BirthDeathChain(q, p) P = bdc.transition_matrix() self.dtraj = generate_traj(P, 10000, start=0) self.tau = 1 """Estimate MSM""" self.C_MSM = count_matrix(self.dtraj, self.tau, sliding=True) self.lcc_MSM = largest_connected_set(self.C_MSM) self.Ccc_MSM = largest_connected_submatrix(self.C_MSM, lcc=self.lcc_MSM) self.P_MSM = transition_matrix(self.Ccc_MSM, reversible=True) self.mu_MSM = stationary_distribution(self.P_MSM) self.k = 3 self.ts = timescales(self.P_MSM, k=self.k, tau=self.tau)
def setUpClass(cls) -> None: """Store state of the rng""" cls.state = np.random.mtrand.get_state() """Reseed the rng to enforce 'deterministic' behavior""" np.random.mtrand.seed(42) """Meta-stable birth-death chain""" b = 2 q = np.zeros(7) p = np.zeros(7) q[1:] = 0.5 p[0:-1] = 0.5 q[2] = 1.0 - 10 ** (-b) q[4] = 10 ** (-b) p[2] = 10 ** (-b) p[4] = 1.0 - 10 ** (-b) bdc = BirthDeathChain(q, p) P = bdc.transition_matrix() cls.dtraj = generate_traj(P, 10000, start=0) cls.tau = 1 """Estimate MSM""" import inspect argspec = inspect.getfullargspec(MaximumLikelihoodMSM) default_maxerr = argspec.defaults[argspec.args.index('maxerr') - 1] cls.C_MSM = msmest.count_matrix(cls.dtraj, cls.tau, sliding=True) cls.lcc_MSM = msmest.largest_connected_set(cls.C_MSM) cls.Ccc_MSM = msmest.largest_connected_submatrix(cls.C_MSM, lcc=cls.lcc_MSM) cls.P_MSM = msmest.transition_matrix(cls.Ccc_MSM, reversible=True, maxerr=default_maxerr) cls.mu_MSM = msmana.stationary_distribution(cls.P_MSM) cls.k = 3 cls.ts = msmana.timescales(cls.P_MSM, k=cls.k, tau=cls.tau)
def setUpClass(cls): path = pkg_resources.resource_filename(__name__, 'data') + os.path.sep cls.pdb_file = os.path.join(path, 'bpti_ca.pdb') cls.feat = MDFeaturizer(cls.pdb_file) cls.feat.add_all() cls.traj_files = [ os.path.join(path, 'bpti_001-033.xtc'), os.path.join(path, 'bpti_067-100.xtc') ] # generate HMM with two gaussians p = np.array([[0.99, 0.01], [0.01, 0.99]]) t = 10000 means = [np.array([-1, 1]), np.array([1, -1])] widths = [np.array([0.3, 2]), np.array([0.3, 2])] # continuous trajectory x = np.zeros((t, 2)) # hidden trajectory dtraj = msmgen.generate_traj(p, t) for t in range(t): s = dtraj[t] x[t, 0] = widths[s][0] * np.random.randn() + means[s][0] x[t, 1] = widths[s][1] * np.random.randn() + means[s][1] cls.generated_data = x cls.generated_lag = 10
def test_transitionmatrix(self): # test if transition matrix can be reconstructed N = 5000 trajs = msmgen.generate_traj(self.P, N, random_state=self.random_state) C = msmest.count_matrix(trajs, 1, sparse_return=False) T = msmest.transition_matrix(C) np.testing.assert_allclose(T, self.P, atol=.01)
def setUp(self): """Store state of the rng""" self.state = np.random.mtrand.get_state() """Reseed the rng to enforce 'deterministic' behavior""" np.random.mtrand.seed(0xDEADBEEF) """Meta-stable birth-death chain""" b = 2 q = np.zeros(7) p = np.zeros(7) q[1:] = 0.5 p[0:-1] = 0.5 q[2] = 1.0 - 10**(-b) q[4] = 10**(-b) p[2] = 10**(-b) p[4] = 1.0 - 10**(-b) bdc = BirthDeathChain(q, p) P = bdc.transition_matrix() self.dtraj = generate_traj(P, 10000, start=0) self.tau = 1 self.k = 3 """ Predictions and experimental data """ self.E = np.vstack((np.linspace(-0.1, 1., 7), np.linspace(1.5, -0.1, 7))).T self.m = np.array([0.0, 0.0]) self.w = np.array([2.0, 2.5]) self.sigmas = 1. / np.sqrt(2) / np.sqrt(self.w) """ Feature trajectory """ self.ftraj = self.E[self.dtraj, :] self.AMM = AugmentedMarkovModel(E=self.E, m=self.m, w=self.w) self.AMM.estimate([self.dtraj])
def test_discrete_4_2(self): # 4x4 transition matrix nstates = 2 P = np.array([[0.90, 0.10, 0.00, 0.00], [0.10, 0.89, 0.01, 0.00], [0.00, 0.01, 0.89, 0.10], [0.00, 0.00, 0.10, 0.90]]) # generate realization import msmtools.generation as msmgen T = 10000 dtrajs = [msmgen.generate_traj(P, T)] C = msmest.count_matrix(dtrajs, 1).toarray() # estimate initial HMM with 2 states - should be identical to P hmm = init_discrete_hmm(dtrajs, nstates) # Test if model fit is close to reference. Note that we do not have an exact reference, so we cannot set the # tolerance in a rigorous way to test statistical significance. These are just sanity checks. Tij = hmm.transition_matrix B = hmm.output_model.output_probabilities # Test stochasticity import msmtools.analysis as msmana msmana.is_transition_matrix(Tij) np.allclose(B.sum(axis=1), np.ones(B.shape[0])) # if (B[0,0]<B[1,0]): # B = B[np.array([1,0]),:] Tij_ref = np.array([[0.99, 0.01], [0.01, 0.99]]) Bref = np.array([[0.5, 0.5, 0.0, 0.0], [0.0, 0.0, 0.5, 0.5]]) assert(np.max(Tij-Tij_ref) < 0.01) assert(np.max(B-Bref) < 0.05 or np.max(B[[1, 0]]-Bref) < 0.05)
def simulate(self, N, start=None, stop=None, dt=1): """ Generates a realization of the Markov Model Parameters ---------- N : int trajectory length in steps of the lag time start : int, optional, default = None starting hidden state. If not given, will sample from the stationary distribution of the hidden transition matrix. stop : int or int-array-like, optional, default = None stopping hidden set. If given, the trajectory will be stopped before N steps once a hidden state of the stop set is reached dt : int trajectory will be saved every dt time steps. Internally, the dt'th power of P is taken to ensure a more efficient simulation. Returns ------- htraj: (N/dt, ) ndarray The state trajectory with length N/dt """ import msmtools.generation as msmgen return msmgen.generate_traj(self.transition_matrix, N, start=start, stop=stop, dt=dt)
def generate_synthetic_state_trajectory(self, nsteps, initial_Pi=None, start=None, stop=None, dtype=np.int32): """Generate a synthetic state trajectory. Parameters ---------- nsteps : int Number of steps in the synthetic state trajectory to be generated. initial_Pi : np.array of shape (n_states,), optional, default=None The initial probability distribution, if samples are not to be taken from the intrinsic initial distribution. start : int starting state. Exclusive with initial_Pi stop : int stopping state. Trajectory will terminate when reaching the stopping state before length number of steps. dtype : numpy.dtype, optional, default=numpy.int32 The numpy dtype to use to store the synthetic trajectory. Returns ------- states : np.array of shape (n_states,) of dtype=np.int32 The trajectory of hidden states, with each element in range(0,n_states). Examples -------- Generate a synthetic state trajectory of a specified length. >>> from sktime.markovprocess.bhmm import testsystems >>> model = testsystems.dalton_model() >>> states = model.generate_synthetic_state_trajectory(nsteps=100) """ # consistency check if initial_Pi is not None and start is not None: raise ValueError( 'Arguments initial_Pi and start are exclusive. Only set one of them.' ) # Generate first state sample. if start is None: if initial_Pi is not None: start = np.random.choice(self._n_states, size=1, p=initial_Pi) else: start = np.random.choice(self._n_states, size=1, p=self._Pi) # Generate and return trajectory from msmtools import generation as msmgen traj = msmgen.generate_traj(self.transition_matrix, nsteps, start=start, stop=stop, dt=1) return traj.astype(dtype)
def simulate(self, n_jumps=None, start=None, target=None): """Generate a realization of the chain. The simulation is stopped after a given number of jumps or when a given target is reached. If both are provided, the simulation is stopped at the earlier of the two stopping times. Parameters ---------- n_jumps : int, optional Number of jumps to simulate. Required when `target` is None. start : hashable, optional The starting state. If not provided, it will be drawn according to the stationary distribution of self.jump_matrix. target : Set, optional The set of target states. Required when `n_jumps` is None. Returns ------- dtraj : sequence The sequence of states visited by the chain. arrival_times : sequence of floats The increasing sequence of arrival (jump) times. The arrival time at the starting state is defined to be zero. See Also -------- :func:`msmtools.generation.generate_traj` Used to generate a realization of the embedded Markov chain. """ if n_jumps is None: if target is None: raise ValueError('must provide a stopping criterion') n_jumps = sys.maxsize elif n_jumps < 0: raise ValueError('number of jumps must be nonnegative') if start is not None: start = self.index(start) if target is not None: target = [self.index(state) for state in set(target)] dtraj = msmgen.generate_traj(self.jump_matrix, n_jumps + 1, start=start, stop=target) arrival_times = np.zeros_like(dtraj, dtype=float) if len(dtraj) > 1: rng = np.random.default_rng() mean_lifetimes = 1. / self.jump_rates[dtraj[:-1]] lifetimes = rng.exponential(scale=mean_lifetimes) arrival_times[1:] = np.cumsum(lifetimes) return self.states[dtraj], arrival_times
def setUpClass(cls): P = np.array([ [0.5, .25, .25, 0.], [0., .25, .5, .25], [.25, .25, .5, 0], [.25, .25, .25, .25], ]) # bogus its object lags = [1, 2, 3, 5, 10] cls.its = its(generate_traj(P, 100), lags=lags, errors='bayes') cls.refs = cls.its.timescales[-1] return cls
def setUp(self): from msmtools.generation import generate_traj self.dtrajs = [] # simple case dtraj_simple = [0, 1, 1, 1, 0] self.dtrajs.append([dtraj_simple]) # as ndarray self.dtrajs.append([np.array(dtraj_simple)]) dtraj_disc = [0, 1, 1, 0, 0] self.dtrajs.append([dtraj_disc]) # multitrajectory case self.dtrajs.append([[0], [1, 1, 1, 1], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1]]) # large-scale case large_trajs = [] for i in range(10): large_trajs.append(np.random.randint(10, size=1000)) self.dtrajs.append(large_trajs) # Markovian timeseries with timescale about 5 self.P2 = np.array([[0.9, 0.1], [0.1, 0.9]]) self.dtraj2 = generate_traj(self.P2, 1000) self.dtrajs.append([self.dtraj2]) # Markovian timeseries with timescale about 5 self.P4 = np.array([[0.95, 0.05, 0.0, 0.0], [0.05, 0.93, 0.02, 0.0], [0.0, 0.02, 0.93, 0.05], [0.0, 0.0, 0.05, 0.95]]) self.dtraj4_2 = generate_traj(self.P4, 20000) I = [0, 0, 1, 1] # coarse-graining for i in range(len(self.dtraj4_2)): self.dtraj4_2[i] = I[self.dtraj4_2[i]] self.dtrajs.append([self.dtraj4_2])
def test_trajectory(self): P = np.array([[0.9, 0.1], [0.1, 0.9]]) N = 1000 traj = msmgen.generate_traj(P, N, start=0) # test shapes and sizes assert traj.size == N assert traj.min() >= 0 assert traj.max() <= 1 # test statistics of transition matrix C = msmest.count_matrix(traj, 1) Pest = msmest.transition_matrix(C) assert np.max(np.abs(Pest - P)) < 0.025
def setUpClass(cls): with numpy_random_seed(123): import msmtools.generation as msmgen # generate HMM with two Gaussians cls.P = np.array([[0.99, 0.01], [0.01, 0.99]]) cls.T = 40000 means = [np.array([-1, 1]), np.array([1, -1])] widths = [np.array([0.3, 2]), np.array([0.3, 2])] # continuous trajectory cls.X = np.zeros((cls.T, 2)) # hidden trajectory dtraj = msmgen.generate_traj(cls.P, cls.T) for t in range(cls.T): s = dtraj[t] cls.X[t, 0] = widths[s][0] * np.random.randn() + means[s][0] cls.X[t, 1] = widths[s][1] * np.random.randn() + means[s][1] # Set the lag time: cls.lag = 10 # Compute mean free data: mref = (np.sum(cls.X[:-cls.lag, :], axis=0) + np.sum( cls.X[cls.lag:, :], axis=0)) / float(2 * (cls.T - cls.lag)) mref_nr = np.sum(cls.X[:-cls.lag, :], axis=0) / float(cls.T - cls.lag) cls.X_mf = cls.X - mref[None, :] cls.X_mf_nr = cls.X - mref_nr[None, :] # Compute correlation matrices: cls.cov_ref = (np.dot(cls.X_mf[:-cls.lag, :].T, cls.X_mf[:-cls.lag, :]) +\ np.dot(cls.X_mf[cls.lag:, :].T, cls.X_mf[cls.lag:, :])) / float(2*(cls.T-cls.lag)) cls.cov_ref_nr = np.dot( cls.X_mf_nr[:-cls.lag, :].T, cls.X_mf_nr[:-cls.lag, :]) / float(cls.T - cls.lag) cls.cov_tau_ref = (np.dot(cls.X_mf[:-cls.lag, :].T, cls.X_mf[cls.lag:, :]) +\ np.dot(cls.X_mf[cls.lag:, :].T, cls.X_mf[:-cls.lag, :])) / float(2*(cls.T-cls.lag)) cls.cov_tau_ref_nr = np.dot( cls.X_mf_nr[:-cls.lag, :].T, cls.X_mf_nr[cls.lag:, :]) / float(cls.T - cls.lag) # do unscaled TICA reader = api.source(cls.X, chunksize=0) cls.tica_obj = api.tica(data=reader, lag=cls.lag, dim=1, kinetic_map=False) # non-reversible TICA cls.tica_obj_nr = api.tica(data=reader, lag=cls.lag, dim=1, kinetic_map=False, reversible=False)
def setUpClass(cls): P = np.array([[0.5, .25, .25, 0.], [0., .25, .5, .25], [.25, .25, .5, 0], [.25, .25, .25, .25], ]) # bogus its object lags = [1, 2, 3, 5, 10] dtraj = generate_traj(P, 1000) estimator = MaximumLikelihoodMSM(dt_traj='10 ps') cls.its = ImpliedTimescales(estimator=estimator) cls.its.estimate(dtraj, lags=lags) cls.refs = cls.its.timescales[-1] return cls
def generate_hmm_test_data(): import msmtools.generation as msmgen state = np.random.RandomState(123) # generate HMM with two Gaussians P = np.array([[0.99, 0.01], [0.01, 0.99]]) T = 40000 means = [np.array([-1, 1]), np.array([1, -1])] widths = [np.array([0.3, 2]), np.array([0.3, 2])] # continuous trajectory X = np.zeros((T, 2)) X2 = np.zeros((T, 2)) # hidden trajectory dtraj = msmgen.generate_traj(P, T) means = np.array(means) widths = np.array(widths) normal_vals = state.normal(size=(len(X), 2)) X[:, 0] = means[dtraj][:, 0] + widths[dtraj][:, 0] * normal_vals[:, 0] X[:, 1] = means[dtraj][:, 1] + widths[dtraj][:, 1] * normal_vals[:, 1] # Set the lag time: lag = 10 # Compute mean free data: mref = (np.sum(X[:-lag, :], axis=0) + np.sum(X[lag:, :], axis=0)) / float( 2 * (T - lag)) mref_nr = np.sum(X[:-lag, :], axis=0) / float(T - lag) X_mf = X - mref[None, :] X_mf_nr = X - mref_nr[None, :] # Compute correlation matrices: cov_ref = (np.dot(X_mf[:-lag, :].T, X_mf[:-lag, :]) + np.dot(X_mf[lag:, :].T, X_mf[lag:, :])) / float(2 * (T - lag)) cov_ref_nr = np.dot(X_mf_nr[:-lag, :].T, X_mf_nr[:-lag, :]) / float(T - lag) cov_tau_ref = (np.dot(X_mf[:-lag, :].T, X_mf[lag:, :]) + np.dot(X_mf[lag:, :].T, X_mf[:-lag, :])) / float(2 * (T - lag)) cov_tau_ref_nr = np.dot(X_mf_nr[:-lag, :].T, X_mf_nr[lag:, :]) / float(T - lag) return dict(lagtime=lag, cov_ref_00=cov_ref, cov_ref_00_nr=cov_ref_nr, cov_ref_0t=cov_tau_ref, cov_ref_0t_nr=cov_tau_ref_nr, data=X)
def test_trajectory(self): N = 1000 traj = msmgen.generate_traj(self.P, N, start=0, random_state=self.random_state) # test shapes and sizes assert traj.size == N assert traj.min() >= 0 assert traj.max() <= 1 # test statistics of transition matrix C = msmest.count_matrix(traj, 1) Pest = msmest.transition_matrix(C) assert np.max(np.abs(Pest - self.P)) < 0.025
def generate_synthetic_state_trajectory(self, nsteps, initial_Pi=None, start=None, stop=None, dtype=np.int32): """Generate a synthetic state trajectory. Parameters ---------- nsteps : int Number of steps in the synthetic state trajectory to be generated. initial_Pi : np.array of shape (nstates,), optional, default=None The initial probability distribution, if samples are not to be taken from the intrinsic initial distribution. start : int starting state. Exclusive with initial_Pi stop : int stopping state. Trajectory will terminate when reaching the stopping state before length number of steps. dtype : numpy.dtype, optional, default=numpy.int32 The numpy dtype to use to store the synthetic trajectory. Returns ------- states : np.array of shape (nstates,) of dtype=np.int32 The trajectory of hidden states, with each element in range(0,nstates). Examples -------- Generate a synthetic state trajectory of a specified length. >>> from bhmm import testsystems >>> model = testsystems.dalton_model() >>> states = model.generate_synthetic_state_trajectory(nsteps=100) """ # consistency check if initial_Pi is not None and start is not None: raise ValueError('Arguments initial_Pi and start are exclusive. Only set one of them.') # Generate first state sample. if start is None: if initial_Pi is not None: start = np.random.choice(range(self._nstates), size=1, p=initial_Pi) else: start = np.random.choice(range(self._nstates), size=1, p=self._Pi) # Generate and return trajectory from msmtools import generation as msmgen traj = msmgen.generate_traj(self.transition_matrix, nsteps, start=start, stop=stop, dt=1) return traj.astype(dtype)
def simulate(self, N, start=None, stop=None, dt=1): """ Generates a realization of the Hidden Markov Model Parameters ---------- N : int trajectory length in steps of the lag time start : int, optional, default = None starting hidden state. If not given, will sample from the stationary distribution of the hidden transition matrix. stop : int or int-array-like, optional, default = None stopping hidden set. If given, the trajectory will be stopped before N steps once a hidden state of the stop set is reached dt : int trajectory will be saved every dt time steps. Internally, the dt'th power of P is taken to ensure a more efficient simulation. Returns ------- htraj : (N/dt, ) ndarray The hidden state trajectory with length N/dt otraj : (N/dt, ) ndarray The observable state discrete trajectory with length N/dt """ from scipy import stats import msmtools.generation as msmgen # generate output distributions # TODO: replace this with numpy.random.choice output_distributions = [ stats.rv_discrete( values=(np.arange(self._observation_probabilities.shape[1]), pobs_i)) for pobs_i in self._observation_probabilities ] # sample hidden trajectory htraj = msmgen.generate_traj(self.transition_matrix, N, start=start, stop=stop, dt=dt) otraj = np.zeros(htraj.size, dtype=int) # for each time step, sample microstate for t, h in enumerate(htraj): otraj[t] = output_distributions[h].rvs() # current cluster return htraj, otraj
def setUpClass(cls): import msmtools.generation as msmgen # set random state, remember old one and set it back in tearDownClass cls.old_state = np.random.get_state() np.random.seed(0) # generate HMM with two Gaussians cls.P = np.array([[0.99, 0.01], [0.01, 0.99]]) cls.T = 10000 means = [np.array([-1, 1]), np.array([1, -1])] widths = [np.array([0.3, 2]), np.array([0.3, 2])] # continuous trajectory cls.X = np.zeros((cls.T, 2)) # hidden trajectory dtraj = msmgen.generate_traj(cls.P, cls.T) for t in range(cls.T): s = dtraj[t] cls.X[t, 0] = widths[s][0] * np.random.randn() + means[s][0] cls.X[t, 1] = widths[s][1] * np.random.randn() + means[s][1] cls.pca_obj = pca(data=cls.X, dim=1)
def test_discrete_2_2(self): # 2x2 transition matrix P = np.array([[0.99, 0.01], [0.01, 0.99]]) # generate realization import msmtools.generation as msmgen T = 10000 dtrajs = [msmgen.generate_traj(P, T)] # estimate initial HMM with 2 states - should be identical to P hmm = initdisc.initial_model_discrete(dtrajs, 2) # test A = hmm.transition_matrix B = hmm.output_model.output_probabilities # Test stochasticity import msmtools.analysis as msmana msmana.is_transition_matrix(A) np.allclose(B.sum(axis=1), np.ones(B.shape[0])) # A should be close to P if (B[0, 0] < B[1, 0]): B = B[np.array([1, 0]), :] assert (np.max(A - P) < 0.01) assert (np.max(B - np.eye(2)) < 0.01)
def test_discrete_6_3(self): # 4x4 transition matrix nstates = 3 P = np.array([[0.90, 0.10, 0.00, 0.00, 0.00, 0.00], [0.20, 0.79, 0.01, 0.00, 0.00, 0.00], [0.00, 0.01, 0.84, 0.15, 0.00, 0.00], [0.00, 0.00, 0.05, 0.94, 0.01, 0.00], [0.00, 0.00, 0.00, 0.02, 0.78, 0.20], [0.00, 0.00, 0.00, 0.00, 0.10, 0.90]]) # generate realization import msmtools.generation as msmgen T = 10000 dtrajs = [msmgen.generate_traj(P, T)] # estimate initial HMM with 2 states - should be identical to P hmm = initdisc.initial_model_discrete(dtrajs, nstates) # Test stochasticity and reversibility Tij = hmm.transition_matrix B = hmm.output_model.output_probabilities import msmtools.analysis as msmana msmana.is_transition_matrix(Tij) msmana.is_reversible(Tij) np.allclose(B.sum(axis=1), np.ones(B.shape[0]))
def setUpClass(cls): import msmtools.generation as msmgen # generate HMM with two Gaussians cls.P = np.array([[0.99, 0.01], [0.01, 0.99]]) cls.T = 40000 means = [np.array([-1, 1]), np.array([1, -1])] widths = [np.array([0.3, 2]), np.array([0.3, 2])] # continuous trajectory cls.X = np.zeros((cls.T, 2)) # hidden trajectory dtraj = msmgen.generate_traj(cls.P, cls.T) for t in range(cls.T): s = dtraj[t] cls.X[t, 0] = widths[s][0] * np.random.randn() + means[s][0] cls.X[t, 1] = widths[s][1] * np.random.randn() + means[s][1] cls.lag = 10 # do unscaled TICA cls.tica_obj = api.tica(data=cls.X, lag=cls.lag, dim=1, kinetic_map=False)
def test_discrete_2_2(self): # 2x2 transition matrix P = np.array([[0.99, 0.01], [0.01, 0.99]]) # generate realization import msmtools.generation as msmgen T = 10000 dtrajs = [msmgen.generate_traj(P, T)] C = msmest.count_matrix(dtrajs, 1).toarray() # estimate initial HMM with 2 states - should be identical to P hmm = init_discrete_hmm(dtrajs, 2) # test A = hmm.transition_matrix B = hmm.output_model.output_probabilities # Test stochasticity import msmtools.analysis as msmana msmana.is_transition_matrix(A) np.allclose(B.sum(axis=1), np.ones(B.shape[0])) # A should be close to P if B[0, 0] < B[1, 0]: B = B[np.array([1, 0]), :] assert(np.max(A-P) < 0.01) assert(np.max(B-np.eye(2)) < 0.01)
def test_discrete_6_3(self): # 4x4 transition matrix nstates = 3 P = np.array([[0.90, 0.10, 0.00, 0.00, 0.00, 0.00], [0.20, 0.79, 0.01, 0.00, 0.00, 0.00], [0.00, 0.01, 0.84, 0.15, 0.00, 0.00], [0.00, 0.00, 0.05, 0.94, 0.01, 0.00], [0.00, 0.00, 0.00, 0.02, 0.78, 0.20], [0.00, 0.00, 0.00, 0.00, 0.10, 0.90]]) # generate realization import msmtools.generation as msmgen T = 10000 dtrajs = [msmgen.generate_traj(P, T)] C = msmest.count_matrix(dtrajs, 1).toarray() # estimate initial HMM with 2 states - should be identical to P hmm = init_discrete_hmm(dtrajs, nstates) # Test stochasticity and reversibility Tij = hmm.transition_matrix B = hmm.output_model.output_probabilities import msmtools.analysis as msmana msmana.is_transition_matrix(Tij) msmana.is_reversible(Tij) np.allclose(B.sum(axis=1), np.ones(B.shape[0]))
def generate_traj(self, N, start=None, stop=None, dt=1): """ Generates a random trajectory of length N with time step dt """ from msmtools.generation import generate_traj return generate_traj(self._P, N, start=start, stop=stop, dt=dt)
def setUp(self): """Store state of the rng""" self.state = np.random.mtrand.get_state() """Reseed the rng to enforce 'deterministic' behavior""" np.random.mtrand.seed(42) """Meta-stable birth-death chain""" b = 2 q = np.zeros(7) p = np.zeros(7) q[1:] = 0.5 p[0:-1] = 0.5 q[2] = 1.0 - 10 ** (-b) q[4] = 10 ** (-b) p[2] = 10 ** (-b) p[4] = 1.0 - 10 ** (-b) bdc = BirthDeathChain(q, p) P = bdc.transition_matrix() dtraj = generate_traj(P, 10000, start=0) tau = 1 """Estimate MSM""" MSM = estimate_markov_model(dtraj, tau) C_MSM = MSM.count_matrix_full lcc_MSM = MSM.largest_connected_set Ccc_MSM = MSM.count_matrix_active P_MSM = MSM.transition_matrix mu_MSM = MSM.stationary_distribution """Meta-stable sets""" A = [0, 1, 2] B = [4, 5, 6] w_MSM = np.zeros((2, mu_MSM.shape[0])) w_MSM[0, A] = mu_MSM[A] / mu_MSM[A].sum() w_MSM[1, B] = mu_MSM[B] / mu_MSM[B].sum() K = 10 P_MSM_dense = P_MSM p_MSM = np.zeros((K, 2)) w_MSM_k = 1.0 * w_MSM for k in range(1, K): w_MSM_k = np.dot(w_MSM_k, P_MSM_dense) p_MSM[k, 0] = w_MSM_k[0, A].sum() p_MSM[k, 1] = w_MSM_k[1, B].sum() """Assume that sets are equal, A(\tau)=A(k \tau) for all k""" w_MD = 1.0 * w_MSM p_MD = np.zeros((K, 2)) eps_MD = np.zeros((K, 2)) p_MSM[0, :] = 1.0 p_MD[0, :] = 1.0 eps_MD[0, :] = 0.0 for k in range(1, K): """Build MSM at lagtime k*tau""" C_MD = count_matrix(dtraj, k * tau, sliding=True) / (k * tau) lcc_MD = largest_connected_set(C_MD) Ccc_MD = largest_connected_submatrix(C_MD, lcc=lcc_MD) c_MD = Ccc_MD.sum(axis=1) P_MD = transition_matrix(Ccc_MD).toarray() w_MD_k = np.dot(w_MD, P_MD) """Set A""" prob_MD = w_MD_k[0, A].sum() c = c_MD[A].sum() p_MD[k, 0] = prob_MD eps_MD[k, 0] = np.sqrt(k * (prob_MD - prob_MD ** 2) / c) """Set B""" prob_MD = w_MD_k[1, B].sum() c = c_MD[B].sum() p_MD[k, 1] = prob_MD eps_MD[k, 1] = np.sqrt(k * (prob_MD - prob_MD ** 2) / c) """Input""" self.MSM = MSM self.K = K self.A = A self.B = B """Expected results""" self.p_MSM = p_MSM self.p_MD = p_MD self.eps_MD = eps_MD