Predict Future Prices Using Facebook Prophet

Mohd Saquib
The Wisdom
Published in
4 min readJun 16, 2020

Objective-

In this case study, we will understand the theory and intuition behind Facebook Prophet time series prediction tools.

We will predict the future prices of avocados using Facebook Prophet.

Prophet is an open-source tool used for time-series forecasting.

Wait! What is Facebook Prophet??

Don’t give me that look :D

I will explain when we will apply this model to our data set. First, let just prepare our data because without credible data what the use of the model?

Dataset —

The dataset is taken from Hass Avocado Board website in May of 2018 & compiled into a single CSV. Here’s how the Hass Avocado Board describes the data on their website:

Some relevant columns in the dataset:

  • Date - The date of the observation
  • AveragePrice - the average price of a single avocado
  • type - conventional or organic
  • year - the year
  • Region - the city or region of the observation
  • Total Volume - Total number of avocados sold
  • 4046 - Total number of avocados with PLU 4046 sold
  • 4225 - Total number of avocados with PLU 4225 sold
  • 4770 - Total number of avocados with PLU 4770 sold

The dataset is also available on Kaggle-https://www.kaggle.com/neuromusic/avocado-prices

Import Libraries and Dataset

# import libraries 
import pandas as pd # Import Pandas for data manipulation using dataframes
import numpy as np # Import Numpy for data statistical analysis
import matplotlib.pyplot as plt # Import matplotlib for data vis
import random
import seaborn as sns
from fbprophet import Prophet

Reading the data using Pandas

# dataframes creation for both training and testing datasets 
avocado_df = pd.read_csv('../input/avocado-prices/avocado.csv')

Let's get the head of Dataset

# Let's view the head of the training dataset
avocado_df.head()

Now let's perform some Preprocessing

We will only consider only two features — ‘Date’ and ‘AveragePrice’ as our objective is to predict prices.

avocado_prophet_df = avocado_df[['Date', 'AveragePrice']]avocado_prophet_df

Now let’s rename these columns —

avocado_prophet_df.rename(columns={'Date':'ds','AveragePrice' : 'y'},inplace='true')avocado_prophet_df

So now we are ready to use the Facebook Prophet Model to predict the prices of avocados.

Understanding the intuition behind Facebook Prophet

  • The prophet is open source software released by Facebook’s Core Data Science team.
  • The prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects.
  • Prophet works best with time series that have strong seasonal effects and several seasons of historical data.
  • Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.

Accurate and fast.

Prophet is used in many applications across Facebook for producing reliable forecasts for planning and goal setting. They have found it to perform better than any other approach in the majority of cases. We fit models in Stan so that you get forecasts in just a few seconds.

Fully automatic.

Get a reasonable forecast on messy data with no manual effort. Prophet is robust to outliers, missing data, and dramatic changes in your time series.

Tunable forecasts.

The Prophet procedure includes many possibilities for users to tweak and adjust forecasts. You can use human-interpretable parameters to improve your forecast by adding your domain knowledge.

Available in both R or Python.

For more details visit — https://facebook.github.io/prophet

Now let us develop the model and make Prediction

m = Prophet()
m.fit(avocado_prophet_df)

Forecasting into the Future

# Forcasting into the future
future = m.make_future_dataframe(periods = 365)
forecast = m.predict(future)

Plotting the Prediction

figure = m.plot(forecast, xlabel='Date', ylabel='Price')

We have successfully predicted the price of avocados for the next year for all the regions.

If you want to see the forecast components, you can use the Prophet.plot_components method. By default, you’ll see the trend and yearly seasonality of the time series.

figure2 = m.plot_components(forecast)

We can observe from above figures that the prices of avocado for the next year, range between 1 $ and 1.5$, with a maximum price in 2018–8 and after that the prices again drop near to 1$.

Great Job! We have successfully predicted future prices, so now what next? Buy some stocks and predict their prices and become a billionaire? :D

Of course, I am joking predicting stocks is not that easy (it is possible). Anyways if you enjoy this blog, or find it useful don’t forget to give a clap.

--

--

Mohd Saquib
The Wisdom

“The goal is to turn data into information, and information into insight.”