コード例 #1
0
    def process_run_df(self, df):
        auto_refresh = self._auto_refresh
        self._auto_refresh = False

        try:
            for i in range(len(df)):
                time = df.iloc[i]['time']
                observable_name = df.iloc[i]['observable_name']
                accepted = df.iloc[i]['accepted']
                obs_mean = df.iloc[i]['obs_mean']
                obs_cov = df.iloc[i]['obs_cov']
                predicted_obs_mean = df.iloc[i]['predicted_obs_mean']
                predicted_obs_cov = df.iloc[i]['predicted_obs_cov']
                cross_cov = df.iloc[i]['cross_cov']
                innov_mean = df.iloc[i]['innov_mean']
                innov_cov = df.iloc[i]['innov_cov']
                prior_state_mean = df.iloc[i]['prior_state_mean']
                prior_state_cov = df.iloc[i]['prior_state_cov']
                posterior_state_mean = df.iloc[i]['posterior_state_mean']
                posterior_state_cov = df.iloc[i]['posterior_state_cov']
                true_value = df.iloc[i]['true_value']
                log_likelihood = df.iloc[i]['log_likelihood']
                gain = df.iloc[i]['gain']

                # TODO Use an appropriate FilterState, it doesn't have to be KalmanFilterState
                prior_filter_state_object = kalman.KalmanFilterState(
                    None, time, False, N(prior_state_mean, prior_state_cov),
                    self._filter_name)
                # TODO Use an appropriate FilterState, it doesn't have to be KalmanFilterState
                posterior_filter_state_object = kalman.KalmanFilterState(
                    None, time, True,
                    N(posterior_state_mean, posterior_state_cov),
                    self._filter_name)

                self.process_filter_object(prior_filter_state_object)
                self.process_filter_object(posterior_filter_state_object)

                # Need the following check to skip the initial state row, which
                # may be present in the DataFrame:
                if not (i == 0 and observable_name is None):
                    true_value_object = filtering.TrueValue(
                        None, time, true_value, self._filter_name)
                    obs = filtering.Obs(None, time, N(obs_mean, obs_cov),
                                        observable_name)
                    predicted_obs = filtering.PredictedObs(
                        None, time, N(predicted_obs_mean, predicted_obs_cov),
                        cross_cov, observable_name)
                    innov_distr = N(innov_mean, innov_cov)
                    # TODO Use an appropriate ObsResult, it doesn't have to be KalmanObsResult
                    obs_result_object = kalman.KalmanObsResult(
                        accepted, obs, predicted_obs, innov_distr,
                        log_likelihood, gain)
                    if true_value is not None:
                        self.process_filter_object(true_value_object)
                    if obs_mean is not None:
                        self.process_filter_object(obs_result_object)
        finally:
            self.refresh()
            self._auto_refresh = auto_refresh
コード例 #2
0
 def predict(self, time, true_value=None):
     self.filter.predict(time, true_value)
     predicted_obs = self._obs_model.predict_obs(time, self._sub_state_distr(self.filter._state_distr), self)
     
     cc = predicted_obs.cross_cov
     
     # While cc is the cross-covariance between the "observed" processes and the observation, we need the
     # cross-covariance between the full compound process and the observation. Therefore we enlarge this matrix
     # by inserting columns of zeros at appropriate indices
     cc_nrow = npu.nrow(cc)
     cross_cov = np.zeros((cc_nrow, self.filter._state_distr.dim))
     col = 0
     for r in self._state_mean_rects:
         size = r[0].stop - r[0].start
         cross_cov[0:cc_nrow, r[0].start:r[0].start+size] = cc[0:cc_nrow, col:col+size]
         col += size
     
     return filtering.PredictedObs(self, time, predicted_obs.distr, cross_cov)
コード例 #3
0
ファイル: kalman.py プロジェクト: vishalbelsare/tsa
 def predict_obs(self, time, state_distr, observable=None):
     obs_mean = np.dot(self._obs_matrix, state_distr.mean)
     cross_cov = np.dot(self._obs_matrix, state_distr.cov)
     obs_cov = np.dot(cross_cov, self._obs_matrix.T)
     return filtering.PredictedObs(observable, time,
                                   N(mean=obs_mean, cov=obs_cov), cross_cov)