示例#1
0
    def _forward_backward(self, itraj):
        """
        Estimation step: Runs the forward-back algorithm on trajectory with index itraj

        Parameters
        ----------
        itraj : int
            index of the observation trajectory to process

        Results
        -------
        logprob : float
            The probability to observe the observation sequence given the HMM
            parameters
        gamma : ndarray(T,N, dtype=float)
            state probabilities for each t
        count_matrix : ndarray(N,N, dtype=float)
            the Baum-Welch transition count matrix from the hidden state
            trajectory

        """
        # get parameters
        A = self._hmm.transition_matrix
        pi = self._hmm.initial_distribution
        obs = self._observations[itraj]
        T = len(obs)
        # compute output probability matrix
        # t1 = time.time()
        self._hmm.output_model.p_obs(obs, out=self._pobs)
        # t2 = time.time()
        # self._fbtimings[0] += t2-t1
        # forward variables
        logprob = hidden.forward(A, self._pobs, pi, T=T,
                                 alpha_out=self._alpha)[0]
        # t3 = time.time()
        # self._fbtimings[1] += t3-t2
        # backward variables
        hidden.backward(A, self._pobs, T=T, beta_out=self._beta)
        # t4 = time.time()
        # self._fbtimings[2] += t4-t3
        # gamma
        hidden.state_probabilities(self._alpha,
                                   self._beta,
                                   T=T,
                                   gamma_out=self._gammas[itraj])
        # t5 = time.time()
        # self._fbtimings[3] += t5-t4
        # count matrix
        hidden.transition_counts(self._alpha,
                                 self._beta,
                                 A,
                                 self._pobs,
                                 T=T,
                                 out=self._Cs[itraj])
        # t6 = time.time()
        # self._fbtimings[4] += t6-t5
        # return results
        return logprob
示例#2
0
 def run_forward(self, i, out):
     logprob = 0
     alpha = None
     hidden.set_implementation(self.kernel)
     time1 = time.time()
     for k in range(self.nrep):
         logprob, alpha = hidden.forward(self.A[i], self.pobs[i], self.pi[i], alpha_out=out)
     # compare
     time2 = time.time()
     d = (time2-time1)/(1.0*self.nrep)
     return (logprob, alpha, d)
示例#3
0
 def run_forward(self, i, out):
     logprob = 0
     alpha = None
     hidden.set_implementation(self.kernel)
     time1 = time.time()
     for k in range(self.nrep):
         logprob, alpha = hidden.forward(self.A[i], self.pobs[i], self.pi[i], alpha_out=out)
     # compare
     time2 = time.time()
     d = (time2-time1)/(1.0*self.nrep)
     return logprob, alpha, d
示例#4
0
 def run_forward(self, i, kernel, out):
     nrep = max(1, int(10000/self.T[i]))
     logprob = 0
     alpha = None
     hidden.set_implementation(kernel)
     time1 = time.time()
     for k in range(nrep):
         logprob, alpha = hidden.forward(self.A[i], self.pobs[i], self.pi[i], alpha_out=out)
     # compare
     time2 = time.time()
     d = (time2-time1) / (1.0*nrep)
     return logprob, alpha, d
示例#5
0
    def _forward_backward(self, itraj):
        """
        Estimation step: Runs the forward-back algorithm on trajectory with index itraj

        Parameters
        ----------
        itraj : int
            index of the observation trajectory to process

        Results
        -------
        logprob : float
            The probability to observe the observation sequence given the HMM
            parameters
        gamma : ndarray(T,N, dtype=float)
            state probabilities for each t
        count_matrix : ndarray(N,N, dtype=float)
            the Baum-Welch transition count matrix from the hidden state
            trajectory

        """
        # get parameters
        A = self._hmm.transition_matrix
        pi = self._hmm.initial_distribution
        obs = self._observations[itraj]
        T = len(obs)
        # compute output probability matrix
        # t1 = time.time()
        self._hmm.output_model.p_obs(obs, out=self._pobs)
        # t2 = time.time()
        # self._fbtimings[0] += t2-t1
        # forward variables
        logprob = hidden.forward(A, self._pobs, pi, T=T, alpha_out=self._alpha)[0]
        # t3 = time.time()
        # self._fbtimings[1] += t3-t2
        # backward variables
        hidden.backward(A, self._pobs, T=T, beta_out=self._beta)
        # t4 = time.time()
        # self._fbtimings[2] += t4-t3
        # gamma
        hidden.state_probabilities(self._alpha, self._beta, T=T, gamma_out=self._gammas[itraj])
        # t5 = time.time()
        # self._fbtimings[3] += t5-t4
        # count matrix
        hidden.transition_counts(self._alpha, self._beta, A, self._pobs, T=T, out=self._Cs[itraj])
        # t6 = time.time()
        # self._fbtimings[4] += t6-t5
        # return results
        return logprob
示例#6
0
 def run_forward(self, i, kernel, out):
     nrep = max(1, int(10000 / self.T[i]))
     logprob = 0
     alpha = None
     hidden.set_implementation(kernel)
     time1 = time.time()
     for k in range(nrep):
         logprob, alpha = hidden.forward(self.A[i],
                                         self.pobs[i],
                                         self.pi[i],
                                         alpha_out=out)
     # compare
     time2 = time.time()
     d = (time2 - time1) / (1.0 * nrep)
     return (logprob, alpha, d)
