Exemplo n.º 1
0
    def resample_states(self, **kwargs):
        from autoregressive.messages import resample_arhmm
        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]))
            stats, transcounts, loglikes = resample_arhmm(
                [s.pi_0.astype(self.dtype) for s in self.states_list],
                [s.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._normalizer = loglike

            self._obs_stats = stats
            self._transcounts = transcounts
        else:
            self._obs_stats = None
            self._transcounts = []
Exemplo n.º 2
0
    def _plot_stateseq_data_values(self,
                                   s,
                                   ax,
                                   state_colors,
                                   plot_slice,
                                   update,
                                   data=None):
        data = undo_AR_striding(s.data, self.nlags)[plot_slice]
        stateseq = np.concatenate(
            (np.repeat(s.stateseq[0],
                       self.nlags), s.stateseq[:-1]))[plot_slice]
        colorseq = np.tile(
            np.array([state_colors[state] for state in stateseq]),
            data.shape[1])

        if update and hasattr(s, '_data_lc'):
            s._data_lc.set_array(colorseq)
        else:
            ts = np.arange(data.shape[0])
            segments = np.vstack([
                AR_striding(np.hstack((ts[:, None], scalarseq[:, None])),
                            1).reshape(-1, 2, 2) for scalarseq in data.T
            ])
            lc = s._data_lc = LineCollection(segments)
            lc.set_array(colorseq)
            lc.set_linewidth(0.5)
            ax.add_collection(lc)

        return s._data_lc
Exemplo n.º 3
0
    def _generate_obs(self, s):
        if s.data is None:
            # generating brand new data sequence
            data = np.zeros((s.T + self.nlags, self.D))

            if hasattr(self, 'prefix'):
                data[:self.nlags] = self.prefix
            else:
                data[:self.nlags] = self.init_emission_distn\
                    .rvs().reshape(data[:self.nlags].shape)

            for idx, state in enumerate(s.stateseq):
                data[idx+self.nlags] = \
                    self.obs_distns[state].rvs(lagged_data=data[idx:idx+self.nlags])

            s.data = AR_striding(data, self.nlags)

        else:
            # filling in missing data
            data = undo_AR_striding(s.data, self.nlags)

            # TODO should sample from init_emission_distn if there are nans in
            # data[:self.nlags]
            assert not np.isnan(data[:self.nlags]).any(
            ), "can't have missing data (nans) in prefix"

            nan_idx, = np.where(np.isnan(data[self.nlags:]).any(1))
            for idx, state in zip(nan_idx, s.stateseq[nan_idx]):
                data[idx+self.nlags] = \
                    self.obs_distns[state].rvs(lagged_data=data[idx:idx+self.nlags])

        return data
Exemplo n.º 4
0
    def _generate_obs(self,s):
        if s.data is None:
            # generating brand new data sequence
            data = np.zeros((s.T+self.nlags,self.D))

            if hasattr(self,'prefix'):
                data[:self.nlags] = self.prefix
            else:
                data[:self.nlags] = self.init_emission_distn\
                    .rvs().reshape(data[:self.nlags].shape)

            for idx, state in enumerate(s.stateseq):
                data[idx+self.nlags] = \
                    self.obs_distns[state].rvs(lagged_data=data[idx:idx+self.nlags])

            s.data = AR_striding(data,self.nlags)

        else:
            # filling in missing data
            data = undo_AR_striding(s.data,self.nlags)

            # TODO should sample from init_emission_distn if there are nans in
            # data[:self.nlags]
            assert not np.isnan(data[:self.nlags]).any(), "can't have missing data (nans) in prefix"

            nan_idx, = np.where(np.isnan(data[self.nlags:]).any(1))
            for idx, state in zip(nan_idx,s.stateseq[nan_idx]):
                data[idx+self.nlags] = \
                    self.obs_distns[state].rvs(lagged_data=data[idx:idx+self.nlags])

        return data
Exemplo n.º 5
0
    def _plot_stateseq_data_values(self,s,ax,state_colors,plot_slice,update,data=None):
        data = undo_AR_striding(s.data,self.nlags)[plot_slice]
        stateseq = np.concatenate((np.repeat(s.stateseq[0],self.nlags),s.stateseq[:-1]))[plot_slice]
        colorseq = np.tile(np.array([state_colors[state] for state in stateseq]),data.shape[1])

        if update and hasattr(s,'_data_lc'):
            s._data_lc.set_array(colorseq)
        else:
            ts = np.arange(data.shape[0])
            segments = np.vstack(
                [AR_striding(np.hstack((ts[:,None], scalarseq[:,None])),1).reshape(-1,2,2)
                    for scalarseq in data.T])
            lc = s._data_lc = LineCollection(segments)
            lc.set_array(colorseq)
            lc.set_linewidth(0.5)
            ax.add_collection(lc)

        return s._data_lc
Exemplo n.º 6
0
    def resample_states(self,**kwargs):
        from autoregressive.messages import resample_arhmm
        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]))
            stats, transcounts, loglikes = resample_arhmm(
                    [s.pi_0.astype(self.dtype) for s in self.states_list],
                    [s.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._normalizer = loglike

            self._obs_stats = stats
            self._transcounts = transcounts
        else:
            self._obs_stats = None
            self._transcounts = []
Exemplo n.º 7
0
 def datas(self):
     return [undo_AR_striding(s.data, self.nlags) for s in self.states_list]
Exemplo n.º 8
0
 def _get_joblib_pair(self, s):
     return (undo_AR_striding(s.data, self.nlags), s._kwargs)
Exemplo n.º 9
0
 def datas(self):
     return [undo_AR_striding(s.data,self.nlags) for s in self.states_list]
Exemplo n.º 10
0
 def _get_joblib_pair(self,s):
     return (undo_AR_striding(s.data,self.nlags),s._kwargs)