Ejemplo n.º 1
0
    def __init__(self, B):
        """
        Create a 1D Gaussian output model.

        Parameters
        ----------
        B : ndarray((N,M),dtype=float)
            output probability matrix using N hidden states and M observable symbols.
            This matrix needs to be row-stochastic.

        Examples
        --------

        Create an observation model.

        >>> import numpy as np
        >>> B = np.array([[0.5,0.5],[0.1,0.9]])
        >>> output_model = DiscreteOutputModel(B)

        """
        self._output_probabilities = np.array(B, dtype=config.dtype)
        nstates,self._nsymbols = self._output_probabilities.shape[0],self._output_probabilities.shape[1]
        # superclass constructor
        OutputModel.__init__(self, nstates)
        # test if row-stochastic
        assert np.allclose(np.sum(self._output_probabilities, axis=1), np.ones(self.nstates)), 'B is not a stochastic matrix'
        # set output matrix
        self._output_probabilities = B
Ejemplo n.º 2
0
    def __init__(self, B):
        """
        Create a 1D Gaussian output model.

        Parameters
        ----------
        B : ndarray((N,M),dtype=float)
            output probability matrix using N hidden states and M observable symbols.
            This matrix needs to be row-stochastic.

        Examples
        --------

        Create an observation model.

        >>> import numpy as np
        >>> B = np.array([[0.5,0.5],[0.1,0.9]])
        >>> output_model = DiscreteOutputModel(B)

        """
        self._output_probabilities = np.array(B, dtype=config.dtype)
        nstates,self._nsymbols = self._output_probabilities.shape[0],self._output_probabilities.shape[1]
        # superclass constructor
        OutputModel.__init__(self, nstates)
        # test if row-stochastic
        assert np.allclose(np.sum(self._output_probabilities, axis=1), np.ones(self.nstates)), 'B is not a stochastic matrix'
        # set output matrix
        self._output_probabilities = B
Ejemplo n.º 3
0
    def __init__(self, B, prior=None, ignore_outliers=False):
        """
        Create a 1D Gaussian output model.

        Parameters
        ----------
        B : ndarray((N, M), dtype=float)
            output probability matrix using N hidden states and M observable symbols.
            This matrix needs to be row-stochastic.
        prior : None or broadcastable to ndarray((N, M), dtype=float)
            Prior for the initial distribution of the HMM.
            Currently implements the Dirichlet prior that is conjugate to the
            Dirichlet distribution of :math:`b_i`. :math:`b_i` is sampled from:
            .. math:
                b_i \sim \prod_j b_{ij}_i^{a_{ij} + n_{ij} - 1}
            where :math:`n_{ij}` are the number of times symbol :math:`j` has
            been observed when the hidden trajectory was in state :math:`i`
            and :math:`a_{ij}` is the prior count.
            The default prior=None corresponds to :math:`a_{ij} = 0`.
                This option ensures coincidence between sample mean an MLE.

        Examples
        --------

        Create an observation model.

        >>> import numpy as np
        >>> B = np.array([[0.5, 0.5], [0.1, 0.9]])
        >>> output_model = DiscreteOutputModel(B)

        """
        self._output_probabilities = np.array(B, dtype=config.dtype)
        nstates, self._nsymbols = self._output_probabilities.shape[
            0], self._output_probabilities.shape[1]
        # superclass constructor
        OutputModel.__init__(self, nstates, ignore_outliers=ignore_outliers)
        # test if row-stochastic
        assert np.allclose(self._output_probabilities.sum(axis=1),
                           np.ones(self.nstates)), 'B is no stochastic matrix'
        # set prior matrix
        if prior is None:
            prior = np.zeros((nstates, self._nsymbols))
        else:
            prior = np.zeros(
                (nstates,
                 self._nsymbols)) + prior  # will fail if not broadcastable
        self.prior = prior
Ejemplo n.º 4
0
 def __repr__(self):
     from bhmm.output_models import OutputModel
     if issubclass(self.__class__, OutputModel):
         outrepr = repr(OutputModel.__repr__(self))
     else:
         outrepr = repr(self.output_model)
     """ Returns a string representation of the HMM """
     return "HMM(%d, %s, %s, Pi=%s, stationary=%s, reversible=%s)" % (
         self._nstates, repr(self._Tij), outrepr, repr(
             self._Pi), repr(self._stationary), repr(self._reversible))
Ejemplo n.º 5
0
    def __init__(self, nstates, means=None, sigmas=None):
        """
        Create a 1D Gaussian output model.

        Parameters
        ----------
        nstates : int
            The number of output states.
        means : array_like of shape (nstates,), optional, default=None
            If specified, initialize the Gaussian means to these values.
        sigmas : array_like of shape (nstates,), optional, default=None
            If specified, initialize the Gaussian variances to these values.

        Examples
        --------

        Create an observation model.

        >>> output_model = GaussianOutputModel(nstates=3, means=[-1, 0, 1], sigmas=[0.5, 1, 2])

        """
        OutputModel.__init__(self, nstates)

        dtype = config.dtype  # type for internal storage

        if means is not None:
            self._means = np.array(means, dtype=dtype)
            if self._means.shape != (nstates, ):
                raise Exception('means must have shape (%d,); instead got %s' %
                                (nstates, str(self._means.shape)))
        else:
            self._means = np.zeros([nstates], dtype=dtype)

        if sigmas is not None:
            self._sigmas = np.array(sigmas, dtype=dtype)
            if self._sigmas.shape != (nstates, ):
                raise Exception(
                    'sigmas must have shape (%d,); instead got %s' %
                    (nstates, str(self._sigmas.shape)))
        else:
            self._sigmas = np.zeros([nstates], dtype=dtype)

        return
