def test_predict(): # Tests of invalid calls to the predict function warnings.simplefilter("always") endog = np.ones((10, 1)) mod = KalmanFilter(endog, k_states=1, initialization="approximate_diffuse") mod["design", :] = 1 mod["obs_intercept"] = np.zeros((1, 10)) mod["selection", :] = 1 mod["state_cov", :] = 1 # Check that we need both forecasts and predicted output for prediction mod.memory_no_forecast = True res = mod.filter() assert_raises(ValueError, res.predict) mod.memory_no_forecast = False mod.memory_no_predicted = True res = mod.filter() assert_raises(ValueError, res.predict) mod.memory_no_predicted = False # Now get a clean filter object res = mod.filter() # Check that start < 0 is an error assert_raises(ValueError, res.predict, start=-1) # Check that end < start is an error assert_raises(ValueError, res.predict, start=2, end=1) # Check that dynamic < 0 is an error assert_raises(ValueError, res.predict, dynamic=-1) # Check that dynamic > end is an warning with warnings.catch_warnings(record=True) as w: res.predict(end=1, dynamic=2) message = "Dynamic prediction specified to begin after the end of" " prediction, and so has no effect." assert_equal(str(w[0].message), message) # Check that dynamic > nobs is an warning with warnings.catch_warnings(record=True) as w: res.predict(end=11, dynamic=11, obs_intercept=np.zeros((1, 1))) message = ( "Dynamic prediction specified to begin during" " out-of-sample forecasting period, and so has no" " effect." ) assert_equal(str(w[0].message), message) # Check for a warning when providing a non-used statespace matrix with warnings.catch_warnings(record=True) as w: res.predict(end=res.nobs + 1, design=True, obs_intercept=np.zeros((1, 1))) message = "Model has time-invariant design matrix, so the design" " argument to `predict` has been ignored." assert_equal(str(w[0].message), message) # Check that an error is raised when a new time-varying matrix is not # provided assert_raises(ValueError, res.predict, end=res.nobs + 1) # Check that an error is raised when a non-two-dimensional obs_intercept # is given assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_intercept=np.zeros(1)) # Check that an error is raised when an obs_intercept with incorrect length # is given assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_intercept=np.zeros(2)) # Check that start=None gives start=0 and end=None gives end=nobs assert_equal(res.predict().shape, (1, res.nobs)) # Check that dynamic=True begins dynamic prediction immediately # TODO just a smoke test res.predict(dynamic=True) # Check that full_results=True yields a FilterResults object assert_equal(isinstance(res.predict(full_results=True), FilterResults), True) # Check that an error is raised when a non-two-dimensional obs_cov # is given # ...and... # Check that an error is raised when an obs_cov with incorrect length # is given mod = KalmanFilter(endog, k_states=1, initialization="approximate_diffuse") mod["design", :] = 1 mod["obs_cov"] = np.zeros((1, 1, 10)) mod["selection", :] = 1 mod["state_cov", :] = 1 res = mod.filter() assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_cov=np.zeros((1, 1))) assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_cov=np.zeros((1, 1, 2)))
def test_predict(): # Tests of invalid calls to the predict function warnings.simplefilter("always") endog = np.ones((10, 1)) mod = KalmanFilter(endog, k_states=1, initialization='approximate_diffuse') mod['design', :] = 1 mod['obs_intercept'] = np.zeros((1, 10)) mod['selection', :] = 1 mod['state_cov', :] = 1 # Check that we need both forecasts and predicted output for prediction mod.memory_no_forecast = True res = mod.filter() assert_raises(ValueError, res.predict) mod.memory_no_forecast = False mod.memory_no_predicted = True res = mod.filter() assert_raises(ValueError, res.predict) mod.memory_no_predicted = False # Now get a clean filter object res = mod.filter() # Check that start < 0 is an error assert_raises(ValueError, res.predict, start=-1) # Check that end < start is an error assert_raises(ValueError, res.predict, start=2, end=1) # Check that dynamic < 0 is an error assert_raises(ValueError, res.predict, dynamic=-1) # Check that dynamic > end is an warning with warnings.catch_warnings(record=True) as w: res.predict(end=1, dynamic=2) message = ('Dynamic prediction specified to begin after the end of' ' prediction, and so has no effect.') assert_equal(str(w[0].message), message) # Check that dynamic > nobs is an warning with warnings.catch_warnings(record=True) as w: res.predict(end=11, dynamic=11, obs_intercept=np.zeros((1, 1))) message = ('Dynamic prediction specified to begin during' ' out-of-sample forecasting period, and so has no' ' effect.') assert_equal(str(w[0].message), message) # Check for a warning when providing a non-used statespace matrix with warnings.catch_warnings(record=True) as w: res.predict(end=res.nobs + 1, design=True, obs_intercept=np.zeros((1, 1))) message = ('Model has time-invariant design matrix, so the design' ' argument to `predict` has been ignored.') assert_equal(str(w[0].message), message) # Check that an error is raised when a new time-varying matrix is not # provided assert_raises(ValueError, res.predict, end=res.nobs + 1) # Check that an error is raised when a non-two-dimensional obs_intercept # is given assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_intercept=np.zeros(1)) # Check that an error is raised when an obs_intercept with incorrect length # is given assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_intercept=np.zeros(2)) # Check that start=None gives start=0 and end=None gives end=nobs assert_equal(res.predict().forecasts.shape, (1, res.nobs)) # Check that dynamic=True begins dynamic prediction immediately # TODO just a smoke test res.predict(dynamic=True) # Check that on success, PredictionResults object is returned prediction_results = res.predict(start=3, end=5) assert_equal(isinstance(prediction_results, PredictionResults), True) # Check for correctly subset representation arrays # (k_endog, npredictions) = (1, 2) assert_equal(prediction_results.endog.shape, (1, 2)) # (k_endog, npredictions) = (1, 2) assert_equal(prediction_results.obs_intercept.shape, (1, 2)) # (k_endog, k_states) = (1, 1) assert_equal(prediction_results.design.shape, (1, 1)) # (k_endog, k_endog) = (1, 1) assert_equal(prediction_results.obs_cov.shape, (1, 1)) # (k_state,) = (1,) assert_equal(prediction_results.state_intercept.shape, (1, )) # (k_state, npredictions) = (1, 2) assert_equal(prediction_results.obs_intercept.shape, (1, 2)) # (k_state, k_state) = (1, 1) assert_equal(prediction_results.transition.shape, (1, 1)) # (k_state, k_posdef) = (1, 1) assert_equal(prediction_results.selection.shape, (1, 1)) # (k_posdef, k_posdef) = (1, 1) assert_equal(prediction_results.state_cov.shape, (1, 1)) # Check for correctly subset filter output arrays # (k_endog, npredictions) = (1, 2) assert_equal(prediction_results.forecasts.shape, (1, 2)) assert_equal(prediction_results.forecasts_error.shape, (1, 2)) # (k_states, npredictions) = (1, 2) assert_equal(prediction_results.filtered_state.shape, (1, 2)) assert_equal(prediction_results.predicted_state.shape, (1, 2)) # (k_endog, k_endog, npredictions) = (1, 1, 2) assert_equal(prediction_results.forecasts_error_cov.shape, (1, 1, 2)) # (k_states, k_states, npredictions) = (1, 1, 2) assert_equal(prediction_results.filtered_state_cov.shape, (1, 1, 2)) assert_equal(prediction_results.predicted_state_cov.shape, (1, 1, 2)) # Check for invalid attribute assert_raises(AttributeError, getattr, prediction_results, 'test') # Check that an error is raised when a non-two-dimensional obs_cov # is given # ...and... # Check that an error is raised when an obs_cov with incorrect length # is given mod = KalmanFilter(endog, k_states=1, initialization='approximate_diffuse') mod['design', :] = 1 mod['obs_cov'] = np.zeros((1, 1, 10)) mod['selection', :] = 1 mod['state_cov', :] = 1 res = mod.filter() assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_cov=np.zeros((1, 1))) assert_raises(ValueError, res.predict, end=res.nobs + 1, obs_cov=np.zeros((1, 1, 2)))
def test_predict(): # Tests of invalid calls to the predict function warnings.simplefilter("always") endog = np.ones((10,1)) mod = KalmanFilter(endog, k_states=1, initialization='approximate_diffuse') mod['design', :] = 1 mod['obs_intercept'] = np.zeros((1,10)) mod['selection', :] = 1 mod['state_cov', :] = 1 # Check that we need both forecasts and predicted output for prediction mod.memory_no_forecast = True res = mod.filter() assert_raises(ValueError, res.predict) mod.memory_no_forecast = False mod.memory_no_predicted = True res = mod.filter() assert_raises(ValueError, res.predict) mod.memory_no_predicted = False # Now get a clean filter object res = mod.filter() # Check that start < 0 is an error assert_raises(ValueError, res.predict, start=-1) # Check that end < start is an error assert_raises(ValueError, res.predict, start=2, end=1) # Check that dynamic < 0 is an error assert_raises(ValueError, res.predict, dynamic=-1) # Check that dynamic > end is an warning with warnings.catch_warnings(record=True) as w: res.predict(end=1, dynamic=2) message = ('Dynamic prediction specified to begin after the end of' ' prediction, and so has no effect.') assert_equal(str(w[0].message), message) # Check that dynamic > nobs is an warning with warnings.catch_warnings(record=True) as w: res.predict(end=11, dynamic=11, obs_intercept=np.zeros((1,1))) message = ('Dynamic prediction specified to begin during' ' out-of-sample forecasting period, and so has no' ' effect.') assert_equal(str(w[0].message), message) # Check for a warning when providing a non-used statespace matrix with warnings.catch_warnings(record=True) as w: res.predict(end=res.nobs+1, design=True, obs_intercept=np.zeros((1,1))) message = ('Model has time-invariant design matrix, so the design' ' argument to `predict` has been ignored.') assert_equal(str(w[0].message), message) # Check that an error is raised when a new time-varying matrix is not # provided assert_raises(ValueError, res.predict, end=res.nobs+1) # Check that an error is raised when a non-two-dimensional obs_intercept # is given assert_raises(ValueError, res.predict, end=res.nobs+1, obs_intercept=np.zeros(1)) # Check that an error is raised when an obs_intercept with incorrect length # is given assert_raises(ValueError, res.predict, end=res.nobs+1, obs_intercept=np.zeros(2)) # Check that start=None gives start=0 and end=None gives end=nobs assert_equal(res.predict().forecasts.shape, (1,res.nobs)) # Check that dynamic=True begins dynamic prediction immediately # TODO just a smoke test res.predict(dynamic=True) # Check that on success, PredictionResults object is returned prediction_results = res.predict(start=3, end=5) assert_equal(isinstance(prediction_results, PredictionResults), True) # Check for correctly subset representation arrays # (k_endog, npredictions) = (1, 2) assert_equal(prediction_results.endog.shape, (1, 2)) # (k_endog, npredictions) = (1, 2) assert_equal(prediction_results.obs_intercept.shape, (1, 2)) # (k_endog, k_states) = (1, 1) assert_equal(prediction_results.design.shape, (1, 1)) # (k_endog, k_endog) = (1, 1) assert_equal(prediction_results.obs_cov.shape, (1, 1)) # (k_state,) = (1,) assert_equal(prediction_results.state_intercept.shape, (1,)) # (k_state, npredictions) = (1, 2) assert_equal(prediction_results.obs_intercept.shape, (1, 2)) # (k_state, k_state) = (1, 1) assert_equal(prediction_results.transition.shape, (1, 1)) # (k_state, k_posdef) = (1, 1) assert_equal(prediction_results.selection.shape, (1, 1)) # (k_posdef, k_posdef) = (1, 1) assert_equal(prediction_results.state_cov.shape, (1, 1)) # Check for correctly subset filter output arrays # (k_endog, npredictions) = (1, 2) assert_equal(prediction_results.forecasts.shape, (1, 2)) assert_equal(prediction_results.forecasts_error.shape, (1, 2)) # (k_states, npredictions) = (1, 2) assert_equal(prediction_results.filtered_state.shape, (1, 2)) assert_equal(prediction_results.predicted_state.shape, (1, 2)) # (k_endog, k_endog, npredictions) = (1, 1, 2) assert_equal(prediction_results.forecasts_error_cov.shape, (1, 1, 2)) # (k_states, k_states, npredictions) = (1, 1, 2) assert_equal(prediction_results.filtered_state_cov.shape, (1, 1, 2)) assert_equal(prediction_results.predicted_state_cov.shape, (1, 1, 2)) # Check for invalid attribute assert_raises(AttributeError, getattr, prediction_results, 'test') # Check that an error is raised when a non-two-dimensional obs_cov # is given # ...and... # Check that an error is raised when an obs_cov with incorrect length # is given mod = KalmanFilter(endog, k_states=1, initialization='approximate_diffuse') mod['design', :] = 1 mod['obs_cov'] = np.zeros((1,1,10)) mod['selection', :] = 1 mod['state_cov', :] = 1 res = mod.filter() assert_raises(ValueError, res.predict, end=res.nobs+1, obs_cov=np.zeros((1,1))) assert_raises(ValueError, res.predict, end=res.nobs+1, obs_cov=np.zeros((1,1,2)))