Пример #1
0
def gaussian_hmm(P, means, sigmas, pi=None, stationary=True, reversible=True):
    """ Initializes a 1D-Gaussian HMM

    Parameters
    ----------
    P : ndarray(nstates,nstates)
        Hidden transition matrix
    means : ndarray(nstates, )
        Means of Gaussian output distributions
    sigmas : ndarray(nstates, )
        Standard deviations of Gaussian output distributions
    pi : ndarray(nstates, )
        Fixed initial (if stationary=False) or fixed stationary distribution (if stationary=True).
    stationary : bool, optional, default=True
        If True: initial distribution is equal to stationary distribution of transition matrix
    reversible : bool, optional, default=True
        If True: transition matrix will fulfill detailed balance constraints.

    """
    from bhmm.hmm.gaussian_hmm import GaussianHMM
    from bhmm.output_models.gaussian import GaussianOutputModel
    # count states
    nstates = _np.array(P).shape[0]
    # initialize output model
    output_model = GaussianOutputModel(nstates, means, sigmas)
    # initialize general HMM
    from bhmm.hmm.generic_hmm import HMM as _HMM
    ghmm = _HMM(P,
                output_model,
                Pi=pi,
                stationary=stationary,
                reversible=reversible)
    # turn it into a Gaussian HMM
    ghmm = GaussianHMM(ghmm)
    return ghmm
Пример #2
0
    def __init__(self, nrep=10, kernel='c'):
        self.kernel = kernel
        self.nrep = nrep

        # variables
        self.nexamples = 0
        self.A = []
        self.pi = []
        self.pobs = []
        self.T = []
        self.N = []
        self.alpha = []
        self.beta = []
        self.gamma = []
        self.time_alpha = []
        self.time_beta = []
        self.time_gamma = []
        self.time_c = []
        self.time_C = []
        self.time_vpath = []
        self.alpha_mem = []
        self.beta_mem = []
        self.gamma_mem = []
        self.C_mem = []

        # second example
        A = np.array([[0.97, 0.02, 0.01], [0.1, 0.8, 0.1], [0.01, 0.02, 0.97]])
        pi = np.array([0.45, 0.1, 0.45])
        T = 1000000
        means = np.array([-1.0, 0.0, 1.0])
        sigmas = np.array([0.5, 0.5, 0.5])
        gom = GaussianOutputModel(3, means=means, sigmas=sigmas)
        obs = np.random.randint(3, size=T)
        pobs = gom.p_obs(obs)
        self.append_example(A, pi, pobs)
Пример #3
0
    def setUp(self):
        nstates = 3
        means    = np.array([-0.5, 0.0, 0.5])
        sigmas = np.array([0.2, 0.2, 0.2])
        self.G = GaussianOutputModel(nstates, means=means, sigmas=sigmas)

        # random Gaussian samples
        self.obs = np.random.randn((10000))
Пример #4
0
    def setUp(self):
        self.nexamples = 0
        self.A = []
        self.pi = []
        self.pobs = []
        self.T = []
        self.N = []
        self.logprob = []
        self.alpha = []
        self.time_alpha = []
        self.beta = []
        self.time_beta = []
        self.gamma = []
        self.time_gamma = []
        self.c = []
        self.time_c = []
        self.C = []
        self.time_C = []
        self.vpath = []
        self.time_vpath = []
        self.alpha_mem = []
        self.beta_mem = []
        self.gamma_mem = []
        self.C_mem = []

        # first toy example
        A = np.array([[0.9, 0.1], [0.1, 0.9]])
        pi = np.array([0.5, 0.5])
        pobs = np.array([[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.1, 0.9],
                         [0.5, 0.5], [0.9, 0.1], [0.9, 0.1], [0.9, 0.1],
                         [0.9, 0.1], [0.9, 0.1]])
        self.append_example(A, pi, pobs)

        # second example
        A = np.array([[0.97, 0.02, 0.01], [0.1, 0.8, 0.1], [0.01, 0.02, 0.97]])
        pi = np.array([0.45, 0.1, 0.45])
        T = 10000
        means = np.array([-1.0, 0.0, 1.0])
        sigmas = np.array([0.5, 0.5, 0.5])
        gom = GaussianOutputModel(3, means=means, sigmas=sigmas)
        obs = np.random.randint(3, size=T)
        pobs = gom.p_obs(obs)
        self.append_example(A, pi, pobs)