def setup_class(cls, which='mixed', *args, **kwargs): # Data dta = datasets.macrodata.load_pandas().data dta.index = pd.date_range(start='1959-01-01', end='2009-7-01', freq='QS') obs = np.log(dta[['realgdp','realcons','realinv']]).diff().iloc[1:] if which == 'all': obs.iloc[:50, :] = np.nan obs.iloc[119:130, :] = np.nan elif which == 'partial': obs.iloc[0:50, 0] = np.nan obs.iloc[119:130, 0] = np.nan elif which == 'mixed': obs.iloc[0:50, 0] = np.nan obs.iloc[19:70, 1] = np.nan obs.iloc[39:90, 2] = np.nan obs.iloc[119:130, 0] = np.nan obs.iloc[119:130, 2] = np.nan # Create the model with typical state space mod = mlemodel.MLEModel(obs, k_states=3, k_posdef=3, **kwargs) mod['design'] = np.eye(3) mod['obs_cov'] = np.array([[ 609.0746647855, 0. , 0. ], [ 0. , 1.8774916622, 0. ], [ 0. , 0. , 124.6768281675]]) mod['transition'] = np.array([[-0.8110473405, 1.8005304445, 1.0215975772], [-1.9846632699, 2.4091302213, 1.9264449765], [ 0.9181658823, -0.2442384581, -0.6393462272]]) mod['selection'] = np.eye(3) mod['state_cov'] = np.array([[ 1552.9758843938, 612.7185121905, 877.6157204992], [ 612.7185121905, 467.8739411204, 70.608037339 ], [ 877.6157204992, 70.608037339 , 900.5440385836]]) mod.initialize_approximate_diffuse(1e6) cls.model = mod cls.results = mod.smooth([], return_ssm=True) # Create the model with augmented state space kwargs.pop('filter_collapsed', None) mod = mlemodel.MLEModel(obs, k_states=6, k_posdef=3, **kwargs) mod['design', :3, :3] = np.eye(3) mod['obs_cov'] = np.array([[ 609.0746647855, 0. , 0. ], [ 0. , 1.8774916622, 0. ], [ 0. , 0. , 124.6768281675]]) mod['transition', :3, :3] = np.array([[-0.8110473405, 1.8005304445, 1.0215975772], [-1.9846632699, 2.4091302213, 1.9264449765], [ 0.9181658823, -0.2442384581, -0.6393462272]]) mod['transition', 3:, :3] = np.eye(3) mod['selection', :3, :3] = np.eye(3) mod['state_cov'] = np.array([[ 1552.9758843938, 612.7185121905, 877.6157204992], [ 612.7185121905, 467.8739411204, 70.608037339 ], [ 877.6157204992, 70.608037339 , 900.5440385836]]) mod.initialize_approximate_diffuse(1e6) cls.augmented_model = mod cls.augmented_results = mod.smooth([], return_ssm=True)
def get_model(univariate, missing=None, init=None): if univariate: endog = np.array([0.5, 1.2, -0.2, 0.3, -0.1, 0.4, 1.4, 0.9]) if missing == 'init': endog[0:2] = np.nan elif missing == 'mixed': endog[2:4] = np.nan elif missing == 'all': endog[:] = np.nan mod = mlemodel.MLEModel(endog, k_states=1, k_posdef=1) mod['design', 0, 0] = 1. mod['transition', 0, 0] = 0.5 mod['selection', 0, 0] = 1. mod['state_cov', 0, 0] = 1. mod['state_intercept', 0, 0] = 1. else: endog = np.array([[0.5, 1.2, -0.2, 0.3, -0.1, 0.4, 1.4, 0.9], [-0.2, -0.3, -0.1, 0.1, 0.01, 0.05, -0.13, -0.2]]).T if missing == 'init': endog[0:2, :] = np.nan elif missing == 'mixed': endog[2:4, 0] = np.nan endog[3:6, 1] = np.nan elif missing == 'all': endog[:] = np.nan mod = mlemodel.MLEModel(endog, k_states=3, k_posdef=2) mod['obs_intercept'] = np.array([0.5, 0.2]) mod['design'] = np.array([[0.1, -0.1, 0], [0.2, 0.3, 0]]) mod['obs_cov'] = np.array([[5, -0.2], [-0.2, 3.]]) mod['transition', 0, 0] = 1 mod['transition', 1:, 1:] = np.array([[0.5, -0.1], [1., 0.]]) mod['selection', :2, :2] = np.eye(2) mod['state_cov'] = np.array([[1.2, 0.2], [0.2, 2.5]]) mod['state_intercept', :2] = np.array([1., -1.]) if init == 'diffuse': mod.ssm.initialize_diffuse() elif init == 'approximate_diffuse': mod.ssm.initialize_approximate_diffuse() elif init == 'stationary': mod.ssm.initialize_stationary() return mod
def test_mle_validate(): mod = mlemodel.MLEModel([], 1) mod._param_names = ['a', 'b', 'c'] msg = 'Invalid parameter name passed: "d"' with pytest.raises(ValueError, match=msg): with mod.fix_params({'d': 1}): pass
def setup_class(cls, *args, **kwargs): if compatibility_mode: raise SkipTest # Results path = current_path + os.sep + 'results/results_smoothing2_R.csv' cls.desired = pd.read_csv(path) # Data dta = datasets.macrodata.load_pandas().data dta.index = pd.date_range(start='1959-01-01', end='2009-7-01', freq='QS') obs = np.log(dta[['realgdp', 'realcons', 'realinv']]).diff().iloc[1:] # Create the model mod = mlemodel.MLEModel(obs, k_states=3, k_posdef=3, **kwargs) mod.ssm.filter_univariate = True mod['design'] = np.eye(3) mod['obs_cov'] = np.array([[0.0000640649, 0., 0.], [0., 0.0000572802, 0.], [0., 0., 0.0017088585]]) mod['transition'] = np.array( [[-0.1119908792, 0.8441841604, 0.0238725303], [0.2629347724, 0.4996718412, -0.0173023305], [-3.2192369082, 4.1536028244, 0.4514379215]]) mod['selection'] = np.eye(3) mod['state_cov'] = np.array( [[0.0000640649, 0.0000388496, 0.0002148769], [0.0000388496, 0.0000572802, 0.000001555], [0.0002148769, 0.000001555, 0.0017088585]]) mod.initialize_approximate_diffuse(1e6) cls.model = mod cls.results = mod.smooth([], return_ssm=True) # Calculate the determinant of the covariance matrices (for easy # comparison to other languages without having to store 2-dim arrays) cls.results.det_scaled_smoothed_estimator_cov = (np.zeros( (1, cls.model.nobs))) cls.results.det_predicted_state_cov = np.zeros((1, cls.model.nobs)) cls.results.det_smoothed_state_cov = np.zeros((1, cls.model.nobs)) cls.results.det_smoothed_state_disturbance_cov = (np.zeros( (1, cls.model.nobs))) for i in range(cls.model.nobs): cls.results.det_scaled_smoothed_estimator_cov[0, i] = ( np.linalg.det(cls.results.scaled_smoothed_estimator_cov[:, :, i])) cls.results.det_predicted_state_cov[0, i] = np.linalg.det( cls.results.predicted_state_cov[:, :, i + 1]) cls.results.det_smoothed_state_cov[0, i] = np.linalg.det( cls.results.smoothed_state_cov[:, :, i]) cls.results.det_smoothed_state_disturbance_cov[0, i] = ( np.linalg.det(cls.results.smoothed_state_disturbance_cov[:, :, i]))
def test_fix_params(): # Just create a dummy model to test the basic `fix_params` mechanics mod = mlemodel.MLEModel([], 1) mod._param_names = ['a', 'b', 'c'] with mod.fix_params({'b': 1.}): assert_(mod._has_fixed_params) assert_equal(mod._fixed_params, {'b': 1.}) assert_equal(mod._fixed_params_index, [1]) assert_equal(mod._free_params_index, [0, 2]) assert_(not mod._has_fixed_params) assert_equal(mod._fixed_params, {}) assert_equal(mod._fixed_params_index, None) assert_equal(mod._free_params_index, None)
def test_nested_fix_params(): mod = mlemodel.MLEModel([], 1) mod._param_names = ['a', 'b', 'c'] with mod.fix_params({'a': 2, 'b': 0}): with mod.fix_params({'b': 1.}): assert_(mod._has_fixed_params) assert_equal(mod._fixed_params, {'a': 2, 'b': 1.}) assert_equal(mod._fixed_params_index, [0, 1]) assert_equal(mod._free_params_index, [2]) assert_(not mod._has_fixed_params) assert_equal(mod._fixed_params, {}) assert_equal(mod._fixed_params_index, None) assert_equal(mod._free_params_index, None)
def setup_class(cls, missing=None, test_against_KFAS=True, *args, **kwargs): cls.test_against_KFAS = test_against_KFAS # Data dta = datasets.macrodata.load_pandas().data dta.index = pd.date_range(start='1959-01-01', end='2009-7-01', freq='QS') obs = np.log(dta[['realgdp', 'realcons', 'realinv']]).diff().ix[1:] if missing == 'all': obs.ix[0:50, :] = np.nan elif missing == 'partial': obs.ix[0:50, 0] = np.nan elif missing == 'mixed': obs.ix[0:50, 0] = np.nan obs.ix[19:70, 1] = np.nan obs.ix[39:90, 2] = np.nan obs.ix[119:130, 0] = np.nan obs.ix[119:130, 2] = np.nan obs.iloc[-10:, :] = np.nan if test_against_KFAS: obs = obs.iloc[:9] # Create the model mod = mlemodel.MLEModel(obs, k_states=3, k_posdef=3, **kwargs) mod['design'] = np.eye(3) mod['obs_cov'] = np.array([[0.0000640649, 0., 0.], [0., 0.0000572802, 0.], [0., 0., 0.0017088585]]) mod['transition'] = np.array( [[-0.1119908792, 0.8441841604, 0.0238725303], [0.2629347724, 0.4996718412, -0.0173023305], [-3.2192369082, 4.1536028244, 0.4514379215]]) mod['selection'] = np.eye(3) mod['state_cov'] = np.array( [[0.0000640649, 0.0000388496, 0.0002148769], [0.0000388496, 0.0000572802, 0.000001555], [0.0002148769, 0.000001555, 0.0017088585]]) mod.initialize_approximate_diffuse(1e6) mod.ssm.filter_univariate = True cls.model = mod cls.results = mod.smooth([], return_ssm=True) if not compatibility_mode: cls.sim = cls.model.simulation_smoother()
def setup_class(cls, **kwargs): # Results path = os.path.join(current_path, 'results', 'results_smoothing_R.csv') cls.desired = pd.read_csv(path) # Data dta = datasets.macrodata.load_pandas().data dta.index = pd.date_range(start='1959-01-01', end='2009-7-01', freq='QS') obs = dta[['realgdp', 'realcons', 'realinv']].diff().iloc[1:] obs.iloc[0:50, 0] = np.nan obs.iloc[19:70, 1] = np.nan obs.iloc[39:90, 2] = np.nan obs.iloc[119:130, 0] = np.nan obs.iloc[119:130, 2] = np.nan # Create the model mod = mlemodel.MLEModel(obs, k_states=3, k_posdef=3, **kwargs) mod['design'] = np.eye(3) mod['obs_cov'] = np.eye(3) mod['transition'] = np.eye(3) mod['selection'] = np.eye(3) mod['state_cov'] = np.eye(3) mod.initialize_approximate_diffuse(1e6) cls.model = mod cls.results = mod.smooth([], return_ssm=True) # Calculate the determinant of the covariance matrices (for easy # comparison to other languages without having to store 2-dim arrays) cls.results.det_scaled_smoothed_estimator_cov = (np.zeros( (1, cls.model.nobs))) cls.results.det_predicted_state_cov = np.zeros((1, cls.model.nobs)) cls.results.det_smoothed_state_cov = np.zeros((1, cls.model.nobs)) cls.results.det_smoothed_state_disturbance_cov = (np.zeros( (1, cls.model.nobs))) for i in range(cls.model.nobs): cls.results.det_scaled_smoothed_estimator_cov[0, i] = ( np.linalg.det(cls.results.scaled_smoothed_estimator_cov[:, :, i])) cls.results.det_predicted_state_cov[0, i] = np.linalg.det( cls.results.predicted_state_cov[:, :, i + 1]) cls.results.det_smoothed_state_cov[0, i] = np.linalg.det( cls.results.smoothed_state_cov[:, :, i]) cls.results.det_smoothed_state_disturbance_cov[0, i] = ( np.linalg.det(cls.results.smoothed_state_disturbance_cov[:, :, i]))
def setup_class(cls, which='none', *args, **kwargs): if compatibility_mode: raise SkipTest # Data dta = datasets.macrodata.load_pandas().data dta.index = pd.date_range(start='1959-01-01', end='2009-7-01', freq='QS') obs = np.log(dta[['realgdp', 'realcons', 'realinv' ]]).diff().ix[1:] * 400 if which == 'all': obs.ix[:50, :] = np.nan obs.ix[119:130, :] = np.nan elif which == 'partial': obs.ix[0:50, 0] = np.nan obs.ix[119:130, 0] = np.nan elif which == 'mixed': obs.ix[0:50, 0] = np.nan obs.ix[19:70, 1] = np.nan obs.ix[39:90, 2] = np.nan obs.ix[119:130, 0] = np.nan obs.ix[119:130, 2] = np.nan # Create the model with typical state space mod = mlemodel.MLEModel(obs, k_states=2, k_posdef=2, **kwargs) mod['design'] = np.array([[-32.47143586, 17.33779024], [-7.40264169, 1.69279859], [-209.04702853, 125.2879374]]) mod['obs_cov'] = np.diag(np.array([0.0622668, 1.95666886, 58.37473642])) mod['transition'] = np.array([[0.29935707, 0.33289005], [-0.7639868, 1.2844237]]) mod['selection'] = np.eye(2) mod['state_cov'] = np.array([[1.2, -0.25], [-0.25, 1.1]]) mod.initialize_approximate_diffuse(1e6) mod.ssm.filter_univariate = True mod.ssm.filter_collapsed = True cls.model = mod cls.results = mod.smooth([], return_ssm=True) if not compatibility_mode: cls.sim = cls.model.simulation_smoother()
def test_simulation_smoothing_2(self): # Test with measurement and state disturbances as np.arange / 10., # initial state variates are zeros. sim = self.sim Z = self.model['design'] T = self.model['transition'] # Construct the variates measurement_disturbance_variates = np.reshape( np.arange(self.model.nobs * self.model.k_endog) / 10., (self.model.nobs, self.model.k_endog)) state_disturbance_variates = np.reshape( np.arange(self.model.nobs * self.model.ssm.k_posdef) / 10., (self.model.nobs, self.model.ssm.k_posdef)) disturbance_variates = np.r_[measurement_disturbance_variates.ravel(), state_disturbance_variates.ravel()] initial_state_variates = np.zeros(self.model.k_states) # Compute some additional known quantities generated_measurement_disturbance = np.zeros( measurement_disturbance_variates.shape) chol = np.linalg.cholesky(self.model['obs_cov']) for t in range(self.model.nobs): generated_measurement_disturbance[t] = np.dot( chol, measurement_disturbance_variates[t]) generated_state_disturbance = np.zeros( state_disturbance_variates.shape) chol = np.linalg.cholesky(self.model['state_cov']) for t in range(self.model.nobs): generated_state_disturbance[t] = np.dot( chol, state_disturbance_variates[t]) generated_obs = np.zeros((self.model.k_endog, self.model.nobs)) generated_state = np.zeros((self.model.k_states, self.model.nobs + 1)) chol = np.linalg.cholesky(self.results.initial_state_cov) generated_state[:, 0] = (self.results.initial_state + np.dot(chol, initial_state_variates)) for t in range(self.model.nobs): generated_state[:, t + 1] = (np.dot(T, generated_state[:, t]) + generated_state_disturbance.T[:, t]) generated_obs[:, t] = (np.dot(Z, generated_state[:, t]) + generated_measurement_disturbance.T[:, t]) generated_model = mlemodel.MLEModel(generated_obs.T, k_states=self.model.k_states, k_posdef=self.model.ssm.k_posdef) for name in [ 'design', 'obs_cov', 'transition', 'selection', 'state_cov' ]: generated_model[name] = self.model[name] generated_model.initialize_approximate_diffuse(1e6) generated_model.ssm.filter_univariate = True generated_res = generated_model.ssm.smooth() simulated_state = (generated_state[:, :-1] - generated_res.smoothed_state + self.results.smoothed_state) if not self.model.ssm.filter_collapsed: simulated_measurement_disturbance = ( generated_measurement_disturbance.T - generated_res.smoothed_measurement_disturbance + self.results.smoothed_measurement_disturbance) simulated_state_disturbance = ( generated_state_disturbance.T - generated_res.smoothed_state_disturbance + self.results.smoothed_state_disturbance) # Test against known values sim.simulate(disturbance_variates=disturbance_variates, initial_state_variates=np.zeros(self.model.k_states)) assert_allclose(sim.generated_measurement_disturbance, generated_measurement_disturbance) assert_allclose(sim.generated_state_disturbance, generated_state_disturbance) assert_allclose(sim.generated_state, generated_state) assert_allclose(sim.generated_obs, generated_obs) assert_allclose(sim.simulated_state, simulated_state) if not self.model.ssm.filter_collapsed: assert_allclose(sim.simulated_measurement_disturbance.T, simulated_measurement_disturbance.T) assert_allclose(sim.simulated_state_disturbance, simulated_state_disturbance) # Test against R package KFAS values if self.test_against_KFAS: path = os.path.join(current_path, 'results', 'results_simulation_smoothing2.csv') true = pd.read_csv(path) assert_allclose(sim.simulated_state.T, true[['state1', 'state2', 'state3']], atol=1e-7) assert_allclose(sim.simulated_measurement_disturbance, true[['eps1', 'eps2', 'eps3']].T, atol=1e-7) assert_allclose(sim.simulated_state_disturbance, true[['eta1', 'eta2', 'eta3']].T, atol=1e-7) signals = np.zeros((3, self.model.nobs)) for t in range(self.model.nobs): signals[:, t] = np.dot(Z, sim.simulated_state[:, t]) assert_allclose(signals, true[['signal1', 'signal2', 'signal3']].T, atol=1e-7)