Exemple #1
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)]
            # hyperparameters not used
            mu_0=np.zeros_like(mu),sigma_0=np.eye(sigma.shape[0]),kappa_0=1.,nu_0=mu.shape[0]+5,
            ) for mu, sigma in zip(means,sigmas)]

library_size = len(component_library)

obs_distns = [FrozenMixtureDistribution(
    components=component_library,
    a_0=1.0,b_0=0.05)
    for i in xrange(Nmax)]

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

dur_distns = [NegativeBinomialIntegerRVariantDuration(np.r_[0.,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1],alpha_0=25.,beta_0=25.)
        for state in range(Nmax)]

model = LibraryHSMMIntNegBinVariant(
        init_state_concentration=10.,
        alpha_a_0=1.0,alpha_b_0=0.1,
        gamma_a_0=0.1,gamma_b_0=200.,
        obs_distns=obs_distns,
        dur_distns=dur_distns)

for data in training_datas:
    model.add_data_parallel(data,left_censoring=True)
    # model.add_data(data,left_censoring=True)

##################
#  infer things  #
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.,
                                 obs_distns=GMMs,
                                 dur_distns=true_dur_distns)

training_datas = [truemodel.rvs(1000) for i in range(num_training_seqs)]
test_data = truemodel.rvs(1000)

#####################################
#  set up FrozenMixture components  #
Exemple #4
0
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, 1, 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=6.,
                                 obs_distns=GMMs,
                                 dur_distns=true_dur_distns)

datas = [truemodel.generate(40000)[0][5:] for i in range(6)]

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