Exemplo n.º 1
0
    def _estimate(self, dtrajs):
        # ensure right format
        dtrajs = ensure_dtraj_list(dtrajs)
        # conduct MLE estimation (superclass) first
        _MLMSM._estimate(self, dtrajs)

        # transition matrix sampler
        from msmtools.estimation import tmatrix_sampler
        from math import sqrt
        if self.nsteps is None:
            self.nsteps = int(sqrt(
                self.nstates))  # heuristic for number of steps to decorrelate
        # use the same count matrix as the MLE. This is why we have effective as a default
        if self.statdist_constraint is None:
            tsampler = tmatrix_sampler(self.count_matrix_active,
                                       reversible=self.reversible,
                                       T0=self.transition_matrix,
                                       nsteps=self.nsteps)
        else:
            # Use the stationary distribution on the active set of states
            statdist_active = self.pi
            # We can not uise the MLE as T0. Use the initialization in the reversible pi sampler
            tsampler = tmatrix_sampler(self.count_matrix_active,
                                       reversible=self.reversible,
                                       mu=statdist_active,
                                       nsteps=self.nsteps)

        self._progress_register(self.nsamples,
                                description="Sampling MSMs",
                                stage=0)

        if self.show_progress:

            def call_back():
                self._progress_update(1, stage=0)
        else:
            call_back = None

        sample_Ps, sample_mus = tsampler.sample(nsamples=self.nsamples,
                                                return_statdist=True,
                                                call_back=call_back)
        self._progress_force_finish(0)

        # construct sampled MSMs
        samples = []
        for i in range(self.nsamples):
            samples.append(
                _MSM(sample_Ps[i],
                     pi=sample_mus[i],
                     reversible=self.reversible,
                     dt_model=self.dt_model))

        # update self model
        self.update_model_params(samples=samples)

        # done
        return self
Exemplo n.º 2
0
    def _estimate(self, dtrajs):

        if self.core_set is not None and self.count_mode == 'effective':
            raise RuntimeError(
                'Cannot estimate core set MSM with effective counting.')

        # conduct MLE estimation (superclass) first
        _MLMSM._estimate(self, dtrajs)

        # transition matrix sampler
        from msmtools.estimation import tmatrix_sampler
        from math import sqrt
        if self.nsteps is None:
            self.nsteps = int(sqrt(
                self.nstates))  # heuristic for number of steps to decorrelate
        # use the same count matrix as the MLE. This is why we have effective as a default
        if self.statdist_constraint is None:
            tsampler = tmatrix_sampler(self.count_matrix_active,
                                       reversible=self.reversible,
                                       T0=self.transition_matrix,
                                       nsteps=self.nsteps)
        else:
            # Use the stationary distribution on the active set of states
            statdist_active = self.pi
            # We can not use the MLE as T0. Use the initialization in the reversible pi sampler
            tsampler = tmatrix_sampler(self.count_matrix_active,
                                       reversible=self.reversible,
                                       mu=statdist_active,
                                       nsteps=self.nsteps)

        if self.show_progress:  #and self.nstates >= 1000:
            self._progress_register(self.nsamples,
                                    '{}: Sampling MSMs'.format(self.name),
                                    stage=0)
            call_back = lambda: self._progress_update(1)
        else:
            call_back = None

        with self._progress_context(stage='all'):
            sample_Ps, sample_mus = tsampler.sample(nsamples=self.nsamples,
                                                    return_statdist=True,
                                                    call_back=call_back)
        # construct sampled MSMs
        samples = []
        for P, pi in zip(sample_Ps, sample_mus):
            samples.append(
                _MSM(P,
                     pi=pi,
                     reversible=self.reversible,
                     dt_model=self.dt_model))

        # update self model
        self.update_model_params(samples=samples)

        # done
        return self
Exemplo n.º 3
0
    def simulate(self, num_traj, time_steps):
        """
        Generates trajectories of the model with initial states sampled from the
        stationary distribution.

        Parameters
        ----------
        num_traj : int 
            number of trajectories
        time_steps : int 
            number of time steps to simulate
        """
        M = _MSM(self.P)
        hidden_trajectories = M.simulate(num_traj, time_steps)
        observables = _np.ndarray(_np.shape(hidden_trajectories), int)
        kr = _np.shape(observables)[0]
        kc = _np.shape(observables)[1]
        for i in range(0, kr):
            for j in range(0, kc):
                hs = hidden_trajectories[i, j]
                x = _np.random.choice(a=3, p=self.pobs[hs])
                observables[i, j] = x
        return [hidden_trajectories, observables]
Exemplo n.º 4
0
    def simulate_initialized(self, initial_states, time_steps):
        """
        Generates trajectories of the model given a set of initial states

        Parameters
        ----------
        initial_states : int array 
            set of initial states
        time_steps : int 
            number of time steps to simulate
        """
        num_traj = _np.size(initial_states)
        M = _MSM(self.P)
        hidden_trajectories = M.simulate_initialized(initial_states,
                                                     time_steps)
        observables = _np.ndarray(_np.shape(hidden_trajectories), int)
        kr = _np.shape(observables)[0]
        kc = _np.shape(observables)[1]
        for i in range(0, kr):
            for j in range(0, kc):
                hs = hidden_trajectories[i, j]
                x = _np.random.choice(a=3, p=self.pobs[hs])
                observables[i, j] = x
        return [hidden_trajectories, observables]
Exemplo n.º 5
0
    def _estimate(self, dtrajs):
        """

        Parameters
        ----------
        dtrajs : list containing ndarrays(dtype=int) or ndarray(n, dtype=int)
            discrete trajectories, stored as integer ndarrays (arbitrary size)
            or a single ndarray for only one trajectory.

        Return
        ------
        hmsm : :class:`EstimatedHMSM <pyemma.msm.estimators.hmsm_estimated.EstimatedHMSM>`
            Estimated Hidden Markov state model

        """
        # ensure right format
        dtrajs = ensure_dtraj_list(dtrajs)
        # conduct MLE estimation (superclass) first
        _MLMSM._estimate(self, dtrajs)

        # transition matrix sampler
        from msmtools.estimation import tmatrix_sampler
        from math import sqrt
        if self.nsteps is None:
            self.nsteps = int(sqrt(
                self.nstates))  # heuristic for number of steps to decorrelate
        # use the same count matrix as the MLE. This is why we have effective as a default
        if self.statdist_constraint is None:
            tsampler = tmatrix_sampler(self.count_matrix_active,
                                       reversible=self.reversible,
                                       T0=self.transition_matrix,
                                       nsteps=self.nsteps)
        else:
            # Use the stationary distribution on the active set of states
            statdist_active = self.pi
            # We can not uise the MLE as T0. Use the initialization in the reversible pi sampler
            tsampler = tmatrix_sampler(self.count_matrix_active,
                                       reversible=self.reversible,
                                       mu=statdist_active,
                                       nsteps=self.nsteps)

        self._progress_register(self.nsamples,
                                description="Sampling MSMs",
                                stage=0)

        if self.show_progress:

            def call_back():
                self._progress_update(1, stage=0)
        else:
            call_back = None

        sample_Ps, sample_mus = tsampler.sample(nsamples=self.nsamples,
                                                return_statdist=True,
                                                call_back=call_back)
        self._progress_force_finish(0)

        # construct sampled MSMs
        samples = []
        for i in range(self.nsamples):
            samples.append(
                _MSM(sample_Ps[i],
                     pi=sample_mus[i],
                     reversible=self.reversible,
                     dt_model=self.dt_model))

        # update self model
        self.update_model_params(samples=samples)

        # done
        return self