def unfreeze(self, destructive=False):
        if destructive:
            obs_distns = [
                MixtureDistribution(weights_obj=o.weights,
                                    components=o.components)
                for o in self.obs_distns
            ]
            dur_distns = self.dur_distns
            trans_distn = self.trans_distn
        else:
            obs_distns = [
                MixtureDistribution(
                    weights_obj=copy.deepcopy(o.weights),
                    components=[copy.deepcopy(c) for c in o.components])
                for o in self.obs_distns
            ]
            dur_distns = copy.deepcopy(self.dur_distns)
            trans_distn = copy.deepcopy(self.trans_distn)

        new = pyhsmm.models.HSMMIntNegBinVariant(obs_distns=obs_distns,
                                                 dur_distns=dur_distns,
                                                 trans_distn=trans_distn)
        for s in self.states_list:
            new.add_data(s.data,
                         stateseq=s.stateseq,
                         left_censoring=s.left_censoring)

        return new
Ejemplo n.º 2
0
    def test_hssmm(self):
        import numpy as np
        from matplotlib import pyplot as plt

        from pyhsmm.models import HSMMIntNegBinVariant
        from pyhsmm.basic.models import MixtureDistribution
        from pyhsmm.basic.distributions import Gaussian, NegativeBinomialIntegerRVariantDuration
        from pyhsmm.util.text import progprint_xrange

        #############################
        #  generate synthetic data  #
        #############################

        states_in_hsmm = 5
        components_per_GMM = 3
        component_hyperparameters = dict(mu_0=np.zeros(2), sigma_0=np.eye(2), kappa_0=0.01, nu_0=3)

        GMMs = [MixtureDistribution(
            alpha_0=4.,
            components=[Gaussian(**component_hyperparameters) for i in range(components_per_GMM)])
            for state in range(states_in_hsmm)]

        true_dur_distns = [
            NegativeBinomialIntegerRVariantDuration(np.r_[0., 0, 0, 0, 0, 1, 1, 1], alpha_0=5., beta_0=5.)
            for state in range(states_in_hsmm)]

        truemodel = HSMMIntNegBinVariant(
            init_state_concentration=10.,
            alpha=6., gamma=6.,
            obs_distns=GMMs,
            dur_distns=true_dur_distns)

        training_datas = [truemodel.generate(1000)[0] for i in range(5)]
        test_data = truemodel.generate(5000)[0]

        #####################################
        #  set up FrozenMixture components  #
        #####################################

        # list of all Gaussians
        component_library = [c for m in GMMs for c in m.components]
        library_size = len(component_library)

        # initialize weights to indicator on one component
        init_weights = np.eye(library_size)

        #obs_distns = [FrozenMixtureDistribution(
        #    components=component_library,
        #    alpha_0=4,
        #    weights=row)
        #    for row in init_weights]

        ################
        #  build HSMM  #
        ################

        dur_distns = [NegativeBinomialIntegerRVariantDuration(np.r_[0., 0, 0, 0, 0, 1, 1, 1], alpha_0=5., beta_0=5.)
                      for state in range(library_size)]
from pyhsmm.basic.models import Mixture, MixtureDistribution
from library_models import FrozenMixtureDistribution, LibraryMM
from pyhsmm.basic.distributions import Gaussian, NegativeBinomialIntegerRVariantDuration
from pyhsmm.util.text import progprint_xrange

#############################
#  generate synthetic data  #
#############################

groups_in_metamm = 5
components_per_gmm = 2
component_hyperparameters = dict(mu_0=np.zeros(2),sigma_0=np.eye(2),kappa_0=0.01,nu_0=3)

GMMs = [MixtureDistribution(
    alpha_0=4.,
    components=[Gaussian(**component_hyperparameters) for i in range(components_per_gmm)])
    for state in range(groups_in_metamm)]

truemodel = Mixture(
        alpha_0=6,
        components=GMMs)

data, truelabels = truemodel.generate(2000)

#####################################
#  set up FrozenMixture components  #
#####################################

# list of all Gaussians
component_library = [c for m in GMMs for c in m.components]
#############################
#  generate synthetic data  #
#############################

states_in_hsmm = 5
components_per_GMM = 3
component_hyperparameters = dict(mu_0=np.zeros(2),
                                 sigma_0=np.eye(2),
                                 kappa_0=0.025,
                                 nu_0=3)

GMMs = [
    MixtureDistribution(alpha_0=4.,
                        components=[
                            Gaussian(**component_hyperparameters)
                            for i in range(components_per_GMM)
                        ]) for state in range(states_in_hsmm)
]

true_dur_distns = [
    NegativeBinomialIntegerRVariantDuration(np.r_[0., 0, 0, 0, 0, 0, 1, 1, 1,
                                                  1],
                                            alpha_0=5.,
                                            beta_0=5.)
    for state in range(states_in_hsmm)
]

truemodel = HSMMIntNegBinVariant(init_state_concentration=10.,
                                 alpha=6.,
                                 gamma=2.,