test_prophet.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 os
  12. import numpy as np
  13. import pandas as pd
  14. # fb-block 1 start
  15. from unittest import TestCase
  16. from fbprophet import Prophet
  17. # fb-block 1 end
  18. # fb-block 2
  19. DATA = pd.read_csv(
  20. os.path.join(os.path.dirname(__file__), 'data.csv'),
  21. parse_dates=['ds'],
  22. )
  23. DATA2 = pd.read_csv(
  24. os.path.join(os.path.dirname(__file__), 'data2.csv'),
  25. parse_dates=['ds'],
  26. )
  27. class TestProphet(TestCase):
  28. def test_fit_predict(self):
  29. N = DATA.shape[0]
  30. train = DATA.head(N // 2)
  31. future = DATA.tail(N // 2)
  32. forecaster = Prophet()
  33. forecaster.fit(train)
  34. forecaster.predict(future)
  35. def test_fit_predict_no_seasons(self):
  36. N = DATA.shape[0]
  37. train = DATA.head(N // 2)
  38. future = DATA.tail(N // 2)
  39. forecaster = Prophet(weekly_seasonality=False, yearly_seasonality=False)
  40. forecaster.fit(train)
  41. forecaster.predict(future)
  42. def test_fit_predict_no_changepoints(self):
  43. N = DATA.shape[0]
  44. train = DATA.head(N // 2)
  45. future = DATA.tail(N // 2)
  46. forecaster = Prophet(n_changepoints=0)
  47. forecaster.fit(train)
  48. forecaster.predict(future)
  49. def test_fit_changepoint_not_in_history(self):
  50. train = DATA[(DATA['ds'] < '2013-01-01') | (DATA['ds'] > '2014-01-01')]
  51. train[(train['ds'] > '2014-01-01')] += 20
  52. future = pd.DataFrame({'ds': DATA['ds']})
  53. forecaster = Prophet(changepoints=['2013-06-06'])
  54. forecaster.fit(train)
  55. forecaster.predict(future)
  56. def test_fit_predict_duplicates(self):
  57. N = DATA.shape[0]
  58. train1 = DATA.head(N // 2).copy()
  59. train2 = DATA.head(N // 2).copy()
  60. train2['y'] += 10
  61. train = train1.append(train2)
  62. future = pd.DataFrame({'ds': DATA['ds'].tail(N // 2)})
  63. forecaster = Prophet()
  64. forecaster.fit(train)
  65. forecaster.predict(future)
  66. def test_setup_dataframe(self):
  67. m = Prophet()
  68. N = DATA.shape[0]
  69. history = DATA.head(N // 2).copy()
  70. history = m.setup_dataframe(history, initialize_scales=True)
  71. self.assertTrue('t' in history)
  72. self.assertEqual(history['t'].min(), 0.0)
  73. self.assertEqual(history['t'].max(), 1.0)
  74. self.assertTrue('y_scaled' in history)
  75. self.assertEqual(history['y_scaled'].max(), 1.0)
  76. def test_get_changepoints(self):
  77. m = Prophet()
  78. N = DATA.shape[0]
  79. history = DATA.head(N // 2).copy()
  80. history = m.setup_dataframe(history, initialize_scales=True)
  81. m.history = history
  82. m.set_changepoints()
  83. cp = m.changepoints_t
  84. self.assertEqual(cp.shape[0], m.n_changepoints)
  85. self.assertEqual(len(cp.shape), 1)
  86. self.assertTrue(cp.min() > 0)
  87. self.assertTrue(cp.max() < N)
  88. mat = m.get_changepoint_matrix()
  89. self.assertEqual(mat.shape[0], N // 2)
  90. self.assertEqual(mat.shape[1], m.n_changepoints)
  91. def test_get_zero_changepoints(self):
  92. m = Prophet(n_changepoints=0)
  93. N = DATA.shape[0]
  94. history = DATA.head(N // 2).copy()
  95. history = m.setup_dataframe(history, initialize_scales=True)
  96. m.history = history
  97. m.set_changepoints()
  98. cp = m.changepoints_t
  99. self.assertEqual(cp.shape[0], 1)
  100. self.assertEqual(cp[0], 0)
  101. mat = m.get_changepoint_matrix()
  102. self.assertEqual(mat.shape[0], N // 2)
  103. self.assertEqual(mat.shape[1], 1)
  104. def test_fourier_series_weekly(self):
  105. mat = Prophet.fourier_series(DATA['ds'], 7, 3)
  106. # These are from the R forecast package directly.
  107. true_values = np.array([
  108. 0.7818315, 0.6234898, 0.9749279, -0.2225209, 0.4338837, -0.9009689,
  109. ])
  110. self.assertAlmostEqual(np.sum((mat[0] - true_values)**2), 0.0)
  111. def test_fourier_series_yearly(self):
  112. mat = Prophet.fourier_series(DATA['ds'], 365.25, 3)
  113. # These are from the R forecast package directly.
  114. true_values = np.array([
  115. 0.7006152, -0.7135393, -0.9998330, 0.01827656, 0.7262249, 0.6874572,
  116. ])
  117. self.assertAlmostEqual(np.sum((mat[0] - true_values)**2), 0.0)
  118. def test_growth_init(self):
  119. model = Prophet(growth='logistic')
  120. history = DATA.iloc[:468].copy()
  121. history['cap'] = history['y'].max()
  122. history = model.setup_dataframe(history, initialize_scales=True)
  123. k, m = model.linear_growth_init(history)
  124. self.assertAlmostEqual(k, 0.3055671)
  125. self.assertAlmostEqual(m, 0.5307511)
  126. k, m = model.logistic_growth_init(history)
  127. self.assertAlmostEqual(k, 1.507925, places=4)
  128. self.assertAlmostEqual(m, -0.08167497, places=4)
  129. def test_piecewise_linear(self):
  130. model = Prophet()
  131. t = np.arange(11.)
  132. m = 0
  133. k = 1.0
  134. deltas = np.array([0.5])
  135. changepoint_ts = np.array([5])
  136. y = model.piecewise_linear(t, deltas, k, m, changepoint_ts)
  137. y_true = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0,
  138. 6.5, 8.0, 9.5, 11.0, 12.5])
  139. self.assertEqual((y - y_true).sum(), 0.0)
  140. t = t[8:]
  141. y_true = y_true[8:]
  142. y = model.piecewise_linear(t, deltas, k, m, changepoint_ts)
  143. self.assertEqual((y - y_true).sum(), 0.0)
  144. def test_piecewise_logistic(self):
  145. model = Prophet()
  146. t = np.arange(11.)
  147. cap = np.ones(11) * 10
  148. m = 0
  149. k = 1.0
  150. deltas = np.array([0.5])
  151. changepoint_ts = np.array([5])
  152. y = model.piecewise_logistic(t, cap, deltas, k, m, changepoint_ts)
  153. y_true = np.array([5.000000, 7.310586, 8.807971, 9.525741, 9.820138,
  154. 9.933071, 9.984988, 9.996646, 9.999252, 9.999833,
  155. 9.999963])
  156. self.assertAlmostEqual((y - y_true).sum(), 0.0, places=5)
  157. t = t[8:]
  158. y_true = y_true[8:]
  159. cap = cap[8:]
  160. y = model.piecewise_logistic(t, cap, deltas, k, m, changepoint_ts)
  161. self.assertAlmostEqual((y - y_true).sum(), 0.0, places=5)
  162. def test_holidays(self):
  163. holidays = pd.DataFrame({
  164. 'ds': pd.to_datetime(['2016-12-25']),
  165. 'holiday': ['xmas'],
  166. 'lower_window': [-1],
  167. 'upper_window': [0],
  168. })
  169. model = Prophet(holidays=holidays)
  170. df = pd.DataFrame({
  171. 'ds': pd.date_range('2016-12-20', '2016-12-31')
  172. })
  173. feats = model.make_holiday_features(df['ds'])
  174. # 11 columns generated even though only 8 overlap
  175. self.assertEqual(feats.shape, (df.shape[0], 2))
  176. self.assertEqual((feats.sum(0) - np.array([1.0, 1.0])).sum(), 0)
  177. holidays = pd.DataFrame({
  178. 'ds': pd.to_datetime(['2016-12-25']),
  179. 'holiday': ['xmas'],
  180. 'lower_window': [-1],
  181. 'upper_window': [10],
  182. })
  183. feats = Prophet(holidays=holidays).make_holiday_features(df['ds'])
  184. # 12 columns generated even though only 8 overlap
  185. self.assertEqual(feats.shape, (df.shape[0], 12))
  186. def test_fit_with_holidays(self):
  187. holidays = pd.DataFrame({
  188. 'ds': pd.to_datetime(['2012-06-06', '2013-06-06']),
  189. 'holiday': ['seans-bday'] * 2,
  190. 'lower_window': [0] * 2,
  191. 'upper_window': [1] * 2,
  192. })
  193. model = Prophet(holidays=holidays, uncertainty_samples=0)
  194. model.fit(DATA).predict()
  195. def test_make_future_dataframe(self):
  196. N = 468
  197. train = DATA.head(N // 2)
  198. forecaster = Prophet()
  199. forecaster.fit(train)
  200. future = forecaster.make_future_dataframe(periods=3, freq='D',
  201. include_history=False)
  202. correct = pd.DatetimeIndex(['2013-04-26', '2013-04-27', '2013-04-28'])
  203. self.assertEqual(len(future), 3)
  204. for i in range(3):
  205. self.assertEqual(future.iloc[i]['ds'], correct[i])
  206. future = forecaster.make_future_dataframe(periods=3, freq='M',
  207. include_history=False)
  208. correct = pd.DatetimeIndex(['2013-04-30', '2013-05-31', '2013-06-30'])
  209. self.assertEqual(len(future), 3)
  210. for i in range(3):
  211. self.assertEqual(future.iloc[i]['ds'], correct[i])
  212. def test_auto_weekly_seasonality(self):
  213. # Should be enabled
  214. N = 15
  215. train = DATA.head(N)
  216. m = Prophet()
  217. self.assertEqual(m.weekly_seasonality, 'auto')
  218. m.fit(train)
  219. self.assertIn('weekly', m.seasonalities)
  220. self.assertEqual(m.seasonalities['weekly'], (7, 3))
  221. # Should be disabled due to too short history
  222. N = 9
  223. train = DATA.head(N)
  224. m = Prophet()
  225. m.fit(train)
  226. self.assertNotIn('weekly', m.seasonalities)
  227. m = Prophet(weekly_seasonality=True)
  228. m.fit(train)
  229. self.assertIn('weekly', m.seasonalities)
  230. # Should be False due to weekly spacing
  231. train = DATA.iloc[::7, :]
  232. m = Prophet()
  233. m.fit(train)
  234. self.assertNotIn('weekly', m.seasonalities)
  235. m = Prophet(weekly_seasonality=2)
  236. m.fit(DATA)
  237. self.assertEqual(m.seasonalities['weekly'], (7, 2))
  238. def test_auto_yearly_seasonality(self):
  239. # Should be enabled
  240. m = Prophet()
  241. self.assertEqual(m.yearly_seasonality, 'auto')
  242. m.fit(DATA)
  243. self.assertIn('yearly', m.seasonalities)
  244. self.assertEqual(m.seasonalities['yearly'], (365.25, 10))
  245. # Should be disabled due to too short history
  246. N = 240
  247. train = DATA.head(N)
  248. m = Prophet()
  249. m.fit(train)
  250. self.assertNotIn('yearly', m.seasonalities)
  251. m = Prophet(yearly_seasonality=True)
  252. m.fit(train)
  253. self.assertIn('yearly', m.seasonalities)
  254. m = Prophet(yearly_seasonality=7)
  255. m.fit(DATA)
  256. self.assertEqual(m.seasonalities['yearly'], (365.25, 7))
  257. def test_auto_daily_seasonality(self):
  258. # Should be enabled
  259. m = Prophet()
  260. self.assertEqual(m.yearly_seasonality, 'auto')
  261. m.fit(DATA2)
  262. self.assertIn('daily', m.seasonalities)
  263. self.assertEqual(m.seasonalities['daily'], (1, 4))
  264. # Should be disabled due to too short history
  265. N = 430
  266. train = DATA2.head(N)
  267. m = Prophet()
  268. m.fit(train)
  269. self.assertNotIn('daily', m.seasonalities)
  270. m = Prophet(daily_seasonality=True)
  271. m.fit(train)
  272. self.assertIn('daily', m.seasonalities)
  273. m = Prophet(daily_seasonality=7)
  274. m.fit(DATA2)
  275. self.assertEqual(m.seasonalities['daily'], (1, 7))
  276. m = Prophet()
  277. m.fit(DATA)
  278. self.assertNotIn('daily', m.seasonalities)
  279. def test_subdaily_holidays(self):
  280. holidays = pd.DataFrame({
  281. 'ds': pd.to_datetime(['2017-01-02']),
  282. 'holiday': ['new_years'],
  283. })
  284. m = Prophet(holidays=holidays)
  285. m.fit(DATA2)
  286. fcst = m.predict()
  287. self.assertEqual(sum(fcst['new_years'] == 0), 575)
  288. def test_custom_seasonality(self):
  289. m = Prophet()
  290. m.add_seasonality(name='monthly', period=30, fourier_order=5)
  291. self.assertEqual(m.seasonalities['monthly'], (30, 5))