forecaster.py 55 KB

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