Example #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
Example #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
Example #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
Example #4
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
Example #5
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
Example #6
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