Time Series Analysis using Facebook Prophet
Last Updated :
06 Aug, 2025
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.
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:
- 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.
- 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.
- 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.
- 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).Ā
- Logistic Growth ModelĀ
- 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
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
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 DataThe 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
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.
Similar Reads
Data Science Tutorial Data Science is a field that combines statistics, machine learning and data visualization to extract meaningful insights from vast amounts of raw data and make informed decisions, helping businesses and industries to optimize their operations and predict future trends.This Data Science tutorial offe
3 min read
Introduction to Machine Learning
What is Data Science?Data science is the study of data that helps us derive useful insight for business decision making. Data Science is all about using tools, techniques, and creativity to uncover insights hidden within data. It combines math, computer science, and domain expertise to tackle real-world challenges in a
8 min read
Top 25 Python Libraries for Data Science in 2025Data Science continues to evolve with new challenges and innovations. In 2025, the role of Python has only grown stronger as it powers data science workflows. It will remain the dominant programming language in the field of data science. Its extensive ecosystem of libraries makes data manipulation,
10 min read
Difference between Structured, Semi-structured and Unstructured dataBig Data includes huge volume, high velocity, and extensible variety of data. There are 3 types: Structured data, Semi-structured data, and Unstructured data. Structured data - Structured data is data whose elements are addressable for effective analysis. It has been organized into a formatted repos
2 min read
Types of Machine LearningMachine learning is the branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data and improve from previous experience without being explicitly programmed for every task.In simple words, ML teaches the systems to think and understand like h
13 min read
What's Data Science Pipeline?Data Science is a field that focuses on extracting knowledge from data sets that are huge in amount. It includes preparing data, doing analysis and presenting findings to make informed decisions in an organization. A pipeline in data science is a set of actions which changes the raw data from variou
3 min read
Applications of Data ScienceData Science is the deep study of a large quantity of data, which involves extracting some meaning from the raw, structured, and unstructured data. Extracting meaningful data from large amounts usesalgorithms processing of data and this processing can be done using statistical techniques and algorit
6 min read
Python for Machine Learning
Learn Data Science Tutorial With PythonData Science has become one of the fastest-growing fields in recent years, helping organizations to make informed decisions, solve problems and understand human behavior. As the volume of data grows so does the demand for skilled data scientists. The most common languages used for data science are P
3 min read
Pandas TutorialPandas is an open-source software library designed for data manipulation and analysis. It provides data structures like series and DataFrames to easily clean, transform and analyze large datasets and integrates with other Python libraries, such as NumPy and Matplotlib. It offers functions for data t
6 min read
NumPy Tutorial - Python LibraryNumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Scikit Learn TutorialScikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
3 min read
ML | Data Preprocessing in PythonData preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
6 min read
EDA - Exploratory Data Analysis in PythonExploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
6 min read
Introduction to Statistics
Statistics For Data ScienceStatistics is like a toolkit we use to understand and make sense of information. It helps us collect, organize, analyze and interpret data to find patterns, trends and relationships in the world around us.From analyzing scientific experiments to making informed business decisions, statistics plays a
12 min read
Descriptive StatisticStatistics is the foundation of data science. Descriptive statistics are simple tools that help us understand and summarize data. They show the basic features of a dataset, like the average, highest and lowest values and how spread out the numbers are. It's the first step in making sense of informat
5 min read
What is Inferential Statistics?Inferential statistics is an important tool that allows us to make predictions and conclusions about a population based on sample data. Unlike descriptive statistics, which only summarize data, inferential statistics let us test hypotheses, make estimates, and measure the uncertainty about our predi
7 min read
Bayes' TheoremBayes' Theorem is a mathematical formula used to determine the conditional probability of an event based on prior knowledge and new evidence. It adjusts probabilities when new information comes in and helps make better decisions in uncertain situations.Bayes' Theorem helps us update probabilities ba
13 min read
Probability Data Distributions in Data ScienceUnderstanding how data behaves is one of the first steps in data science. Before we dive into building models or running analysis, we need to understand how the values in our dataset are spread out and thatâs where probability distributions come in.Let us start with a simple example: If you roll a f
8 min read
Parametric Methods in StatisticsParametric statistical methods are those that make assumptions regarding the distribution of the population. These methods presume that the data have a known distribution (e.g., normal, binomial, Poisson) and rely on parameters (e.g., mean and variance) to define the data.Key AssumptionsParametric t
6 min read
Non-Parametric TestsNon-parametric tests are applied in hypothesis testing when the data does not satisfy the assumptions necessary for parametric tests, such as normality or equal variances. These tests are especially helpful for analyzing ordinal data, small sample sizes, or data with outliers.Common Non-Parametric T
5 min read
Hypothesis TestingHypothesis testing compares two opposite ideas about a group of people or things and uses data from a small part of that group (a sample) to decide which idea is more likely true. We collect and study the sample data to check if the claim is correct.Hypothesis TestingFor example, if a company says i
9 min read
ANOVA for Data Science and Data AnalyticsANOVA is useful when we need to compare more than two groups and determine whether their means are significantly different. Suppose you're trying to understand which ingredients in a recipe affect its taste. Some ingredients, like spices might have a strong influence while others like a pinch of sal
9 min read
Bayesian Statistics & ProbabilityBayesian statistics sees unknown values as things that can change and updates what we believe about them whenever we get new information. It uses Bayesâ Theorem to combine what we already know with new data to get better estimates. In simple words, it means changing our initial guesses based on the
6 min read
Feature Engineering
Model Evaluation and Tuning
Data Science Practice