示例#7
0
 def run_all(self, A, pobs, pi):
     # forward
     logprob, alpha = hidden.forward(A, pobs, pi)
     # backward
     beta = hidden.backward(A, pobs)
     # gamma
     gamma = hidden.state_probabilities(alpha, beta)
     # state counts
     T = pobs.shape[0]
     statecount = hidden.state_counts(gamma, T)
     # transition counts
     C = hidden.transition_counts(alpha, beta, A, pobs)
     # viterbi path
     vpath = hidden.viterbi(A, pobs, pi)
     # return
     return logprob, alpha, beta, gamma, statecount, C, vpath
示例#8
0
 def run_all(self, A, pobs, pi):
     # forward
     logprob, alpha = hidden.forward(A, pobs, pi)
     # backward
     beta = hidden.backward(A, pobs)
     # gamma
     gamma = hidden.state_probabilities(alpha, beta)
     # state counts
     T = pobs.shape[0]
     statecount = hidden.state_counts(gamma, T)
     # transition counts
     C = hidden.transition_counts(alpha, beta, A, pobs)
     # viterbi path
     vpath = hidden.viterbi(A, pobs, pi)
     # return
     return (logprob, alpha, beta, gamma, statecount, C, vpath)
示例#9
0
 def run_all_mem(self, A, pobs, pi):
     T = pobs.shape[0]
     N = A.shape[0]
     alpha = np.zeros((T, N))
     beta = np.zeros((T, N))
     gamma = np.zeros((T, N))
     C = np.zeros((N, N))
     logprob, alpha = hidden.forward(A, pobs, pi, alpha_out=alpha)
     # backward
     hidden.backward(A, pobs, beta_out=beta)
     # gamma
     hidden.state_probabilities(alpha, beta, gamma_out=gamma)
     # state counts
     statecount = hidden.state_counts(gamma, T)
     # transition counts
     hidden.transition_counts(alpha, beta, A, pobs, out=self.C)
     # viterbi path
     vpath = hidden.viterbi(A, pobs, pi)
     # return
     return logprob, alpha, beta, gamma, statecount, C, vpath
示例#10
0
 def run_all_mem(self, A, pobs, pi):
     T = pobs.shape[0]
     N = A.shape[0]
     alpha = np.zeros((T, N))
     beta = np.zeros((T, N))
     gamma = np.zeros((T, N))
     C = np.zeros((N, N))
     logprob, alpha = hidden.forward(A, pobs, pi, alpha_out=alpha)
     # backward
     hidden.backward(A, pobs, beta_out=beta)
     # gamma
     hidden.state_probabilities(alpha, beta, gamma_out=gamma)
     # state counts
     statecount = hidden.state_counts(gamma, T)
     # transition counts
     hidden.transition_counts(alpha, beta, A, pobs, out=self.C)
     # viterbi path
     vpath = hidden.viterbi(A, pobs, pi)
     # return
     return (logprob, alpha, beta, gamma, statecount, C, vpath)
示例#11
0
    def _sampleHiddenStateTrajectory(self, obs, dtype=np.int32):
        """Sample a hidden state trajectory from the conditional distribution P(s | T, E, o)

        Parameters
        ----------
        o_t : numpy.array with dimensions (T,)
            observation[n] is the nth observation
        dtype : numpy.dtype, optional, default=numpy.int32
            The dtype to to use for returned state trajectory.

        Returns
        -------
        s_t : numpy.array with dimensions (T,) of type `dtype`
            Hidden state trajectory, with s_t[t] the hidden state corresponding to observation o_t[t]

        Examples
        --------
        >>> import bhmm
        >>> [model, observations, states, sampled_model] = bhmm.testsystems.generate_random_bhmm(ntrajectories=5, length=1000)
        >>> o_t = observations[0]
        >>> s_t = sampled_model._sampleHiddenStateTrajectory(o_t)

        """

        # Determine observation trajectory length
        T = obs.shape[0]

        # Convenience access.
        A = self.model.transition_matrix
        pi = self.model.initial_distribution

        # compute output probability matrix
        self.model.output_model.p_obs(obs, out=self.pobs)
        # compute forward variables
        logprob = hidden.forward(A, self.pobs, pi, T=T,
                                 alpha_out=self.alpha)[0]
        # sample path
        S = hidden.sample_path(self.alpha, A, self.pobs, T=T)

        return S
示例#12
0
    def _sampleHiddenStateTrajectory(self, obs, dtype=np.int32):
        """Sample a hidden state trajectory from the conditional distribution P(s | T, E, o)

        Parameters
        ----------
        o_t : numpy.array with dimensions (T,)
            observation[n] is the nth observation
        dtype : numpy.dtype, optional, default=numpy.int32
            The dtype to to use for returned state trajectory.

        Returns
        -------
        s_t : numpy.array with dimensions (T,) of type `dtype`
            Hidden state trajectory, with s_t[t] the hidden state corresponding to observation o_t[t]

        Examples
        --------
        >>> import bhmm
        >>> [model, observations, states, sampled_model] = bhmm.testsystems.generate_random_bhmm(ntrajectories=5, length=1000)
        >>> o_t = observations[0]
        >>> s_t = sampled_model._sampleHiddenStateTrajectory(o_t)

        """

        # Determine observation trajectory length
        T = obs.shape[0]

        # Convenience access.
        A = self.model.transition_matrix
        pi = self.model.initial_distribution

        # compute output probability matrix
        self.model.output_model.p_obs(obs, out=self.pobs)
        # forward variables
        logprob = hidden.forward(A, self.pobs, pi, T=T, alpha_out=self.alpha)[0]
        # sample path
        S = hidden.sample_path(self.alpha, A, self.pobs, T=T)

        return S