Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python
  • R Language
  • Python for Data Science
  • NumPy
  • Pandas
  • OpenCV
  • Data Analysis
  • ML Math
  • Machine Learning
  • NLP
  • Deep Learning
  • Deep Learning Interview Questions
  • Machine Learning
  • ML Projects
  • ML Interview Questions
Open In App
Next Article:
Time Series Analysis using Facebook Prophet
Next article icon

Time Series Analysis using Facebook Prophet

Last Updated : 21 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Time series analysis is one of the important methodologies that helps us to understand the hidden patterns in a dataset that is too related to the time at which it is being recorded. The article aims to explore the fundamentals of time series analysis and demonstrates the analysis using Facebook Prophet.

Table of Content

  • What is Time Series Analysis?
  • Facebook Prophet Library
  • Understanding the Prophet Model 
  • Need of Facebook Prophet
  • Implementation - Analyzing Time Series Data using Prophet

What is Time Series Analysis?

Time series analysis is a statistical approach that entails gathering data at consistent intervals to recognize patterns and trends. This methodology is employed for making well-informed decisions and precise forecasts by leveraging insights derived from historical data.

And the process of predicting the future values of the data by analyzing the previous trends and patterns hidden in the data is known as time series forecasting. Time series forecasting can be done using various forecasting techniques like ARIMA, SARIMA, Prophet, Theta and other statistical method. Time series data is a sequential data hence, deep learning-based methods like RNN, LSTM, BLSTM and GRU are also used for time series forecasting.

Key components of Time Series analysis

The key components of time series analysis include:

  1. Time series data commonly displays temporal relationships, meaning that the values at a particular moment are impacted by preceding values. Grasping these dependencies is essential for accurate predictions and gaining valuable insights.
  2. Trend analysis involves recognizing the fundamental direction or long-term movement in a time series, whether it is upward, downward, or stable. This understanding provides insights into the overall trajectory of the data.
  3. Seasonal patterns, observed in numerous time series, depict recurring cycles at fixed intervals, such as daily, monthly, quarterly, or yearly. Analyzing these patterns through seasonal analysis helps identify and comprehend their periodic nature.
  4. Time series data often incorporates random fluctuations or noise that lacks a specific trend or pattern. It is crucial to eliminate this noise to unveil meaningful information within the data.

Facebook Prophet Library

Prophet is an open-source tool from Facebook used for forecasting time series data which helps businesses understand and possibly predict the market. It is based on a decomposable additive model where non-linear trends fit with seasonality, it also takes into account the effects of holidays. Before we head right into coding, let's learn certain terms that are required to understand this. 

  • Trend: The trend shows the tendency of the data to increase or decrease over a long period of time and it filters out the seasonal variations. 
  • Seasonality: Seasonality is the variations that occur over a short period of time and is not prominent enough to be called a "trend". 

Understanding the Prophet Model 

The general idea of the model is similar to a generalized additive model. The "Prophet Equation" fits, as mentioned above, trends, seasonality, and holidays. This is given by,

y(t) = g(t) + s(t) + h(t) + e(t)

here,

  • g(t) refers to trend (changes over a long period of time)
  • s(t) refers to seasonality (periodic or short-term changes)
  • h(t) refers to effects of holidays to the forecast
  • e(t) refers to the unconditional changes that is specific to a business or a person or a circumstance. It is also called the error term.
  • y(t) is the forecast.

Need of Facebook Prophet

We need it because, although the basic decomposable additive model looks simply, the calculation of the terms within is hugely mathematical. If you do not know what you are doing, it may lead to making wrong forecasts, which might have severe repercussions in the real world. So, to automate this process, we are going to use Prophet. However, to understand the math behind this process and how Prophet actually works, let's see how it forecasts the data. 

Prophet provides us with two models (however, newer models can be written or extended according to specific requirements). 

  1. Logistic Growth Model 
  2. Piece-Wise Linear Model

By default, Prophet uses a piece-wise linear model, but it can be changed by specifying the model. Choosing a model is delicate as it is dependent on a variety of factors such as company size, growth rate, business model, etc., If the data to be forecasted, has saturating and non-linear data (grows non-linearly and after reaching the saturation point, shows little to no growth or shrink and only exhibits some seasonal changes), then logistic growth model is the best option. Nevertheless, if the data shows linear properties and had growth or shrink trends in the past then, the piece-wise linear model is a better choice. The logistic growth model is fit using the following statistical equation, 

g(t) = \frac{C}{1+ e^{-k(t-m)}}

where,

  • C is the carrying capacity
  • k is the growth rate
  • m is an offset parameter

The piece-wise linear model is fit using the following statistical equations,  

y = \begin{cases} \beta_{0} + \beta_{1} x, & \text{if } x \leq c, \\ \beta_{0} - \beta_{2} c + (\beta_{1} + \beta_{2}) x, & \text{if } x > c. \end{cases}

where c is the trend change point (it defines the change in the trend).? is the trend parameter and can be tuned as per requirement for forecasting.  

Implementation - Analyzing Time Series Data using Prophet

Now let's try and build a model that is going to forecast the number of passengers for the next five years using time series analysis.

Installing Prophet and Other Dependencies

Install pandas for data manipulation and for the dataframe data structure.

!pip install pandas

Install Prophet for time series analysis and forecasting.

!pip install prophet

Importing Required Libraries

Python
import pandas as pd from prophet import Prophet from prophet.plot import add_changepoints_to_plot 


Loading Air Passenger Dataset

Now let's load the csv file in the pandas data frame. The dataset contains the number of air passengers in the USA from January 1949 to December 1960. The frequency of the data is 1 month.

