def model_local_linear_trend(endog=None, params=None, direct=False): if endog is None: y1 = 10.2394 y2 = 4.2039 y3 = 6.123123 endog = np.r_[y1, y2, y3, [1] * 7] if params is None: params = [1.993, 8.253, 2.334] sigma2_y, sigma2_mu, sigma2_beta = params if direct: mod = None # Construct the basic representation ssm = KalmanSmoother(k_endog=1, k_states=2, k_posdef=2) ssm.bind(endog) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a local level model ssm['design', 0, 0] = 1 ssm['obs_cov', 0, 0] = sigma2_y ssm['transition'] = np.array([[1, 1], [0, 1]]) ssm['selection'] = np.eye(2) ssm['state_cov'] = np.diag([sigma2_mu, sigma2_beta]) else: mod = UnobservedComponents(endog, 'lltrend') mod.update(params) ssm = mod.ssm ssm.initialize(Initialization(ssm.k_states, 'diffuse')) return mod, ssm
def model_local_level(endog=None, params=None, direct=False): if endog is None: y1 = 10.2394 endog = np.r_[y1, [1] * 9] if params is None: params = [1.993, 8.253] sigma2_y, sigma2_mu = params if direct: mod = None # Construct the basic representation ssm = KalmanSmoother(k_endog=1, k_states=1, k_posdef=1) ssm.bind(endog) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a local level model ssm['design', :] = 1 ssm['obs_cov', :] = sigma2_y ssm['transition', :] = 1 ssm['selection', :] = 1 ssm['state_cov', :] = sigma2_mu else: mod = UnobservedComponents(endog, 'llevel') mod.update(params) ssm = mod.ssm ssm.initialize(Initialization(ssm.k_states, 'diffuse')) return mod, ssm
def model_local_level(endog=None, params=None, direct=False): if endog is None: y1 = 10.2394 endog = np.r_[y1, [1] * 9] if params is None: params = [1.993, 8.253] sigma2_y, sigma2_mu = params if direct: mod = None # Construct the basic representation ssm = KalmanSmoother(k_endog=1, k_states=1, k_posdef=1) ssm.bind(endog) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a local level model ssm['design', :] = 1 ssm['obs_cov', :] = sigma2_y ssm['transition', :] = 1 ssm['selection', :] = 1 ssm['state_cov', :] = sigma2_mu else: mod = UnobservedComponents(endog, 'llevel') mod.update(params) ssm = mod.ssm ssm.initialize(Initialization(ssm.k_states, 'diffuse')) return mod, ssm
def model_local_linear_trend(endog=None, params=None, direct=False): if endog is None: y1 = 10.2394 y2 = 4.2039 y3 = 6.123123 endog = np.r_[y1, y2, y3, [1] * 7] if params is None: params = [1.993, 8.253, 2.334] sigma2_y, sigma2_mu, sigma2_beta = params if direct: mod = None # Construct the basic representation ssm = KalmanSmoother(k_endog=1, k_states=2, k_posdef=2) ssm.bind(endog) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a local level model ssm['design', 0, 0] = 1 ssm['obs_cov', 0, 0] = sigma2_y ssm['transition'] = np.array([[1, 1], [0, 1]]) ssm['selection'] = np.eye(2) ssm['state_cov'] = np.diag([sigma2_mu, sigma2_beta]) else: mod = UnobservedComponents(endog, 'lltrend') mod.update(params) ssm = mod.ssm ssm.initialize(Initialization(ssm.k_states, 'diffuse')) return mod, ssm
def test_matrices_somewhat_complicated_model(): values = dta.copy() model = UnobservedComponents(values['unemp'], level='lltrend', freq_seasonal=[{'period': 4}, {'period': 9, 'harmonics': 3}], cycle=True, cycle_period_bounds=[2, 30], damped_cycle=True, stochastic_freq_seasonal=[True, False], stochastic_cycle=True ) # Selected parameters params = [1, # irregular_var 3, 4, # lltrend parameters: level_var, trend_var 5, # freq_seasonal parameters: freq_seasonal_var_0 # cycle parameters: cycle_var, cycle_freq, cycle_damp 6, 2*np.pi/30., .9 ] model.update(params) # Check scalar properties assert_equal(model.k_states, 2 + 4 + 6 + 2) assert_equal(model.k_state_cov, 2 + 1 + 0 + 1) assert_equal(model.loglikelihood_burn, 2 + 4 + 6 + 2) assert_allclose(model.ssm.k_posdef, 2 + 4 + 0 + 2) assert_equal(model.k_params, len(params)) # Check the statespace model matrices against hand-constructed answers # We group the terms by the component expected_design = np.r_[[1, 0], [1, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0]].reshape(1, 14) assert_allclose(model.ssm.design[:, :, 0], expected_design) expected_transition = __direct_sum([ np.array([[1, 1], [0, 1]]), np.array([[0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, -1, 0], [0, 0, 0, -1]]), np.array([[np.cos(2*np.pi*1/9.), np.sin(2*np.pi*1/9.), 0, 0, 0, 0], [-np.sin(2*np.pi*1/9.), np.cos(2*np.pi*1/9.), 0, 0, 0, 0], [0, 0, np.cos(2*np.pi*2/9.), np.sin(2*np.pi*2/9.), 0, 0], [0, 0, -np.sin(2*np.pi*2/9.), np.cos(2*np.pi*2/9.), 0, 0], [0, 0, 0, 0, np.cos(2*np.pi/3.), np.sin(2*np.pi/3.)], [0, 0, 0, 0, -np.sin(2*np.pi/3.), np.cos(2*np.pi/3.)]]), np.array([[.9*np.cos(2*np.pi/30.), .9*np.sin(2*np.pi/30.)], [-.9*np.sin(2*np.pi/30.), .9*np.cos(2*np.pi/30.)]]) ]) assert_allclose( model.ssm.transition[:, :, 0], expected_transition, atol=1e-7) # Since the second seasonal term is not stochastic, # the dimensionality of the state disturbance is 14 - 6 = 8 expected_selection = np.zeros((14, 14 - 6)) expected_selection[0:2, 0:2] = np.eye(2) expected_selection[2:6, 2:6] = np.eye(4) expected_selection[-2:, -2:] = np.eye(2) assert_allclose(model.ssm.selection[:, :, 0], expected_selection) expected_state_cov = __direct_sum([ np.diag(params[1:3]), np.eye(4)*params[3], np.eye(2)*params[4] ]) assert_allclose(model.ssm.state_cov[:, :, 0], expected_state_cov)
def test_matrices_somewhat_complicated_model(): values = dta.copy() model = UnobservedComponents(values['unemp'], level='lltrend', freq_seasonal=[{'period': 4}, {'period': 9, 'harmonics': 3}], cycle=True, cycle_period_bounds=[2, 30], damped_cycle=True, stochastic_freq_seasonal=[True, False], stochastic_cycle=True ) # Selected parameters params = [1, # irregular_var 3, 4, # lltrend parameters: level_var, trend_var 5, # freq_seasonal parameters: freq_seasonal_var_0 # cycle parameters: cycle_var, cycle_freq, cycle_damp 6, 2*np.pi/30., .9 ] model.update(params) # Check scalar properties assert_equal(model.k_states, 2 + 4 + 6 + 2) assert_equal(model.k_state_cov, 2 + 1 + 0 + 1) assert_equal(model.loglikelihood_burn, 2 + 4 + 6 + 2) assert_allclose(model.ssm.k_posdef, 2 + 4 + 0 + 2) assert_equal(model.k_params, len(params)) # Check the statespace model matrices against hand-constructed answers # We group the terms by the component expected_design = np.r_[[1, 0], [1, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0]].reshape(1, 14) assert_allclose(model.ssm.design[:, :, 0], expected_design) expected_transition = __direct_sum([ np.array([[1, 1], [0, 1]]), np.array([[0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, -1, 0], [0, 0, 0, -1]]), np.array([[np.cos(2*np.pi*1/9.), np.sin(2*np.pi*1/9.), 0, 0, 0, 0], [-np.sin(2*np.pi*1/9.), np.cos(2*np.pi*1/9.), 0, 0, 0, 0], [0, 0, np.cos(2*np.pi*2/9.), np.sin(2*np.pi*2/9.), 0, 0], [0, 0, -np.sin(2*np.pi*2/9.), np.cos(2*np.pi*2/9.), 0, 0], [0, 0, 0, 0, np.cos(2*np.pi/3.), np.sin(2*np.pi/3.)], [0, 0, 0, 0, -np.sin(2*np.pi/3.), np.cos(2*np.pi/3.)]]), np.array([[.9*np.cos(2*np.pi/30.), .9*np.sin(2*np.pi/30.)], [-.9*np.sin(2*np.pi/30.), .9*np.cos(2*np.pi/30.)]]) ]) assert_allclose( model.ssm.transition[:, :, 0], expected_transition, atol=1e-7) # Since the second seasonal term is not stochastic, # the dimensionality of the state disturbance is 14 - 6 = 8 expected_selection = np.zeros((14, 14 - 6)) expected_selection[0:2, 0:2] = np.eye(2) expected_selection[2:6, 2:6] = np.eye(4) expected_selection[-2:, -2:] = np.eye(2) assert_allclose(model.ssm.selection[:, :, 0], expected_selection) expected_state_cov = __direct_sum([ np.diag(params[1:3]), np.eye(4)*params[3], np.eye(2)*params[4] ]) assert_allclose(model.ssm.state_cov[:, :, 0], expected_state_cov)