def sample_transition_full_rank(model, state, hyperparams, pseudo_dof=None, pseudo_sd=None):
    """
    MCMC iteration (Gibbs sampling) for transition matrix and covariance
    with full rank model
    """

    Q = model.transition_covariance()

    # Sampke a pseudo-observation to constrain the size of move
    if pseudo_dof is not None:
        extra_nu = pseudo_dof
        extra_Psi = smp.sample_wishart(pseudo_dof, Q)
    else:
        extra_nu = 0
        extra_Psi = 0

    # Calculate sufficient statistics
    suffStats = smp.evaluate_transition_sufficient_statistics(state)

    # Sample a new projected transition matrix and transition covariance
    nu0 = model.ds + extra_nu
    Psi0 = model.ds*hyperparams['rPsi0'] + extra_Psi
    nu,Psi,M,V = smp.hyperparam_update_basic_mniw_transition(
                                                suffStats,
                                                nu0,
                                                Psi0,
                                                hyperparams['M0'],
                                                hyperparams['V0'])
    Q = la.inv(smp.sample_wishart(nu, la.inv(Psi)))
    F = smp.sample_matrix_normal(M, Q, V)

    model.parameters['F'] = F
    model.parameters['Q']= Q

    return model
Пример #2
0
def sample_transition_within_subspace(model, state, hyperparams):
    """
    MCMC iteration (Gibbs sampling) for transition matrix and covariance
    within the constrained subspace
    """

    # Calculate sufficient statistics
    suffStats = smp.evaluate_transition_sufficient_statistics(state)

    # Convert to Givens factorisation form
    U, D = model.convert_to_givens_form()

    # Sample a new projected transition matrix and transition covariance
    rank = model.parameters['rank'][0]
    nu0 = rank
    Psi0 = rank * hyperparams['rPsi0']
    nu, Psi, M, V = smp.hyperparam_update_degenerate_mniw_transition(
        suffStats, U, nu0, Psi0, hyperparams['M0'], hyperparams['V0'])
    D = la.inv(smp.sample_wishart(nu, la.inv(Psi)))
    FU = smp.sample_matrix_normal(M, D, V)

    # Project out
    Fold = model.parameters['F']
    F = smp.project_degenerate_transition_matrix(Fold, FU, U)
    model.parameters['F'] = F

    # Convert back to eigen-decomposition form
    model.update_from_givens_form(U, D)

    return model
def sample_transition_covariance_within_subspace(model, state, hyperparams, pseudo_dof=None):
    """
    MCMC iteration (Gibbs sampling) for transition matrix and covariance
    within the constrained subspace
    """

    # Calculate sufficient statistics
    suffStats = smp.evaluate_transition_sufficient_statistics(state)

    # Convert to Givens factorisation form
    U,D = model.convert_to_givens_form()

    # Sampke a pseudo-observation to constrain the size of move
    if pseudo_dof is not None:
        extra_nu = pseudo_dof
        extra_Psi = smp.sample_wishart(pseudo_dof, D)
    else:
        extra_nu = 0
        extra_Psi = 0

    # Sample a new projected transition matrix and transition covariance
    rank = model.parameters['rank'][0]
    nu0 = rank + extra_nu
    Psi0 = rank*hyperparams['rPsi0'] + np.dot(U, np.dot(extra_Psi, U.T))
    nu,Psi = smp.hyperparam_update_degenerate_iw_transition_covariance(
                                                suffStats, U,
                                                model.parameters['F'],
                                                nu0,
                                                Psi0)
    D = la.inv(smp.sample_wishart(nu, la.inv(Psi)))

    # Convert back to eigen-decomposition form
    model.update_from_givens_form(U, D)

    return model
Пример #4
0
def sample_transition_covariance_within_subspace(model,
                                                 state,
                                                 hyperparams,
                                                 pseudo_dof=None):
    """
    MCMC iteration (Gibbs sampling) for transition matrix and covariance
    within the constrained subspace
    """

    # Calculate sufficient statistics
    suffStats = smp.evaluate_transition_sufficient_statistics(state)

    # Convert to Givens factorisation form
    U, D = model.convert_to_givens_form()

    # Sampke a pseudo-observation to constrain the size of move
    if pseudo_dof is not None:
        extra_nu = pseudo_dof
        extra_Psi = smp.sample_wishart(pseudo_dof, D)
    else:
        extra_nu = 0
        extra_Psi = 0

    # Sample a new projected transition matrix and transition covariance
    rank = model.parameters['rank'][0]
    nu0 = rank + extra_nu
    Psi0 = rank * hyperparams['rPsi0'] + np.dot(U, np.dot(extra_Psi, U.T))
    nu, Psi = smp.hyperparam_update_degenerate_iw_transition_covariance(
        suffStats, U, model.parameters['F'], nu0, Psi0)
    D = la.inv(smp.sample_wishart(nu, la.inv(Psi)))

    # Convert back to eigen-decomposition form
    model.update_from_givens_form(U, D)

    return model
