def init_meanfield_from_sample(self): self.expected_states = np.hstack( [(self.stateseq == i).astype("float64")[:, na] for i in range(self.num_states)] ) from pyhsmm.util.general import count_transitions self.expected_transcounts = count_transitions(self.stateseq_norep, minlength=self.num_states) self.expected_durations = expected_durations = np.zeros((self.num_states, self.T)) for state in xrange(self.num_states): expected_durations[state] += np.bincount( self.durations_censored[self.stateseq_norep == state], minlength=self.T )[: self.T]
def init_meanfield_from_sample(self): self.expected_states = \ np.hstack([(self.stateseq == i).astype('float64')[:,na] for i in range(self.num_states)]) from pyhsmm.util.general import count_transitions self.expected_transcounts = \ count_transitions(self.stateseq_norep,minlength=self.num_states) self.expected_durations = expected_durations = \ np.zeros((self.num_states,self.T)) for state in range(self.num_states): expected_durations[state] += \ np.bincount( self.durations_censored[self.stateseq_norep == state], minlength=self.T)[:self.T]
def init_meanfield_from_sample(self): # NOTE: only durations is different here; uses Tfull self.expected_states = \ np.hstack([(self.stateseq == i).astype('float64')[:,na] for i in range(self.num_states)]) from pyhsmm.util.general import count_transitions self.expected_transcounts = \ count_transitions(self.stateseq_norep,minlength=self.num_states) self.expected_durations = expected_durations = \ np.zeros((self.num_states,self.Tfull)) for state in xrange(self.num_states): expected_durations[state] += \ np.bincount( self.durations_censored[self.stateseq_norep == state], minlength=self.Tfull)[:self.Tfull]
def init_meanfield_from_sample(self): # NOTE: only durations is different here; uses Tfull self.expected_states = np.hstack([ (self.stateseq == i).astype("float64")[:, na] for i in range(self.num_states) ]) from pyhsmm.util.general import count_transitions self.expected_transcounts = count_transitions( self.stateseq_norep, minlength=self.num_states) self.expected_durations = expected_durations = np.zeros( (self.num_states, self.Tfull)) for state in range(self.num_states): expected_durations[state] += np.bincount( self.durations_censored[self.stateseq_norep == state], minlength=self.Tfull, )[:self.Tfull]
posteriormodel = pyhsmm.models.DiagGaussGMMHSMMPossibleChangepointsSeparateTrans( alpha=6., init_state_concentration=6., obs_distns=obs_distns, dur_distns=dur_distns) # posteriormodel = pyhsmm.models.DiagGaussHSMMPossibleChangepointsSeparateTrans( # alpha=6.,init_state_concentration=6., # obs_distns=obs_distns,dur_distns=dur_distns) posteriormodel.add_data(data, changepoints, group_id=0, stateseq=labels) posteriormodel.init_meanfield_from_sample() plt.figure() posteriormodel.plot() def normalize(A): return A / A.sum(1)[:, None] plt.matshow(truemodel.trans_distn.trans_matrix) from pyhsmm.util.general import count_transitions plt.matshow(count_transitions(truemodel.states_list[0].stateseq_norep)) plt.matshow( normalize( posteriormodel.trans_distns[0].exp_expected_log_trans_matrix)[:N, :N]) plt.show()
components=[pyhsmm.distributions.DiagonalGaussian(**obs_hypparams) for component in xrange(2)]) for state in xrange(Nmax)] # obs_distns = [pyhsmm.distributions.DiagonalGaussian(**obs_hypparams) for state in xrange(Nmax)] dur_distns = [pyhsmm.distributions.PoissonDuration(**dur_hypparams) for state in xrange(Nmax)] posteriormodel = pyhsmm.models.DiagGaussGMMHSMMPossibleChangepointsSeparateTrans( alpha=6.,init_state_concentration=6., obs_distns=obs_distns,dur_distns=dur_distns) # posteriormodel = pyhsmm.models.DiagGaussHSMMPossibleChangepointsSeparateTrans( # alpha=6.,init_state_concentration=6., # obs_distns=obs_distns,dur_distns=dur_distns) posteriormodel.add_data(data,changepoints,group_id=0,stateseq=labels) posteriormodel.init_meanfield_from_sample() plt.figure() posteriormodel.plot() def normalize(A): return A / A.sum(1)[:,None] plt.matshow(truemodel.trans_distn.trans_matrix) from pyhsmm.util.general import count_transitions plt.matshow(count_transitions(truemodel.states_list[0].stateseq_norep)) plt.matshow(normalize(posteriormodel.trans_distns[0].exp_expected_log_trans_matrix)[:N,:N]) plt.show()