{"id":6358,"date":"2018-03-14T08:26:02","date_gmt":"2018-03-14T12:26:02","guid":{"rendered":"https:\/\/demand-planning.com\/?p=6358"},"modified":"2018-10-03T10:30:15","modified_gmt":"2018-10-03T14:30:15","slug":"forecasting-with-facebooks-prophet","status":"publish","type":"post","link":"https:\/\/demand-planning.com\/2018\/03\/14\/forecasting-with-facebooks-prophet\/","title":{"rendered":"How To Use Facebook\u2019s Prophet"},"content":{"rendered":"<span class=\"cb-itemprop\" itemprop=\"reviewBody\"><p><strong>Working in a SME with limited resources, the kind of sophisticated forecasting tools used by the major multinationals can seem far out of reach. For people in smaller companies like mine, the abundance of free to use, open-sourced, state of the art software like Facebook Prophet offers access to game-changing functionality.<\/strong><\/p>\n<p>The last couple of years have seen several major internet names open sourcing powerful predictive analytic API\u2019s, making them free to use for developers and professionals. Google\u2019s TensorFlow deep-learning library is probably the most widely used and influential library, but there are many more libraries that provide valuable functionality for <a href=\"https:\/\/ibf.org\/knowledge\/glossary\/demand-planner-78\">Demand Planners<\/a> and Purchasers that don\u2019t require the kind of GPU computing power that deep-learning requires.<\/p>\n<h2>What Is Facebook Prophet?<\/h2>\n<p>Facebook describe the software as\u00a0&#8220;a procedure for forecasting time series data. It is based on an additive model where non-linear trends are fit with yearly and weekly seasonality, plus holidays. It works best with daily periodicity data with at least one year of historical data. Prophet is robust to missing data, shifts in the trend, and large outliers.\u201d<\/p>\n<p>Prophet was designed to tackle the problem that quality forecasts are required faster than analysts can produce them, while automated forecasting techniques are too inflexible to incorporate useful assumptions or rules gleaned from experience. In a business context we\u2019ve all seen automatically generated forecasts that don\u2019t factor in change points in demand such as market breakout for a booming trend of the slump of a major customer moving to a competitor. At the same time, with thousands or tens of thousands of <a href=\"https:\/\/ibf.org\/knowledge\/glossary\/sku-stock-keeping-unit-256\">SKU\u2019s<\/a> to monitor, we know that finding highly skilled analysts to complete the workload consistently and rapidly can be a major challenge.<\/p>\n<h2>Why Use Facebook Prophet For Forecasting?<\/h2>\n<p>Firstly, Prophet is stupidly easy to use and generates reasonable results without having to worry about choosing between models and tuning hyperparameters.<\/p>\n<p>Secondly, Prophets parameters allow for customization in ways that make sense to a non-expert and in a business context, such as the ability to inject S&amp;OP information about how the forecast will likely change, the ability to set caps on possible demand based on experience and market knowledge, and the ability to model irregular holidays like Chinese New Year or Easter.<\/p>\n<p>As a keen Pythonista, one of the best things for me about Prophet is that it can be used in Python and is easily installed from either pip or conda. Generally, R has had the edge over Python for time series regression problems. The <a href=\"https:\/\/ibf.org\/knowledge\/glossary\/arima-22\">auto.arima<\/a> function in R is hard to beat for ease of use and accuracy of results. R also has some recent additions for dealing with time series problems \u2013 CausalImpact from Google which identifies the causal effects of things like marketing campaigns on sales, and AnomalyDetection\/BreakoutDetection from Twitter, that help identify anomalies and shifts in trends. Facebook Prophet is therefore a very welcome addition to the Python ecosystem.<\/p>\n<h2>What is Facebook Prophet Optimized To Solve?<\/h2>\n<p>Prophet was designed by Facebook, so it\u2019s well suited to regularly spaced timeseries observations and works best with at least a year\u2019s observations to catch seasonal trends. It has a very useful function for incorporating national holidays, which depending on your business might represent peaks (television ratings during holidays) or dips (stores closed or open half-days on national holidays). It also has a useful parameter called a \u2018changepoint\u2019 which enables you to specify a point after which demand is likely to change, such as the launch of a competitor\u2019s product or a major television campaign.<\/p>\n<p>One important note is that Prophet is an additive regression model built up from trend, annual seasonality, weekly seasonality, and a user-specified holiday list. If you\u2019re concerned that your model might be inherently multiplicative in nature, then it might be worth log-transforming the data and then using the inverse log to normalize the predictions.<\/p>\n<p>I should also make clear from the start that as impressive as Prophet is, you can get better results by stacking the model in an ensemble of various techniques if you have the computing power to do so. On my laptop (and my work desktop), it would take several days to fit a very sophisticated ensemble model, whereas Prophet is able to do a reasonable job on all 3000 <a href=\"https:\/\/ibf.org\/knowledge\/glossary\/sku-stock-keeping-unit-256\">SKU\u2019s<\/a> for my current company in a matter of minutes, so there is a trade-off of accuracy against computational\/time cost.<\/p>\n<h2>Installing Facebook Prophet in Python<\/h2>\n<p>Prophet can be installed very easily in Python, either through pip or through conda install. I used the conda installation which also loads all the dependencies and is very convenient: <a href=\"https:\/\/anaconda.org\/conda-forge\/fbprophet\">https:\/\/anaconda.org\/conda-forge\/fbprophet<\/a><\/p>\n<div id=\"attachment_6365\" style=\"width: 411px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6365\" class=\"size-full wp-image-6365\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Installing-Prophet-in-Python.jpg.png\" alt=\"Installing Prophet in Python\" width=\"401\" height=\"280\" srcset=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Installing-Prophet-in-Python.jpg.png 401w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Installing-Prophet-in-Python.jpg-300x209.png 300w\" sizes=\"(max-width: 401px) 100vw, 401px\" \/><p id=\"caption-attachment-6365\" class=\"wp-caption-text\">Installing Prophet in Python is straightforward.<\/p><\/div>\n<h2>Preparing Facebook&#8217;s Prophet Datasets<\/h2>\n<p>Prophet accepts the primary dataset of time series data and an optional list of holidays. I read these with this into Python with Pandas read_csv function passing parse_dates=True. If you\u2019re unfamiliar with the Pandas commands, you can gain a quick understanding with the excellent 10 minutes to Pandas guide here: <a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/10min.html\">https:\/\/pandas.pydata.org\/pandas-docs\/stable\/10min.html<\/a><\/p>\n<p>The Prophet documentation shows that the variables for the primary dataset should be labelled \u2018ds\u2019 for the time series and \u2018y\u2019 for the variable. The holiday list should also be labelled \u2018ds\u2019 for the time series and \u2018holiday\u2019 for the list of notable events. Time series should be sorted and formatted as datetime datatype. This is easily done inside the workflow. I\u2019ve take a single line of crystal glass tableware as an example having already sliced the dataframe down to one <a href=\"https:\/\/ibf.org\/knowledge\/glossary\/sku-stock-keeping-unit-256\">SKU<\/a>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6367\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting.jpg\" alt=\"Facebook prophet forecasting\" width=\"289\" height=\"294\" \/><\/p>\n<h2>Setting Up The Process<\/h2>\n<p>I tend to import a range of packages to perform basic exploratory data analysis, but the only essential packages for this will be pandas and fbprophet.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6368\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting-2.jpg\" alt=\"Facebook prophet forecasting\" width=\"251\" height=\"105\" \/><\/p>\n<p>Here\u2019s a quick plot of the time series:<\/p>\n<div id=\"attachment_6369\" style=\"width: 998px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6369\" class=\"wp-image-6369 size-full\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-time-series.jpg\" alt=\"Facebook Prophet time series\" width=\"988\" height=\"475\" srcset=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-time-series.jpg 988w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-time-series-300x144.jpg 300w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-time-series-768x369.jpg 768w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-time-series-600x288.jpg 600w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-time-series-800x385.jpg 800w\" sizes=\"(max-width: 988px) 100vw, 988px\" \/><p id=\"caption-attachment-6369\" class=\"wp-caption-text\">Facebook Prophet time series<\/p><\/div>\n<p>Eyeballing the data, you should notice an increase in both frequency and volume of orders in a regular annual cycle with perhaps a faint downward trend over the three years.<\/p>\n<p>Anyone familiar with sci-kit learns fit_predict\/fit_transform methods will find Prophet follows a very similar pattern.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6370 size-full\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting-3.jpg\" alt=\"Facebook prophet forecasting \" width=\"503\" height=\"100\" srcset=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting-3.jpg 503w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting-3-300x60.jpg 300w\" sizes=\"(max-width: 503px) 100vw, 503px\" \/><\/p>\n<p>Here I instantiate the model with an uncertainty window of 95% (Prophet defaults to 80% even though 95% is normally standard in many business fields). I feed it my holiday list as a parameter, and then fit the model to my filtered dataframe (Ndf). I then project a future dataframe of around 3 months using Prophets \u2018make_future_dataframe\u2019 function.<\/p>\n<p>Once the model is fit. All that remains to do is to predict the model over the future dates and have a look at the dataframe to insanity check the results.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6371\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting-4.jpg.png\" alt=\"Facebook prophet forecasting \" width=\"517\" height=\"93\" srcset=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting-4.jpg.png 517w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-forecasting-4.jpg-300x54.png 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/p>\n<p>Then using the model.plot(forecast) we can have a look at the fit and projected values:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6372\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-seasonal-patterns.jpg\" alt=\"Facebook prophet seasonal patterns\" width=\"838\" height=\"446\" srcset=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-seasonal-patterns.jpg 838w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-seasonal-patterns-300x160.jpg 300w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-seasonal-patterns-768x409.jpg 768w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-seasonal-patterns-600x319.jpg 600w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-prophet-seasonal-patterns-800x426.jpg 800w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/p>\n<p>As you can see the model has done an excellent job in finding the seasonal pattern and correctly identified the downward trend over the last three years. One of the best features of Prophet is that it will return the model components. Here we can see the overall trend and holidays have been isolated:<\/p>\n<div id=\"attachment_6373\" style=\"width: 744px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6373\" class=\"wp-image-6373 size-full\" src=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-model-components.jpg\" alt=\"Facebook prophet seasonal patterns\" width=\"734\" height=\"473\" srcset=\"https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-model-components.jpg 734w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-model-components-300x193.jpg 300w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-model-components-100x65.jpg 100w, https:\/\/demand-planning.com\/wp-content\/uploads\/2018\/03\/Facebook-Prophet-model-components-600x387.jpg 600w\" sizes=\"(max-width: 734px) 100vw, 734px\" \/><p id=\"caption-attachment-6373\" class=\"wp-caption-text\">Facebook prophet seasonal patterns<\/p><\/div>\n<p>And the weekly and annual seasonality analysis is a very good fit for my experience in the table top HORECA trade, with annual demand peaking slightly at the start of summer and rising to a peak in the lead up to Christmas when parties fill up the hotels, restaurants and bars.<\/p>\n<p>As you can also see, we close for weekends, so not so many invoices raised then. The busiest weekday is a Wendesday.<\/p>\n<h2>Final Thoughts On Facebook Prophet<\/h2>\n<p>With very little coding and without setting any of the numerous other hyperparameters, Prophet did an excellent job on the time series, despite the large number of outliers in the data, achieving a coefficient of determination of 0.84.<\/p>\n<p>There are certainly many advantages for an SME purchaser or demand planner considering forecasting with Prophet:<\/p>\n<p>The software is free to use, open-source code and is ridiculously easy to deploy.<\/p>\n<p>Prophet is extremely quick, taking only a few seconds even on my now badly outdated laptop \u2013 more advanced neural networks are notorious for requiring multiple GPU\u2019s or burning out the CPU\u2019s of machine\u2019s after several days running.<\/p>\n<p>The optional hyperparameters as intuitive even to the less technically minded demand planner.<\/p>\n<p>The predictions are returned with a confidence interval around the forecast, which can often be more useful that the predicted value itself when making decisions about stock levels.<\/p>\n<p>All in all, R still has the edge when it comes to comprehensively tackling time series regression tasks, but if you\u2019re a Pythonista working in demand planning and you want to upgrade your forecasting accuracy, then I\u2019d strongly recommend Prophet as a tool to consider.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"<p>Working in a SME with limited resources, the kind of sophisticated forecasting tools used by the major multinationals can seem far out of reach. For people in smaller companies like mine, the abundance of free to use, open-sourced, state of the art software like Facebook Prophet offers access to game-changing functionality. The last couple of [&hellip;]<\/p>\n","protected":false},"author":5255,"featured_media":6361,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,393,391,335],"tags":[373],"class_list":{"0":"post-6358","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-forecasting-and-planning","8":"category-how-to-guides-forecasting","9":"category-software","10":"category-technology","11":"tag-facebook-prophet"},"_links":{"self":[{"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/posts\/6358"}],"collection":[{"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/users\/5255"}],"replies":[{"embeddable":true,"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/comments?post=6358"}],"version-history":[{"count":0,"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/posts\/6358\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/media\/6361"}],"wp:attachment":[{"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/media?parent=6358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/categories?post=6358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demand-planning.com\/wp-json\/wp\/v2\/tags?post=6358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}