forecaster.py 29 KB

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