Exemplo n.º 1
0
    def test_backward_against_ghmm(self):
        from kerehmm.test.util import ghmm_from_gaussian_hmm
        import ghmm
        hmm = self.new_hmm(nDimensions=1,
                           random_emissions=True,
                           random_transitions=True,
                           lower_bounds=[0],
                           upper_bounds=[10])
        hmm_reference = ghmm_from_gaussian_hmm(hmm)
        observation_size = 5
        observed = [
            np.random.randint(0, 10) for _ in range(self.nDimensions)
            for i in range(observation_size)
        ]
        seq = ghmm.EmissionSequence(hmm_reference.emissionDomain,
                                    np.array(observed).flatten().tolist())

        # remember that we have to convert stuff from ghmm to log scale
        _, scale_reference = map(np.array, hmm_reference.forward(seq))
        # print "Forward referece", forward
        print "Scale reference", scale_reference

        # this is the reference backward array, untransformed (scaled)
        backward_reference = np.array(
            hmm_reference.backward(seq, scalingVector=scale_reference))
        print "Backward reference (scaled)", backward_reference

        # unscale the reference array
        # get the product of scale_t,scale_t+1,...,scale_T for each t.
        # coefficients = np.array([np.prod(scale_reference[i:]) for i, _ in enumerate(scale_reference)])
        coefficients = np.array([
            np.multiply.reduce(scale_reference[t + 1:])
            for t, _ in enumerate(scale_reference)
        ])
        print "Reference coefficients:", coefficients

        # multiply each backwards_reference[i] by coefficients[i]
        backward_reference[:] = (np.expand_dims(coefficients, axis=1) *
                                 backward_reference)

        # test shape
        print "Backward reference (unscaled)", backward_reference

        # this is our backward array, log transformed
        backward = hmm.backward(observed)

        print "Backward", np.exp(backward)

        assert backward.shape == backward_reference.shape

        # test values
        # print "Diff:", np.exp(backward) - backward_reference
        backward_unscaled = np.exp(backward)
        assert np.allclose(backward_unscaled, backward_reference)
Exemplo n.º 2
0
    def test_backward_against_ghmm(self):
        from kerehmm.test.util import ghmm_from_gaussian_hmm
        import ghmm

        hmm = self.new_hmm(
            nDimensions=1, random_emissions=True, random_transitions=True, lower_bounds=[0], upper_bounds=[10]
        )
        hmm_reference = ghmm_from_gaussian_hmm(hmm)
        observation_size = 5
        observed = [np.random.randint(0, 10) for _ in range(self.nDimensions) for i in range(observation_size)]
        seq = ghmm.EmissionSequence(hmm_reference.emissionDomain, np.array(observed).flatten().tolist())

        # remember that we have to convert stuff from ghmm to log scale
        _, scale_reference = map(np.array, hmm_reference.forward(seq))
        # print "Forward referece", forward
        print "Scale reference", scale_reference

        # this is the reference backward array, untransformed (scaled)
        backward_reference = np.array(hmm_reference.backward(seq, scalingVector=scale_reference))
        print "Backward reference (scaled)", backward_reference

        # unscale the reference array
        # get the product of scale_t,scale_t+1,...,scale_T for each t.
        # coefficients = np.array([np.prod(scale_reference[i:]) for i, _ in enumerate(scale_reference)])
        coefficients = np.array([np.multiply.reduce(scale_reference[t + 1 :]) for t, _ in enumerate(scale_reference)])
        print "Reference coefficients:", coefficients

        # multiply each backwards_reference[i] by coefficients[i]
        backward_reference[:] = np.expand_dims(coefficients, axis=1) * backward_reference

        # test shape
        print "Backward reference (unscaled)", backward_reference

        # this is our backward array, log transformed
        backward = hmm.backward(observed)

        print "Backward", np.exp(backward)

        assert backward.shape == backward_reference.shape

        # test values
        # print "Diff:", np.exp(backward) - backward_reference
        backward_unscaled = np.exp(backward)
        assert np.allclose(backward_unscaled, backward_reference)
Exemplo n.º 3
0
    def test_forward_against_ghmm(self):
        import ghmm

        hmm = self.new_hmm(
            nDimensions=1, upper_bounds=[10], lower_bounds=[0]  # random_transitions=True, random_emissions=True,
        )
        hmm_reference = ghmm_from_gaussian_hmm(hmm)
        observed = [0.4, 0.1, 0.2, 0.2]
        seq = ghmm.EmissionSequence(hmm_reference.emissionDomain, observed)
        forward = hmm.forward(observed)

        # remember that we have to convert stuff from ghmm to log scale
        forward_reference, scale_reference = map(np.array, hmm_reference.forward(seq))
        forward_reference_log = np.log(forward_reference)
        print "Forward reference (scaled):\n", forward_reference
        print "Scale reference: {}".format(scale_reference)
        for i, c in enumerate(scale_reference):
            forward_reference_log[i] += sum(np.log(scale_reference[: i + 1]))
        print "Forward reference (unscaled):\n", np.exp(forward_reference_log)

        print "Forward:\n", np.exp(forward)

        assert np.allclose(forward, forward_reference_log)
Exemplo n.º 4
0
    def test_forward_against_ghmm(self):
        import ghmm
        hmm = self.new_hmm(
            nDimensions=1,  # random_transitions=True, random_emissions=True,
            upper_bounds=[10],
            lower_bounds=[0])
        hmm_reference = ghmm_from_gaussian_hmm(hmm)
        observed = [.4, .1, .2, .2]
        seq = ghmm.EmissionSequence(hmm_reference.emissionDomain, observed)
        forward = hmm.forward(observed)

        # remember that we have to convert stuff from ghmm to log scale
        forward_reference, scale_reference = map(np.array,
                                                 hmm_reference.forward(seq))
        forward_reference_log = np.log(forward_reference)
        print "Forward reference (scaled):\n", forward_reference
        print "Scale reference: {}".format(scale_reference)
        for i, c in enumerate(scale_reference):
            forward_reference_log[i] += sum(np.log(scale_reference[:i + 1]))
        print "Forward reference (unscaled):\n", np.exp(forward_reference_log)

        print "Forward:\n", np.exp(forward)

        assert np.allclose(forward, forward_reference_log)
Exemplo n.º 5
0
 def to_ghmm(self, hmm):
     if self.nDimensions == 1:
         return ghmm_from_gaussian_hmm(hmm)
     else:
         return ghmm_from_multivariate_continuous_hmm(hmm)
Exemplo n.º 6
0
 def to_ghmm(self, hmm):
     if self.nDimensions == 1:
         return ghmm_from_gaussian_hmm(hmm)
     else:
         return ghmm_from_multivariate_continuous_hmm(hmm)