def setUp(self) -> None: self._horizon = 3 self._predicted = [{ 'zlib': TimeSeries([1, 1.5, 2], dtype=float) }, { 'zlib': TimeSeries([1.4, 1.6, 1.8], dtype=float) }] self._observed = [ TimeSeries([1.1, 1.4, 1.8], dtype=float), TimeSeries([1.4, 1.8, 2.0], dtype=float) ]
def test_postprocessing_made_correctly(self): forecasts = { 'zlib': TimeSeries([2, 3, 2, 3], dtype=int, frequency=1), 'ppmd': TimeSeries([3, 3, 2, 3], dtype=int, frequency=1) } expected_result = { 'zlib': TimeSeries([9, 11, 11, 12], dtype=int, frequency=1), 'ppmd': TimeSeries([10, 11, 11, 13], dtype=int, frequency=1) } self._hook.preprocess(self._series) actual_result = self._hook.postprocess(forecasts) self.assertEqual(actual_result, expected_result)
def setUp(self): self._time_series = TimeSeries([1, 2, 3], dtype=int, frequency=2) self._statistics_handler = MagicMock() self._task = SimpleTask(self._statistics_handler, Types(int, TimeSeries, DiscreteUnivariateElemetaryTask), time_series=self._time_series, compressors='', horizon=0, difference=0, sparse=-1) self._result_of_computation = { 'zlib': TimeSeries([1, 2, 3], dtype=int, frequency=1) }
def setUp(self) -> None: self._time_series = TimeSeries([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int, frequency=1) self._horizon = 3 self._statistics_handler = MagicMock() self._task = ComplexTask( self._statistics_handler, Types(int, TimeSeries, DiscreteUnivariateElemetaryTask), self._time_series, 5, ['zlib'], self._horizon, 0, 0) self._prediction0 = {'zlib': TimeSeries([10, 11, 12], dtype=int)} self._prediction1 = {'zlib': TimeSeries([13, 14, 15], dtype=int)} self._prediction2 = {'zlib': TimeSeries([16, 17, 18], dtype=int)} self._actual_forecast = {'zlib': TimeSeries([19, 20, 21], dtype=int)} self._expected_predicted_values = [ self._prediction0, self._prediction1, self._prediction2 ] self._expected_observed_values = [ TimeSeries([5, 6, 7], dtype=int), TimeSeries([6, 7, 8], dtype=int), TimeSeries([7, 8, 9], dtype=int) ] self._result_of_computation = [ self._prediction0, self._prediction1, self._prediction2, self._actual_forecast ]
def test_reverse_transformation_works_correctly(self): series = TimeSeries([0, 1, 2, 3, 4, 5, 7, 9, 11], dtype=int, frequency=1) result = { 'zlib': TimeSeries([8, 6, 6], dtype=int, frequency=1), 'ppmd': TimeSeries([8, 7, 6], dtype=int, frequency=1) } expected_result = { 'zlib': TimeSeries([12, 11, 13], dtype=int, frequency=1), 'ppmd': TimeSeries([12, 12, 13], dtype=int, frequency=1) } self._hook.preprocess(series) self.assertEqual(self._hook.postprocess(result), expected_result)
def test_returns_right_relative_errors(self): expected_relative_errors = TimeSeries( [0.07142857, 0.21428571, 0.28571429], dtype=float) obtained_relative_errors = self._statistics.relative_errors('zlib') for i in range(len(expected_relative_errors)): self.assertAlmostEqual(obtained_relative_errors[i], expected_relative_errors[i])
def test_returns_right_mean_absolute_errors(self): expected_absolute_errors = TimeSeries([0.05, 0.15, 0.2], dtype=float) obtained_absolute_errors = self._statistics.mean_absolute_errors( 'zlib') for i in range(len(expected_absolute_errors)): self.assertAlmostEqual(obtained_absolute_errors[i], expected_absolute_errors[i])
def test_compute_standard_deviations_returns_right_answer(self) -> None: expected_result = {'zlib': TimeSeries([0.05, 0.05, 0.0], dtype=float)} actual_result = sut._compute_standard_deviations( self._predicted, self._observed, self._horizon) for i in range(len(expected_result['zlib'])): self.assertAlmostEqual(expected_result['zlib'][i], actual_result['zlib'][i])
def test_compute_sum_of_errors_returns_right_answer(self) -> None: expected_result = {'zlib': TimeSeries([0.1, 0.3, 0.4], dtype=float)} actual_result = sut._compute_sum_of_errors(self._predicted, self._observed, self._horizon) for i in range(len(expected_result['zlib'])): self.assertAlmostEqual(expected_result['zlib'][i], actual_result['zlib'][i])
def forecast_discrete(time_series, compressors, horizon, difference, sparse) -> Dict[str, TimeSeries]: result = p.make_forecast_discrete(time_series.to_list(), compressors, horizon, difference, sparse) return { key: TimeSeries([round(x) for x in value], time_series.frequency(), time_series.dtype()) for key, value in result.items() }
def forecast_multialphabet(time_series, compressors, horizon, difference, max_quanta_count, sparse) -> Dict[str, TimeSeries]: result = p.make_forecast_multialphabet(time_series.to_list(), compressors, horizon, difference, max_quanta_count, sparse) return { key: TimeSeries(value, time_series.frequency(), time_series.dtype()) for key, value in result.items() }
def setUp(self) -> None: self._history = TimeSeries([0.5, 0.8, 0.8], dtype=float) self._horizon = 3 self._predicted = [{ 'zlib': TimeSeries([1, 1.5, 2], dtype=float) }, { 'zlib': TimeSeries([1.4, 1.6, 1.8], dtype=float) }] self._observed = [ TimeSeries([1.1, 1.4, 1.8], dtype=float), TimeSeries([1.4, 1.8, 2.0], dtype=float) ] self._forecast = {'zlib': TimeSeries([1, 1.5, 2], dtype=float)} self._statistics = sut.ComplexTaskStatisticsHandler() self._statistics.set_results_of_computations(self._history, self._forecast, self._predicted, self._observed, self._horizon)
def test_works_correctly_on_series_of_minimal_correct_length(self): series = TimeSeries([1, 2, 3], dtype=int, frequency=1) expected_result = TimeSeries([(2 * 3 + 2 + 1) / 4], dtype=int, frequency=1) self.assertEqual(self._hook.preprocess(series), expected_result)
def test_returns_right_upper_bounds(self): expected_upper_bounds = TimeSeries([1.05, 1.55, 2], dtype=float) obtained_upper_bounds = self._statistics.upper_bounds('zlib') for i in range(len(expected_upper_bounds)): self.assertAlmostEqual(obtained_upper_bounds[i], expected_upper_bounds[i])
def setUp(self) -> None: self._history = TimeSeries([0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1], dtype=int) self._horizon = 1 self._predicted = [{ 'zlib': TimeSeries([1]), 'automaton': TimeSeries([0]) }, { 'zlib': TimeSeries([1]), 'automaton': TimeSeries([0]) }, { 'zlib': TimeSeries([0]), 'automaton': TimeSeries([0]) }, { 'zlib': TimeSeries([1]), 'automaton': TimeSeries([1]) }, { 'zlib': TimeSeries([0]), 'automaton': TimeSeries([1]) }, { 'zlib': TimeSeries([1]), 'automaton': TimeSeries([1]) }] self._observed = [ TimeSeries([0]), TimeSeries([0]), TimeSeries([0]), TimeSeries([1]), TimeSeries([1]), TimeSeries([1]) ] self._forecast = { 'zlib': TimeSeries([1]), 'automaton': TimeSeries([0]) } self._statistics = sut.SumOfErrorsStatisticsHandler() self._statistics.set_results_of_computations(self._history, self._forecast, self._predicted, self._observed, self._horizon)
def test_returns_right_sum_of_errors(self): self._sequences_almost_equal(self._statistics.sum_of_errors('zlib'), TimeSeries([3])) self._sequences_almost_equal( self._statistics.sum_of_errors('automaton'), TimeSeries([0]))
def test_signals_if_length_is_not_enough(self): series = TimeSeries([1], dtype=int, frequency=1) self.assertRaises(HookError, self._hook.preprocess, series)
def test_ts_standard_deviation_works_in_univariate_case(self): self.assertAlmostEqual( sut.ts_standard_deviation( TimeSeries([1.5, 2.5, 3.8], dtype=float, frequency=1)), np.std([1.5, 2.5, 3.8]))
def test_preprocessing_made_correctly(self): actual_result = self._hook.preprocess(self._series) expected_result = TimeSeries([1, 1, 0, 1, 1, 2, 3, 3, 3], dtype=int, frequency=1) self.assertEqual(actual_result, expected_result)
def setUp(self) -> None: self._series = TimeSeries([1, 2, 3, 4, 5, 3, 4, 5, 5, 6, 7, 8, 9, 10], dtype=int, frequency=1) self._hook = CombinedHook(BasicSmoothingHook(), LagDifferencingHook(3))
def test_ts_mean_signals_error_on_empty_input(self): self.assertRaises(ValueError, sut.ts_mean, TimeSeries())
def test_works_correctly_if_series_length_greater_than_lag(self): series = TimeSeries([0, 1, 2, 3, 4, 5, 7, 9, 11], dtype=int, frequency=1) expected_series = TimeSeries([5, 6, 7, 8], dtype=int, frequency=1) self.assertEqual(self._hook.preprocess(series), expected_series)
def test_returns_input_series_if_lag_equals_to_series_length(self): series = TimeSeries([0, 1, 2, 3, 4], dtype=int, frequency=1) self.assertEqual(self._hook.preprocess(series), series)
def test_returns_input_series_if_its_size_less_than_lag(self): series = TimeSeries([1, 2, 3], dtype=int, frequency=1) self.assertEqual(self._hook.preprocess(series), series)
def test_ts_mean_works_in_univariate_case(self): self.assertAlmostEqual( sut.ts_mean(TimeSeries([1.5, 2.5, 3.8], dtype=float, frequency=1)), 2.6)