Пример #5
0
def sample_transition_full_rank(model,
                                state,
                                hyperparams,
                                pseudo_dof=None,
                                pseudo_sd=None):
    """
    MCMC iteration (Gibbs sampling) for transition matrix and covariance
    with full rank model
    """

    Q = model.transition_covariance()

    # Sampke a pseudo-observation to constrain the size of move
    if pseudo_dof is not None:
        extra_nu = pseudo_dof
        extra_Psi = smp.sample_wishart(pseudo_dof, Q)
    else:
        extra_nu = 0
        extra_Psi = 0

    # Calculate sufficient statistics
    suffStats = smp.evaluate_transition_sufficient_statistics(state)

    # Sample a new projected transition matrix and transition covariance
    nu0 = model.ds + extra_nu
    Psi0 = model.ds * hyperparams['rPsi0'] + extra_Psi
    nu, Psi, M, V = smp.hyperparam_update_basic_mniw_transition(
        suffStats, nu0, Psi0, hyperparams['M0'], hyperparams['V0'])
    Q = la.inv(smp.sample_wishart(nu, la.inv(Psi)))
    F = smp.sample_matrix_normal(M, Q, V)

    model.parameters['F'] = F
    model.parameters['Q'] = Q

    return model
Пример #6
0
def sample_transition_within_subspace(model, state, hyperparams):
    """
    MCMC iteration (Gibbs sampling) for transition matrix and covariance
    within the constrained subspace
    """

    # Calculate sufficient statistics
    suffStats = smp.evaluate_transition_sufficient_statistics(state)

    # Convert to Givens factorisation form
    U,D = model.convert_to_givens_form()

    # Sample a new projected transition matrix and transition covariance
    rank = model.parameters['rank'][0]
    nu0 = rank
    Psi0 = rank*hyperparams['rPsi0']
    nu,Psi,M,V = smp.hyperparam_update_degenerate_mniw_transition(
                                                suffStats, U,
                                                nu0,
                                                Psi0,
                                                hyperparams['M0'],
                                                hyperparams['V0'])
    D = la.inv(smp.sample_wishart(nu, la.inv(Psi)))
    FU = smp.sample_matrix_normal(M, D, V)

    # Project out
    Fold = model.parameters['F']
    F = smp.project_degenerate_transition_matrix(Fold, FU, U)
    model.parameters['F'] = F

    # Convert back to eigen-decomposition form
    model.update_from_givens_form(U, D)

    return model
    def sample_transition(self):
        """
        MCMC iteration (Gibbs sampling) for transition matrix and covariance
        """

        # Calculate sufficient statistics using current state trajectory
        suffStats = smp.evaluate_transition_sufficient_statistics(self.state)

        # Update hyperparameters
        nu,Psi,M,V = smp.hyperparam_update_basic_mniw_transition(
                                                    suffStats,
                                                    self.hyperparams['nu0'],
                                                    self.hyperparams['Psi0'],
                                                    self.hyperparams['M0'],
                                                    self.hyperparams['V0'])

        # Sample new parameters
        Q = la.inv(smp.sample_wishart(nu, la.inv(Psi)))
        F = smp.sample_matrix_normal(M, Q, V)

        # Update the model
        self.model.parameters['F'] = F
        self.model.parameters['Q'] = Q

        # self.flt and self.lhood are no longer up-to-date
        self.filter_current = False
    def sample_transition_ncvm_covariance(self):
        """
        MCMC iteration (Gibbs sampling) for diagonal observation covariance
        matrix with inverse-gamma prior
        """

        # Calculate sufficient statistics using current state trajectory
        suffStats = smp.evaluate_transition_sufficient_statistics(self.state)

        # Update hyperparameters
        Qs = self.model.parameters['Q']
        Qs /= Qs[0,0]
        a,b = smp.hyperparam_update_ncvm_ig_transition_variance(
                                    suffStats,
                                    self.model.parameters['F'],
                                    Qs,
                                    self.hyperparams['a0'],
                                    self.hyperparams['b0'])  # This uses the same hyperparameters as the observation variance because I'm lazy and want to go to bed.

        # Sample new parameter
        s = stats.invgamma.rvs(a, scale=b)
        self.model.parameters['Q'] = s*Qs

        # self.flt and self.lhood are no longer up-to-date
        self.filter_current = False
Пример #9
0
    def sample_transition(self):
        """
        MCMC iteration (Gibbs sampling) for transition matrix and covariance
        """

        # Calculate sufficient statistics using current state trajectory
        suffStats = smp.evaluate_transition_sufficient_statistics(self.state)

        # Update hyperparameters
        nu, Psi, M, V = smp.hyperparam_update_basic_mniw_transition(
            suffStats, self.hyperparams['nu0'], self.hyperparams['Psi0'],
            self.hyperparams['M0'], self.hyperparams['V0'])

        # Sample new parameters
        Q = la.inv(smp.sample_wishart(nu, la.inv(Psi)))
        F = smp.sample_matrix_normal(M, Q, V)

        # Update the model
        self.model.parameters['F'] = F
        self.model.parameters['Q'] = Q

        # self.flt and self.lhood are no longer up-to-date
        self.filter_current = False
Пример #10
0
    def sample_transition_ncvm_covariance(self):
        """
        MCMC iteration (Gibbs sampling) for diagonal observation covariance
        matrix with inverse-gamma prior
        """

        # Calculate sufficient statistics using current state trajectory
        suffStats = smp.evaluate_transition_sufficient_statistics(self.state)

        # Update hyperparameters
        Qs = self.model.parameters['Q']
        Qs /= Qs[0, 0]
        a, b = smp.hyperparam_update_ncvm_ig_transition_variance(
            suffStats, self.model.parameters['F'], Qs, self.hyperparams['a0'],
            self.hyperparams['b0']
        )  # This uses the same hyperparameters as the observation variance because I'm lazy and want to go to bed.

        # Sample new parameter
        s = stats.invgamma.rvs(a, scale=b)
        self.model.parameters['Q'] = s * Qs

        # self.flt and self.lhood are no longer up-to-date
        self.filter_current = False