forecaster.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. from collections import defaultdict
  12. from datetime import timedelta
  13. import pickle
  14. from matplotlib import pyplot as plt
  15. from matplotlib.dates import DateFormatter, MonthLocator, num2date
  16. from matplotlib.ticker import FuncFormatter
  17. import numpy as np
  18. import pandas as pd
  19. # fb-block 1 start
  20. import pkg_resources
  21. # fb-block 1 end
  22. try:
  23. import pystan
  24. except ImportError:
  25. print('You cannot run prophet without pystan installed')
  26. raise
  27. # fb-block 2
  28. class Prophet(object):
  29. def __init__(
  30. self,
  31. growth='linear',
  32. changepoints=None,
  33. n_changepoints=25,
  34. yearly_seasonality=True,
  35. weekly_seasonality=True,
  36. holidays=None,
  37. seasonality_prior_scale=10.0,
  38. holidays_prior_scale=10.0,
  39. changepoint_prior_scale=0.05,
  40. mcmc_samples=0,
  41. interval_width=0.80,
  42. uncertainty_samples=1000,
  43. ):
  44. self.growth = growth
  45. self.changepoints = pd.to_datetime(changepoints)
  46. if self.changepoints is not None:
  47. self.n_changepoints = len(self.changepoints)
  48. else:
  49. self.n_changepoints = n_changepoints
  50. self.yearly_seasonality = yearly_seasonality
  51. self.weekly_seasonality = weekly_seasonality
  52. if holidays is not None:
  53. if not (
  54. isinstance(holidays, pd.DataFrame)
  55. and 'ds' in holidays
  56. and 'holiday' in holidays
  57. ):
  58. raise ValueError("holidays must be a DataFrame with 'ds' and "
  59. "'holiday' columns.")
  60. holidays['ds'] = pd.to_datetime(holidays['ds'])
  61. self.holidays = holidays
  62. self.seasonality_prior_scale = float(seasonality_prior_scale)
  63. self.changepoint_prior_scale = float(changepoint_prior_scale)
  64. self.holidays_prior_scale = float(holidays_prior_scale)
  65. self.mcmc_samples = mcmc_samples
  66. self.interval_width = interval_width
  67. self.uncertainty_samples = uncertainty_samples
  68. # Set during fitting
  69. self.start = None
  70. self.y_scale = None
  71. self.t_scale = None
  72. self.changepoints_t = None
  73. self.stan_fit = None
  74. self.params = {}
  75. self.history = None
  76. self.history_dates = None
  77. self.validate_inputs()
  78. def validate_inputs(self):
  79. if self.growth not in ('linear', 'logistic'):
  80. raise ValueError(
  81. "Parameter 'growth' should be 'linear' or 'logistic'.")
  82. if self.holidays is not None:
  83. has_lower = 'lower_window' in self.holidays
  84. has_upper = 'upper_window' in self.holidays
  85. if has_lower + has_upper == 1:
  86. raise ValueError('Holidays must have both lower_window and ' +
  87. 'upper_window, or neither')
  88. if has_lower:
  89. if max(self.holidays['lower_window']) > 0:
  90. raise ValueError('Holiday lower_window should be <= 0')
  91. if min(self.holidays['upper_window']) < 0:
  92. raise ValueError('Holiday upper_window should be >= 0')
  93. for h in self.holidays['holiday'].unique():
  94. if '_delim_' in h:
  95. raise ValueError('Holiday name cannot contain "_delim_"')
  96. if h in ['zeros', 'yearly', 'weekly', 'yhat', 'seasonal',
  97. 'trend']:
  98. raise ValueError('Holiday name {} reserved.'.format(h))
  99. @classmethod
  100. def get_linear_model(cls):
  101. # fb-block 3
  102. # fb-block 4 start
  103. model_file = pkg_resources.resource_filename(
  104. 'fbprophet',
  105. 'stan_models/linear_growth.pkl'
  106. )
  107. # fb-block 4 end
  108. with open(model_file, 'rb') as f:
  109. return pickle.load(f)
  110. @classmethod
  111. def get_logistic_model(cls):
  112. # fb-block 5
  113. # fb-block 6 start
  114. model_file = pkg_resources.resource_filename(
  115. 'fbprophet',
  116. 'stan_models/logistic_growth.pkl'
  117. )
  118. # fb-block 6 end
  119. with open(model_file, 'rb') as f:
  120. return pickle.load(f)
  121. def setup_dataframe(self, df, initialize_scales=False):
  122. """Create auxillary columns 't', 't_ix', 'y_scaled', and 'cap_scaled'.
  123. These columns are used during both fitting and prediction.
  124. """
  125. if 'y' in df:
  126. df['y'] = pd.to_numeric(df['y'])
  127. df['ds'] = pd.to_datetime(df['ds'])
  128. df = df.sort_values('ds')
  129. df.reset_index(inplace=True, drop=True)
  130. if initialize_scales:
  131. self.y_scale = df['y'].max()
  132. self.start = df['ds'].min()
  133. self.t_scale = df['ds'].max() - self.start
  134. df['t'] = (df['ds'] - self.start) / self.t_scale
  135. if 'y' in df:
  136. df['y_scaled'] = df['y'] / self.y_scale
  137. if self.growth == 'logistic':
  138. assert 'cap' in df
  139. df['cap_scaled'] = df['cap'] / self.y_scale
  140. return df
  141. def set_changepoints(self):
  142. """Generate a list of changepoints.
  143. Either:
  144. 1) the changepoints were passed in explicitly
  145. A) they are empty
  146. B) not empty, needs validation
  147. 2) we are generating a grid of them
  148. 3) the user prefers no changepoints to be used
  149. """
  150. if self.changepoints is not None:
  151. if len(self.changepoints) == 0:
  152. pass
  153. else:
  154. too_low = min(self.changepoints) < self.history['ds'].min()
  155. too_high = max(self.changepoints) > self.history['ds'].max()
  156. if too_low or too_high:
  157. raise ValueError('Changepoints must fall within training data.')
  158. elif self.n_changepoints > 0:
  159. # Place potential changepoints evenly throuh first 80% of history
  160. max_ix = np.floor(self.history.shape[0] * 0.8)
  161. cp_indexes = (
  162. np.linspace(0, max_ix, self.n_changepoints + 1)
  163. .round()
  164. .astype(np.int)
  165. )
  166. self.changepoints = self.history.ix[cp_indexes]['ds'].tail(-1)
  167. else:
  168. # set empty changepoints
  169. self.changepoints = []
  170. if len(self.changepoints) > 0:
  171. self.changepoints_t = np.sort(np.array(
  172. (self.changepoints - self.start) / self.t_scale))
  173. else:
  174. self.changepoints_t = np.array([0]) # dummy changepoint
  175. def get_changepoint_matrix(self):
  176. A = np.zeros((self.history.shape[0], len(self.changepoints_t)))
  177. for i, t_i in enumerate(self.changepoints_t):
  178. A[self.history['t'].values >= t_i, i] = 1
  179. return A
  180. @staticmethod
  181. def fourier_series(dates, period, series_order):
  182. """Generate a Fourier expansion for a fixed frequency and order.
  183. Parameters
  184. ----------
  185. dates: a pd.Series containing timestamps
  186. period: an integer frequency (number of days)
  187. series_order: number of components to generate
  188. Returns
  189. -------
  190. a 2-dimensional np.array with one row per row in `dt`
  191. """
  192. # convert to days since epoch
  193. t = np.array(
  194. (dates - pd.datetime(1970, 1, 1))
  195. .dt.days
  196. .astype(np.float)
  197. )
  198. return np.column_stack([
  199. fun((2.0 * (i + 1) * np.pi * t / period))
  200. for i in range(series_order)
  201. for fun in (np.sin, np.cos)
  202. ])
  203. @classmethod
  204. def make_seasonality_features(cls, dates, period, series_order, prefix):
  205. features = cls.fourier_series(dates, period, series_order)
  206. columns = [
  207. '{}_delim_{}'.format(prefix, i + 1)
  208. for i in range(features.shape[1])
  209. ]
  210. return pd.DataFrame(features, columns=columns)
  211. def make_holiday_features(self, dates):
  212. """Generate a DataFrame with each column corresponding to a holiday.
  213. """
  214. # A smaller prior scale will shrink holiday estimates more
  215. scale_ratio = self.holidays_prior_scale / self.seasonality_prior_scale
  216. # Holds columns of our future matrix.
  217. expanded_holidays = defaultdict(lambda: np.zeros(dates.shape[0]))
  218. # Makes an index so we can perform `get_loc` below.
  219. row_index = pd.DatetimeIndex(dates)
  220. for ix, row in self.holidays.iterrows():
  221. dt = row.ds.date()
  222. try:
  223. lw = int(row.get('lower_window', 0))
  224. uw = int(row.get('upper_window', 0))
  225. except ValueError:
  226. lw = 0
  227. uw = 0
  228. for offset in range(lw, uw + 1):
  229. occurrence = dt + timedelta(days=offset)
  230. try:
  231. loc = row_index.get_loc(occurrence)
  232. except KeyError:
  233. loc = None
  234. key = '{}_delim_{}{}'.format(
  235. row.holiday,
  236. '+' if offset >= 0 else '-',
  237. abs(offset)
  238. )
  239. if loc is not None:
  240. expanded_holidays[key][loc] = scale_ratio
  241. else:
  242. # Access key to generate value
  243. expanded_holidays[key]
  244. # This relies pretty importantly on pandas keeping the columns in order.
  245. return pd.DataFrame(expanded_holidays)
  246. def make_all_seasonality_features(self, df):
  247. seasonal_features = [
  248. # Add a column of zeros in case no seasonality is used.
  249. pd.DataFrame({'zeros': np.zeros(df.shape[0])})
  250. ]
  251. # Seasonality features
  252. if self.yearly_seasonality:
  253. seasonal_features.append(self.make_seasonality_features(
  254. df['ds'],
  255. 365.25,
  256. 10,
  257. 'yearly',
  258. ))
  259. if self.weekly_seasonality:
  260. seasonal_features.append(self.make_seasonality_features(
  261. df['ds'],
  262. 7,
  263. 3,
  264. 'weekly',
  265. ))
  266. if self.holidays is not None:
  267. seasonal_features.append(self.make_holiday_features(df['ds']))
  268. return pd.concat(seasonal_features, axis=1)
  269. @staticmethod
  270. def linear_growth_init(df):
  271. i0, i1 = df['ds'].idxmin(), df['ds'].idxmax()
  272. T = df['t'].ix[i1] - df['t'].ix[i0]
  273. k = (df['y_scaled'].ix[i1] - df['y_scaled'].ix[i0]) / T
  274. m = df['y_scaled'].ix[i0] - k * df['t'].ix[i0]
  275. return (k, m)
  276. @staticmethod
  277. def logistic_growth_init(df):
  278. i0, i1 = df['ds'].idxmin(), df['ds'].idxmax()
  279. T = df['t'].ix[i1] - df['t'].ix[i0]
  280. # Force valid values, in case y > cap.
  281. r0 = max(1.01, df['cap_scaled'].ix[i0] / df['y_scaled'].ix[i0])
  282. r1 = max(1.01, df['cap_scaled'].ix[i1] / df['y_scaled'].ix[i1])
  283. if abs(r0 - r1) <= 0.01:
  284. r0 = 1.05 * r0
  285. L0 = np.log(r0 - 1)
  286. L1 = np.log(r1 - 1)
  287. # Initialize the offset
  288. m = L0 * T / (L0 - L1)
  289. # And the rate
  290. k = L0 / m
  291. return (k, m)
  292. # fb-block 7
  293. def fit(self, df, **kwargs):
  294. """Fit the Prophet model to data.
  295. Parameters
  296. ----------
  297. df: pd.DataFrame containing history. Must have columns 'ds', 'y', and
  298. if logistic growth, 'cap'.
  299. kwargs: Additional arguments passed to Stan's sampling or optimizing
  300. function, as appropriate.
  301. Returns
  302. -------
  303. The fitted Prophet object.
  304. """
  305. history = df[df['y'].notnull()].copy()
  306. self.history_dates = pd.to_datetime(df['ds']).sort_values()
  307. history = self.setup_dataframe(history, initialize_scales=True)
  308. self.history = history
  309. seasonal_features = self.make_all_seasonality_features(history)
  310. self.set_changepoints()
  311. A = self.get_changepoint_matrix()
  312. dat = {
  313. 'T': history.shape[0],
  314. 'K': seasonal_features.shape[1],
  315. 'S': len(self.changepoints_t),
  316. 'y': history['y_scaled'],
  317. 't': history['t'],
  318. 'A': A,
  319. 't_change': self.changepoints_t,
  320. 'X': seasonal_features,
  321. 'sigma': self.seasonality_prior_scale,
  322. 'tau': self.changepoint_prior_scale,
  323. }
  324. if self.growth == 'linear':
  325. kinit = self.linear_growth_init(history)
  326. model = self.get_linear_model()
  327. else:
  328. dat['cap'] = history['cap_scaled']
  329. kinit = self.logistic_growth_init(history)
  330. model = self.get_logistic_model()
  331. def stan_init():
  332. return {
  333. 'k': kinit[0],
  334. 'm': kinit[1],
  335. 'delta': np.zeros(len(self.changepoints_t)),
  336. 'beta': np.zeros(seasonal_features.shape[1]),
  337. 'sigma_obs': 1,
  338. }
  339. if self.mcmc_samples > 0:
  340. stan_fit = model.sampling(
  341. dat,
  342. init=stan_init,
  343. iter=self.mcmc_samples,
  344. **kwargs
  345. )
  346. for par in stan_fit.model_pars:
  347. self.params[par] = stan_fit[par]
  348. else:
  349. params = model.optimizing(dat, init=stan_init, iter=1e4, **kwargs)
  350. for par in params:
  351. self.params[par] = params[par].reshape((1, -1))
  352. # If no changepoints were requested, replace delta with 0s
  353. if len(self.changepoints) == 0:
  354. # Fold delta into the base rate k
  355. params['k'] = params['k'] + params['delta']
  356. params['delta'] = np.zeros(params['delta'].shape)
  357. return self
  358. # fb-block 8
  359. def predict(self, df=None):
  360. """Predict historical and future values for y.
  361. Note: you must only pass in future dates here.
  362. Historical dates are prepended before predictions are made.
  363. `df` can be None, in which case we predict only on history.
  364. """
  365. if df is None:
  366. df = self.history.copy()
  367. else:
  368. df = self.setup_dataframe(df)
  369. df['trend'] = self.predict_trend(df)
  370. seasonal_components = self.predict_seasonal_components(df)
  371. intervals = self.predict_uncertainty(df)
  372. df2 = pd.concat((df, intervals, seasonal_components), axis=1)
  373. df2['yhat'] = df2['trend'] + df2['seasonal']
  374. return df2
  375. @staticmethod
  376. def piecewise_linear(t, deltas, k, m, changepoint_ts):
  377. # Intercept changes
  378. gammas = -changepoint_ts * deltas
  379. # Get cumulative slope and intercept at each t
  380. k_t = k * np.ones_like(t)
  381. m_t = m * np.ones_like(t)
  382. for s, t_s in enumerate(changepoint_ts):
  383. indx = t >= t_s
  384. k_t[indx] += deltas[s]
  385. m_t[indx] += gammas[s]
  386. return k_t * t + m_t
  387. @staticmethod
  388. def piecewise_logistic(t, cap, deltas, k, m, changepoint_ts):
  389. # Compute offset changes
  390. k_cum = np.concatenate((np.atleast_1d(k), np.cumsum(deltas) + k))
  391. gammas = np.zeros(len(changepoint_ts))
  392. for i, t_s in enumerate(changepoint_ts):
  393. gammas[i] = (
  394. (t_s - m - np.sum(gammas))
  395. * (1 - k_cum[i] / k_cum[i + 1])
  396. )
  397. # Get cumulative rate and offset at each t
  398. k_t = k * np.ones_like(t)
  399. m_t = m * np.ones_like(t)
  400. for s, t_s in enumerate(changepoint_ts):
  401. indx = t >= t_s
  402. k_t[indx] += deltas[s]
  403. m_t[indx] += gammas[s]
  404. return cap / (1 + np.exp(-k_t * (t - m_t)))
  405. def predict_trend(self, df):
  406. k = np.nanmean(self.params['k'])
  407. m = np.nanmean(self.params['m'])
  408. deltas = np.nanmean(self.params['delta'], axis=0)
  409. t = np.array(df['t'])
  410. if self.growth == 'linear':
  411. trend = self.piecewise_linear(t, deltas, k, m, self.changepoints_t)
  412. else:
  413. cap = df['cap_scaled']
  414. trend = self.piecewise_logistic(
  415. t, cap, deltas, k, m, self.changepoints_t)
  416. return trend * self.y_scale
  417. def predict_seasonal_components(self, df):
  418. seasonal_features = self.make_all_seasonality_features(df)
  419. lower_p = 100 * (1.0 - self.interval_width) / 2
  420. upper_p = 100 * (1.0 + self.interval_width) / 2
  421. components = pd.DataFrame({
  422. 'col': np.arange(seasonal_features.shape[1]),
  423. 'component': [x.split('_delim_')[0] for x in seasonal_features.columns],
  424. })
  425. # Remove the placeholder
  426. components = components[components['component'] != 'zeros']
  427. if components.shape[0] > 0:
  428. X = seasonal_features.as_matrix()
  429. data = {}
  430. for component, features in components.groupby('component'):
  431. cols = features.col.tolist()
  432. comp_beta = self.params['beta'][:, cols]
  433. comp_features = X[:, cols]
  434. comp = (
  435. np.matmul(comp_features, comp_beta.transpose())
  436. * self.y_scale
  437. )
  438. data[component] = np.nanmean(comp, axis=1)
  439. data[component + '_lower'] = np.nanpercentile(comp, lower_p,
  440. axis=1)
  441. data[component + '_upper'] = np.nanpercentile(comp, upper_p,
  442. axis=1)
  443. component_predictions = pd.DataFrame(data)
  444. component_predictions['seasonal'] = (
  445. component_predictions[components['component'].unique()].sum(1))
  446. else:
  447. component_predictions = pd.DataFrame(
  448. {'seasonal': np.zeros(df.shape[0])})
  449. return component_predictions
  450. def predict_uncertainty(self, df):
  451. n_iterations = self.params['k'].shape[0]
  452. samp_per_iter = max(1, int(np.ceil(
  453. self.uncertainty_samples / float(n_iterations)
  454. )))
  455. # Generate seasonality features once so we can re-use them.
  456. seasonal_features = self.make_all_seasonality_features(df)
  457. sim_values = {'yhat': [], 'trend': [], 'seasonal': []}
  458. for i in range(n_iterations):
  459. for j in range(samp_per_iter):
  460. sim = self.sample_model(df, seasonal_features, i)
  461. for key in sim_values:
  462. sim_values[key].append(sim[key])
  463. lower_p = 100 * (1.0 - self.interval_width) / 2
  464. upper_p = 100 * (1.0 + self.interval_width) / 2
  465. series = {}
  466. for key, value in sim_values.items():
  467. mat = np.column_stack(value)
  468. series['{}_lower'.format(key)] = np.nanpercentile(mat, lower_p,
  469. axis=1)
  470. series['{}_upper'.format(key)] = np.nanpercentile(mat, upper_p,
  471. axis=1)
  472. return pd.DataFrame(series)
  473. def sample_model(self, df, seasonal_features, iteration):
  474. trend = self.sample_predictive_trend(df, iteration)
  475. beta = self.params['beta'][iteration]
  476. seasonal = np.matmul(seasonal_features.as_matrix(), beta) * self.y_scale
  477. sigma = self.params['sigma_obs'][iteration]
  478. noise = np.random.normal(0, sigma, df.shape[0]) * self.y_scale
  479. return pd.DataFrame({
  480. 'yhat': trend + seasonal + noise,
  481. 'trend': trend,
  482. 'seasonal': seasonal,
  483. })
  484. def sample_predictive_trend(self, df, iteration):
  485. k = self.params['k'][iteration]
  486. m = self.params['m'][iteration]
  487. deltas = self.params['delta'][iteration]
  488. t = np.array(df['t'])
  489. T = t.max()
  490. if T > 1:
  491. # Get the time discretization of the history
  492. dt = np.diff(self.history['t'])
  493. dt = np.min(dt[dt > 0])
  494. # Number of time periods in the future
  495. N = np.ceil((T - 1) / float(dt))
  496. S = len(self.changepoints_t)
  497. prob_change = min(1, (S * (T - 1)) / N)
  498. n_changes = np.random.binomial(N, prob_change)
  499. # Sample ts
  500. changepoint_ts_new = sorted(np.random.uniform(1, T, n_changes))
  501. else:
  502. # Case where we're not extrapolating.
  503. changepoint_ts_new = []
  504. n_changes = 0
  505. # Get the empirical scale of the deltas, plus epsilon to avoid NaNs.
  506. lambda_ = np.mean(np.abs(deltas)) + 1e-8
  507. # Sample deltas
  508. deltas_new = np.random.laplace(0, lambda_, n_changes)
  509. # Prepend the times and deltas from the history
  510. changepoint_ts = np.concatenate((self.changepoints_t,
  511. changepoint_ts_new))
  512. deltas = np.concatenate((deltas, deltas_new))
  513. if self.growth == 'linear':
  514. trend = self.piecewise_linear(t, deltas, k, m, changepoint_ts)
  515. else:
  516. cap = df['cap_scaled']
  517. trend = self.piecewise_logistic(t, cap, deltas, k, m,
  518. changepoint_ts)
  519. return trend * self.y_scale
  520. def make_future_dataframe(self, periods, freq='D', include_history=True):
  521. last_date = self.history_dates.max()
  522. dates = pd.date_range(
  523. start=last_date,
  524. periods=periods + 1, # An extra in case we include start
  525. freq=freq)
  526. dates = dates[dates > last_date] # Drop start if equals last_date
  527. dates = dates[:periods] # Return correct number of periods
  528. if include_history:
  529. dates = np.concatenate((np.array(self.history_dates), dates))
  530. return pd.DataFrame({'ds': dates})
  531. def plot(self, fcst, uncertainty=True, xlabel='ds', ylabel='y'):
  532. """Plot the Prophet forecast.
  533. Parameters
  534. ----------
  535. fcst: pd.DataFrame output of self.predict.
  536. uncertainty: Optional boolean to plot uncertainty intervals.
  537. xlabel: Optional label name on X-axis
  538. ylabel: Optional label name on Y-axis
  539. Returns
  540. -------
  541. a matplotlib figure.
  542. """
  543. fig = plt.figure(facecolor='w', figsize=(10, 6))
  544. ax = fig.add_subplot(111)
  545. ax.plot(self.history['ds'].values, self.history['y'], 'k.')
  546. ax.plot(fcst['ds'].values, fcst['yhat'], ls='-', c='#0072B2')
  547. if 'cap' in fcst:
  548. ax.plot(fcst['ds'].values, fcst['cap'], ls='--', c='k')
  549. if uncertainty:
  550. ax.fill_between(fcst['ds'].values, fcst['yhat_lower'],
  551. fcst['yhat_upper'], color='#0072B2',
  552. alpha=0.2)
  553. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  554. ax.set_xlabel(xlabel)
  555. ax.set_ylabel(ylabel)
  556. fig.tight_layout()
  557. return fig
  558. def plot_components(self, fcst, uncertainty=True):
  559. """Plot the Prophet forecast components.
  560. Will plot whichever are available of: trend, holidays, weekly
  561. seasonality, and yearly seasonality.
  562. Parameters
  563. ----------
  564. fcst: pd.DataFrame output of self.predict.
  565. uncertainty: Optional boolean to plot uncertainty intervals.
  566. Returns
  567. -------
  568. a matplotlib figure.
  569. """
  570. # Identify components to be plotted
  571. components = [('plot_trend', True),
  572. ('plot_holidays', self.holidays is not None),
  573. ('plot_weekly', 'weekly' in fcst),
  574. ('plot_yearly', 'yearly' in fcst)]
  575. components = [(plot, cond) for plot, cond in components if cond]
  576. npanel = len(components)
  577. fig, axes = plt.subplots(npanel, 1, facecolor='w',
  578. figsize=(9, 3 * npanel))
  579. artists = []
  580. for ax, plot in zip(axes,
  581. [getattr(self, plot) for plot, _ in components]):
  582. artists += plot(fcst, ax=ax, uncertainty=uncertainty)
  583. fig.tight_layout()
  584. return artists
  585. def plot_trend(self, fcst, ax=None, uncertainty=True):
  586. """Plot the trend component of the forecast.
  587. Parameters
  588. ----------
  589. fcst: pd.DataFrame output of self.predict.
  590. ax: Optional matplotlib Axes to plot on.
  591. uncertainty: Optional boolean to plot uncertainty intervals.
  592. Returns
  593. -------
  594. a list of matplotlib artists
  595. """
  596. artists = []
  597. if not ax:
  598. fig = plt.figure(facecolor='w', figsize=(10, 6))
  599. ax = fig.add_subplot(111)
  600. artists += ax.plot(fcst['ds'].values, fcst['trend'], ls='-',
  601. c='#0072B2')
  602. if 'cap' in fcst:
  603. artists += ax.plot(fcst['ds'].values, fcst['cap'], ls='--', c='k')
  604. if uncertainty:
  605. artists += [ax.fill_between(
  606. fcst['ds'].values, fcst['trend_lower'], fcst['trend_upper'],
  607. color='#0072B2', alpha=0.2)]
  608. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  609. ax.set_xlabel('ds')
  610. ax.set_ylabel('trend')
  611. return artists
  612. def plot_holidays(self, fcst, ax=None, uncertainty=True):
  613. """Plot the holidays component of the forecast.
  614. Parameters
  615. ----------
  616. fcst: pd.DataFrame output of self.predict.
  617. ax: Optional matplotlib Axes to plot on. One will be created if this
  618. is not provided.
  619. uncertainty: Optional boolean to plot uncertainty intervals.
  620. Returns
  621. -------
  622. a list of matplotlib artists
  623. """
  624. artists = []
  625. if not ax:
  626. fig = plt.figure(facecolor='w', figsize=(10, 6))
  627. ax = fig.add_subplot(111)
  628. holiday_comps = self.holidays['holiday'].unique()
  629. y_holiday = fcst[holiday_comps].sum(1)
  630. y_holiday_l = fcst[[h + '_lower' for h in holiday_comps]].sum(1)
  631. y_holiday_u = fcst[[h + '_upper' for h in holiday_comps]].sum(1)
  632. # NOTE the above CI calculation is incorrect if holidays overlap
  633. # in time. Since it is just for the visualization we will not
  634. # worry about it now.
  635. artists += ax.plot(fcst['ds'].values, y_holiday, ls='-',
  636. c='#0072B2')
  637. if uncertainty:
  638. artists += [ax.fill_between(fcst['ds'].values,
  639. y_holiday_l, y_holiday_u,
  640. color='#0072B2', alpha=0.2)]
  641. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  642. ax.set_xlabel('ds')
  643. ax.set_ylabel('holidays')
  644. return artists
  645. def plot_weekly(self, fcst, ax=None, uncertainty=True):
  646. """Plot the weekly component of the forecast.
  647. Parameters
  648. ----------
  649. fcst: pd.DataFrame output of self.predict.
  650. ax: Optional matplotlib Axes to plot on. One will be created if this
  651. is not provided.
  652. uncertainty: Optional boolean to plot uncertainty intervals.
  653. Returns
  654. -------
  655. a list of matplotlib artists
  656. """
  657. artists = []
  658. if not ax:
  659. fig = plt.figure(facecolor='w', figsize=(10, 6))
  660. ax = fig.add_subplot(111)
  661. df_s = fcst.copy()
  662. df_s['dow'] = df_s['ds'].dt.weekday_name
  663. df_s = df_s.groupby('dow').first()
  664. days = pd.date_range(start='2017-01-01', periods=7).weekday_name
  665. y_weekly = [df_s.loc[d]['weekly'] for d in days]
  666. y_weekly_l = [df_s.loc[d]['weekly_lower'] for d in days]
  667. y_weekly_u = [df_s.loc[d]['weekly_upper'] for d in days]
  668. artists += ax.plot(range(len(days)), y_weekly, ls='-',
  669. c='#0072B2')
  670. if uncertainty:
  671. artists += [ax.fill_between(range(len(days)),
  672. y_weekly_l, y_weekly_u,
  673. color='#0072B2', alpha=0.2)]
  674. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  675. ax.set_xticks(range(len(days)))
  676. ax.set_xticklabels(days)
  677. ax.set_xlabel('Day of week')
  678. ax.set_ylabel('weekly')
  679. return artists
  680. def plot_yearly(self, fcst, ax=None, uncertainty=True):
  681. """Plot the yearly component of the forecast.
  682. Parameters
  683. ----------
  684. fcst: pd.DataFrame output of self.predict.
  685. ax: Optional matplotlib Axes to plot on. One will be created if
  686. this is not provided.
  687. uncertainty: Optional boolean to plot uncertainty intervals.
  688. Returns
  689. -------
  690. a list of matplotlib artists
  691. """
  692. artists = []
  693. if not ax:
  694. fig = plt.figure(facecolor='w', figsize=(10, 6))
  695. ax = fig.add_subplot(111)
  696. df_s = fcst.copy()
  697. df_s['doy'] = df_s['ds'].map(lambda x: x.strftime('2000-%m-%d'))
  698. df_s = df_s.groupby('doy').first().sort_index()
  699. artists += ax.plot(pd.to_datetime(df_s.index), df_s['yearly'], ls='-',
  700. c='#0072B2')
  701. if uncertainty:
  702. artists += [ax.fill_between(
  703. pd.to_datetime(df_s.index), df_s['yearly_lower'],
  704. df_s['yearly_upper'], color='#0072B2', alpha=0.2)]
  705. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  706. months = MonthLocator(range(1, 13), bymonthday=1, interval=2)
  707. ax.xaxis.set_major_formatter(FuncFormatter(
  708. lambda x, pos=None: '{dt:%B} {dt.day}'.format(dt=num2date(x))))
  709. ax.xaxis.set_major_locator(months)
  710. ax.set_xlabel('Day of year')
  711. ax.set_ylabel('yearly')
  712. return artists
  713. # fb-block 9