|
| 1 | +# Copyright 2021 The TensorFlow Probability Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================ |
| 15 | +"""Tests for holiday_effects.""" |
| 16 | +from absl.testing import parameterized |
| 17 | +import pandas as pd |
| 18 | +import tensorflow as tf |
| 19 | +from tensorflow_probability.python.internal import test_util |
| 20 | +from tensorflow_probability.python.sts import holiday_effects |
| 21 | + |
| 22 | +HOLIDAY_FILE_FIELDS = ['geo', 'holiday', 'date'] |
| 23 | + |
| 24 | + |
| 25 | +class HolidayEffectsTest(test_util.TestCase): |
| 26 | + |
| 27 | + @parameterized.named_parameters( |
| 28 | + ('date_wrong_order', |
| 29 | + pd.DataFrame([['US', 'TestHoliday', '12-20-2013']], |
| 30 | + columns=HOLIDAY_FILE_FIELDS)), |
| 31 | + ('date_invalid', |
| 32 | + pd.DataFrame([['US', 'TestHoliday', '12-00-2013']], |
| 33 | + columns=HOLIDAY_FILE_FIELDS)), |
| 34 | + ('bad_column_names', |
| 35 | + pd.DataFrame([['US', 'TestHoliday', '2013-12-20']], |
| 36 | + columns=['geo', 'wrong', 'date']))) |
| 37 | + def test_holidays_raise_error(self, holidays): |
| 38 | + times = pd.date_range( |
| 39 | + start='2013-12-20', end='2015-12-20', freq=pd.DateOffset(years=1)) |
| 40 | + with self.assertRaises(ValueError): |
| 41 | + holiday_effects.create_holiday_regressors(times, holidays) |
| 42 | + |
| 43 | + @parameterized.named_parameters( |
| 44 | + ('data_wrong_format', pd.Series(['2013-12-20'])), |
| 45 | + ('data_no_frequency', pd.DatetimeIndex(['2013-12-20']))) |
| 46 | + def test_times_raise_error(self, times): |
| 47 | + holidays = pd.DataFrame([['US', 'TestHoliday', '2013-12-20']], |
| 48 | + columns=HOLIDAY_FILE_FIELDS) |
| 49 | + with self.assertRaises(ValueError): |
| 50 | + holiday_effects.create_holiday_regressors(times, holidays) |
| 51 | + |
| 52 | + @parameterized.named_parameters( |
| 53 | + ('holiday_daily', pd.DateOffset(days=1), '2012-01-01', '2012-12-31', |
| 54 | + [0] * 359 + [1] + [0] * 6), |
| 55 | + ('holiday_hourly', pd.DateOffset(hours=1), '2012-01-01', |
| 56 | + '2012-12-31 23:00:00', [0] * 359 * 24 + [1] * 24 + [0] * 6 * 24), |
| 57 | + # Note that expected should be `[0] * 51 + [1] + [0]` if |
| 58 | + # _match_dates supports rounding timestamps to the nearest prior day |
| 59 | + ('holiday_weekly', pd.DateOffset(weeks=1), '2012-01-01', '2012-12-31', |
| 60 | + [0] * 51 + [0] + [0])) |
| 61 | + def test_match_dates_by_frequency(self, freq, start, end, expected): |
| 62 | + holiday_dates = pd.to_datetime(['2012-12-25']) |
| 63 | + index = pd.date_range(start, end, freq=freq) |
| 64 | + matched_dates = holiday_effects._match_dates(index, holiday_dates) |
| 65 | + self.assertEqual(matched_dates, expected) |
| 66 | + |
| 67 | + @parameterized.named_parameters( |
| 68 | + ('holiday_disjoint', '2011-01-01', '2011-12-31', [0] * 365), |
| 69 | + ('holiday_intersection', '2011-02-01', '2012-01-31', |
| 70 | + [0] * 334 + [1] * 31), |
| 71 | + ('holiday_subset', '2012-01-01', '2012-01-31', [1] * 31)) |
| 72 | + def test_match_dates_by_overlap(self, start, end, expected): |
| 73 | + holiday_dates = pd.date_range( |
| 74 | + '2012-01-01', '2012-12-31', freq=pd.DateOffset(days=1)) |
| 75 | + index = pd.date_range(start, end, freq=pd.DateOffset(days=1)) |
| 76 | + matched_dates = holiday_effects._match_dates(index, holiday_dates) |
| 77 | + self.assertEqual(matched_dates, expected) |
| 78 | + |
| 79 | + @parameterized.named_parameters( |
| 80 | + ('diagonal_pattern', [('H1', 0), ('H2', 1)], [[1, 0], [0, 1]]), |
| 81 | + ('row_pattern', [('H1', 0), ('H2', 0)], [[1, 1], [0, 0]]), |
| 82 | + ('column_pattern', [('H1', 0), ('H1', 1)], [[1], [1]])) |
| 83 | + def test_create_holiday_regressors(self, holiday_patterns, expected): |
| 84 | + times = pd.date_range( |
| 85 | + '2011-01-01', '2012-01-01', freq=pd.DateOffset(years=1)) |
| 86 | + holidays_list = [] |
| 87 | + for name, date_index in holiday_patterns: |
| 88 | + holidays_list.append(['US', name, times[date_index]]) |
| 89 | + holidays = pd.DataFrame(holidays_list, columns=HOLIDAY_FILE_FIELDS) |
| 90 | + holiday_regressors = holiday_effects.create_holiday_regressors( |
| 91 | + times, holidays) |
| 92 | + self.assertEqual(holiday_regressors.values.tolist(), expected) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + tf.test.main() |
0 commit comments