def test_innovation_error(self): model_ma = PureARMA(theta=[-0.9], sigma_sq=1) self.assertAlmostEqual(model_ma.get_innovations_error(0), 1.81, 3) self.assertAlmostEqual(model_ma.get_innovations_error(1), 1.362, 3) self.assertAlmostEqual(model_ma.get_innovations_error(2), 1.215, 3) self.assertAlmostEqual(model_ma.get_innovations_error(3), 1.144, 3) self.assertAlmostEqual(model_ma.get_innovations_error(4), 1.102, 3) self.assertAlmostEqual(model_ma.get_innovations_error(5), 1.075, 3)
def test_state_space_repr(self): F = np.matrix([[0, 1], [0, 0]]) G = np.matrix([[0.5, 1]]) S = np.matrix([[0], [0]]) R = np.matrix([[0]]) Q = np.matrix([[0, 0], [0, 1]]) ma_model = PureARMA(theta=[0.5]) ma_statespace = ma_model.get_state_space_repr() np.testing.assert_equal(ma_statespace.get_F(), F) np.testing.assert_equal(ma_statespace.get_G(), G) np.testing.assert_equal(ma_statespace.get_S(), S) np.testing.assert_equal(ma_statespace.get_R(), R) np.testing.assert_equal(ma_statespace.get_Q(), Q)
def test_innovation_r(self): model_arma11 = PureARMA([0.2], [0.4], sigma_sq=5) model_ma = PureARMA(theta=[0.5], sigma_sq=3) model_arma23 = PureARMA(phi=[1, -0.24], theta=[0.4, 0.2, 0.1], sigma_sq=0.2) self.assertAlmostEqual(model_arma11.get_r(0), 1.375, 4) self.assertAlmostEqual(model_arma11.get_r(1), 1.0436, 4) self.assertAlmostEqual(model_arma11.get_r(2), 1.0067, 4) self.assertAlmostEqual(model_arma11.get_r(3), 1.0011, 4) self.assertAlmostEqual(model_arma11.get_r(4), 1.0002, 4) self.assertAlmostEqual(model_arma11.get_r(5), 1.0000, 4) for n in range(6, 11): self.assertAlmostEqual(model_arma11.get_r(n), 1, 5) self.assertAlmostEqual(model_ma.get_r(0), (1 + 0.5 ** 2), 8) self.assertAlmostEqual(model_arma23.get_r(0), 7.1713, 4) self.assertAlmostEqual(model_arma23.get_r(1), 1.3856, 4) self.assertAlmostEqual(model_arma23.get_r(2), 1.0057, 4)
def test_F(self): ar_model = PureARMA(phi=[1, 2, 3], sigma_sq=2).get_state_space_repr() ma_model = PureARMA(theta=[1, 2, 3], sigma_sq=3).get_state_space_repr() arma_model = PureARMA(phi=[1, 2, 3], theta=[1, 2, 3], sigma_sq=4).get_state_space_repr() noise_model = PureARMA(sigma_sq=5).get_state_space_repr() ar_F = np.matrix([[0, 1, 0], [0, 0, 1], [3, 2, 1]]) ma_F = np.matrix([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]) arma_F = np.matrix([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 3, 2, 1]]) noise_F = np.matrix([[0]]) np.testing.assert_equal(ar_model.get_F(), ar_F) np.testing.assert_equal(ma_model.get_F(), ma_F) np.testing.assert_equal(arma_model.get_F(), arma_F) np.testing.assert_equal(noise_model.get_F(), noise_F)
def test_Q(self): ar_model = PureARMA(phi=[1, 2, 3], sigma_sq=2).get_state_space_repr() ma_model = PureARMA(theta=[1, 2, 3], sigma_sq=3).get_state_space_repr() arma_model = PureARMA(phi=[1, 2, 3], theta=[1, 2, 3], sigma_sq=4).get_state_space_repr() noise_model = PureARMA(sigma_sq=5).get_state_space_repr() ar_Q = np.matrix([[0, 0, 0], [0, 0, 0], [0, 0, 2]]) ma_Q = np.matrix([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 3]]) arma_Q = np.matrix([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 4]]) noise_Q = np.matrix([[5]]) np.testing.assert_equal(ar_model.get_Q(), ar_Q) np.testing.assert_equal(ma_model.get_Q(), ma_Q) np.testing.assert_equal(arma_model.get_Q(), arma_Q) np.testing.assert_equal(noise_model.get_Q(), noise_Q)
def test_innovation_coefs(self): model_arma11 = PureARMA([0.2], [0.4]) model_empty = PureARMA(sigma_sq=4) model_arma23 = PureARMA(phi=[1, -0.24], theta=[0.4, 0.2, 0.1]) self.assertAlmostEqual(model_arma11.get_innovation_coef(1, 1), 0.2909, 4) self.assertAlmostEqual(model_arma11.get_innovation_coef(2, 1), 0.3833, 4) for n in range(6, 11): self.assertAlmostEqual(model_arma11.get_innovation_coef(n, 1), 0.4, 4) self.assertEqual(model_empty.get_innovation_coef(1, 1), 0) self.assertEqual(model_empty.get_innovation_coef(9, 3), 0) self.assertAlmostEqual(model_arma23.get_innovation_coef(1, 1), 0.8982, 4) self.assertAlmostEqual(model_arma23.get_innovation_coef(2, 1), 1.3685, 4) self.assertAlmostEqual(model_arma23.get_innovation_coef(2, 2), 0.7056, 4)
def test_parameters(self): sigma_sq = 2 model = PureARMA([-1, 2, 3], [4, 5], sigma_sq) self.assertEqual(model.get_phi(2), 2) self.assertEqual(model.get_phi(0), 1) self.assertEqual(model.get_phi(4), 0) self.assertEqual(model.get_theta(2), 5) self.assertEqual(model.get_theta(0), 1) self.assertEqual(model.get_theta(3), 0) self.assertEqual(model.get_sigma_sq(), sigma_sq)
def test_acf(self): model_332 = PureARMA([1, -0.25], [1], 3) model_ma = PureARMA([], [1, 2, 3]) model_empty = PureARMA() model_arma23 = PureARMA(phi=[1, -0.24], theta=[0.4, 0.2, 0.1]) arma_model = PureARMA(phi=[0.7], theta=[0.3], sigma_sq=4) for k in range(20): self.assertAlmostEqual(model_332.auto_cov_funct(k), 3 * 2 ** -k * (32 / 3 + 8 * k)) for k in range(4, 100): self.assertEqual(model_ma.auto_cov_funct(k), 0) self.assertEqual(model_empty.auto_cov_funct(0), 1) self.assertEqual(model_empty.auto_cov_funct(1), 0) self.assertEqual(model_empty.auto_cov_funct(10), 0) self.assertAlmostEqual(model_arma23.auto_cov_funct(0), 7.17133, 5) self.assertAlmostEqual(model_arma23.auto_cov_funct(1), 6.44139, 5) self.assertAlmostEqual(model_arma23.auto_cov_funct(2), 5.06027, 5) self.assertAlmostEqual(arma_model.auto_cov_funct(0), 4 * (1 + 2 * 0.3 * 0.7 + 0.3 ** 2) / (1 - 0.7 ** 2), 6)
def test_ma_infty(self): model_ma = PureARMA([], [1, 2, 3]) model_ar = PureARMA([0.5]) model_empty = PureARMA() self.assertEqual(model_ma.get_ma_infty_coef(0), 1) self.assertEqual(model_ma.get_ma_infty_coef(2), 2) self.assertEqual(model_ma.get_ma_infty_coef(3), 3) self.assertEqual(model_ma.get_ma_infty_coef(5), 0) self.assertEqual(model_ma.get_ma_infty_coef(3), 3) self.assertEqual(model_ar.get_ma_infty_coef(0), 1) self.assertEqual(model_ar.get_ma_infty_coef(1), 0.5) self.assertEqual(model_ar.get_ma_infty_coef(2), 0.25) self.assertEqual(model_ar.get_ma_infty_coef(50), 0.5 ** 50) self.assertEqual(model_empty.get_ma_infty_coef(0), 1) self.assertEqual(model_empty.get_ma_infty_coef(1), 0) self.assertEqual(model_empty.get_ma_infty_coef(10), 0)
def test_kappa_w(self): theta = 0.4 sigma_sq = 3 ma_model = PureARMA(theta=[theta], sigma_sq=sigma_sq) arma_model = PureARMA(phi=[0.7], theta=[0.3], sigma_sq=4) model_arma23 = PureARMA(phi=[1, -0.24], theta=[0.4, 0.2, 0.1]) self.assertAlmostEqual(ma_model._kappa_w(1, 1), 1 + theta ** 2) self.assertAlmostEqual(ma_model._kappa_w(2, 2), 1 + theta ** 2) self.assertAlmostEqual(ma_model._kappa_w(3, 3), 1 + theta ** 2) self.assertAlmostEqual(ma_model._kappa_w(1, 3), 0) self.assertAlmostEqual(ma_model._kappa_w(1, 2), theta) self.assertAlmostEqual(arma_model._kappa_w(1, 1), (1 + 2 * 0.3 * 0.7 + 0.3 ** 2) / (1 - 0.7 ** 2)) self.assertAlmostEqual(arma_model._kappa_w(2, 2), 1 + 0.3 ** 2) self.assertAlmostEqual(arma_model._kappa_w(3, 3), 1 + 0.3 ** 2) self.assertAlmostEqual(arma_model._kappa_w(1, 3), 0) self.assertAlmostEqual(arma_model._kappa_w(1, 4), 0) self.assertAlmostEqual(arma_model._kappa_w(1, 5), 0) self.assertAlmostEqual(arma_model._kappa_w(20, 30), 0) self.assertAlmostEqual(arma_model._kappa_w(1, 2), 0.3) self.assertAlmostEqual(arma_model._kappa_w(3, 2), 0.3) self.assertAlmostEqual(arma_model._kappa_w(16, 15), 0.3) self.assertAlmostEqual(model_arma23._kappa_w(1, 1), 7.17133, 5) self.assertAlmostEqual(model_arma23._kappa_w(1, 2), 6.44139, 5) self.assertAlmostEqual(model_arma23._kappa_w(1, 3), 5.06027, 5) self.assertAlmostEqual(model_arma23._kappa_w(4, 7), 0.1, 5) self.assertAlmostEqual(model_arma23._kappa_w(1, 5), 0, 5) self.assertAlmostEqual(model_arma23._kappa_w(2, 2), 7.17133, 5)