def bmsm_double_well(lagtime=100, nsamples=100, reversible=True, constrain_to_coarse_pi=False, **kwargs) -> BayesianMSM: """ :param lagtime: :param nsamples: :param statdist_contraint: :return: tuple(Estimator, Model) """ # load observations obs_micro = datasets.double_well_discrete().dtraj # stationary distribution pi_micro = datasets.double_well_discrete().analytic_msm.stationary_distribution pi_macro = np.zeros(2) pi_macro[0] = pi_micro[0:50].sum() pi_macro[1] = pi_micro[50:].sum() # coarse-grain microstates to two metastable states cg = np.zeros(100, dtype=int) cg[50:] = 1 obs_macro = cg[obs_micro] distribution_constraint = pi_macro if constrain_to_coarse_pi else None counting = TransitionCountEstimator(lagtime=lagtime, count_mode="effective")\ .fit(obs_macro).fetch_model().submodel_largest(probability_constraint=distribution_constraint) est = BayesianMSM(reversible=reversible, n_samples=nsamples, stationary_distribution_constraint=distribution_constraint, **kwargs) est.fit(counting) return est
def test_bmsm(self): cc = TransitionCountEstimator(lagtime=1, count_mode="effective").fit(self.dtraj).fetch_model() msm = BayesianMSM().fit(cc.submodel_largest(connectivity_threshold='1/n')).fetch_model() msm_restricted = BayesianMSM().fit(cc.submodel_largest(connectivity_threshold=self.mincount_connectivity)) \ .fetch_model() assert_equal(msm.prior.count_model.state_symbols, self.active_set_unrestricted) assert_equal(msm.samples[0].count_model.state_symbols, self.active_set_unrestricted) assert_equal(msm_restricted.prior.count_model.state_symbols, self.active_set_restricted) assert_equal(msm_restricted.samples[0].count_model.state_symbols, self.active_set_restricted) i = id(msm_restricted.prior.count_model) assert all(id(x.count_model) == i for x in msm_restricted.samples)
def test_with_count_model(self): dtraj = np.random.randint(0, 10, size=(10000,)) with self.assertRaises(ValueError): counts = TransitionCountEstimator(lagtime=1, count_mode="sliding").fit(dtraj).fetch_model() BayesianMSM().fit(counts) # fails because its not effective or sliding-effective counts = TransitionCountEstimator(lagtime=1, count_mode="effective").fit(dtraj).fetch_model() bmsm = BayesianMSM(n_samples=44).fit(counts).fetch_model() np.testing.assert_equal(len(bmsm.samples), 44) bmsm = bmsm.submodel(np.array([3, 4, 5])) np.testing.assert_equal(bmsm.prior.count_model.state_symbols, [3, 4, 5]) for sample in bmsm: np.testing.assert_equal(sample.count_model.state_symbols, [3, 4, 5])
def test_estimator_params(self): estimator = BayesianMSM(n_samples=13, n_steps=55, reversible=False, stationary_distribution_constraint=np.array([0.5, 0.5]), sparse=True, confidence=0.9, maxiter=5000, maxerr=1e-12) np.testing.assert_equal(estimator.n_samples, 13) np.testing.assert_equal(estimator.n_steps, 55) np.testing.assert_equal(estimator.reversible, False) np.testing.assert_equal(estimator.stationary_distribution_constraint, [0.5, 0.5]) np.testing.assert_equal(estimator.sparse, True) np.testing.assert_equal(estimator.confidence, 0.9) np.testing.assert_equal(estimator.maxiter, 5000) np.testing.assert_equal(estimator.maxerr, 1e-12) with self.assertRaises(ValueError): estimator.stationary_distribution_constraint = np.array([1.1, .5]) with self.assertRaises(ValueError): estimator.stationary_distribution_constraint = np.array([.5, -.1])
def test_with_count_matrix(self): count_matrix = np.ones((5, 5), dtype=np.float32) posterior = BayesianMSM(n_samples=33).fit(count_matrix).fetch_model() np.testing.assert_equal(len(posterior.samples), 33)