forecaster.py 29 KB

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