Ejemplo n.º 1
0
    def fit(self, sequences, y=None):
        """Estimate model parameters.

        Parameters
        ----------
        sequences : list
            List of integer sequences, each of which is one-dimensional
        y : unused parameter

        Returns
        -------
        self
        """
        if self.n_states is None:
            self.n_states = np.max([np.max(x) for x in sequences]) + 1

        from msmbuilder import MSMLib

        MSMLib.logger.info = lambda *args: None
        from msmbuilder.msm_analysis import get_eigenvectors
        from msmbuilder.MSMLib import mle_reversible_count_matrix, estimate_transition_matrix, ergodic_trim

        self.rawcounts_ = self._count_transitions(sequences)
        if self.prior_counts > 0:
            self.rawcounts_ = scipy.sparse.csr_matrix(self.rawcounts_.todense() + self.prior_counts)

        # STEP (1): Ergodic trimming
        if self.ergodic_trim:
            self.rawcounts_, mapping = ergodic_trim(scipy.sparse.csr_matrix(self.rawcounts_))
            self.mapping_ = {}
            for i, j in enumerate(mapping):
                if j != -1:
                    self.mapping_[i] = j
        else:
            self.mapping_ = dict((zip(np.arange(self.n_states), np.arange(self.n_states))))

        # STEP (2): Reversible counts matrix
        if self.reversible_type in ["mle", "MLE"]:
            self.countsmat_ = mle_reversible_count_matrix(self.rawcounts_)
        elif self.reversible_type in ["transpose", "Transpose"]:
            self.countsmat_ = 0.5 * (self.rawcounts_ + self.rawcounts_.T)
        elif self.reversible_type is None:
            self.countsmat_ = self.rawcounts_
        else:
            raise RuntimeError()

        # STEP (3): transition matrix
        self.transmat_ = estimate_transition_matrix(self.countsmat_)

        # STEP (3.5): Stationary eigenvector
        if self.reversible_type in ["mle", "MLE", "transpose", "Transpose"]:
            self.populations_ = np.array(self.countsmat_.sum(0)).flatten()
        elif self.reversible_type is None:
            vectors = get_eigenvectors(self.transmat_, 5)[1]
            self.populations_ = vectors[:, 0]
        else:
            raise RuntimeError()
        self.populations_ /= self.populations_.sum()  # ensure normalization

        return self
Ejemplo n.º 2
0
def estimate_mle_populations(matrix):
    if msmb_version == '2.8.2':
        t_matrix = estimate_transition_matrix(matrix)
        populations = get_eigenvectors(t_matrix, 1, **kwargs)[1][:, 0]
        return populations
    elif msmb_version == '3.2.0':
        obj = MarkovStateModel()
        populations = obj._fit_mle(matrix)[1]
        return populations
Ejemplo n.º 3
0
    def tprob(self):
        # associate each point with a unique index starting at 0
        n_pts = len(self.all_points)
        mapping = {}
        for i, pt in enumerate(self.all_points):
            mapping[pt] = i

        # convert the indices of the counts matrix to ints
        tcounts = scipy.sparse.dok_matrix((n_pts, n_pts), dtype=np.double)
        for key, val in self.tcounts_dd.iteritems():
            p1, p2 = key
            i, j = mapping[p1], mapping[p2]
            tcounts[i, j] = val

        tcounts = tcounts + tcounts.T

        # call msmbuilder libraries
        # this one is just the regular row normalization estimator, and is not
        # guarenteed to be reversible
        return mapping, estimate_transition_matrix(tcounts)
Ejemplo n.º 4
0
    def tprob(self):
        # associate each point with a unique index starting at 0
        n_pts = len(self.all_points)
        mapping = {}
        for i, pt in enumerate(self.all_points):
            mapping[pt] = i

        # convert the indices of the counts matrix to ints
        tcounts = scipy.sparse.dok_matrix((n_pts,n_pts), dtype=np.double)
        for key, val in self.tcounts_dd.iteritems():
            p1, p2 = key
            i, j = mapping[p1], mapping[p2]
            tcounts[i,j] = val
        
        tcounts = tcounts + tcounts.T

        # call msmbuilder libraries
        # this one is just the regular row normalization estimator, and is not
        # guarenteed to be reversible
        return mapping, estimate_transition_matrix(tcounts)