forecaster.py 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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 copy import deepcopy
  13. from datetime import timedelta
  14. import logging
  15. logging.basicConfig()
  16. logger = logging.getLogger(__name__)
  17. try:
  18. from matplotlib import pyplot as plt
  19. from matplotlib.dates import MonthLocator, num2date
  20. from matplotlib.ticker import FuncFormatter
  21. except ImportError:
  22. logger.exception('Importing matplotlib failed. Plotting will not work.')
  23. import numpy as np
  24. import pandas as pd
  25. # fb-block 1 start
  26. from fbprophet.models import prophet_stan_models
  27. # fb-block 1 end
  28. try:
  29. import pystan # noqa F401
  30. except ImportError:
  31. logger.error('You cannot run prophet without pystan installed')
  32. raise
  33. # fb-block 2
  34. class Prophet(object):
  35. """Prophet forecaster.
  36. Parameters
  37. ----------
  38. growth: String 'linear' or 'logistic' to specify a linear or logistic
  39. trend.
  40. changepoints: List of dates at which to include potential changepoints. If
  41. not specified, potential changepoints are selected automatically.
  42. n_changepoints: Number of potential changepoints to include. Not used
  43. if input `changepoints` is supplied. If `changepoints` is not supplied,
  44. then n_changepoints potential changepoints are selected uniformly from
  45. the first 80 percent of the history.
  46. yearly_seasonality: Fit yearly seasonality.
  47. Can be 'auto', True, False, or a number of Fourier terms to generate.
  48. weekly_seasonality: Fit weekly seasonality.
  49. Can be 'auto', True, False, or a number of Fourier terms to generate.
  50. daily_seasonality: Fit daily seasonality.
  51. Can be 'auto', True, False, or a number of Fourier terms to generate.
  52. holidays: pd.DataFrame with columns holiday (string) and ds (date type)
  53. and optionally columns lower_window and upper_window which specify a
  54. range of days around the date to be included as holidays.
  55. lower_window=-2 will include 2 days prior to the date as holidays. Also
  56. optionally can have a column prior_scale specifying the prior scale for
  57. that holiday.
  58. seasonality_prior_scale: Parameter modulating the strength of the
  59. seasonality model. Larger values allow the model to fit larger seasonal
  60. fluctuations, smaller values dampen the seasonality. Can be specified
  61. for individual seasonalities using add_seasonality.
  62. holidays_prior_scale: Parameter modulating the strength of the holiday
  63. components model, unless overridden in the holidays input.
  64. changepoint_prior_scale: Parameter modulating the flexibility of the
  65. automatic changepoint selection. Large values will allow many
  66. changepoints, small values will allow few changepoints.
  67. mcmc_samples: Integer, if greater than 0, will do full Bayesian inference
  68. with the specified number of MCMC samples. If 0, will do MAP
  69. estimation.
  70. interval_width: Float, width of the uncertainty intervals provided
  71. for the forecast. If mcmc_samples=0, this will be only the uncertainty
  72. in the trend using the MAP estimate of the extrapolated generative
  73. model. If mcmc.samples>0, this will be integrated over all model
  74. parameters, which will include uncertainty in seasonality.
  75. uncertainty_samples: Number of simulated draws used to estimate
  76. uncertainty intervals.
  77. """
  78. def __init__(
  79. self,
  80. growth='linear',
  81. changepoints=None,
  82. n_changepoints=25,
  83. yearly_seasonality='auto',
  84. weekly_seasonality='auto',
  85. daily_seasonality='auto',
  86. holidays=None,
  87. seasonality_prior_scale=10.0,
  88. holidays_prior_scale=10.0,
  89. changepoint_prior_scale=0.05,
  90. mcmc_samples=0,
  91. interval_width=0.80,
  92. uncertainty_samples=1000,
  93. ):
  94. self.growth = growth
  95. self.changepoints = pd.to_datetime(changepoints)
  96. if self.changepoints is not None:
  97. self.n_changepoints = len(self.changepoints)
  98. self.specified_changepoints = True
  99. else:
  100. self.n_changepoints = n_changepoints
  101. self.specified_changepoints = False
  102. self.yearly_seasonality = yearly_seasonality
  103. self.weekly_seasonality = weekly_seasonality
  104. self.daily_seasonality = daily_seasonality
  105. if holidays is not None:
  106. if not (
  107. isinstance(holidays, pd.DataFrame)
  108. and 'ds' in holidays # noqa W503
  109. and 'holiday' in holidays # noqa W503
  110. ):
  111. raise ValueError("holidays must be a DataFrame with 'ds' and "
  112. "'holiday' columns.")
  113. holidays['ds'] = pd.to_datetime(holidays['ds'])
  114. self.holidays = holidays
  115. self.seasonality_prior_scale = float(seasonality_prior_scale)
  116. self.changepoint_prior_scale = float(changepoint_prior_scale)
  117. self.holidays_prior_scale = float(holidays_prior_scale)
  118. self.mcmc_samples = mcmc_samples
  119. self.interval_width = interval_width
  120. self.uncertainty_samples = uncertainty_samples
  121. # Set during fitting
  122. self.start = None
  123. self.y_scale = None
  124. self.logistic_floor = False
  125. self.t_scale = None
  126. self.changepoints_t = None
  127. self.seasonalities = {}
  128. self.extra_regressors = {}
  129. self.stan_fit = None
  130. self.params = {}
  131. self.history = None
  132. self.history_dates = None
  133. self.validate_inputs()
  134. def validate_inputs(self):
  135. """Validates the inputs to Prophet."""
  136. if self.growth not in ('linear', 'logistic'):
  137. raise ValueError(
  138. "Parameter 'growth' should be 'linear' or 'logistic'.")
  139. if self.holidays is not None:
  140. has_lower = 'lower_window' in self.holidays
  141. has_upper = 'upper_window' in self.holidays
  142. if has_lower + has_upper == 1:
  143. raise ValueError('Holidays must have both lower_window and ' +
  144. 'upper_window, or neither')
  145. if has_lower:
  146. if self.holidays['lower_window'].max() > 0:
  147. raise ValueError('Holiday lower_window should be <= 0')
  148. if self.holidays['upper_window'].min() < 0:
  149. raise ValueError('Holiday upper_window should be >= 0')
  150. for h in self.holidays['holiday'].unique():
  151. self.validate_column_name(h, check_holidays=False)
  152. def validate_column_name(self, name, check_holidays=True,
  153. check_seasonalities=True, check_regressors=True):
  154. """Validates the name of a seasonality, holiday, or regressor.
  155. Parameters
  156. ----------
  157. name: string
  158. check_holidays: bool check if name already used for holiday
  159. check_seasonalities: bool check if name already used for seasonality
  160. check_regressors: bool check if name already used for regressor
  161. """
  162. if '_delim_' in name:
  163. raise ValueError('Name cannot contain "_delim_"')
  164. reserved_names = [
  165. 'trend', 'seasonal', 'seasonalities', 'daily', 'weekly', 'yearly',
  166. 'holidays', 'zeros', 'extra_regressors', 'yhat'
  167. ]
  168. rn_l = [n + '_lower' for n in reserved_names]
  169. rn_u = [n + '_upper' for n in reserved_names]
  170. reserved_names.extend(rn_l)
  171. reserved_names.extend(rn_u)
  172. reserved_names.extend([
  173. 'ds', 'y', 'cap', 'floor', 'y_scaled', 'cap_scaled'])
  174. if name in reserved_names:
  175. raise ValueError('Name "{}" is reserved.'.format(name))
  176. if (check_holidays and self.holidays is not None and
  177. name in self.holidays['holiday'].unique()):
  178. raise ValueError(
  179. 'Name "{}" already used for a holiday.'.format(name))
  180. if check_seasonalities and name in self.seasonalities:
  181. raise ValueError(
  182. 'Name "{}" already used for a seasonality.'.format(name))
  183. if check_regressors and name in self.extra_regressors:
  184. raise ValueError(
  185. 'Name "{}" already used for an added regressor.'.format(name))
  186. def setup_dataframe(self, df, initialize_scales=False):
  187. """Prepare dataframe for fitting or predicting.
  188. Adds a time index and scales y. Creates auxiliary columns 't', 't_ix',
  189. 'y_scaled', and 'cap_scaled'. These columns are used during both
  190. fitting and predicting.
  191. Parameters
  192. ----------
  193. df: pd.DataFrame with columns ds, y, and cap if logistic growth. Any
  194. specified additional regressors must also be present.
  195. initialize_scales: Boolean set scaling factors in self from df.
  196. Returns
  197. -------
  198. pd.DataFrame prepared for fitting or predicting.
  199. """
  200. if 'y' in df:
  201. df['y'] = pd.to_numeric(df['y'])
  202. if np.isinf(df['y'].values).any():
  203. raise ValueError('Found infinity in column y.')
  204. df['ds'] = pd.to_datetime(df['ds'])
  205. if df['ds'].isnull().any():
  206. raise ValueError('Found NaN in column ds.')
  207. for name in self.extra_regressors:
  208. if name not in df:
  209. raise ValueError(
  210. 'Regressor "{}" missing from dataframe'.format(name))
  211. df = df.sort_values('ds')
  212. df.reset_index(inplace=True, drop=True)
  213. self.initialize_scales(initialize_scales, df)
  214. if self.logistic_floor:
  215. if 'floor' not in df:
  216. raise ValueError("Expected column 'floor'.")
  217. else:
  218. df['floor'] = 0
  219. if self.growth == 'logistic':
  220. assert 'cap' in df
  221. df['cap_scaled'] = (df['cap'] - df['floor']) / self.y_scale
  222. df['t'] = (df['ds'] - self.start) / self.t_scale
  223. if 'y' in df:
  224. df['y_scaled'] = (df['y'] - df['floor']) / self.y_scale
  225. for name, props in self.extra_regressors.items():
  226. df[name] = pd.to_numeric(df[name])
  227. df[name] = ((df[name] - props['mu']) / props['std'])
  228. if df[name].isnull().any():
  229. raise ValueError('Found NaN in column ' + name)
  230. return df
  231. def initialize_scales(self, initialize_scales, df):
  232. """Initialize model scales.
  233. Sets model scaling factors using df.
  234. Parameters
  235. ----------
  236. initialize_scales: Boolean set the scales or not.
  237. df: pd.DataFrame for setting scales.
  238. """
  239. if not initialize_scales:
  240. return
  241. if self.growth == 'logistic' and 'floor' in df:
  242. self.logistic_floor = True
  243. floor = df['floor']
  244. else:
  245. floor = 0.
  246. self.y_scale = (df['y'] - floor).abs().max()
  247. if self.y_scale == 0:
  248. self.y_scale = 1
  249. self.start = df['ds'].min()
  250. self.t_scale = df['ds'].max() - self.start
  251. for name, props in self.extra_regressors.items():
  252. standardize = props['standardize']
  253. n_vals = len(df[name].unique())
  254. if n_vals < 2:
  255. raise ValueError('Regressor {} is constant.'.format(name))
  256. if standardize == 'auto':
  257. if set(df[name].unique()) == set([1, 0]):
  258. # Don't standardize binary variables.
  259. standardize = False
  260. else:
  261. standardize = True
  262. if standardize:
  263. mu = df[name].mean()
  264. std = df[name].std()
  265. self.extra_regressors[name]['mu'] = mu
  266. self.extra_regressors[name]['std'] = std
  267. def set_changepoints(self):
  268. """Set changepoints
  269. Sets m$changepoints to the dates of changepoints. Either:
  270. 1) The changepoints were passed in explicitly.
  271. A) They are empty.
  272. B) They are not empty, and need validation.
  273. 2) We are generating a grid of them.
  274. 3) The user prefers no changepoints be used.
  275. """
  276. if self.changepoints is not None:
  277. if len(self.changepoints) == 0:
  278. pass
  279. else:
  280. too_low = min(self.changepoints) < self.history['ds'].min()
  281. too_high = max(self.changepoints) > self.history['ds'].max()
  282. if too_low or too_high:
  283. raise ValueError(
  284. 'Changepoints must fall within training data.')
  285. else:
  286. # Place potential changepoints evenly through first 80% of history
  287. hist_size = np.floor(self.history.shape[0] * 0.8)
  288. if self.n_changepoints + 1 > hist_size:
  289. self.n_changepoints = hist_size - 1
  290. logger.info(
  291. 'n_changepoints greater than number of observations.'
  292. 'Using {}.'.format(self.n_changepoints)
  293. )
  294. if self.n_changepoints > 0:
  295. cp_indexes = (
  296. np.linspace(0, hist_size, self.n_changepoints + 1)
  297. .round()
  298. .astype(np.int)
  299. )
  300. self.changepoints = (
  301. self.history.iloc[cp_indexes]['ds'].tail(-1)
  302. )
  303. else:
  304. # set empty changepoints
  305. self.changepoints = []
  306. if len(self.changepoints) > 0:
  307. self.changepoints_t = np.sort(np.array(
  308. (self.changepoints - self.start) / self.t_scale))
  309. else:
  310. self.changepoints_t = np.array([0]) # dummy changepoint
  311. def get_changepoint_matrix(self):
  312. """Gets changepoint matrix for history dataframe."""
  313. A = np.zeros((self.history.shape[0], len(self.changepoints_t)))
  314. for i, t_i in enumerate(self.changepoints_t):
  315. A[self.history['t'].values >= t_i, i] = 1
  316. return A
  317. @staticmethod
  318. def fourier_series(dates, period, series_order):
  319. """Provides Fourier series components with the specified frequency
  320. and order.
  321. Parameters
  322. ----------
  323. dates: pd.Series containing timestamps.
  324. period: Number of days of the period.
  325. series_order: Number of components.
  326. Returns
  327. -------
  328. Matrix with seasonality features.
  329. """
  330. # convert to days since epoch
  331. t = np.array(
  332. (dates - pd.datetime(1970, 1, 1))
  333. .dt.total_seconds()
  334. .astype(np.float)
  335. ) / (3600 * 24.)
  336. return np.column_stack([
  337. fun((2.0 * (i + 1) * np.pi * t / period))
  338. for i in range(series_order)
  339. for fun in (np.sin, np.cos)
  340. ])
  341. @classmethod
  342. def make_seasonality_features(cls, dates, period, series_order, prefix):
  343. """Data frame with seasonality features.
  344. Parameters
  345. ----------
  346. cls: Prophet class.
  347. dates: pd.Series containing timestamps.
  348. period: Number of days of the period.
  349. series_order: Number of components.
  350. prefix: Column name prefix.
  351. Returns
  352. -------
  353. pd.DataFrame with seasonality features.
  354. """
  355. features = cls.fourier_series(dates, period, series_order)
  356. columns = [
  357. '{}_delim_{}'.format(prefix, i + 1)
  358. for i in range(features.shape[1])
  359. ]
  360. return pd.DataFrame(features, columns=columns)
  361. def make_holiday_features(self, dates):
  362. """Construct a dataframe of holiday features.
  363. Parameters
  364. ----------
  365. dates: pd.Series containing timestamps used for computing seasonality.
  366. Returns
  367. -------
  368. holiday_features: pd.DataFrame with a column for each holiday.
  369. prior_scale_list: List of prior scales for each holiday column.
  370. """
  371. # Holds columns of our future matrix.
  372. expanded_holidays = defaultdict(lambda: np.zeros(dates.shape[0]))
  373. prior_scales = {}
  374. # Makes an index so we can perform `get_loc` below.
  375. # Strip to just dates.
  376. row_index = pd.DatetimeIndex(dates.apply(lambda x: x.date()))
  377. for _ix, row in self.holidays.iterrows():
  378. dt = row.ds.date()
  379. try:
  380. lw = int(row.get('lower_window', 0))
  381. uw = int(row.get('upper_window', 0))
  382. except ValueError:
  383. lw = 0
  384. uw = 0
  385. ps = float(row.get('prior_scale', self.holidays_prior_scale))
  386. if np.isnan(ps):
  387. ps = float(self.holidays_prior_scale)
  388. if (
  389. row.holiday in prior_scales and prior_scales[row.holiday] != ps
  390. ):
  391. raise ValueError(
  392. 'Holiday {} does not have consistent prior scale '
  393. 'specification.'.format(row.holiday))
  394. if ps <= 0:
  395. raise ValueError('Prior scale must be > 0')
  396. prior_scales[row.holiday] = ps
  397. for offset in range(lw, uw + 1):
  398. occurrence = dt + timedelta(days=offset)
  399. try:
  400. loc = row_index.get_loc(occurrence)
  401. except KeyError:
  402. loc = None
  403. key = '{}_delim_{}{}'.format(
  404. row.holiday,
  405. '+' if offset >= 0 else '-',
  406. abs(offset)
  407. )
  408. if loc is not None:
  409. expanded_holidays[key][loc] = 1.
  410. else:
  411. # Access key to generate value
  412. expanded_holidays[key]
  413. holiday_features = pd.DataFrame(expanded_holidays)
  414. prior_scale_list = [
  415. prior_scales[h.split('_delim_')[0]]
  416. for h in holiday_features.columns
  417. ]
  418. return holiday_features, prior_scale_list
  419. def add_regressor(self, name, prior_scale=None, standardize='auto'):
  420. """Add an additional regressor to be used for fitting and predicting.
  421. The dataframe passed to `fit` and `predict` will have a column with the
  422. specified name to be used as a regressor. When standardize='auto', the
  423. regressor will be standardized unless it is binary. The regression
  424. coefficient is given a prior with the specified scale parameter.
  425. Decreasing the prior scale will add additional regularization. If no
  426. prior scale is provided, self.holidays_prior_scale will be used.
  427. Parameters
  428. ----------
  429. name: string name of the regressor.
  430. prior_scale: optional float scale for the normal prior. If not
  431. provided, self.holidays_prior_scale will be used.
  432. standardize: optional, specify whether this regressor will be
  433. standardized prior to fitting. Can be 'auto' (standardize if not
  434. binary), True, or False.
  435. Returns
  436. -------
  437. The prophet object.
  438. """
  439. if self.history is not None:
  440. raise Exception(
  441. "Regressors must be added prior to model fitting.")
  442. self.validate_column_name(name, check_regressors=False)
  443. if prior_scale is None:
  444. prior_scale = float(self.holidays_prior_scale)
  445. assert prior_scale > 0
  446. self.extra_regressors[name] = {
  447. 'prior_scale': prior_scale,
  448. 'standardize': standardize,
  449. 'mu': 0.,
  450. 'std': 1.,
  451. }
  452. return self
  453. def add_seasonality(self, name, period, fourier_order, prior_scale=None):
  454. """Add a seasonal component with specified period, number of Fourier
  455. components, and prior scale.
  456. Increasing the number of Fourier components allows the seasonality to
  457. change more quickly (at risk of overfitting). Default values for yearly
  458. and weekly seasonalities are 10 and 3 respectively.
  459. Increasing prior scale will allow this seasonality component more
  460. flexibility, decreasing will dampen it. If not provided, will use the
  461. seasonality_prior_scale provided on Prophet initialization (defaults
  462. to 10).
  463. Parameters
  464. ----------
  465. name: string name of the seasonality component.
  466. period: float number of days in one period.
  467. fourier_order: int number of Fourier components to use.
  468. prior_scale: float prior scale for this component.
  469. Returns
  470. -------
  471. The prophet object.
  472. """
  473. if self.history is not None:
  474. raise Exception(
  475. "Seasonality must be added prior to model fitting.")
  476. if name not in ['daily', 'weekly', 'yearly']:
  477. # Allow overwriting built-in seasonalities
  478. self.validate_column_name(name, check_seasonalities=False)
  479. if prior_scale is None:
  480. ps = self.seasonality_prior_scale
  481. else:
  482. ps = float(prior_scale)
  483. if ps <= 0:
  484. raise ValueError('Prior scale must be > 0')
  485. self.seasonalities[name] = {
  486. 'period': period,
  487. 'fourier_order': fourier_order,
  488. 'prior_scale': ps,
  489. }
  490. return self
  491. def make_all_seasonality_features(self, df):
  492. """Dataframe with seasonality features.
  493. Includes seasonality features, holiday features, and added regressors.
  494. Parameters
  495. ----------
  496. df: pd.DataFrame with dates for computing seasonality features and any
  497. added regressors.
  498. Returns
  499. -------
  500. pd.DataFrame with regression features.
  501. list of prior scales for each column of the features dataframe.
  502. """
  503. seasonal_features = []
  504. prior_scales = []
  505. # Seasonality features
  506. for name, props in self.seasonalities.items():
  507. features = self.make_seasonality_features(
  508. df['ds'],
  509. props['period'],
  510. props['fourier_order'],
  511. name,
  512. )
  513. seasonal_features.append(features)
  514. prior_scales.extend(
  515. [props['prior_scale']] * features.shape[1])
  516. # Holiday features
  517. if self.holidays is not None:
  518. features, holiday_priors = self.make_holiday_features(df['ds'])
  519. seasonal_features.append(features)
  520. prior_scales.extend(holiday_priors)
  521. # Additional regressors
  522. for name, props in self.extra_regressors.items():
  523. seasonal_features.append(pd.DataFrame(df[name]))
  524. prior_scales.append(props['prior_scale'])
  525. if len(seasonal_features) == 0:
  526. seasonal_features.append(
  527. pd.DataFrame({'zeros': np.zeros(df.shape[0])}))
  528. prior_scales.append(1.)
  529. return pd.concat(seasonal_features, axis=1), prior_scales
  530. def parse_seasonality_args(self, name, arg, auto_disable, default_order):
  531. """Get number of fourier components for built-in seasonalities.
  532. Parameters
  533. ----------
  534. name: string name of the seasonality component.
  535. arg: 'auto', True, False, or number of fourier components as provided.
  536. auto_disable: bool if seasonality should be disabled when 'auto'.
  537. default_order: int default fourier order
  538. Returns
  539. -------
  540. Number of fourier components, or 0 for disabled.
  541. """
  542. if arg == 'auto':
  543. fourier_order = 0
  544. if name in self.seasonalities:
  545. logger.info(
  546. 'Found custom seasonality named "{name}", '
  547. 'disabling built-in {name} seasonality.'.format(name=name)
  548. )
  549. elif auto_disable:
  550. logger.info(
  551. 'Disabling {name} seasonality. Run prophet with '
  552. '{name}_seasonality=True to override this.'.format(
  553. name=name)
  554. )
  555. else:
  556. fourier_order = default_order
  557. elif arg is True:
  558. fourier_order = default_order
  559. elif arg is False:
  560. fourier_order = 0
  561. else:
  562. fourier_order = int(arg)
  563. return fourier_order
  564. def set_auto_seasonalities(self):
  565. """Set seasonalities that were left on auto.
  566. Turns on yearly seasonality if there is >=2 years of history.
  567. Turns on weekly seasonality if there is >=2 weeks of history, and the
  568. spacing between dates in the history is <7 days.
  569. Turns on daily seasonality if there is >=2 days of history, and the
  570. spacing between dates in the history is <1 day.
  571. """
  572. first = self.history['ds'].min()
  573. last = self.history['ds'].max()
  574. dt = self.history['ds'].diff()
  575. min_dt = dt.iloc[dt.nonzero()[0]].min()
  576. # Yearly seasonality
  577. yearly_disable = last - first < pd.Timedelta(days=730)
  578. fourier_order = self.parse_seasonality_args(
  579. 'yearly', self.yearly_seasonality, yearly_disable, 10)
  580. if fourier_order > 0:
  581. self.seasonalities['yearly'] = {
  582. 'period': 365.25,
  583. 'fourier_order': fourier_order,
  584. 'prior_scale': self.seasonality_prior_scale,
  585. }
  586. # Weekly seasonality
  587. weekly_disable = ((last - first < pd.Timedelta(weeks=2)) or
  588. (min_dt >= pd.Timedelta(weeks=1)))
  589. fourier_order = self.parse_seasonality_args(
  590. 'weekly', self.weekly_seasonality, weekly_disable, 3)
  591. if fourier_order > 0:
  592. self.seasonalities['weekly'] = {
  593. 'period': 7,
  594. 'fourier_order': fourier_order,
  595. 'prior_scale': self.seasonality_prior_scale,
  596. }
  597. # Daily seasonality
  598. daily_disable = ((last - first < pd.Timedelta(days=2)) or
  599. (min_dt >= pd.Timedelta(days=1)))
  600. fourier_order = self.parse_seasonality_args(
  601. 'daily', self.daily_seasonality, daily_disable, 4)
  602. if fourier_order > 0:
  603. self.seasonalities['daily'] = {
  604. 'period': 1,
  605. 'fourier_order': fourier_order,
  606. 'prior_scale': self.seasonality_prior_scale,
  607. }
  608. @staticmethod
  609. def linear_growth_init(df):
  610. """Initialize linear growth.
  611. Provides a strong initialization for linear growth by calculating the
  612. growth and offset parameters that pass the function through the first
  613. and last points in the time series.
  614. Parameters
  615. ----------
  616. df: pd.DataFrame with columns ds (date), y_scaled (scaled time series),
  617. and t (scaled time).
  618. Returns
  619. -------
  620. A tuple (k, m) with the rate (k) and offset (m) of the linear growth
  621. function.
  622. """
  623. i0, i1 = df['ds'].idxmin(), df['ds'].idxmax()
  624. T = df['t'].iloc[i1] - df['t'].iloc[i0]
  625. k = (df['y_scaled'].iloc[i1] - df['y_scaled'].iloc[i0]) / T
  626. m = df['y_scaled'].iloc[i0] - k * df['t'].iloc[i0]
  627. return (k, m)
  628. @staticmethod
  629. def logistic_growth_init(df):
  630. """Initialize logistic growth.
  631. Provides a strong initialization for logistic growth by calculating the
  632. growth and offset parameters that pass the function through the first
  633. and last points in the time series.
  634. Parameters
  635. ----------
  636. df: pd.DataFrame with columns ds (date), cap_scaled (scaled capacity),
  637. y_scaled (scaled time series), and t (scaled time).
  638. Returns
  639. -------
  640. A tuple (k, m) with the rate (k) and offset (m) of the logistic growth
  641. function.
  642. """
  643. i0, i1 = df['ds'].idxmin(), df['ds'].idxmax()
  644. T = df['t'].iloc[i1] - df['t'].iloc[i0]
  645. # Force valid values, in case y > cap or y < 0
  646. C0 = df['cap_scaled'].iloc[i0]
  647. C1 = df['cap_scaled'].iloc[i1]
  648. y0 = max(0.01 * C0, min(0.99 * C0, df['y_scaled'].iloc[i0]))
  649. y1 = max(0.01 * C1, min(0.99 * C1, df['y_scaled'].iloc[i1]))
  650. r0 = C0 / y0
  651. r1 = C1 / y1
  652. if abs(r0 - r1) <= 0.01:
  653. r0 = 1.05 * r0
  654. L0 = np.log(r0 - 1)
  655. L1 = np.log(r1 - 1)
  656. # Initialize the offset
  657. m = L0 * T / (L0 - L1)
  658. # And the rate
  659. k = (L0 - L1) / T
  660. return (k, m)
  661. # fb-block 7
  662. def fit(self, df, **kwargs):
  663. """Fit the Prophet model.
  664. This sets self.params to contain the fitted model parameters. It is a
  665. dictionary parameter names as keys and the following items:
  666. k (Mx1 array): M posterior samples of the initial slope.
  667. m (Mx1 array): The initial intercept.
  668. delta (MxN array): The slope change at each of N changepoints.
  669. beta (MxK matrix): Coefficients for K seasonality features.
  670. sigma_obs (Mx1 array): Noise level.
  671. Note that M=1 if MAP estimation.
  672. Parameters
  673. ----------
  674. df: pd.DataFrame containing the history. Must have columns ds (date
  675. type) and y, the time series. If self.growth is 'logistic', then
  676. df must also have a column cap that specifies the capacity at
  677. each ds.
  678. kwargs: Additional arguments passed to the optimizing or sampling
  679. functions in Stan.
  680. Returns
  681. -------
  682. The fitted Prophet object.
  683. """
  684. if self.history is not None:
  685. raise Exception('Prophet object can only be fit once. '
  686. 'Instantiate a new object.')
  687. history = df[df['y'].notnull()].copy()
  688. if history.shape[0] < 2:
  689. raise ValueError('Dataframe has less than 2 non-NaN rows.')
  690. self.history_dates = pd.to_datetime(df['ds']).sort_values()
  691. history = self.setup_dataframe(history, initialize_scales=True)
  692. self.history = history
  693. self.set_auto_seasonalities()
  694. seasonal_features, prior_scales = (
  695. self.make_all_seasonality_features(history))
  696. self.set_changepoints()
  697. A = self.get_changepoint_matrix()
  698. dat = {
  699. 'T': history.shape[0],
  700. 'K': seasonal_features.shape[1],
  701. 'S': len(self.changepoints_t),
  702. 'y': history['y_scaled'],
  703. 't': history['t'],
  704. 'A': A,
  705. 't_change': self.changepoints_t,
  706. 'X': seasonal_features,
  707. 'sigmas': prior_scales,
  708. 'tau': self.changepoint_prior_scale,
  709. }
  710. if self.growth == 'linear':
  711. kinit = self.linear_growth_init(history)
  712. else:
  713. dat['cap'] = history['cap_scaled']
  714. kinit = self.logistic_growth_init(history)
  715. model = prophet_stan_models[self.growth]
  716. def stan_init():
  717. return {
  718. 'k': kinit[0],
  719. 'm': kinit[1],
  720. 'delta': np.zeros(len(self.changepoints_t)),
  721. 'beta': np.zeros(seasonal_features.shape[1]),
  722. 'sigma_obs': 1,
  723. }
  724. if history['y'].min() == history['y'].max():
  725. # Nothing to fit.
  726. self.params = stan_init()
  727. self.params['sigma_obs'] = 1e-9
  728. for par in self.params:
  729. self.params[par] = np.array([self.params[par]])
  730. elif self.mcmc_samples > 0:
  731. stan_fit = model.sampling(
  732. dat,
  733. init=stan_init,
  734. iter=self.mcmc_samples,
  735. **kwargs
  736. )
  737. for par in stan_fit.model_pars:
  738. self.params[par] = stan_fit[par]
  739. else:
  740. try:
  741. params = model.optimizing(
  742. dat, init=stan_init, iter=1e4, **kwargs)
  743. except RuntimeError:
  744. params = model.optimizing(
  745. dat, init=stan_init, iter=1e4, algorithm='Newton',
  746. **kwargs
  747. )
  748. for par in params:
  749. self.params[par] = params[par].reshape((1, -1))
  750. # If no changepoints were requested, replace delta with 0s
  751. if len(self.changepoints) == 0:
  752. # Fold delta into the base rate k
  753. self.params['k'] = self.params['k'] + self.params['delta']
  754. self.params['delta'] = np.zeros(self.params['delta'].shape)
  755. return self
  756. # fb-block 8
  757. def predict(self, df=None):
  758. """Predict using the prophet model.
  759. Parameters
  760. ----------
  761. df: pd.DataFrame with dates for predictions (column ds), and capacity
  762. (column cap) if logistic growth. If not provided, predictions are
  763. made on the history.
  764. Returns
  765. -------
  766. A pd.DataFrame with the forecast components.
  767. """
  768. if df is None:
  769. df = self.history.copy()
  770. else:
  771. if df.shape[0] == 0:
  772. raise ValueError('Dataframe has no rows.')
  773. df = self.setup_dataframe(df.copy())
  774. df['trend'] = self.predict_trend(df)
  775. seasonal_components = self.predict_seasonal_components(df)
  776. intervals = self.predict_uncertainty(df)
  777. # Drop columns except ds, cap, floor, and trend
  778. cols = ['ds', 'trend']
  779. if 'cap' in df:
  780. cols.append('cap')
  781. if self.logistic_floor:
  782. cols.append('floor')
  783. # Add in forecast components
  784. df2 = pd.concat((df[cols], intervals, seasonal_components), axis=1)
  785. df2['yhat'] = df2['trend'] + df2['seasonal']
  786. return df2
  787. @staticmethod
  788. def piecewise_linear(t, deltas, k, m, changepoint_ts):
  789. """Evaluate the piecewise linear function.
  790. Parameters
  791. ----------
  792. t: np.array of times on which the function is evaluated.
  793. deltas: np.array of rate changes at each changepoint.
  794. k: Float initial rate.
  795. m: Float initial offset.
  796. changepoint_ts: np.array of changepoint times.
  797. Returns
  798. -------
  799. Vector y(t).
  800. """
  801. # Intercept changes
  802. gammas = -changepoint_ts * deltas
  803. # Get cumulative slope and intercept at each t
  804. k_t = k * np.ones_like(t)
  805. m_t = m * np.ones_like(t)
  806. for s, t_s in enumerate(changepoint_ts):
  807. indx = t >= t_s
  808. k_t[indx] += deltas[s]
  809. m_t[indx] += gammas[s]
  810. return k_t * t + m_t
  811. @staticmethod
  812. def piecewise_logistic(t, cap, deltas, k, m, changepoint_ts):
  813. """Evaluate the piecewise logistic function.
  814. Parameters
  815. ----------
  816. t: np.array of times on which the function is evaluated.
  817. cap: np.array of capacities at each t.
  818. deltas: np.array of rate changes at each changepoint.
  819. k: Float initial rate.
  820. m: Float initial offset.
  821. changepoint_ts: np.array of changepoint times.
  822. Returns
  823. -------
  824. Vector y(t).
  825. """
  826. # Compute offset changes
  827. k_cum = np.concatenate((np.atleast_1d(k), np.cumsum(deltas) + k))
  828. gammas = np.zeros(len(changepoint_ts))
  829. for i, t_s in enumerate(changepoint_ts):
  830. gammas[i] = (
  831. (t_s - m - np.sum(gammas))
  832. * (1 - k_cum[i] / k_cum[i + 1]) # noqa W503
  833. )
  834. # Get cumulative rate and offset at each t
  835. k_t = k * np.ones_like(t)
  836. m_t = m * np.ones_like(t)
  837. for s, t_s in enumerate(changepoint_ts):
  838. indx = t >= t_s
  839. k_t[indx] += deltas[s]
  840. m_t[indx] += gammas[s]
  841. return cap / (1 + np.exp(-k_t * (t - m_t)))
  842. def predict_trend(self, df):
  843. """Predict trend using the prophet model.
  844. Parameters
  845. ----------
  846. df: Prediction dataframe.
  847. Returns
  848. -------
  849. Vector with trend on prediction dates.
  850. """
  851. k = np.nanmean(self.params['k'])
  852. m = np.nanmean(self.params['m'])
  853. deltas = np.nanmean(self.params['delta'], axis=0)
  854. t = np.array(df['t'])
  855. if self.growth == 'linear':
  856. trend = self.piecewise_linear(t, deltas, k, m, self.changepoints_t)
  857. else:
  858. cap = df['cap_scaled']
  859. trend = self.piecewise_logistic(
  860. t, cap, deltas, k, m, self.changepoints_t)
  861. return trend * self.y_scale + df['floor']
  862. def predict_seasonal_components(self, df):
  863. """Predict seasonality components, holidays, and added regressors.
  864. Parameters
  865. ----------
  866. df: Prediction dataframe.
  867. Returns
  868. -------
  869. Dataframe with seasonal components.
  870. """
  871. seasonal_features, _ = self.make_all_seasonality_features(df)
  872. lower_p = 100 * (1.0 - self.interval_width) / 2
  873. upper_p = 100 * (1.0 + self.interval_width) / 2
  874. components = pd.DataFrame({
  875. 'col': np.arange(seasonal_features.shape[1]),
  876. 'component': [x.split('_delim_')[0] for x in seasonal_features.columns],
  877. })
  878. # Add total for all regression components
  879. components = components.append(pd.DataFrame({
  880. 'col': np.arange(seasonal_features.shape[1]),
  881. 'component': 'seasonal',
  882. }))
  883. # Add totals for seasonality, holiday, and extra regressors
  884. components = self.add_group_component(
  885. components, 'seasonalities', self.seasonalities.keys())
  886. if self.holidays is not None:
  887. components = self.add_group_component(
  888. components, 'holidays', self.holidays['holiday'].unique())
  889. components = self.add_group_component(
  890. components, 'extra_regressors', self.extra_regressors.keys())
  891. # Remove the placeholder
  892. components = components[components['component'] != 'zeros']
  893. X = seasonal_features.as_matrix()
  894. data = {}
  895. for component, features in components.groupby('component'):
  896. cols = features.col.tolist()
  897. comp_beta = self.params['beta'][:, cols]
  898. comp_features = X[:, cols]
  899. comp = (
  900. np.matmul(comp_features, comp_beta.transpose())
  901. * self.y_scale # noqa W503
  902. )
  903. data[component] = np.nanmean(comp, axis=1)
  904. data[component + '_lower'] = np.nanpercentile(comp, lower_p,
  905. axis=1)
  906. data[component + '_upper'] = np.nanpercentile(comp, upper_p,
  907. axis=1)
  908. return pd.DataFrame(data)
  909. def add_group_component(self, components, name, group):
  910. """Adds a component with given name that contains all of the components
  911. in group.
  912. Parameters
  913. ----------
  914. components: Dataframe with components.
  915. name: Name of new group component.
  916. group: List of components that form the group.
  917. Returns
  918. -------
  919. Dataframe with components.
  920. """
  921. new_comp = components[components['component'].isin(set(group))].copy()
  922. new_comp['component'] = name
  923. components = components.append(new_comp)
  924. return components
  925. def sample_posterior_predictive(self, df):
  926. """Prophet posterior predictive samples.
  927. Parameters
  928. ----------
  929. df: Prediction dataframe.
  930. Returns
  931. -------
  932. Dictionary with posterior predictive samples for each component.
  933. """
  934. n_iterations = self.params['k'].shape[0]
  935. samp_per_iter = max(1, int(np.ceil(
  936. self.uncertainty_samples / float(n_iterations)
  937. )))
  938. # Generate seasonality features once so we can re-use them.
  939. seasonal_features, _ = self.make_all_seasonality_features(df)
  940. sim_values = {'yhat': [], 'trend': [], 'seasonal': []}
  941. for i in range(n_iterations):
  942. for _j in range(samp_per_iter):
  943. sim = self.sample_model(df, seasonal_features, i)
  944. for key in sim_values:
  945. sim_values[key].append(sim[key])
  946. for k, v in sim_values.items():
  947. sim_values[k] = np.column_stack(v)
  948. return sim_values
  949. def predictive_samples(self, df):
  950. """Sample from the posterior predictive distribution.
  951. Parameters
  952. ----------
  953. df: Dataframe with dates for predictions (column ds), and capacity
  954. (column cap) if logistic growth.
  955. Returns
  956. -------
  957. Dictionary with keys "trend", "seasonal", and "yhat" containing
  958. posterior predictive samples for that component. "seasonal" is the sum
  959. of seasonalities, holidays, and added regressors.
  960. """
  961. df = self.setup_dataframe(df.copy())
  962. sim_values = self.sample_posterior_predictive(df)
  963. return sim_values
  964. def predict_uncertainty(self, df):
  965. """Prediction intervals for yhat and trend.
  966. Parameters
  967. ----------
  968. df: Prediction dataframe.
  969. Returns
  970. -------
  971. Dataframe with uncertainty intervals.
  972. """
  973. sim_values = self.sample_posterior_predictive(df)
  974. lower_p = 100 * (1.0 - self.interval_width) / 2
  975. upper_p = 100 * (1.0 + self.interval_width) / 2
  976. series = {}
  977. for key in ['yhat', 'trend']:
  978. series['{}_lower'.format(key)] = np.nanpercentile(
  979. sim_values[key], lower_p, axis=1)
  980. series['{}_upper'.format(key)] = np.nanpercentile(
  981. sim_values[key], upper_p, axis=1)
  982. return pd.DataFrame(series)
  983. def sample_model(self, df, seasonal_features, iteration):
  984. """Simulate observations from the extrapolated generative model.
  985. Parameters
  986. ----------
  987. df: Prediction dataframe.
  988. seasonal_features: pd.DataFrame of seasonal features.
  989. iteration: Int sampling iteration to use parameters from.
  990. Returns
  991. -------
  992. Dataframe with trend, seasonality, and yhat, each like df['t'].
  993. """
  994. trend = self.sample_predictive_trend(df, iteration)
  995. beta = self.params['beta'][iteration]
  996. seasonal = np.matmul(seasonal_features.as_matrix(), beta) * self.y_scale
  997. sigma = self.params['sigma_obs'][iteration]
  998. noise = np.random.normal(0, sigma, df.shape[0]) * self.y_scale
  999. return pd.DataFrame({
  1000. 'yhat': trend + seasonal + noise,
  1001. 'trend': trend,
  1002. 'seasonal': seasonal,
  1003. })
  1004. def sample_predictive_trend(self, df, iteration):
  1005. """Simulate the trend using the extrapolated generative model.
  1006. Parameters
  1007. ----------
  1008. df: Prediction dataframe.
  1009. iteration: Int sampling iteration to use parameters from.
  1010. Returns
  1011. -------
  1012. np.array of simulated trend over df['t'].
  1013. """
  1014. k = self.params['k'][iteration]
  1015. m = self.params['m'][iteration]
  1016. deltas = self.params['delta'][iteration]
  1017. t = np.array(df['t'])
  1018. T = t.max()
  1019. if T > 1:
  1020. # Get the time discretization of the history
  1021. dt = np.diff(self.history['t'])
  1022. dt = np.min(dt[dt > 0])
  1023. # Number of time periods in the future
  1024. N = np.ceil((T - 1) / float(dt))
  1025. S = len(self.changepoints_t)
  1026. prob_change = min(1, (S * (T - 1)) / N)
  1027. n_changes = np.random.binomial(N, prob_change)
  1028. # Sample ts
  1029. changepoint_ts_new = sorted(np.random.uniform(1, T, n_changes))
  1030. else:
  1031. # Case where we're not extrapolating.
  1032. changepoint_ts_new = []
  1033. n_changes = 0
  1034. # Get the empirical scale of the deltas, plus epsilon to avoid NaNs.
  1035. lambda_ = np.mean(np.abs(deltas)) + 1e-8
  1036. # Sample deltas
  1037. deltas_new = np.random.laplace(0, lambda_, n_changes)
  1038. # Prepend the times and deltas from the history
  1039. changepoint_ts = np.concatenate((self.changepoints_t,
  1040. changepoint_ts_new))
  1041. deltas = np.concatenate((deltas, deltas_new))
  1042. if self.growth == 'linear':
  1043. trend = self.piecewise_linear(t, deltas, k, m, changepoint_ts)
  1044. else:
  1045. cap = df['cap_scaled']
  1046. trend = self.piecewise_logistic(t, cap, deltas, k, m,
  1047. changepoint_ts)
  1048. return trend * self.y_scale + df['floor']
  1049. def make_future_dataframe(self, periods, freq='D', include_history=True):
  1050. """Simulate the trend using the extrapolated generative model.
  1051. Parameters
  1052. ----------
  1053. periods: Int number of periods to forecast forward.
  1054. freq: Any valid frequency for pd.date_range, such as 'D' or 'M'.
  1055. include_history: Boolean to include the historical dates in the data
  1056. frame for predictions.
  1057. Returns
  1058. -------
  1059. pd.Dataframe that extends forward from the end of self.history for the
  1060. requested number of periods.
  1061. """
  1062. if self.history_dates is None:
  1063. raise Exception('Model must be fit before this can be used.')
  1064. last_date = self.history_dates.max()
  1065. dates = pd.date_range(
  1066. start=last_date,
  1067. periods=periods + 1, # An extra in case we include start
  1068. freq=freq)
  1069. dates = dates[dates > last_date] # Drop start if equals last_date
  1070. dates = dates[:periods] # Return correct number of periods
  1071. if include_history:
  1072. dates = np.concatenate((np.array(self.history_dates), dates))
  1073. return pd.DataFrame({'ds': dates})
  1074. def plot(self, fcst, ax=None, uncertainty=True, plot_cap=True, xlabel='ds',
  1075. ylabel='y'):
  1076. """Plot the Prophet forecast.
  1077. Parameters
  1078. ----------
  1079. fcst: pd.DataFrame output of self.predict.
  1080. ax: Optional matplotlib axes on which to plot.
  1081. uncertainty: Optional boolean to plot uncertainty intervals.
  1082. plot_cap: Optional boolean indicating if the capacity should be shown
  1083. in the figure, if available.
  1084. xlabel: Optional label name on X-axis
  1085. ylabel: Optional label name on Y-axis
  1086. Returns
  1087. -------
  1088. A matplotlib figure.
  1089. """
  1090. if ax is None:
  1091. fig = plt.figure(facecolor='w', figsize=(10, 6))
  1092. ax = fig.add_subplot(111)
  1093. else:
  1094. fig = ax.get_figure()
  1095. fcst_t = fcst['ds'].dt.to_pydatetime()
  1096. ax.plot(self.history['ds'].dt.to_pydatetime(), self.history['y'], 'k.')
  1097. ax.plot(fcst_t, fcst['yhat'], ls='-', c='#0072B2')
  1098. if 'cap' in fcst and plot_cap:
  1099. ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
  1100. if self.logistic_floor and 'floor' in fcst and plot_cap:
  1101. ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
  1102. if uncertainty:
  1103. ax.fill_between(fcst_t, fcst['yhat_lower'], fcst['yhat_upper'],
  1104. color='#0072B2', alpha=0.2)
  1105. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  1106. ax.set_xlabel(xlabel)
  1107. ax.set_ylabel(ylabel)
  1108. fig.tight_layout()
  1109. return fig
  1110. def plot_components(self, fcst, uncertainty=True, plot_cap=True,
  1111. weekly_start=0, yearly_start=0):
  1112. """Plot the Prophet forecast components.
  1113. Will plot whichever are available of: trend, holidays, weekly
  1114. seasonality, and yearly seasonality.
  1115. Parameters
  1116. ----------
  1117. fcst: pd.DataFrame output of self.predict.
  1118. uncertainty: Optional boolean to plot uncertainty intervals.
  1119. plot_cap: Optional boolean indicating if the capacity should be shown
  1120. in the figure, if available.
  1121. weekly_start: Optional int specifying the start day of the weekly
  1122. seasonality plot. 0 (default) starts the week on Sunday. 1 shifts
  1123. by 1 day to Monday, and so on.
  1124. yearly_start: Optional int specifying the start day of the yearly
  1125. seasonality plot. 0 (default) starts the year on Jan 1. 1 shifts
  1126. by 1 day to Jan 2, and so on.
  1127. Returns
  1128. -------
  1129. A matplotlib figure.
  1130. """
  1131. # Identify components to be plotted
  1132. components = ['trend']
  1133. if self.holidays is not None and 'holidays' in fcst:
  1134. components.append('holidays')
  1135. components.extend([name for name in self.seasonalities
  1136. if name in fcst])
  1137. if len(self.extra_regressors) > 0 and 'extra_regressors' in fcst:
  1138. components.append('extra_regressors')
  1139. npanel = len(components)
  1140. fig, axes = plt.subplots(npanel, 1, facecolor='w',
  1141. figsize=(9, 3 * npanel))
  1142. if npanel == 1:
  1143. axes = [axes]
  1144. for ax, plot in zip(axes, components):
  1145. if plot == 'trend':
  1146. self.plot_forecast_component(
  1147. fcst, 'trend', ax, uncertainty, plot_cap)
  1148. elif plot == 'holidays':
  1149. self.plot_forecast_component(
  1150. fcst, 'holidays', ax, uncertainty, False)
  1151. elif plot == 'weekly':
  1152. self.plot_weekly(
  1153. ax=ax, uncertainty=uncertainty, weekly_start=weekly_start)
  1154. elif plot == 'yearly':
  1155. self.plot_yearly(
  1156. ax=ax, uncertainty=uncertainty, yearly_start=yearly_start)
  1157. elif plot == 'extra_regressors':
  1158. self.plot_forecast_component(
  1159. fcst, 'extra_regressors', ax, uncertainty, False)
  1160. else:
  1161. self.plot_seasonality(
  1162. name=plot, ax=ax, uncertainty=uncertainty)
  1163. fig.tight_layout()
  1164. return fig
  1165. def plot_forecast_component(
  1166. self, fcst, name, ax=None, uncertainty=True, plot_cap=False):
  1167. """Plot a particular component of the forecast.
  1168. Parameters
  1169. ----------
  1170. fcst: pd.DataFrame output of self.predict.
  1171. name: Name of the component to plot.
  1172. ax: Optional matplotlib Axes to plot on.
  1173. uncertainty: Optional boolean to plot uncertainty intervals.
  1174. plot_cap: Optional boolean indicating if the capacity should be shown
  1175. in the figure, if available.
  1176. Returns
  1177. -------
  1178. a list of matplotlib artists
  1179. """
  1180. artists = []
  1181. if not ax:
  1182. fig = plt.figure(facecolor='w', figsize=(10, 6))
  1183. ax = fig.add_subplot(111)
  1184. fcst_t = fcst['ds'].dt.to_pydatetime()
  1185. artists += ax.plot(fcst_t, fcst[name], ls='-', c='#0072B2')
  1186. if 'cap' in fcst and plot_cap:
  1187. artists += ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
  1188. if self.logistic_floor and 'floor' in fcst and plot_cap:
  1189. ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
  1190. if uncertainty:
  1191. artists += [ax.fill_between(
  1192. fcst_t, fcst[name + '_lower'], fcst[name + '_upper'],
  1193. color='#0072B2', alpha=0.2)]
  1194. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  1195. ax.set_xlabel('ds')
  1196. ax.set_ylabel(name)
  1197. return artists
  1198. def seasonality_plot_df(self, ds):
  1199. """Prepare dataframe for plotting seasonal components.
  1200. Parameters
  1201. ----------
  1202. ds: List of dates for column ds.
  1203. Returns
  1204. -------
  1205. A dataframe with seasonal components on ds.
  1206. """
  1207. df_dict = {'ds': ds, 'cap': 1., 'floor': 0.}
  1208. for name in self.extra_regressors:
  1209. df_dict[name] = 0.
  1210. df = pd.DataFrame(df_dict)
  1211. df = self.setup_dataframe(df)
  1212. return df
  1213. def plot_weekly(self, ax=None, uncertainty=True, weekly_start=0):
  1214. """Plot the weekly component of the forecast.
  1215. Parameters
  1216. ----------
  1217. ax: Optional matplotlib Axes to plot on. One will be created if this
  1218. is not provided.
  1219. uncertainty: Optional boolean to plot uncertainty intervals.
  1220. weekly_start: Optional int specifying the start day of the weekly
  1221. seasonality plot. 0 (default) starts the week on Sunday. 1 shifts
  1222. by 1 day to Monday, and so on.
  1223. Returns
  1224. -------
  1225. a list of matplotlib artists
  1226. """
  1227. artists = []
  1228. if not ax:
  1229. fig = plt.figure(facecolor='w', figsize=(10, 6))
  1230. ax = fig.add_subplot(111)
  1231. # Compute weekly seasonality for a Sun-Sat sequence of dates.
  1232. days = (pd.date_range(start='2017-01-01', periods=7) +
  1233. pd.Timedelta(days=weekly_start))
  1234. df_w = self.seasonality_plot_df(days)
  1235. seas = self.predict_seasonal_components(df_w)
  1236. days = days.weekday_name
  1237. artists += ax.plot(range(len(days)), seas['weekly'], ls='-',
  1238. c='#0072B2')
  1239. if uncertainty:
  1240. artists += [ax.fill_between(range(len(days)),
  1241. seas['weekly_lower'], seas['weekly_upper'],
  1242. color='#0072B2', alpha=0.2)]
  1243. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  1244. ax.set_xticks(range(len(days)))
  1245. ax.set_xticklabels(days)
  1246. ax.set_xlabel('Day of week')
  1247. ax.set_ylabel('weekly')
  1248. return artists
  1249. def plot_yearly(self, ax=None, uncertainty=True, yearly_start=0):
  1250. """Plot the yearly component of the forecast.
  1251. Parameters
  1252. ----------
  1253. ax: Optional matplotlib Axes to plot on. One will be created if
  1254. this is not provided.
  1255. uncertainty: Optional boolean to plot uncertainty intervals.
  1256. yearly_start: Optional int specifying the start day of the yearly
  1257. seasonality plot. 0 (default) starts the year on Jan 1. 1 shifts
  1258. by 1 day to Jan 2, and so on.
  1259. Returns
  1260. -------
  1261. a list of matplotlib artists
  1262. """
  1263. artists = []
  1264. if not ax:
  1265. fig = plt.figure(facecolor='w', figsize=(10, 6))
  1266. ax = fig.add_subplot(111)
  1267. # Compute yearly seasonality for a Jan 1 - Dec 31 sequence of dates.
  1268. days = (pd.date_range(start='2017-01-01', periods=365) +
  1269. pd.Timedelta(days=yearly_start))
  1270. df_y = self.seasonality_plot_df(days)
  1271. seas = self.predict_seasonal_components(df_y)
  1272. artists += ax.plot(
  1273. df_y['ds'].dt.to_pydatetime(), seas['yearly'], ls='-', c='#0072B2')
  1274. if uncertainty:
  1275. artists += [ax.fill_between(
  1276. df_y['ds'].dt.to_pydatetime(), seas['yearly_lower'],
  1277. seas['yearly_upper'], color='#0072B2', alpha=0.2)]
  1278. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  1279. months = MonthLocator(range(1, 13), bymonthday=1, interval=2)
  1280. ax.xaxis.set_major_formatter(FuncFormatter(
  1281. lambda x, pos=None: '{dt:%B} {dt.day}'.format(dt=num2date(x))))
  1282. ax.xaxis.set_major_locator(months)
  1283. ax.set_xlabel('Day of year')
  1284. ax.set_ylabel('yearly')
  1285. return artists
  1286. def plot_seasonality(self, name, ax=None, uncertainty=True):
  1287. """Plot a custom seasonal component.
  1288. Parameters
  1289. ----------
  1290. name: Seasonality name, like 'daily', 'weekly'.
  1291. ax: Optional matplotlib Axes to plot on. One will be created if
  1292. this is not provided.
  1293. uncertainty: Optional boolean to plot uncertainty intervals.
  1294. Returns
  1295. -------
  1296. a list of matplotlib artists
  1297. """
  1298. artists = []
  1299. if not ax:
  1300. fig = plt.figure(facecolor='w', figsize=(10, 6))
  1301. ax = fig.add_subplot(111)
  1302. # Compute seasonality from Jan 1 through a single period.
  1303. start = pd.to_datetime('2017-01-01 0000')
  1304. period = self.seasonalities[name]['period']
  1305. end = start + pd.Timedelta(days=period)
  1306. plot_points = 200
  1307. days = pd.to_datetime(np.linspace(start.value, end.value, plot_points))
  1308. df_y = self.seasonality_plot_df(days)
  1309. seas = self.predict_seasonal_components(df_y)
  1310. artists += ax.plot(df_y['ds'].dt.to_pydatetime(), seas[name], ls='-',
  1311. c='#0072B2')
  1312. if uncertainty:
  1313. artists += [ax.fill_between(
  1314. df_y['ds'].dt.to_pydatetime(), seas[name + '_lower'],
  1315. seas[name + '_upper'], color='#0072B2', alpha=0.2)]
  1316. ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
  1317. xticks = pd.to_datetime(np.linspace(start.value, end.value, 7)
  1318. ).to_pydatetime()
  1319. ax.set_xticks(xticks)
  1320. if period <= 2:
  1321. fmt_str = '{dt:%T}'
  1322. elif period < 14:
  1323. fmt_str = '{dt:%m}/{dt:%d} {dt:%R}'
  1324. else:
  1325. fmt_str = '{dt:%m}/{dt:%d}'
  1326. ax.xaxis.set_major_formatter(FuncFormatter(
  1327. lambda x, pos=None: fmt_str.format(dt=num2date(x))))
  1328. ax.set_xlabel('ds')
  1329. ax.set_ylabel(name)
  1330. return artists
  1331. def copy(self, cutoff=None):
  1332. """Copy Prophet object
  1333. Parameters
  1334. ----------
  1335. cutoff: pd.Timestamp or None, default None.
  1336. cuttoff Timestamp for changepoints member variable.
  1337. changepoints are only retained if 'changepoints <= cutoff'
  1338. Returns
  1339. -------
  1340. Prophet class object with the same parameter with model variable
  1341. """
  1342. if self.history is None:
  1343. raise Exception('This is for copying a fitted Prophet object.')
  1344. if self.specified_changepoints:
  1345. changepoints = self.changepoints
  1346. if cutoff is not None:
  1347. # Filter change points '<= cutoff'
  1348. changepoints = changepoints[changepoints <= cutoff]
  1349. else:
  1350. changepoints = None
  1351. # Auto seasonalities are set to False because they are already set in
  1352. # self.seasonalities.
  1353. m = Prophet(
  1354. growth=self.growth,
  1355. n_changepoints=self.n_changepoints,
  1356. changepoints=changepoints,
  1357. yearly_seasonality=False,
  1358. weekly_seasonality=False,
  1359. daily_seasonality=False,
  1360. holidays=self.holidays,
  1361. seasonality_prior_scale=self.seasonality_prior_scale,
  1362. changepoint_prior_scale=self.changepoint_prior_scale,
  1363. holidays_prior_scale=self.holidays_prior_scale,
  1364. mcmc_samples=self.mcmc_samples,
  1365. interval_width=self.interval_width,
  1366. uncertainty_samples=self.uncertainty_samples,
  1367. )
  1368. m.extra_regressors = deepcopy(self.extra_regressors)
  1369. m.seasonalities = deepcopy(self.seasonalities)
  1370. return m