Python
url = ("https://raw.githubusercontent.com/rahulhegde99"       "/Time-Series-Analysis-and-Forecasting-of-Air-Passengers"         "/master/airpassengers.csv") data = pd.read_csv(url) data.head() 

Output:

     Month  #Passengers
0 1949-01 112
1 1949-02 118
2 1949-03 132
3 1949-04 129
4 1949-05 121


Facebook Prophet predicts data only when it is in a certain format. The dataframe with the data should have a column saved as ds for time series data and y for the data to be forecasted. Here, the time series is the column Month and the data to be forecasted is the column #Passengers. So, let's make a new DataFrame with new column names and the same data. Also, ds should be in a DateTime format.

Python
df = pd.DataFrame() df['ds'] = pd.to_datetime(data['Month']) df['y'] = data['#Passengers'] df.head() 

Output:

          ds    y
0 1949-01-01 112
1 1949-02-01 118
2 1949-03-01 132
3 1949-04-01 129
4 1949-05-01 121

Initializing a Prophet Model

By using the Prophet() command we can initialize an instance of the fbprophet model for the training on our dataset and then help us to perform time series forecasting.

Python
m = Prophet() m.fit(df) 


We want our model to predict the next 5 years, that is, till 1965. The frequency of our data is 1 month and thus for 5 years, it is 12 * 5 = 60 months. So, we need to add 60 to more rows of monthly data to a dataframe.

Python
future = m.make_future_dataframe(periods=12 * 5,                                  freq='M') 


Now in the future dataframe we have just ds values, and we should predict the y values. 

Python
forecast = m.predict(future) forecast[['ds', 'yhat', 'yhat_lower',           'yhat_upper', 'trend',           'trend_lower', 'trend_upper']].tail() 

Output:

            ds        yhat  yhat_lower  yhat_upper       trend  trend_lower  \
199 1965-07-31 723.847886 695.131427 753.432671 656.874802 649.871409
200 1965-08-31 677.972773 649.074148 707.203918 660.006451 652.869703
201 1965-09-30 640.723643 612.025440 670.377970 663.037079 655.722042
202 1965-10-31 610.965273 580.772823 641.770085 666.168728 658.710202
203 1965-11-30 640.594175 611.016447 669.582208 669.199356 661.513728

trend_upper
199 663.255178
200 666.675498
201 669.917580
202 673.241446
203 676.479915

Plotting the Forecast Data

Table ds, as we know, is the time series data. yhat is the prediction, yhat_lower, and yhat_upper are the uncertainty levels (it basically means the prediction and actual values can vary within the bounds of the uncertainty levels). Next up we have a trend that shows the long-term growth, shrink, or stagnancy of the data, trend_lower, and trend_upper is the uncertainty levels.

Python
fig1 = m.plot(forecast, include_legend=True) 

Output:


Time Series Forecasting-Geeksforgeeks
Time Series Forecasting


The below image shows the basic prediction. The light blue is the uncertainty level(yhat_upper and yhat_lower), the dark blue is the prediction(yhat) and the black dots are the original data. We can see that the predicted data is very close to the actual data. In the last five years, there is no "actual" data, but looking at the performance of our model in years where data is available it is safe to say that the predictions are close to accurate.

Python
fig2 = m.plot_components(forecast) 

Output:

Trends and Seasonality in Time Series Data
Trends and Seasonality in Time Series Data

The below images show the trends and seasonality (in a year) of the time series data. We can see there is an increasing trend, meaning the number of air passengers has increased over time. If we look at the seasonality graph, we can see that June and July is the time with the most passengers in a given year.

Python
fig = m.plot(forecast) a = add_changepoints_to_plot(fig.gca(),                              m, forecast) 

Output:


Forecasting using Prophet-Geeksforgeeks
Forecasting using Prophet



Add changepoints to indicate the time in rapid trend growths. The dotted red lines show the time when there was a rapid change in the trend of the passengers. Thus, we have seen how we can design a prediction model using Facebook Prophet with only a few lines of code which would have been very difficult to implement using traditional machine learning algorithms and mathematical and statistical concepts alone.



Next Article
Time Series Analysis using Facebook Prophet

R

rahulhegde97
Improve
Article Tags :
  • Data Science
  • Programming Language
  • Machine Learning
  • Write From Home
  • data mining
  • data-science
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    Time Series Analysis using Facebook Prophet in R Programming
    Time Series Analysis is a way of analysing and learning the behaviour of datasets over a period. Moreover, it helps in learning the behavior of the dataset by plotting the time series object on the graph. In R programming, it can be easily performed by using ts() function that takes the data vector
    3 min read
    Time Series Analysis and Forecasting
    Time series analysis and forecasting are crucial for predicting future trends, behaviors, and behaviours based on historical data. It helps businesses make informed decisions, optimize resources, and mitigate risks by anticipating market demand, sales fluctuations, stock prices, and more. Additional
    15+ min read
    Share Price Forecasting Using Facebook Prophet
    Time series forecast can be used in a wide variety of applications such as Budget Forecasting, Stock Market Analysis, etc. But as useful it is also challenging to forecast the correct projections, Thus can't be easily automated because of the underlying assumptions and factors. The analysts who prod
    6 min read
    Project Idea | Personality Analysis using hashtags from tweets
    Introduction The project is based on the concept of analyzing the personality of an individual, and plotting the sentiments of tweets according to user/community location.Our application would consider the actual mentality of the individuals along with the situation and topic and assess their person
    5 min read
    Time Series Datasets
    Time series datasets are a crucial component of data science and analytics, especially in fields where understanding trends, patterns, and temporal dynamics is essential. A time series is a sequence of data points collected or recorded at specific time intervals. These datasets are omnipresent acros
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences