Пример #1
0
def test_muller_potential_stats():
    # Set constants
    n_seq = 1
    num_trajs = 1
    T = 2500
    num_hotstart = 0

    # Generate data
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    muller = MullerModel()
    data, trajectory, start = \
            muller.generate_dataset(n_seq, num_trajs, T)
    n_features = muller.x_dim
    n_components = muller.K

    # Fit reference model and initial MSLDS model
    refmodel = GaussianHMM(n_components=n_components,
                        covariance_type='full').fit(data)
    model = MetastableSwitchingLDS(n_components, n_features,
            n_hotstart=num_hotstart)
    model.inferrer._sequences = data
    model.means_ = refmodel.means_
    model.covars_ = refmodel.covars_
    model.transmat_ = refmodel.transmat_
    model.populations_ = refmodel.startprob_
    As = []
    for i in range(n_components):
        As.append(np.zeros((n_features, n_features)))
    model.As_ = As
    model.Qs_ = refmodel.covars_
    model.bs_ = refmodel.means_

    iteration = 0 # Remove this step once hot_start is factored out
    logprob, stats = model.inferrer.do_estep()
    rlogprob, rstats = reference_estep(refmodel, data)

    yield lambda: np.testing.assert_array_almost_equal(stats['post'],
            rstats['post'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['post[1:]'],
            rstats['post[1:]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['post[:-1]'],
            rstats['post[:-1]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs'],
            rstats['obs'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs[1:]'],
            rstats['obs[1:]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs[:-1]'],
            rstats['obs[:-1]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs*obs.T'],
            rstats['obs*obs.T'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs*obs[t-1].T'], rstats['obs*obs[t-1].T'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs[1:]*obs[1:].T'], rstats['obs[1:]*obs[1:].T'],
            decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs[:-1]*obs[:-1].T'], rstats['obs[:-1]*obs[:-1].T'],
            decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['trans'], rstats['trans'], decimal=1)
Пример #2
0
    def __init__(self):
        self.K = 2
        self.x_dim = 1
        self.As = np.reshape(np.array([[0.6], [0.6]]),
                                (self.K, self.x_dim, self.x_dim))
        self.bs = np.reshape(np.array([[0.4], [-0.4]]),
                                (self.K, self.x_dim))
        self.Qs = np.reshape(np.array([[0.01], [0.01]]),
                                (self.K, self.x_dim, self.x_dim))
        self.Z = np.reshape(np.array([[0.995, 0.005], [0.005, 0.995]]),
                                (self.K, self.K))
        self.pi = np.reshape(np.array([0.99, 0.01]), (self.K,))
        self.mus = np.reshape(np.array([[1], [-1]]), (self.K, self.x_dim))
        self.Sigmas = np.reshape(np.array([[0.01], [0.01]]),
                                (self.K, self.x_dim, self.x_dim))

        # Generate Solver
        s = MetastableSwitchingLDS(self.K, self.x_dim)
        s.As_ = self.As
        s.bs_ = self.bs
        s.Qs_ = self.Qs
        s.transmat_ = self.Z
        s.populations_ = self.pi
        s.means_ = self.mus
        s.covars_ = self.Sigmas
        self._model = s
Пример #3
0
def test_plusmin_stats():
    # Set constants
    num_hotstart = 3
    n_seq = 1
    T = 2000

    # Generate data
    plusmin = PlusminModel()
    data, hidden = plusmin.generate_dataset(n_seq, T)
    n_features = plusmin.x_dim
    n_components = plusmin.K

    # Fit reference model
    refmodel = GaussianHMM(n_components=n_components,
                        covariance_type='full').fit(data)
    warnings.filterwarnings("ignore", category=DeprecationWarning)

    # Fit initial MSLDS model from reference model
    model = MetastableSwitchingLDS(n_components, n_features,
                                n_hotstart=0)
    model.inferrer._sequences = data
    model.means_ = refmodel.means_
    model.covars_ = refmodel.covars_
    model.transmat_ = refmodel.transmat_
    model.populations_ = refmodel.startprob_
    model.As_ = [np.zeros((n_features, n_features)),
                    np.zeros((n_features, n_features))]
    model.Qs_ = refmodel.covars_
    model.bs_ = refmodel.means_

    iteration = 0 # Remove this step once hot_start is factored out
    logprob, stats = model.inferrer.do_estep()
    rlogprob, rstats = reference_estep(refmodel, data)

    yield lambda: np.testing.assert_array_almost_equal(stats['post'],
            rstats['post'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['post[1:]'],
            rstats['post[1:]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['post[:-1]'],
            rstats['post[:-1]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs'],
            rstats['obs'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs[1:]'],
            rstats['obs[1:]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs[:-1]'],
            rstats['obs[:-1]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs*obs.T'],
            rstats['obs*obs.T'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs*obs[t-1].T'], rstats['obs*obs[t-1].T'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs[1:]*obs[1:].T'], rstats['obs[1:]*obs[1:].T'],
            decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs[:-1]*obs[:-1].T'], rstats['obs[:-1]*obs[:-1].T'],
            decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['trans'], rstats['trans'], decimal=1)
Пример #4
0
def test_randn_stats():
    """
    Sanity test MSLDS sufficient statistic gathering by setting
    dynamics model to 0 and testing that E-step matches that of
    HMM
    """
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    # Generate reference data
    n_states = 2
    n_features = 3
    data = [np.random.randn(100, n_features),
            np.random.randn(100, n_features)]
    refmodel = GaussianHMM(n_components=n_states,
                        covariance_type='full').fit(data)

    # test all of the sufficient statistics against sklearn and pure python

    model = MetastableSwitchingLDS(n_states=n_states,
            n_features=n_features, n_hotstart=0)
    model.inferrer._sequences = data
    model.means_ = refmodel.means_
    model.covars_ = refmodel.covars_
    model.transmat_ = refmodel.transmat_
    model.populations_ = refmodel.startprob_
    # Is there a more elegant way to do this?
    model.As_ = [np.zeros((n_features, n_features)),
                    np.zeros((n_features, n_features))]
    model.Qs_ = refmodel.covars_
    model.bs_ = refmodel.means_

    iteration = 0 # Remove this step once hot_start is factored out
    logprob, stats = model.inferrer.do_estep()
    rlogprob, rstats = reference_estep(refmodel, data)

    yield lambda: np.testing.assert_array_almost_equal(stats['post'],
            rstats['post'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['post[1:]'],
            rstats['post[1:]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['post[:-1]'],
            rstats['post[:-1]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs'],
            rstats['obs'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs[1:]'],
            rstats['obs[1:]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs[:-1]'],
            rstats['obs[:-1]'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(stats['obs*obs.T'],
            rstats['obs*obs.T'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs*obs[t-1].T'], rstats['obs*obs[t-1].T'], decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs[1:]*obs[1:].T'], rstats['obs[1:]*obs[1:].T'],
            decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['obs[:-1]*obs[:-1].T'], rstats['obs[:-1]*obs[:-1].T'],
            decimal=3)
    yield lambda: np.testing.assert_array_almost_equal(
            stats['trans'], rstats['trans'], decimal=3)
Пример #5
0
def test_alanine_dipeptide_stats():
    import pdb, traceback, sys
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    try:
        b = fetch_alanine_dipeptide()
        trajs = b.trajectories
        # While debugging, restrict to first trajectory only
        trajs = [trajs[0]]
        n_seq = len(trajs)
        n_frames = trajs[0].n_frames
        n_atoms = trajs[0].n_atoms
        n_features = n_atoms * 3

        data_home = get_data_home()
        data_dir = join(data_home, TARGET_DIRECTORY_ALANINE)
        top = md.load(join(data_dir, 'ala2.pdb'))
        n_components = 2
        # Superpose m
        data = []
        for traj in trajs:
            traj.superpose(top)
            Z = traj.xyz
            Z = np.reshape(Z, (n_frames, n_features), order='F')
            data.append(Z)

        n_hotstart = 3
        # Fit reference model and initial MSLDS model
        refmodel = GaussianHMM(n_components=n_components,
                            covariance_type='full').fit(data)
        rlogprob, rstats = reference_estep(refmodel, data)

        model = MetastableSwitchingLDS(n_components, n_features,
                n_hotstart=n_hotstart)
        model.inferrer._sequences = data
        model.means_ = refmodel.means_
        model.covars_ = refmodel.covars_
        model.transmat_ = refmodel.transmat_
        model.populations_ = refmodel.startprob_
        As = []
        for i in range(n_components):
            As.append(np.zeros((n_features, n_features)))
        model.As_ = As
        Qs = []
        eps = 1e-7
        for i in range(n_components):
            Q = refmodel.covars_[i] + eps*np.eye(n_features)
            Qs.append(Q)
        model.Qs_ = Qs
        model.bs_ = refmodel.means_
        logprob, stats = model.inferrer.do_estep()

        yield lambda: np.testing.assert_array_almost_equal(stats['post'],
                rstats['post'], decimal=2)
        yield lambda: np.testing.assert_array_almost_equal(stats['post[1:]'],
                rstats['post[1:]'], decimal=2)
        yield lambda: np.testing.assert_array_almost_equal(stats['post[:-1]'],
                rstats['post[:-1]'], decimal=2)
        yield lambda: np.testing.assert_array_almost_equal(stats['obs'],
                rstats['obs'], decimal=1)
        yield lambda: np.testing.assert_array_almost_equal(stats['obs[1:]'],
                rstats['obs[1:]'], decimal=1)
        yield lambda: np.testing.assert_array_almost_equal(stats['obs[:-1]'],
                rstats['obs[:-1]'], decimal=1)
        yield lambda: np.testing.assert_array_almost_equal(stats['obs*obs.T'],
                rstats['obs*obs.T'], decimal=1)
        yield lambda: np.testing.assert_array_almost_equal(
                stats['obs*obs[t-1].T'], rstats['obs*obs[t-1].T'], decimal=1)
        yield lambda: np.testing.assert_array_almost_equal(
                stats['obs[1:]*obs[1:].T'], rstats['obs[1:]*obs[1:].T'],
                decimal=1)
        yield lambda: np.testing.assert_array_almost_equal(
                stats['obs[:-1]*obs[:-1].T'], rstats['obs[:-1]*obs[:-1].T'],
                decimal=1)
        # This test fails consistently. TODO: Figure out why.
        #yield lambda: np.testing.assert_array_almost_equal(
        #        stats['trans'], rstats['trans'], decimal=2)

    except:
        type, value, tb = sys.exc_info()
        traceback.print_exc()
        pdb.post_mortem(tb)