Пример #1
0
 def __init__(self, hmm):
     # superclass constructors
     if not isinstance(hmm.output_model, GaussianOutputModel):
         raise TypeError('Given hmm is not a Gaussian HMM, but has an output model of type: ' +
                         str(type(hmm.output_model)))
     GaussianOutputModel.__init__(self, hmm.nstates, means=hmm.output_model.means, sigmas=hmm.output_model.sigmas)
     HMM.__init__(self, hmm.initial_distribution, hmm.transition_matrix, self, lag=hmm.lag)
Пример #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)
Пример #5
0
 def __init__(self, hmm):
     # superclass constructors
     if not isinstance(hmm.output_model, GaussianOutputModel):
         raise TypeError(
             'Given hmm is not a Gaussian HMM, but has an output model of type: '
             + str(type(hmm.output_model)))
     GaussianOutputModel.__init__(self,
                                  hmm.nstates,
                                  means=hmm.output_model.means,
                                  sigmas=hmm.output_model.sigmas)
     HMM.__init__(self,
                  hmm.initial_distribution,
                  hmm.transition_matrix,
                  self,
                  lag=hmm.lag)
Пример #6
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
Пример #7
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)
Пример #8
0
class TestOutputGaussian(unittest.TestCase):
    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)

    def tearDown(self):
        pass

    def test_p_obs(self):
        # compare results
        self.G.set_implementation('c')
        time1 = time.time()
        for i in range(10):
            p_c = self.G.p_obs(self.obs)
        time2 = time.time()
        t_c = time2 - time1

        self.G.set_implementation('python')
        time1 = time.time()
        for i in range(10):
            p_p = self.G.p_obs(self.obs)
        time2 = time.time()
        t_p = time2 - time1

        assert (np.allclose(p_c, p_p))

        # speed report
        if print_speedup:
            print('p_obs speedup c/python = ' + str(t_p / t_c))
Пример #9
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)
Пример #10
0
class TestOutputGaussian(unittest.TestCase):

    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))


    def tearDown(self):
        pass


    def test_p_obs(self):
        # compare results
        self.G.set_implementation('c')
        time1 = time.time()
        for i in range(10):
            p_c = self.G.p_obs(self.obs)
        time2 = time.time()
        t_c = time2-time1

        self.G.set_implementation('python')
        time1 = time.time()
        for i in range(10):
            p_p = self.G.p_obs(self.obs)
        time2 = time.time()
        t_p = time2-time1

        assert(np.allclose(p_c, p_p))

        # speed report
        if print_speedup:
            print('p_obs speedup c/python = '+str(t_p/t_c))