Пример #1
0
def do_first_em_cycle(pars_init, obs_scheme, y, u=None, eps=1e-30, 
                           use_A_flag=True,use_B_flag=False,diag_R_flag=True):
    """ INPUT:
        pars:      collection of initial parameters for LDS
        obs_scheme: observation scheme for given data, stored in dictionary
                   with keys 'sub_pops', 'obs_time', 'obs_pops'
        y:         data array of observed variables
        u:         data array of input variables
        eps:       precision (stopping criterion) for deciding on convergence
                   of latent covariance estimates durgin the E-step                   
        This function serves to quickly get the results of one EM-cycle. It is
        mostly intended to generate results that can quickly be compared with
        other EM implementations or different parameter initialisation methods. 
    """

    y_dim = y.shape[0]
    u_dim = ssm_fit._get_u_dim(u)
    t_tot = y.shape[1]

    pars_init = dict(pars_init)
    ssm_fit.check_pars(pars=pars_init, 
                       y_dim=y_dim, 
                       u_dim=u_dim)         
    obs_scheme = dict(obs_scheme)
    ssm_fit.check_obs_scheme(obs_scheme=obs_scheme,
                             y_dim=y_dim,
                             t_tot=t_tot)                                   

    # do one E-step
    stats_init, ll_init, t_conv_ft, t_conv_sm    = \
      ssm_fit.lds_e_step(pars=pars_init,
                         y=y,
                         u=u, 
                         obs_scheme=obs_scheme,
                         eps=eps)        

    # do one M-step      
    pars_first = ssm_fit.lds_m_step(stats=stats_init,
                                    y=y, 
                                    u=u,
                                    obs_scheme=obs_scheme,
                                    use_A_flag=use_A_flag,
                                    use_B_flag=use_B_flag,
                                    diag_R_flag=diag_R_flag)

    # do another E-step
    stats_first, ll_first, t_conv_ft, t_conv_sm = \
      ssm_fit.lds_e_step(pars=pars_first,
                         y=y,
                         u=u, 
                         obs_scheme=obs_scheme,
                         eps=eps)        

    return stats_init, ll_init, pars_first, stats_first, ll_first
Пример #2
0
def do_e_step(pars, y, u=None, obs_scheme=None, eps=1e-30):
    """ INPUT:
        pars  : (list of) LDS parameters
        y: data array of observed variables
        u: data array of input variables
        obs_scheme: observation scheme for given data, stored in dictionary
                   with keys 'sub_pops', 'obs_time', 'obs_pops'
        eps:       precision (stopping criterion) for deciding on convergence
                   of latent covariance estimates durgin the E-step 
        Wrapper function to quickly compute a single E-step with given data y
        and LDS parameters pars. This function mostly exists to deal with the
        difference between having no input u and not using it (i.e. parameter
        B = 0). 
    """

    if (u is None and 
        not (pars['B'] is None or pars['B'] == [] or
             (isinstance(pars['B'],np.ndarray) and pars['B'].size==0) or
             (isinstance(pars['B'],np.ndarray) and pars['B'].size>0 
              and np.max(abs(pars['B']))==0) or
             (isinstance(pars['B'],(float,numbers.Integral)) 
              and pars['B']==0))):
        print(('Warning: Parameter B is initialised, but input u = None. '
               'Algorithm will ignore input for the LDS, outcomes may be '
               'not as expected.'))

    y_dim = y.shape[0]
    if isinstance(u, np.ndarray):
        u_dim = u.shape[0]
    else:
        u_dim = 0

    if obs_scheme is None:
        t_tot = y.shape[1]
        print('creating default observation scheme: Full population observed')
        obs_scheme = {'sub_pops': [list(range(y_dim))], # creates default case
                     'obs_time': [t_tot],               # of fully observed
                     'obs_pops': [0]}                   # population

    ssm_fit.check_pars(pars=pars,
                       y_dim=y_dim, 
                       u_dim=u_dim)                     

    stats, lltr, t_conv_ft, t_conv_sm = \
        ssm_fit.lds_e_step(pars=pars,
                           y=y,
                           u=u, 
                           obs_scheme=obs_scheme,
                           eps=eps)        

    return stats, lltr, t_conv_ft, t_conv_sm