def test_ma_fit_durbin_levinson(self): np.random.seed(12345) simulated_ma1 = simulate_arma(theta=[0], sigma=0.5, simulations=10000) ma1 = ARMA(simulated_ma1) ma1.fit_ma(q=1, method="durbin_levinson") self.assertAlmostEqual(ma1.model.get_theta(1), 0, 1) self.assertAlmostEqual(ma1.model.get_sigma_sq(), 0.25, 1)
def test_likelihood_kalman(self): ma_model = PureARMA(theta=[0.5]) ma_data = [0, 1] ma_ts = ARMA(ma_data, subtract_mean=False) likelihood = (2 * np.pi) ** -1 * np.exp(-0.5) self.assertAlmostEqual(ma_ts.get_likelihood(model=ma_model, method="kalman"), likelihood)
def test_turning_point_test(self): data = np.array([1, 0.5, 0, 1, 0.4, 0.2, 3]) seq = ARMA(data) model = PureARMA() turning_test_result = abs(10 / 3 - 3) / ((16 * 7 - 29) / 90) self.assertEqual(seq.turning_point_test(model=model), turning_test_result) self.assertEqual(seq.turning_point_test(residuals=data), turning_test_result)
def test_difference_sign_test(self): data = np.array([1, 0.5, 0, 1, 0.4, 0.2, 3]) seq = ARMA(data) model = PureARMA() diff_sign_result = 1 / (8 / 12) self.assertEqual(seq.difference_sign_test(model=model), diff_sign_result) self.assertEqual(seq.difference_sign_test(residuals=data), diff_sign_result)
def test_ar_fit_durbin_levinson(self): np.random.seed(12345) simulated_ar3 = simulate_arma(phi=[0.5, 0.1, 0.2], sigma=0.5) ar3 = ARMA(simulated_ar3) ar3.fit_ar(p=3, method="durbin_levinson") self.assertAlmostEqual(ar3.model.get_phi(1), 0.5, 1) self.assertAlmostEqual(ar3.model.get_phi(2), 0.1, 1) self.assertAlmostEqual(ar3.model.get_phi(3), 0.2, 1) self.assertAlmostEqual(ar3.model.get_sigma_sq(), 0.25, 1)
def test_one_step_kalman(self): ma_model = PureARMA(theta=[0.5], sigma_sq=2) ma_data = [1, 1.5, 0.5, -2] ma = ARMA(ma_data, subtract_mean=False) zeros = np.zeros(20) arma_model = PureARMA(phi=[0.2, -0.6, 0.3], theta=[0.3, -0.4], sigma_sq=0.25) arma = ARMA(zeros) ar_data = [1, 2, 4] ar_model = PureARMA(phi=[0.5, -0.5], sigma_sq=2) ar = ARMA(ar_data, subtract_mean=False) for k in range(21): self.assertEqual(arma.get_one_step_predictor(k, arma_model, method="kalman"), 0) self.assertAlmostEqual(ma.get_one_step_predictor(0, ma_model, method="kalman"), 0) self.assertAlmostEqual(ma.get_one_step_predictor(1, ma_model, method="kalman"), 0.5) self.assertAlmostEqual(ma.get_one_step_predictor(2, ma_model, method="kalman"), 0.5) self.assertAlmostEqual(ma.get_one_step_predictor(3, ma_model, method="kalman"), 0) self.assertAlmostEqual(ma.get_one_step_predictor(4, ma_model, method="kalman"), -1) self.assertAlmostEqual(ar.get_one_step_predictor(0, ar_model, method="kalman"), 0) self.assertAlmostEqual(ar.get_one_step_predictor(1, ar_model, method="kalman"), 0.5) self.assertAlmostEqual(ar.get_one_step_predictor(2, ar_model, method="kalman"), 0.5) self.assertAlmostEqual(ar.get_one_step_predictor(3, ar_model, method="kalman"), 1)
def test_weighted_sum_squared_residuals(self): data = [-2, 0, 2] arma = ARMA(data) model = PureARMA() ma_model = PureARMA(theta=[-0.9], sigma_sq=2) ma_data = [-2.58, 1.62, -0.96, 2.62, -1.36] ma = ARMA(ma_data, subtract_mean=False) self.assertAlmostEqual(arma.get_weighted_sum_squared_residuals(model), 8) self.assertAlmostEqual(ma.get_weighted_sum_squared_residuals(ma_model), 8.02, 1)
def test_aicc_methods(self): np.random.seed(12345) simulated_arma = simulate_arma(phi=[0.2, 0.5], theta=[0.2], simulations=50) arma = ARMA(simulated_arma) arma_model = PureARMA(phi=[0.2, 0.5], theta=[0.2], sigma_sq=0.25) aicc_kalman = arma.get_aicc(model=arma_model, method="kalman") aicc_innovations = arma.get_aicc(model=arma_model, method="innovations_algo") # test <0.5% difference between methods self.assertTrue(abs(aicc_innovations - aicc_kalman) / aicc_innovations < 0.005)
def test_loglikelihood(self): model_arma11 = PureARMA([0.2], [0.4], sigma_sq=1) zeros = np.zeros(6) non_zeros = [0, 0, 0, 0, 0, 1] zero_ts = ARMA(zeros) non_zero_ts = ARMA(non_zeros, subtract_mean=False) loglikelihood_zeros = np.log((2 * np.pi) ** -3 * (1.375 * 1.0436 * 1.0067 * 1.0011 * 1.0002) ** -0.5) loglikelihood_non_zeros = np.log(np.exp(-0.5 * (1 / 1.0002))) + loglikelihood_zeros self.assertAlmostEqual(zero_ts.get_loglikelihood(model_arma11), loglikelihood_zeros, 4) self.assertAlmostEqual(non_zero_ts.get_loglikelihood(model_arma11), loglikelihood_non_zeros, 3)
def test_aic_innovation(self): model_arma11 = PureARMA([0.2], [0.4], sigma_sq=1) zeros = np.zeros(6) non_zeros = [0, 0, 0, 0, 0, 1] zero_ts = ARMA(zeros) non_zero_ts = ARMA(non_zeros, subtract_mean=False) loglikelihood_zeros = np.log((2 * np.pi) ** -3 * (1.375 * 1.0436 * 1.0067 * 1.0011 * 1.0002) ** -0.5) loglikelihood_non_zeros = np.log(np.exp(-0.5 * (1 / 1.0002))) + loglikelihood_zeros aicc_zeros = -2 * loglikelihood_zeros + 6 aicc_nonzeros = -2 * loglikelihood_non_zeros + 6 self.assertAlmostEqual(zero_ts.get_aic(model_arma11, method="innovations_algo"), aicc_zeros, 4) self.assertAlmostEqual(non_zero_ts.get_aic(model_arma11, method="innovations_algo"), aicc_nonzeros, 3)
def test_transform_mean(self): data = [-1, 0.5, 0, -0.5, 1] empty = [] mean_zero = ARMA([1, -1]) pos_mean = ARMA([2]) neg_fraction_mean = ARMA([-0.5]) self.assertListEqual(mean_zero._transform(data).tolist(), data) self.assertListEqual(pos_mean._transform(data).tolist(), [-3, -1.5, -2, -2.5, -1]) self.assertListEqual(neg_fraction_mean._transform(data).tolist(), [-0.5, 1, 0.5, 0, 1.5]) self.assertListEqual(mean_zero._transform(empty).tolist(), empty) self.assertListEqual(pos_mean._transform(empty).tolist(), empty) self.assertListEqual(neg_fraction_mean._transform(empty).tolist(), empty)
def test_FPE(self): short_data = np.zeros(10) long_data = np.ones(100000) low_var_model = PureARMA(phi=[0.3, 0.2], sigma_sq=0.1) high_var_model = PureARMA(phi=[0.1, 0.4, -0.4], sigma_sq=5) noise_model = PureARMA(sigma_sq=2) short_ts = ARMA(short_data) long_ts = ARMA(long_data) short_ts.model = low_var_model long_ts.model = low_var_model self.assertAlmostEqual(short_ts.get_fpe(), 0.1 * 12 / 8) self.assertAlmostEqual(short_ts.get_fpe(model=high_var_model), 5 * 13 / 7) self.assertAlmostEqual(short_ts.get_fpe(model=noise_model), 2) self.assertAlmostEqual(long_ts.get_fpe(), 0.1 * 100002 / 99998) self.assertAlmostEqual(long_ts.get_fpe(model=high_var_model), 5 * 100003 / 99997) self.assertAlmostEqual(long_ts.get_fpe(model=noise_model), 2)
def test_likelihood_inno(self): ma_model = PureARMA(theta=[-0.9], sigma_sq=1) ma_data = [-2.58, 1.62, -0.96, 2.62, -1.36] ma = ARMA(ma_data, subtract_mean=False) model_arma11 = PureARMA([0.2], [0.4], sigma_sq=1) zeros = np.zeros(6) non_zeros = [0, 0, 0, 0, 0, 1] zero_ts = ARMA(zeros) non_zero_ts = ARMA(non_zeros, subtract_mean=False) likelihood_zeros = (2 * np.pi) ** -3 * (1.375 * 1.0436 * 1.0067 * 1.0011 * 1.0002) ** -0.5 likelihood_non_zeros = likelihood_zeros * np.exp(-0.5 * (1 / 1.0002)) # self.assertAlmostEqual(ma.get_likelihood(ma_model), 0.0035943790355147075, 4) self.assertAlmostEqual(zero_ts.get_likelihood(model_arma11, method="innovations_algo"), likelihood_zeros) self.assertAlmostEqual( non_zero_ts.get_likelihood(model_arma11, method="innovations_algo"), likelihood_non_zeros, 6 )
def test_sample_covariance_matrix(self): integers = [1, 2, 3, 4, 5] integer_ts = ARMA(integers) cov_mat_1 = np.matrix([2]) cov_mat_2 = np.matrix([[2, 0.8], [0.8, 2]]) cov_mat_3 = np.matrix([[2, 0.8, -0.2], [0.8, 2, 0.8], [-0.2, 0.8, 2]]) cov_mat_4 = np.matrix([[2, 0.8, -0.2, -0.8], [0.8, 2, 0.8, -0.2], [-0.2, 0.8, 2, 0.8], [-0.8, -0.2, 0.8, 2]]) cov_mat_5 = np.matrix( [ [2, 0.8, -0.2, -0.8, -0.8], [0.8, 2, 0.8, -0.2, -0.8], [-0.2, 0.8, 2, 0.8, -0.2], [-0.8, -0.2, 0.8, 2, 0.8], [-0.8, -0.8, -0.2, 0.8, 2], ] ) np.testing.assert_almost_equal(integer_ts.sample_covariance_matrix(1), cov_mat_1) np.testing.assert_almost_equal(integer_ts.sample_covariance_matrix(2), cov_mat_2) np.testing.assert_almost_equal(integer_ts.sample_covariance_matrix(3), cov_mat_3) np.testing.assert_almost_equal(integer_ts.sample_covariance_matrix(4), cov_mat_4) np.testing.assert_almost_equal(integer_ts.sample_covariance_matrix(5), cov_mat_5)
def test_sample_acf(self): integers = [1, 2, 3, 4, 5] integer_ts = ARMA(integers) self.assertAlmostEqual(integer_ts.sample_acf(0), 1, 10) self.assertAlmostEqual(integer_ts.sample_acf(-1), 0.4, 10) self.assertAlmostEqual(integer_ts.sample_acf(2), -0.1, 10) self.assertAlmostEqual(integer_ts.sample_acf(-3), -0.4, 10) self.assertAlmostEqual(integer_ts.sample_acf(4), -0.4, 10)
def ran_a_test(): a = ARMA([-0.5, 0.11], [], 0.5, "uni") time_series = [a.generater.next() for i in range(2000)] p = ArPredicter(len(a.alphas), max_x = max(time_series),) errors = [] for index, x in enumerate(time_series): if index < p.min_ob: p.predict_and_fit(x) elif random.random() > missing_percent: rec_x = p.predict_and_fit(x) errors.append(rec_x - x) else: p.predict_and_fit('*') mse = sum(map(lambda x: x**2, errors)) / len(errors) result['errors'].append(errors) result['mses'].append(mse)
def run_a_test(): #a = ARMA([0.3, -0.4, 0.4, -0.5, 0.6], [], 0.3, noise_type) a = ARMA([-0.5, 0.11], [], 0.5, noise_type) time_series = [a.generater.next() for i in range(2000)] if_missing = [ 1 if random.random() > missing_rate or time_series.index(t) < 6 else 0 for t in time_series ] time_series = [ t if flag == 1 else 0 for t, flag in zip(time_series, if_missing) ] missing_indexs = [ index for index, flag in enumerate(if_missing) if flag == 0 ] p = ArPredicter(a.p, time_series, missing_indexs) result['errors'].append(p.errors) result['mses'].append(p.mse)
def main(_): flags_obj = tf.flags.FLAGS euler_graph = tf_euler.dataset.get_dataset(flags_obj.dataset) euler_graph.load_graph() dims = [flags_obj.hidden_dim, flags_obj.hidden_dim] if flags_obj.run_mode == 'train': metapath = [euler_graph.train_edge_type] else: metapath = [euler_graph.all_edge_type] num_steps = int((euler_graph.total_size + 1) // flags_obj.batch_size * flags_obj.num_epochs) model = ARMA(dims, metapath, euler_graph.feature_idx, euler_graph.feature_dim, euler_graph.label_idx, euler_graph.label_dim, num_layers=flags_obj.layers, K=flags_obj.K) params = { 'train_node_type': euler_graph.train_node_type[0], 'batch_size': flags_obj.batch_size, 'optimizer': flags_obj.optimizer, 'learning_rate': flags_obj.learning_rate, 'log_steps': flags_obj.log_steps, 'model_dir': flags_obj.model_dir, 'id_file': euler_graph.id_file, 'infer_dir': flags_obj.model_dir, 'total_size': euler_graph.total_size, 'total_step': num_steps } config = tf.estimator.RunConfig(log_step_count_steps=None) model_estimator = NodeEstimator(model, params, config) if flags_obj.run_mode == 'train': model_estimator.train() elif flags_obj.run_mode == 'evaluate': model_estimator.evaluate() elif flags_obj.run_mode == 'infer': model_estimator.infer() else: raise ValueError('Run mode not exist!')
def test_difference_sign(self): const = np.ones(10) decreasing = np.array([2, 1, 0.5, 0, -0.2]) increasing = np.array([-2, -1, 0, 0.1, 0.2, 0.4]) seq = np.array([1, 0.5, 0, 1, 0.4, 0.2, 3]) short = np.array([0, 1, 0]) very_short = np.array([0, 1]) singleton = np.array([0]) empty = np.array([]) arma = ARMA([0]) self.assertEqual(arma._differene_sign(const), 0) self.assertEqual(arma._differene_sign(decreasing), 0) self.assertEqual(arma._differene_sign(increasing), 5) self.assertEqual(arma._differene_sign(seq), 2) self.assertEqual(arma._differene_sign(short), 1) self.assertEqual(arma._differene_sign(very_short), 1) self.assertEqual(arma._differene_sign(singleton), 0) self.assertEqual(arma._differene_sign(empty), 0)
def test_turning_pts(self): const = np.ones(10) decreasing = np.array([2, 1, 0.5, 0, -0.2]) increasing = np.array([-2, -1, 0, 0.1, 0.2, 0.4]) seq = np.array([1, 0.5, 0, 1, 0.4, 0.2, 3]) short = np.array([0, 1, 0]) very_short = np.array([0, 1]) singleton = np.array([0]) empty = np.array([]) arma = ARMA([0]) self.assertEqual(arma._turning_points(const), 0) self.assertEqual(arma._turning_points(decreasing), 0) self.assertEqual(arma._turning_points(increasing), 0) self.assertEqual(arma._turning_points(seq), 3) self.assertEqual(arma._turning_points(short), 1) self.assertEqual(arma._turning_points(very_short), 0) self.assertEqual(arma._turning_points(singleton), 0) self.assertEqual(arma._turning_points(empty), 0)
def test_ar_fit_yule_walker(self): zeros = np.zeros(100) zero_ts = ARMA(zeros) np.random.seed(12345) simulated_ar3 = simulate_arma(phi=[0.5, 0.1, 0.2], sigma=0.5) ar3 = ARMA(simulated_ar3) zero_ts.fit_ar(p=0, method="yule_walker") self.assertEqual(len(zero_ts.model.get_params()[0]), 0) ar3.fit_ar(p=3, method="yule_walker") self.assertAlmostEqual(ar3.model.get_params()[0][0], 0.5, 1) self.assertAlmostEqual(ar3.model.get_params()[0][1], 0.1, 1) self.assertAlmostEqual(ar3.model.get_params()[0][2], 0.2, 1)
def test_sample_autocovariance(self): zeros = np.zeros(100) zero_ts = ARMA(zeros, subtract_mean=True) ones = np.ones(10) ones_ts_mean_corrected = ARMA(ones, subtract_mean=True) ones_ts_mean_uncorrected = ARMA(ones, subtract_mean=False) integers = [1, 2, 3, 4, 5] integer_ts = ARMA(integers) for k in range(100): self.assertEqual(zero_ts.sample_autocovariance(k), 0) for k in range(10): self.assertEqual(ones_ts_mean_corrected.sample_autocovariance(k), 0) self.assertEqual(ones_ts_mean_corrected.sample_autocovariance(-k), 0) for k in range(10): self.assertEqual(ones_ts_mean_uncorrected.sample_autocovariance(-k), 0) self.assertEqual(ones_ts_mean_uncorrected.sample_autocovariance(k), 0) self.assertAlmostEqual(integer_ts.sample_autocovariance(0), 2, 10) self.assertAlmostEqual(integer_ts.sample_autocovariance(1), 0.8, 10) self.assertAlmostEqual(integer_ts.sample_autocovariance(2), -0.2, 10) self.assertAlmostEqual(integer_ts.sample_autocovariance(3), -0.8, 10) self.assertAlmostEqual(integer_ts.sample_autocovariance(4), -0.8, 10)
def test_sample_pacf(self): integers = [1, 2, 3, 4, 5] integer_ts = ARMA(integers) self.assertAlmostEqual(integer_ts.sample_pacf(1), 0.4) self.assertAlmostEqual(integer_ts.sample_pacf(2), -13 / 42)
def test_one_step_predictions(self): ma_model = PureARMA(theta=[-0.9], sigma_sq=1) ma_data = [-2.58, 1.62, -0.96, 2.62, -1.36] ma = ARMA(ma_data, subtract_mean=False) self.assertEqual(ma.get_one_step_predictor(0, ma_model), 0) self.assertAlmostEqual(ma.get_one_step_predictor(1, ma_model, method="innovations_algo"), 1.28, 2) self.assertAlmostEqual(ma.get_one_step_predictor(2, ma_model, method="innovations_algo"), -0.22, 2) self.assertAlmostEqual(ma.get_one_step_predictor(3, ma_model, method="innovations_algo"), 0.55, 2) self.assertAlmostEqual(ma.get_one_step_predictor(4, ma_model, method="innovations_algo"), -1.63, 2) self.assertAlmostEqual(ma.get_one_step_predictor(5, ma_model, method="innovations_algo"), -0.22, 2) arma_data = [-1.1, 0.514, 0.116, -0.845, 0.872, -0.467, -0.977, -1.699, -1.228, -1.093] arma_model = PureARMA(phi=[0.2], theta=[0.4], sigma_sq=1) arma = ARMA(arma_data, subtract_mean=False) self.assertEqual(arma.get_one_step_predictor(0, arma_model, method="innovations_algo"), 0) self.assertAlmostEqual(arma.get_one_step_predictor(1, arma_model, method="innovations_algo"), -0.534, 1) self.assertAlmostEqual(arma.get_one_step_predictor(2, arma_model, method="innovations_algo"), 0.5068, 1) self.assertAlmostEqual(arma.get_one_step_predictor(3, arma_model, method="innovations_algo"), -0.1321, 1) self.assertAlmostEqual(arma.get_one_step_predictor(4, arma_model, method="innovations_algo"), -0.4539, 1) self.assertAlmostEqual(arma.get_one_step_predictor(5, arma_model, method="innovations_algo"), 0.7046, 1) data = [1, 2, 3, 4, 5] empty_model = PureARMA() empty = ARMA(data) empty.model = empty_model for k in range(6): self.assertEqual(empty.get_one_step_predictor(k, method="innovations_algo"), 0)
def test_no_fitting_exceptions(self): np.random.seed(12345) simulated_arma = simulate_arma(phi=[0.2, 0.5], theta=[0.2], simulations=10) arma = ARMA(simulated_arma) for method in arma._implemented_arma_methods: arma.fit_arma(p=2, q=1, method=method)
def test_reduced_likelihood(self): ma_model = PureARMA(theta=[-0.9], sigma_sq=0.5) ma_data = [-2.58, 1.62, -0.96, 2.62, -1.36] ma = ARMA(ma_data, subtract_mean=False) self.assertAlmostEqual(ma.get_reduced_likelihood(ma_model), 0.7387, 2)