def sqrt_unscented_predict( period, sigma_points, flat_sigma_points, s_weights_m, s_weights_c, q, transform_sigma_points_args, out_flat_states, out_flat_covs, ): """Make a unscented Kalman filter predict step in square-root form. The square-root form of the Kalman predict is much more robust than the usual form and also much faster. Args: period (int): the development period in which the predict step is done. sigma_points (np.ndarray): numpy array of (nemf * nind, nsigma, nfac) flat_sigma_points (np.ndarray): array of (nemf * nind * nsigma, nfac). It is a view on sigma_points. s_weights_m (np.ndarray): numpy array of length nsigma with sigma weights for the means. s_weights_c (np.ndarray): numpy array of length nsigma with sigma weights for the covariances. q (np.ndarray): numpy array of (nperiods - 1, nfac, nfac) with vaiances of the transition equation shocks. transform_sigma_points_args (dict): (see transform_sigma_points). out_flat_states (np.ndarray): output array of (nind * nemf, nfac). out_flat_covs (np.ndarray): output array of (nind * nemf, nfac, nfac). References: Van Der Merwe, R. and Wan, E.A. The Square-Root Unscented Kalman Filter for State and Parameter-Estimation. 2001. """ nemf_times_nind, nsigma, nfac = sigma_points.shape q = q[period] transform_sigma_points(period, flat_sigma_points, **transform_sigma_points_args) # get them back into states predicted_states = np.dot(s_weights_m, sigma_points, out=out_flat_states) devs = sigma_points - predicted_states.reshape(nemf_times_nind, 1, nfac) qr_weights = np.sqrt(s_weights_c).reshape(nsigma, 1) qr_points = np.zeros((nemf_times_nind, 3 * nfac + 1, nfac)) qr_points[:, 0:nsigma, :] = devs * qr_weights qr_points[:, nsigma:, :] = np.sqrt(q) out_flat_covs[:, 1:, 1:] = array_qr(qr_points)[:, :nfac, :]
def test_tsp_no_anchoring_no_endog(self, mock_trans): mock_trans.fake1.side_effect = fake1 mock_trans.fake2.side_effect = fake2 exp = np.zeros((10, 2)) exp[:, 0] = np.arange(10) + 1 exp[:, 1] = np.arange(start=10, stop=20) + np.arange(10) - 0.2 transform_sigma_points( stage=self.stage, flat_sigma_points=self.flat_sigma_points, transition_argument_dicts=self.transition_argument_dicts, transition_function_names=self.transition_function_names) calc = self.flat_sigma_points.copy() aaae(calc, exp)
def test_tsp_no_anchoring_no_endog(self, mock_trans): mock_trans.fake1.side_effect = fake1 mock_trans.fake2.side_effect = fake2 exp = np.zeros((10, 2)) exp[:, 0] = np.arange(10) + 1 exp[:, 1] = np.arange(start=10, stop=20) + np.arange(10) - 0.2 transform_sigma_points( stage=self.stage, flat_sigma_points=self.flat_sigma_points, transition_argument_dicts=self.transition_argument_dicts, transition_function_names=self.transition_function_names) calc = self.flat_sigma_points.copy() aaae(calc, exp)
def normal_unscented_predict( period, sigma_points, flat_sigma_points, s_weights_m, s_weights_c, q, transform_sigma_points_args, out_flat_states, out_flat_covs, ): """Make a unscented Kalman filter predict step in square-root form. Args: period (int): period in which the predict step is done. sigma_points (np.ndarray): numpy array of (nemf * nind, nsigma, nfac) flat_sigma_points (np.ndarray): array of (nemf * nind * nsigma, nfac). It is a view on sigma_points. s_weights_m (np.ndarray): numpy array of length nsigma with sigma weights for the means. s_weights_c (np.ndarray): numpy array of length nsigma with sigma weights for the covariances. q (np.ndarray): numpy array of (nperiods - 1, nfac, nfac) with vaiances of the transition equation shocks. transform_sigma_points_args (dict): (see transform_sigma_points). out_flat_states (np.ndarray): output array of (nind * nemf, nfac). out_flat_covs (np.ndarray): output array of (nind * nemf, nfac, nfac). References: Van Der Merwe, R. and Wan, E.A. The Square-Root Unscented Kalman Filter for State and Parameter-Estimation. 2001. """ nemf_times_nind, nsigma, nfac = sigma_points.shape q = q[period] transform_sigma_points(period, flat_sigma_points, **transform_sigma_points_args) # get them back into states predicted_states = np.dot(s_weights_m, sigma_points, out=out_flat_states) devs = sigma_points - predicted_states.reshape(nemf_times_nind, 1, nfac) # dev_outerprod has dimensions (nemf_times_nind, nsigma, nfac, nfac) dev_outerprod = devs.reshape(nemf_times_nind, nsigma, 1, nfac) * devs.reshape( nemf_times_nind, nsigma, nfac, 1) out_flat_covs[:] = (np.sum( (s_weights_c.reshape(nsigma, 1, 1) * dev_outerprod), axis=1) + q)
def test_tsp_with_anchoring_no_endog_integration(self, mock_trans): mock_trans.fake1.side_effect = fake1 mock_trans.fake2.side_effect = fake2 exp = np.zeros((10, 2)) exp[:, 0] = np.arange(10) + 1 exp[:, 1] = np.arange(start=10, stop=20) + 0.5 * np.arange(10) - 0.1 transform_sigma_points( stage=self.stage, flat_sigma_points=self.flat_sigma_points, transition_argument_dicts=self.transition_argument_dicts, transition_function_names=self.transition_function_names, anchoring_type='linear', anchoring_positions=[1], anch_params=np.array([0, 2.0])) calc = self.flat_sigma_points.copy() aaae(calc, exp)
def test_tsp_with_anchoring_no_endog_integration(self, mock_trans): mock_trans.fake1.side_effect = fake1 mock_trans.fake2.side_effect = fake2 exp = np.zeros((10, 2)) exp[:, 0] = np.arange(10) + 1 exp[:, 1] = np.arange(start=10, stop=20) + 0.5 * np.arange(10) - 0.1 transform_sigma_points( stage=self.stage, flat_sigma_points=self.flat_sigma_points, transition_argument_dicts=self.transition_argument_dicts, transition_function_names=self.transition_function_names, anchoring_type='linear', anchoring_positions=[1], anch_params=np.array([0, 2.0])) calc = self.flat_sigma_points.copy() aaae(calc, exp)
def sqrt_unscented_predict(stage, sigma_points, flat_sigma_points, s_weights_m, s_weights_c, Q, transform_sigma_points_args, out_flat_states, out_flat_covs): """Make a unscented Kalman filter predict step in square-root form. The square-root form of the Kalman predict is much more robust than the usual form and also much faster. Args: stage (int): the development stage in which the predict step is done. sigma_points (np.ndarray): numpy array of (nemf * nind, nsigma, nfac) flat_sigma_points (np.ndarray): array of (nemf * nind * nsigma, nfac). It is a view on sigma_points. s_weights_m (np.ndarray): numpy array of length nsigma with sigma weights for the means. s_weights_c (np.ndarray): numpy array of length nsigma with sigma weights for the covariances. Q (np.ndarray): numpy array of (nstages, nfac, nfac) with vaiances of the transition equation shocks. transform_sigma_points_args (dict): (see transform_sigma_points). out_flat_states (np.ndarray): output array of (nind * nemf, nfac). out_flat_covs (np.ndarray): output array of (nind * nemf, nfac, nfac). References: Van Der Merwe, R. and Wan, E.A. The Square-Root Unscented Kalman Filter for State and Parameter-Estimation. 2001. """ nemf_times_nind, nsigma, nfac = sigma_points.shape q = Q[stage] transform_sigma_points(stage, flat_sigma_points, **transform_sigma_points_args) # get them back into states predicted_states = np.dot(s_weights_m, sigma_points, out=out_flat_states) devs = sigma_points - predicted_states.reshape(nemf_times_nind, 1, nfac) qr_weights = np.sqrt(s_weights_c).reshape(nsigma, 1) qr_points = np.zeros((nemf_times_nind, 3 * nfac + 1, nfac)) qr_points[:, 0: nsigma, :] = devs * qr_weights qr_points[:, nsigma:, :] = np.sqrt(q) out_flat_covs[:, 1:, 1:] = array_qr(qr_points)[:, :nfac, :]
def normal_unscented_predict(stage, sigma_points, flat_sigma_points, s_weights_m, s_weights_c, Q, transform_sigma_points_args, out_flat_states, out_flat_covs): """Make a unscented Kalman filter predict step in square-root form. Args: stage (int): the development stage in which the predict step is done. sigma_points (np.ndarray): numpy array of (nemf * nind, nsigma, nfac) flat_sigma_points (np.ndarray): array of (nemf * nind * nsigma, nfac). It is a view on sigma_points. s_weights_m (np.ndarray): numpy array of length nsigma with sigma weights for the means. s_weights_c (np.ndarray): numpy array of length nsigma with sigma weights for the covariances. Q (np.ndarray): numpy array of (nstages, nfac, nfac) with vaiances of the transition equation shocks. transform_sigma_points_args (dict): (see transform_sigma_points). out_flat_states (np.ndarray): output array of (nind * nemf, nfac). out_flat_covs (np.ndarray): output array of (nind * nemf, nfac, nfac). References: Van Der Merwe, R. and Wan, E.A. The Square-Root Unscented Kalman Filter for State and Parameter-Estimation. 2001. """ nemf_times_nind, nsigma, nfac = sigma_points.shape q = Q[stage] transform_sigma_points(stage, flat_sigma_points, **transform_sigma_points_args) # get them back into states predicted_states = np.dot(s_weights_m, sigma_points, out=out_flat_states) devs = sigma_points - predicted_states.reshape(nemf_times_nind, 1, nfac) # dev_outerprod has dimensions (nemf_times_nind, nsigma, nfac, nfac) dev_outerprod = devs.reshape(nemf_times_nind, nsigma, 1, nfac) \ * devs.reshape(nemf_times_nind, nsigma, nfac, 1) out_flat_covs[:] = \ np.sum((s_weights_c.reshape(nsigma, 1, 1) * dev_outerprod), axis=1) + q