Time series with python
Time series with python
1 of 20 3/15/2025, 2:21 PM
Introduction
In this lecture we will cover the following topics:
Basics
3 of 20 3/15/2025, 2:21 PM
Introduction to time series analysis — Time series analysis with Python https://filippomb.github.io/python-time-series-handbook/notebooks/01...
1. To understand and characterize the underlying process that generates the observed
data.
2. To forecast the evolution of the process, i.e., predict the next observed values.
4 of 20 3/15/2025, 2:21 PM
Introduction to time series analysis — Time series analysis with Python https://filippomb.github.io/python-time-series-handbook/notebooks/01...
Statistics perspective
A time series is a sequence of random variables that have some correlation or other
distributional relationship between them.
The sequence is a realization (observed values) of a stochastic process.
Statistical time series approaches focus on finding the parameters of the stochastic
process that most likely produced the observed time series.
5 of 20 3/15/2025, 2:21 PM
Introduction to time series analysis — Time series analysis with Python https://filippomb.github.io/python-time-series-handbook/notebooks/01...
Applications
Time series analysis is applied in many real world applications, including
Economic forecasting
Stock market analysis
Demand planning and forecasting
Anomaly detection
… And much more
Economic Forecasting
Demand forecasting
6 of 20 3/15/2025, 2:21 PM
Anomaly detection
Trend
Trend captures the general direction of the time series.
For example, increasing number of passengers over the years despite seasonal
fluctuations.
Trend can be increasing, decreasing, or constant.
It can increase/decrease in different ways over time (linearly, exponentially, etc…).
time = np.arange(144)
trend = time * 2.65 +100
Seasonality
Periodic fluctuations in time series data that occur at regular intervals due to seasonal
factors.
It is characterized by consistent and predictable patterns over a specific period (e.g.,
daily, monthly, quarterly, yearly).
Residuals
Residuals are the random fluctuations left over after trend and seasonality are
removed from the original time series.
One should not see a trend or seasonal pattern in the residuals.
They represent short term, rather unpredictable fluctuations.
Decomposition Models
Time series components can be decomposed with the following models:
1. Additive decomposition
2. Multiplicative decomposition
3. Pseudoadditive decomposition
Additive model
Additive models assume that the observed time series is the sum of its components:
where
is the time series
is the trend
is the seasonality
is the residual
Additive models are used when the magnitudes of the seasonal and residual values
do not depend on the level of the trend.
Multiplicative Model
Assumes that the observed time series is the product of its components:
Multiplicative models are used when the magnitudes of seasonal and residual values
depends on trend.
multiplicative = trend * seasonal # we do not include the residuals to make the pattern m
Pseudoadditive Model
Pseudoadditive models combine elements of the additive and multiplicative models.
Useful when:
Time series values are close to or equal to zero. Multiplicative models struggle
with zero values, but you still need to model multiplicative seasonality.
Some features are multiplicative (e.g., seasonal effects) and other are additive
(e.g., residuals).
Complex seasonal patterns or data that do not completely align with additive or
multiplicative model.
For example, this model is particularly relevant for modeling series that:
are extremely weather-dependent,
have sharply pronounced seasonal fluctuations and trend-cycle movements.
Formulation:
Next, we will use seasonal_decompose (more information here) to isolate the main
time series components.
This is a simple method that requires us to specify the type of model (additive or
multiplicative) and the main period.
Additive Decomposition
We need to specify an integer that represents the main seasonality of the data.
By looking at the seasonal component, we see that the period is approximately
time steps long.
So, we set period=12 .
Introduction to time series analysis — Time series analysis with Python https://filippomb.github.io/python-time-series-handbook/notebooks/01...
You may notice both trend and residuals are missing data towards the beginning and
end.
This has to do with how trend is calculated (beyond the scope of this lesson).
The residuals are missing simply because , so missing trend values
mean missing residual values as well.
In other words, there is nothing wrong with these graphs.
Multiplicative Decomposition
We use the same function as before, but on the multiplicative time series.
Since we know this is a multiplicative time series, we declare
model='multiplicative' in seasonal_decompose .
14 of 20 3/15/2025, 2:21 PM
Introduction to time series analysis — Time series analysis with Python https://filippomb.github.io/python-time-series-handbook/notebooks/01...
Again, the decomposition does a relatively good job picking up the overall trend and
seasonality.
We can see the shapes follow the patterns we expect.
15 of 20 3/15/2025, 2:21 PM
Introduction to time series analysis — Time series analysis with Python https://filippomb.github.io/python-time-series-handbook/notebooks/01...
The STL decomposition does a very good job on the additive time series.
Next, we try with the multiplicative one.
16 of 20 3/15/2025, 2:21 PM
This decomposition is not as good as the previous one.
Your time series data has a clear and stable seasonal pattern and trend.
You prefer a simpler model with fewer parameters to adjust.
The seasonal amplitude is constant over time (suggesting an additive model) or
varies proportionally with the trend (suggesting a multiplicative model).
Your time series exhibits complex seasonality that may change over time.
You need to handle outliers effectively without them distorting the trend and
seasonal components.
You are dealing with non-linear trends and seasonality, and you need more control
over the decomposition process.
fft_analysis(seasonal_12);
Summary
In this lecture we covered the following topics.
The definition of a time series and examples of time series from the real world.
The definition of time series analysis and examples of its application in different
fields.
A practical understanding of the three components of time series data.
The additive, multiplicative, and pseudo-additive models.
Standard approaches to decompose a time series in its constituent parts.
Exercises
Exercise 1
Consider as the seasonal component the periodic signal with period 12
Use seasonal_12 and the trend and residual components below to define and
plot the additive and the multiplicative models
Introduction to time series analysis — Time series analysis with Python https://filippomb.github.io/python-time-series-handbook/notebooks/01...
Perform the seasonal decomposition with seasonal_decompose and STL on the new
signals and compare the results with the ones obtained in class, where we used an
approximate period.
Exercise 2
Load the two different time series as follows.
import statsmodels.api as sm
ts_A = sm.datasets.get_rdataset("AirPassengers", "datasets").data["value"].values
print(len(ts_A))
ts_B = sm.datasets.get_rdataset("co2", "datasets").data["value"].values
print(len(ts_B))
Exercise 3
Decompose ts_A and ts_B using seasonal_decompose and STL .
Comment on the results you obtain. from statsmodels.tsa.seasonal import
seasonal_decompose, STL
20 of 20 3/15/2025, 2:21 PM