forecaster.py 29 KB

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