truemodel.plot()
plt.gcf().suptitle('truth')

##################
#  create model  #
##################

Nmax = 25
affine = True
nlags = 2
model = m.ARWeakLimitStickyHDPHMM(
        alpha=4.,gamma=4.,kappa=100.,
        init_state_distn='uniform',
        obs_distns=[
            d.AutoRegression(
                nu_0=2.5,
                S_0=2.5*np.eye(2),
                M_0=np.zeros((2,2*nlags+affine)),
                K_0=10*np.eye(2*nlags+affine),
                affine=affine)
            for state in range(Nmax)],
        )

model.add_data(data)

###############
#  inference  #
###############

from moviepy.video.io.bindings import mplfig_to_npimage
from moviepy.editor import VideoClip
                         K_0=np.eye(D_obs * nlags + affine),
                         affine=affine) for state in range(Nmax)
    ],
)

# Or construct a sticky AR-HMM with a Bayesian nonparametric
# prior on the number of states, i.e. an HDP-HMM. We'll do
# inference with a "weak-limit" approximation of the HDP.
model = m.ARWeakLimitStickyHDPHMM(
    alpha=4.,
    gamma=4.,
    kappa=10.,
    init_state_distn='uniform',
    obs_distns=[
        d.AutoRegression(nu_0=3,
                         S_0=np.eye(D_obs),
                         M_0=np.hstack(
                             (np.eye(D_obs),
                              np.zeros(
                                  (D_obs, D_obs * (nlags - 1) + affine)))),
                         K_0=np.eye(D_obs * nlags + affine),
                         affine=affine) for state in range(Nmax)
    ],
)

for data in datas:
    model.add_data(data)

###############
#  inference  #
###############
Exemple #3
0
def fit_ar_pyhsmm_models(
    train_datas,
    val_datas,
    test_datas,
    K=2,
    N_iters=2000,
    seed=1,
    lags=1,
    affine=True,
    alpha=10,
    gamma=10,
    kappa=100,
    init_state_distn="uniform",
    observations="ar",
):
    """
    Fit datasets for multiple values
    """
    npr.seed(seed)
    assert (len(train_datas) > 0) and (type(train_datas) is list)
    assert (len(val_datas) > 0) and (type(val_datas) is list)
    assert (len(test_datas) > 0) and (type(test_datas) is list)

    # Standard AR model (Scale resampling)
    D_obs = train_datas[0].shape[1]

    def evalute_model(model):
        # train log log_likelihood
        train_ll = model.log_likelihood()

        # validation log log_likelihood
        val_pll = 0
        for data in val_datas:
            val_pll += model.log_likelihood(data)

        # Test log log_likelihood
        test_pll = 0
        for data in test_datas:
            test_pll += model.log_likelihood(data)

        return train_ll, val_pll, test_pll

    # Construct a standard AR-HMM
    obs_hypers = dict(
        nu_0=D_obs + 2,
        S_0=np.eye(D_obs),
        M_0=np.hstack(
            (np.eye(D_obs), np.zeros((D_obs, D_obs * (lags - 1) + affine)))),
        K_0=np.eye(D_obs * lags + affine),
        affine=affine,
    )

    obs_hypers = get_empirical_ar_params(train_datas, obs_hypers)
    obs_distns = [
        ardistributions.AutoRegression(**obs_hypers) for _ in range(K)
    ]

    # ----------------
    # Init Model Param
    # ----------------
    model = armodels.ARWeakLimitStickyHDPHMM(
        # sampled from 1d finite pmf
        alpha=alpha,
        gamma=gamma,
        init_state_distn=init_state_distn,
        # create A, Sigma
        obs_distns=obs_distns,
        kappa=kappa,
    )

    # ----------------
    # Add datasets
    # ----------------

    for data in train_datas:
        model.add_data(data)

    # ---------------------
    # Initialize the states
    # ---------------------
    model.resample_states()

    # ------------------------------
    #  Initialize log log_likelihood
    # ------------------------------
    init_val = evalute_model(model)

    # -----------------------
    # Fit with Gibbs sampling
    # -----------------------
    def sample(model):
        tic = time.time()
        model.resample_model()
        timestep = time.time() - tic
        return evalute_model(model), timestep

    # ----------------------
    # Run for each iteration
    # ----------------------

    # values at each timestep
    vals, timesteps = zip(*[sample(model) for _ in trange(N_iters)])

    lls_train, lls_val, lls_test = \
            zip(*((init_val,) + vals))

    timestamps = np.cumsum((0., ) + timesteps)

    # calculate the states after N_iters
    z = [mm.stateseq for mm in model.states_list]

    return model, lls_train, lls_val, lls_test, timestamps, z
def make_joint_models(train_datas, Nmax=10):
    # Define a sequence of models
    if isinstance(train_datas, list) and len(train_datas) > 0:
        data = train_datas
        num_worms = len(train_datas)
    else:
        data = [train_datas]
        num_worms = 1
    print('Making models')
    names_list = []
    fnames_list = []
    hmm_list = []
    color_list = []
    method_list = []
    # Standard AR model (Scale resampling)
    D_obs = data[0].shape[1]
    print('D_obs shape {}'.format(data[0].shape[1]))
    affine = True
    nlags = 1
    init_state_distn = 'uniform'

    # Construct a standard AR-HMM for fitting
    # with just one worm
    obs_hypers = dict(nu_0=D_obs + 2,
                      S_0=np.eye(D_obs),
                      M_0=np.hstack(
                          (np.eye(D_obs),
                           np.zeros((D_obs, D_obs * (nlags - 1) + affine)))),
                      K_0=np.eye(D_obs * nlags + affine),
                      affine=affine)

    # Joint model - fitting all worm at a time
    # Fit range of parameters for each state
    state_array = [1, 2, 4, 6, 8, 10, 12, 15]
    alpha_array = [10.0]
    gamma_array = [10.0]
    kappa_array = 10**np.arange(2, 11)[::2]

    # Vary the hyperparameters of the scale resampling model
    for num_states, alpha_a_0, gamma_a_0, kappa_a_0 in itertools.product(
            state_array, alpha_array, gamma_array, kappa_array):
        # using data of all worms
        obs_hypers = get_empirical_ar_params(data, obs_hypers)
        obs_distns = [
            d.AutoRegression(**obs_hypers) for state in range(num_states)
        ]
        names_list.append("AR-HMM (Scale)")
        fnames_list.append(
            "ar_scale_wormall_states%.1f_alpha%.1f_gamma%.1f_kappa%.1f" %
            (num_states, alpha_a_0, gamma_a_0, kappa_a_0))
        color_list.append(allcolors[1])
        # Init Model Param
        hmm = m.ARWeakLimitStickyHDPHMM(
            # sampled from 1d finite pmf
            alpha=alpha_a_0,
            gamma=gamma_a_0,
            init_state_distn=init_state_distn,
            # create A, Sigma
            obs_distns=obs_distns,
            kappa=kappa_a_0  # kappa
        )
        # Add data of each worm
        for cworm in np.arange(num_worms):
            hmm.add_data(data[cworm])
        # Append model  and store
        hmm_list.append(hmm)
        method_list.append(fit)
    print('Finished making baseline models')
    return names_list, fnames_list, color_list, hmm_list, method_list