Beispiel #1
0
def initialize_model(Nmax, dim):
    """
    Initialize a HSMM model.

    Parameters
    ------
    Nmax : int, optional
        Maximum number of states
    dim : int
        The number of channels

    Returns
    ------
    model : pyhsmm model
        The initial model
    """
    obs_hypparams = {
        'mu_0': np.zeros(dim),  # mean of gaussians
        'sigma_0': np.eye(dim),  # std of gaussians
        'kappa_0': 0.3,
        'nu_0': dim + 5
    }

    # duration is going to be poisson, so prior is a gamma distribution
    # (params alpha beta)
    expected_lam = 12 * 30
    dur_hypparams = {'alpha_0': 2 * expected_lam, 'beta_0': 2}

    obs_distns = [
        distributions.Gaussian(**obs_hypparams) for state in range(Nmax)
    ]
    dur_distns = [
        distributions.PoissonDuration(**dur_hypparams) for state in range(Nmax)
    ]
    model = pyhsmm.models.WeakLimitHDPHSMM(
        alpha=6.,
        gamma=6.,  # priors
        init_state_concentration=6.,  # alpha0 for the initial state
        obs_distns=obs_distns,
        dur_distns=dur_distns)
    return model
Beispiel #2
0
# Plot the truth
plt.figure()
truemodel.plot()
plt.gcf().suptitle('True HSMM')

########################
#  parallel inference  #
########################

# building and running the model looks about the same as it usually does

posteriormodel = pyhsmm.models.HSMM(
    alpha=6,
    gamma=6,
    init_state_concentration=6.,
    obs_distns=[distns.Gaussian(**obs_hypparams) for state in xrange(N)],
    dur_distns=[
        distns.PoissonDuration(**dur_hypparams) for state in xrange(N)
    ])

for data in datas:
    posteriormodel.add_data_parallel(data)  # NOTE: add_data_parallel

# and so does resampling!

for itr in progprint_xrange(100):
    posteriormodel.resample_model_parallel()  # NOTE: resample_model_parallel

plt.figure()
posteriormodel.plot()
plt.gcf().suptitle('Sampled model after 100 iterations')
Beispiel #3
0
Nmax = 5

obs_hypparams = {'mu_0':np.zeros(obs_dim),
                'sigma_0':np.eye(obs_dim),
                'kappa_0':0.3,
                'nu_0':obs_dim+10
                }
# obs_hypparams = {'h_0':np.zeros(obs_dim),
#             'J_0':np.ones(obs_dim) * 0.001, #sq_0 #changes the hidden state detection (the lower the better) #0.001
#             'alpha_0':np.ones(obs_dim) * 0.1, #(make the number of hidden states worse higher the better)
#             'beta_0':np.ones(obs_dim) * 1}

dur_hypparams = {'alpha_0':2*30*500,
                 'beta_0':3}

obs_distns = [distributions.Gaussian(**obs_hypparams) for state in range(Nmax)]
dur_distns = [distributions.PoissonDuration(**dur_hypparams) for state in range(Nmax)]

datas = online_df.loc[online_df['indicator']==3, ['p_del','p_dup']].values

posteriormodel = pyhsmm.models.WeakLimitHDPHSMM(
        # alpha=6.,gamma=6., # better to sample over these; see concentration-resampling.py 10,1
        alpha=1.,gamma=1./4,
        init_state_concentration=600., # pretty inconsequential
        obs_distns=obs_distns,
        dur_distns=dur_distns)
posteriormodel.add_data(datas,trunc=80)
for idx in progprint_xrange(40): # 100->50
    posteriormodel.resample_model()#num_procs=1)

exp_state = np.empty(len(online_df), dtype=int)
 def generate_samples(self, state, num_samples):
     dist_parameters = self.emission_params(state)
     return distributions.Gaussian(**dist_parameters).rvs(num_samples)