test_diagnostics.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the BSD-style license found in the
  5. # LICENSE file in the root directory of this source tree. An additional grant
  6. # of patent rights can be found in the PATENTS file in the same directory.
  7. from __future__ import absolute_import
  8. from __future__ import division
  9. from __future__ import print_function
  10. from __future__ import unicode_literals
  11. import numpy as np
  12. import pandas as pd
  13. # fb-block 1 start
  14. import os
  15. from unittest import TestCase
  16. from fbprophet import Prophet
  17. from fbprophet import diagnostics
  18. DATA = pd.read_csv(
  19. os.path.join(os.path.dirname(__file__), 'data.csv'), parse_dates=['ds']
  20. ).head(100)
  21. # fb-block 1 end
  22. # fb-block 2
  23. class TestDiagnostics(TestCase):
  24. def __init__(self, *args, **kwargs):
  25. super(TestDiagnostics, self).__init__(*args, **kwargs)
  26. # Use first 100 record in data.csv
  27. self.__df = DATA
  28. def test_simulated_historical_forecasts(self):
  29. m = Prophet()
  30. m.fit(self.__df)
  31. k = 2
  32. for p in [1, 10]:
  33. for h in [1, 3]:
  34. period = '{} days'.format(p)
  35. horizon = '{} days'.format(h)
  36. df_shf = diagnostics.simulated_historical_forecasts(
  37. m, horizon=horizon, k=k, period=period)
  38. # All cutoff dates should be less than ds dates
  39. self.assertTrue((df_shf['cutoff'] < df_shf['ds']).all())
  40. # The unique size of output cutoff should be equal to 'k'
  41. self.assertEqual(len(np.unique(df_shf['cutoff'])), k)
  42. self.assertEqual(
  43. max(df_shf['ds'] - df_shf['cutoff']),
  44. pd.Timedelta(horizon),
  45. )
  46. dc = df_shf['cutoff'].diff()
  47. dc = dc[dc > pd.Timedelta(0)].min()
  48. self.assertTrue(dc >= pd.Timedelta(period))
  49. # Each y in df_shf and self.__df with same ds should be equal
  50. df_merged = pd.merge(df_shf, self.__df, 'left', on='ds')
  51. self.assertAlmostEqual(
  52. np.sum((df_merged['y_x'] - df_merged['y_y']) ** 2), 0.0)
  53. def test_simulated_historical_forecasts_logistic(self):
  54. m = Prophet(growth='logistic')
  55. df = self.__df.copy()
  56. df['cap'] = 40
  57. m.fit(df)
  58. df_shf = diagnostics.simulated_historical_forecasts(
  59. m, horizon='3 days', k=2, period='3 days')
  60. # All cutoff dates should be less than ds dates
  61. self.assertTrue((df_shf['cutoff'] < df_shf['ds']).all())
  62. # The unique size of output cutoff should be equal to 'k'
  63. self.assertEqual(len(np.unique(df_shf['cutoff'])), 2)
  64. # Each y in df_shf and self.__df with same ds should be equal
  65. df_merged = pd.merge(df_shf, df, 'left', on='ds')
  66. self.assertAlmostEqual(
  67. np.sum((df_merged['y_x'] - df_merged['y_y']) ** 2), 0.0)
  68. def test_simulated_historical_forecasts_extra_regressors(self):
  69. m = Prophet()
  70. m.add_seasonality(name='monthly', period=30.5, fourier_order=5)
  71. m.add_regressor('extra')
  72. df = self.__df.copy()
  73. df['cap'] = 40
  74. df['extra'] = range(df.shape[0])
  75. m.fit(df)
  76. df_shf = diagnostics.simulated_historical_forecasts(
  77. m, horizon='3 days', k=2, period='3 days')
  78. # All cutoff dates should be less than ds dates
  79. self.assertTrue((df_shf['cutoff'] < df_shf['ds']).all())
  80. # The unique size of output cutoff should be equal to 'k'
  81. self.assertEqual(len(np.unique(df_shf['cutoff'])), 2)
  82. # Each y in df_shf and self.__df with same ds should be equal
  83. df_merged = pd.merge(df_shf, df, 'left', on='ds')
  84. self.assertAlmostEqual(
  85. np.sum((df_merged['y_x'] - df_merged['y_y']) ** 2), 0.0)
  86. def test_simulated_historical_forecasts_default_value_check(self):
  87. m = Prophet()
  88. m.fit(self.__df)
  89. # Default value of period should be equal to 0.5 * horizon
  90. df_shf1 = diagnostics.simulated_historical_forecasts(
  91. m, horizon='10 days', k=1)
  92. df_shf2 = diagnostics.simulated_historical_forecasts(
  93. m, horizon='10 days', k=1, period='5 days')
  94. self.assertAlmostEqual(
  95. ((df_shf1['y'] - df_shf2['y']) ** 2).sum(), 0.0)
  96. self.assertAlmostEqual(
  97. ((df_shf1['yhat'] - df_shf2['yhat']) ** 2).sum(), 0.0)
  98. def test_cross_validation(self):
  99. m = Prophet()
  100. m.fit(self.__df)
  101. # Calculate the number of cutoff points(k)
  102. horizon = pd.Timedelta('4 days')
  103. period = pd.Timedelta('10 days')
  104. k = 5
  105. df_cv = diagnostics.cross_validation(
  106. m, horizon='4 days', period='10 days', initial='90 days')
  107. # The unique size of output cutoff should be equal to 'k'
  108. self.assertEqual(len(np.unique(df_cv['cutoff'])), k)
  109. self.assertEqual(max(df_cv['ds'] - df_cv['cutoff']), horizon)
  110. dc = df_cv['cutoff'].diff()
  111. dc = dc[dc > pd.Timedelta(0)].min()
  112. self.assertTrue(dc >= period)
  113. def test_cross_validation_default_value_check(self):
  114. m = Prophet()
  115. m.fit(self.__df)
  116. # Default value of initial should be equal to 3 * horizon
  117. df_cv1 = diagnostics.cross_validation(
  118. m, horizon='32 days', period='10 days')
  119. df_cv2 = diagnostics.cross_validation(
  120. m, horizon='32 days', period='10 days', initial='96 days')
  121. self.assertAlmostEqual(
  122. ((df_cv1['y'] - df_cv2['y']) ** 2).sum(), 0.0)
  123. self.assertAlmostEqual(
  124. ((df_cv1['yhat'] - df_cv2['yhat']) ** 2).sum(), 0.0)