test_diagnostics.py 4.5 KB

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