test_diagnostics.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_default_value_check(self):
  69. m = Prophet()
  70. m.fit(self.__df)
  71. # Default value of period should be equal to 0.5 * horizon
  72. df_shf1 = diagnostics.simulated_historical_forecasts(
  73. m, horizon='10 days', k=1)
  74. df_shf2 = diagnostics.simulated_historical_forecasts(
  75. m, horizon='10 days', k=1, period='5 days')
  76. self.assertAlmostEqual(
  77. ((df_shf1['y'] - df_shf2['y']) ** 2).sum(), 0.0)
  78. self.assertAlmostEqual(
  79. ((df_shf1['yhat'] - df_shf2['yhat']) ** 2).sum(), 0.0)
  80. def test_cross_validation(self):
  81. m = Prophet()
  82. m.fit(self.__df)
  83. # Calculate the number of cutoff points(k)
  84. horizon = pd.Timedelta('4 days')
  85. period = pd.Timedelta('10 days')
  86. k = 5
  87. df_cv = diagnostics.cross_validation(
  88. m, horizon='4 days', period='10 days', initial='90 days')
  89. # The unique size of output cutoff should be equal to 'k'
  90. self.assertEqual(len(np.unique(df_cv['cutoff'])), k)
  91. self.assertEqual(max(df_cv['ds'] - df_cv['cutoff']), horizon)
  92. dc = df_cv['cutoff'].diff()
  93. dc = dc[dc > pd.Timedelta(0)].min()
  94. self.assertTrue(dc >= period)
  95. def test_cross_validation_default_value_check(self):
  96. m = Prophet()
  97. m.fit(self.__df)
  98. # Default value of initial should be equal to 3 * horizon
  99. df_cv1 = diagnostics.cross_validation(
  100. m, horizon='32 days', period='10 days')
  101. df_cv2 = diagnostics.cross_validation(
  102. m, horizon='32 days', period='10 days', initial='96 days')
  103. self.assertAlmostEqual(
  104. ((df_cv1['y'] - df_cv2['y']) ** 2).sum(), 0.0)
  105. self.assertAlmostEqual(
  106. ((df_cv1['yhat'] - df_cv2['yhat']) ** 2).sum(), 0.0)