Esempio n. 1
0
    def resample_states(self,**kwargs):
        # NOTE: kwargs is just to absorb any multiprocessing stuff
        # TODO only use this when the number/size of sequences warrant it
        from messages import resample_arhmm
        assert self.obs_distns[0].D_out > 1
        if len(self.states_list) > 0:
            stateseqs = [np.empty(s.T,dtype='int32') for s in self.states_list]
            params, normalizers = map(np.array,zip(*[self._param_matrix(o) for o in self.obs_distns]))
            params, normalizers = params.repeat(s.rs,axis=0), normalizers.repeat(s.rs,axis=0)
            stats, _, loglikes = resample_arhmm(
                    [s.hmm_pi_0.astype(self.dtype) for s in self.states_list],
                    [s.hmm_trans_matrix.astype(self.dtype) for s in self.states_list],
                    params.astype(self.dtype), normalizers.astype(self.dtype),
                    [undo_AR_striding(s.data,self.nlags) for s in self.states_list],
                    stateseqs,
                    [np.random.uniform(size=s.T).astype(self.dtype) for s in self.states_list],
                    self.alphans)
            for s, stateseq, loglike in zip(self.states_list,stateseqs,loglikes):
                s.stateseq = stateseq
                s._map_states()
                s._normalizer = loglike

            starts, ends = cumsum(s.rs,strict=True), cumsum(s.rs,strict=False)
            stats = map(np.array,stats)
            stats = [sum(stats[start:end]) for start, end in zip(starts,ends)]

            self._obs_stats = stats
        else:
            self._obs_stats = None
Esempio n. 2
0
def slices_from_indicators(indseq):
    indseq = np.asarray(indseq)
    if not indseq.any():
        return []
    else:
        vals, durs = rle(indseq)
        starts, ends = cumsum(durs, strict=True), cumsum(durs, strict=False)
        return [
            slice(start, end) for val, start, end in zip(vals, starts, ends)
            if val
        ]
Esempio n. 3
0
    def hmm_pi_0(self):
        delays = self.delays
        starts = cumsum(delays,strict=True)

        pi_0 = np.zeros(delays.sum())
        pi_0[starts] = self.pi_0
        return pi_0
Esempio n. 4
0
    def hmm_pi_0(self):
        delays = self.delays
        starts = cumsum(delays,strict=True)

        pi_0 = np.zeros(delays.sum())
        pi_0[starts] = self.pi_0
        return pi_0
Esempio n. 5
0
    def hmm_trans_matrix(self):
        # NOTE: more general version, allows different delays, o/w we could
        # construct with np.kron
        if self._hmm_trans_matrix is None:
            ps, delays = map(np.array,zip(*[(d.p,d.delay) for d in self.dur_distns]))
            starts, ends = cumsum(delays,strict=True), cumsum(delays,strict=False)
            trans_matrix = self._hmm_trans_matrix = np.zeros((ends[-1],ends[-1]))

            for (i,j), Aij in np.ndenumerate(self.trans_matrix):
                block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
                if i == j:
                    block[:-1,1:] = np.eye(block.shape[0]-1)
                    block[-1,-1] = 1-ps[i]
                else:
                    block[-1,0] = ps[j]*Aij

        return self._hmm_trans_matrix
Esempio n. 6
0
    def hmm_trans_matrix(self):
        # NOTE: more general version, allows different delays, o/w we could
        # construct with np.kron
        if self._hmm_trans_matrix is None:
            ps, delays = map(np.array,zip(*[(d.p,d.delay) for d in self.dur_distns]))
            starts, ends = cumsum(delays,strict=True), cumsum(delays,strict=False)
            trans_matrix = self._hmm_trans_matrix = np.zeros((ends[-1],ends[-1]))

            for (i,j), Aij in np.ndenumerate(self.trans_matrix):
                block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
                if i == j:
                    block[:-1,1:] = np.eye(block.shape[0]-1)
                    block[-1,-1] = 1-ps[i]
                else:
                    block[-1,0] = ps[j]*Aij

        return self._hmm_trans_matrix
Esempio n. 7
0
 def _beta_vlb(self):
     return np.log(self.beta).sum() + self.gamma*np.log(1-cumsum(self.beta)).sum() \
            - 3*np.log(1-cumsum(self.beta,strict=True)).sum()
Esempio n. 8
0
 def _grad_log_p_beta(beta,alpha):
     # NOTE: switched argument name gamma <-> alpha
     return  -(alpha-1)*rcumsum(1./(1-cumsum(beta))) \
             + 2*rcumsum(1./(1-cumsum(beta,strict=True)),strict=True)
Esempio n. 9
0
 def _beta_vlb(self):
     return np.log(self.beta).sum() + self.gamma*np.log(1-cumsum(self.beta)).sum() \
            - 3*np.log(1-cumsum(self.beta,strict=True)).sum()
Esempio n. 10
0
 def _grad_log_p_beta(beta,alpha):
     # NOTE: switched argument name gamma <-> alpha
     return  -(alpha-1)*rcumsum(1./(1-cumsum(beta))) \
             + 2*rcumsum(1./(1-cumsum(beta,strict=True)),strict=True)
Esempio n. 11
0
 def feas(pt):
     return (pt > 0.).all() and pt.sum() < 1. and not np.isinf(
         1. / (1 - cumsum(pt))).any()
Esempio n. 12
0
 def feas(pt):
     return (pt>0.).all() and pt.sum() < 1. and not np.isinf(1./(1-cumsum(pt))).any()