Ejemplo n.º 6
0
    def __init__(self, B, prior=None, ignore_outliers=False):
        """
        Create a 1D Gaussian output model.

        Parameters
        ----------
        B : ndarray((N, M), dtype=float)
            output probability matrix using N hidden states and M observable symbols.
            This matrix needs to be row-stochastic.
        prior : None or broadcastable to ndarray((N, M), dtype=float)
            Prior for the initial distribution of the HMM.
            Currently implements the Dirichlet prior that is conjugate to the
            Dirichlet distribution of :math:`b_i`. :math:`b_i` is sampled from:
            .. math:
                b_i \sim \prod_j b_{ij}_i^{a_{ij} + n_{ij} - 1}
            where :math:`n_{ij}` are the number of times symbol :math:`j` has
            been observed when the hidden trajectory was in state :math:`i`
            and :math:`a_{ij}` is the prior count.
            The default prior=None corresponds to :math:`a_{ij} = 0`.
                This option ensures coincidence between sample mean an MLE.

        Examples
        --------

        Create an observation model.

        >>> import numpy as np
        >>> B = np.array([[0.5, 0.5], [0.1, 0.9]])
        >>> output_model = DiscreteOutputModel(B)

        """
        self._output_probabilities = np.array(B, dtype=config.dtype)
        nstates, self._nsymbols = self._output_probabilities.shape[0], self._output_probabilities.shape[1]
        # superclass constructor
        OutputModel.__init__(self, nstates, ignore_outliers=ignore_outliers)
        # test if row-stochastic
        assert np.allclose(self._output_probabilities.sum(axis=1), np.ones(self.nstates)), 'B is no stochastic matrix'
        # set prior matrix
        if prior is None:
            prior = np.zeros((nstates, self._nsymbols))
        else:
            prior = np.zeros((nstates, self._nsymbols)) + prior  # will fail if not broadcastable
        self.prior = prior
Ejemplo n.º 7
0
    def __init__(self, nstates, means=None, sigmas=None, ignore_outliers=True):
        """
        Create a 1D Gaussian output model.

        Parameters
        ----------
        nstates : int
            The number of output states.
        means : array_like of shape (nstates,), optional, default=None
            If specified, initialize the Gaussian means to these values.
        sigmas : array_like of shape (nstates,), optional, default=None
            If specified, initialize the Gaussian variances to these values.

        Examples
        --------

        Create an observation model.

        >>> output_model = GaussianOutputModel(nstates=3, means=[-1, 0, 1], sigmas=[0.5, 1, 2])

        """
        OutputModel.__init__(self, nstates, ignore_outliers=ignore_outliers)

        dtype = config.dtype  # type for internal storage

        if means is not None:
            self._means = np.array(means, dtype=dtype)
            if self._means.shape != (nstates,):
                raise Exception('means must have shape (%d,); instead got %s' % (nstates, str(self._means.shape)))
        else:
            self._means = np.zeros([nstates], dtype=dtype)

        if sigmas is not None:
            self._sigmas = np.array(sigmas, dtype=dtype)
            if self._sigmas.shape != (nstates,):
                raise Exception('sigmas must have shape (%d,); instead got %s' % (nstates, str(self._sigmas.shape)))
        else:
            self._sigmas = np.zeros([nstates], dtype=dtype)

        return
Ejemplo n.º 8
0
 def __repr__(self):
     from bhmm.output_models import OutputModel
     if issubclass(self.__class__, OutputModel):
         outrepr = repr(OutputModel.__repr__(self))
     else:
         outrepr = repr(self.output_model)
     """ Returns a string representation of the HMM """
     return "HMM(%d, %s, %s, Pi=%s, stationary=%s, reversible=%s)" % (self._nstates,
                                                                      repr(self._Tij),
                                                                      outrepr,
                                                                      repr(self._Pi),
                                                                      repr(self._stationary),
                                                                      repr(self._reversible))
Ejemplo n.º 9
0
 def __str__(self):
     """ Returns a human-readable string representation of the HMM """
     output  = 'Hidden Markov model\n'
     output += '-------------------\n'
     output += 'nstates: %d\n' % self._nstates
     output += 'Tij:\n'
     output += str(self._Tij) + '\n'
     output += 'Pi:\n'
     output += str(self._Pi) + '\n'
     output += 'output model:\n'
     from bhmm.output_models import OutputModel
     if issubclass(self.__class__, OutputModel):
         output += str(OutputModel.__str__(self))
     else:
         output += str(self.output_model)
     output += '\n'
     return output
Ejemplo n.º 10
0
 def __str__(self):
     """ Returns a human-readable string representation of the HMM """
     output = 'Hidden Markov model\n'
     output += '-------------------\n'
     output += 'nstates: %d\n' % self._nstates
     output += 'Tij:\n'
     output += str(self._Tij) + '\n'
     output += 'Pi:\n'
     output += str(self._Pi) + '\n'
     output += 'output model:\n'
     from bhmm.output_models import OutputModel
     if issubclass(self.__class__, OutputModel):
         output += str(OutputModel.__str__(self))
     else:
         output += str(self.output_model)
     output += '\n'
     return output
Ejemplo n.º 11
0
    def __str__(self):
        """ Returns a human-readable string representation of the HMM """
        output = "Hidden Markov model\n"
        output += "-------------------\n"
        output += "nstates: %d\n" % self._nstates
        output += "Tij:\n"
        output += str(self._Tij) + "\n"
        output += "Pi:\n"
        output += str(self._Pi) + "\n"
        output += "output model:\n"
        from bhmm.output_models import OutputModel

        if issubclass(self.__class__, OutputModel):
            output += str(OutputModel.__str__(self))
        else:
            output += str(self.output_model)
        output += "\n"
        return output