The Journey into Time Series: Dancing with Trends and Seasons

Data Mastery Series — Episode 13: The Art of Forecasting (Part 4)

Donato_TH
Donato Story
6 min readJul 23, 2023

--

If you are interested in articles related to my experience, please feel free to contact me: linkedin.com/in/nattapong-thanngam

I hope you’ve built a strong foundation in time series forecasting and the related descriptive analysis. This encompasses essential concepts such as mean, variance, standard deviation, and the 25th percentile [Link]. It’s also expected that you’ve familiarized yourself with basic visualization tools like bar charts, histograms, box plots, and line charts [Link]. In previous sessions, we explored Python libraries tailored for Exploratory Data Analysis (EDA), including YData Profiling, D-Tale, SweetViz, and DataPrep. These resources aim to simplify and enhance your analytical journey. Today, our focus shifts to trends and seasonality.

Continuing our Tale

Robust Roast: A Tale of Beans and Bytes — Part 5 ( Image by Author)
Robust Roast: A Tale of Beans and Bytes — Part 6 ( Image by Author)

Have you ever noticed how sales might drop during the rainy season? Ever wondered if this dip is a regular seasonal occurrence or something unusual? In this episode, we’ll uncover how to distinguish the impact of seasonality from general trends.

Note: All of the code and raw files are available in my GitHub repository

Understanding trends and seasonality in product data is of paramount importance. Such insights are goldmines for various departments, from sales and marketing to production and warehousing. A data scientist or analyst who can dissect these patterns and extract both trend and seasonality from each SKU becomes an invaluable asset to their team.

What is a trend?

A trend points to a consistent pattern or direction in data over time. Trends can rise (increasing), fall (decreasing), or remain steady. They often stem from long-lasting influences like technological shifts, economic factors, or population changes.

What is seasonality?

Seasonality describes recurring shifts or patterns in a time series, often linked to particular times — yearly, monthly, weekly, or daily. Factors like holidays, weather variations, and academic calendars drive these shifts. Recognizing seasonality is crucial for accurate time series forecasting because it reveals the data’s consistent and repetitive shifts.

On a more technical note, there’s also the concept of a “Cycle”. It’s akin to seasonality but spans longer than a year, often evident in phenomena like economic cycles. Another crucial aspect is “Irregularity”, which represents unpredictable, random fluctuations in a time series without any discernible pattern.

Components of Time Series Data (Source: ashishpatel26)

Benefits of Understanding Seasonality and Trend:

  1. Strategic Planning: Recognizing trends and seasonality helps businesses predict demand and plan resources better.
  2. Optimized Marketing: Businesses can tailor their marketing campaigns to coincide with the identified high and low seasons.
  3. Efficient Inventory Management: Knowing when certain products are in demand lets businesses manage their stock more effectively, balancing supply with demand.
  4. Informed Decision Making: Understanding and accounting for trends and seasonal changes leads to more accurate targets and smarter business choices.

Leveraging Python for Time Series Analysis

Python boasts a diverse range of techniques to extract and visualize trends and seasonality from data. To simplify the learning curve, I’ll focus on a select few methods that are both effective and straightforward to interpret.

Line Chart:

To recall our discussions from previous episodes and offer a snapshot of product behaviors, we begin with line charts. Consider the chart below illustrating the patterns of three popular products (SKU A, SKU B, and SKU C). The interpretations are straightforward:

  • SKU A exhibits an upward trend with evident seasonality.
  • SKU B presents a downward trend but retains seasonal characteristics.
  • SKU C, much like SKU A, follows an upward trajectory with clear seasonality.
(Recap) Line Chart Showcasing Trends of SKU A, B, C (Image by Author)

Boxplot:

In general, boxplots are more difficult to understand than line charts. However, it could show variations in each product or fluctuations in the median that help us better understand how products behave. I also employ the technique of combining a line chart and a box plot. The boxplot shows the variance, while the line chart shows the overall trend.

Box Plot Representation for SKU A, B, and C (Image by Author)
Combined View: Boxplot with Line Chart (Image by Author)

Heatmap:

Heatmaps are compelling visual tools, representing data tendencies and seasonality via color intensity. The spectrum from light to dark indicates the range from low to high values. A look at the heatmap below reveals:

  • SKU A: The progressive deepening of color year after year indicates its upward movement. The pronounced dark shades at the start of the year highlight peak periods, showcasing its seasonality.
  • SKU B: The shades becoming progressively lighter suggest a downward trend. Yet, the darker shades at the beginning of each year mark peak sales times, pointing to seasonality.
  • SKU C: Similar to SKU A, the darkening shades indicate an upward trend with pronounced seasonality at the year’s outset. However, SKU C’s overall lighter hue compared to SKU A and SKU B indicates its relatively lower sales volume.
Heatmap Display for SKU A, B, and C (Image by Author)

Seasonal Plots

One of the more commonly embraced techniques for visualizing seasonality involves utilizing line plots with years set as the legend. This approach brings seasonality to the forefront with clarity. Take SKU A as an example: its sales peak in February and March and taper off around September and October.

Seasonal Plots (Image by Author)

Seasonal Subseries Plot

A Seasonal Subseries Plot organizes data by its seasonal cadence (e.g., monthly) and juxtaposes the data of each year side by side. This format is instrumental in spotlighting consistent patterns for a specific season spanning various years.

Seasonal Subseries Plot (Image by Author)

Seasonal Plot with Box Plots

This technique is a boon when you’re looking to delve deep into product insights. Let’s explore an illustration with SKU A, which melds a seasonal plot with both year-wise and month-wise box plots, painting a holistic picture of SKU A’s behavior.

Seasonal Plot with Box Plots of SKU A (Image by Author)

Seasonal Decomposition Plot

The essence of seasonal decomposition lies in its ability to break down a time series into its three core facets: trend, seasonality, and residuals (often labeled as ‘noise’). Displaying these elements visually offers an insightful panorama of inherent data patterns. For instance, our seasonal decomposition plot for SKU A showcases the original data in the first chart, an upward trend in the second, discernible seasonality in the third, and residuals or errors in the fourth.

Seasonal Decomposition Plot (Image by Author)

Recognizing patterns in time series data is not solely about forecasting what lies ahead. It’s about diving deep into your dataset’s rhythmic dance, empowering you with knowledge that can enhance every decision you make, be it as a data scientist or a business strategist. With the Python techniques unveiled in this episode, you stand on the threshold of deeper exploration into the vast expanse of forecasting.

In the subsequent episode, we’ll demystify the algorithms that form the backbone of time series forecasting. This ensures you’re well-prepared to further hone your analytical acumen. Continue journeying with us through this realm of data discovery!

Thank you for taking the time to read this article! If you found it enjoyable, we recommend checking out these other articles for your reading pleasure.

Data Science

26 stories

Dashboard

3 stories

Donato_Journey

5 stories

Course_Review

3 stories

Please feel free to contact me, I am willing to share and exchange on topics related to Data Science and Supply Chain.
Facebook:
facebook.com/nattapong.thanngam
Linkedin:
linkedin.com/in/nattapong-thanngam

--

--

Donato_TH
Donato Story

Data Science Team Lead at Data Cafe, Project Manager (PMP #3563199), Black Belt-Lean Six Sigma certificate