Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Seasonality Detection with Fast Fourier Transform (FFT) and Python
Machine Learning

Seasonality Detection with Fast Fourier Transform (FFT) and Python

Last Updated on January 6, 2023 by Editorial Team

Author(s): Satsawat Natakarnkitkul

Machine Learning

Quickly detect seasonality using FFT inΒ Python

Photo by Isaac Smith onΒ Unsplash

Forecasting

Forecasting is one of the process of predicting the future based on past and present data. Most of the forecasting problem associated with time series data (i.e. what is the sale of product A nextΒ month).

Some problems can be easier to forecast than others. The predictability of an event or a quantity depends on several factors, someΒ are:

  1. understanding of the factors contribute to theΒ result;
  2. data availability;
  3. forecasting technique or learning algorithm.

Often, there are many methods in solving forecast accurately, good forecasts capture the genuine patterns and relationships which exist in the historical data, but do not replicate past events that will not occurΒ again.

Seasonality and useΒ cases

In time series data, seasonality refers to the presence of some certain regular intervals, or predictable cyclic variation depending on the specific time frame (i.e. weekly basis, monthly basis). Some examples of seasonality is higher sales during Christmas, higher bookings during holidayΒ period.

Example of seasonality plot

Detecting the seasonality in time series data can improve the forecasting, reveal some hidden insight and lead to insight and recommendation. In general, time series data forecast can be represented onto;

where Y is the metric; S represents seasonality; T represents trends; and e is the errorΒ term.

Some key use cases are as followed:

  • Top up behavior;
  • Specific page, or, application browsing behavior;
  • Sales and demand forecasting;
  • Temperature forecasting.

There are many approaches to detect the seasonality in the time series data. However, in this post, we will focus on FFT (Fast Fourier Transform).

FFT inΒ Python

A fast Fourier transform (FFT) is algorithm that computes the discrete Fourier transform (DFT) of a sequence. It converts a signal from the original data, which is time for this case, to representation in the frequency domain.

To put this into simpler term, Fourier transform takes a time-based data, measures every possible cycle, and return the overall β€œcycle recipe” (the amplitude, offset and rotation speed for every cycle that wasΒ found).

Let’s demonstrate this in Python implementation using sineΒ wave.

From the script, I have generated the sine wave of 2 seconds duration and have 640 points (a 12 Hz frequency wave sampled at 32 times oversampling factor, which is 2 x 32 x 10 = 640). Then we can compute FT of this data and visualize theΒ output.

Example of Sine wave of 12 Hz and its FFTΒ result

From the result, we can see that FT provides the frequency component present in the sine wave. The next figure shows how we add multiple waves into one and use FFT to detect theΒ peak.

To further demonstrate how FT can help detecting seasonal, the next figure demonstrates how two different waves are combined and used FT to detect the seasonal. FT generates two peaks according to respectively waveΒ Hz.

Combined wave (2 Hz sine wave and 20 Hz cosine wave) and its FTΒ result

Now, let’s see the implementation on real use cases. In this demonstration, we will detect the seasonality of natural gas CO2 emission.

Time series plot of CO2Β emission

We then normalized the original by subtracting with the median() method and multiplying with window function value (using blackman for this data). Please note that the window function should be suitable with the data set you have, to further study on available window function, you can refer to this to explore different type of window function.

Comparison of original and normalized data

Now we can compute the FT output and plot the graph, the first few frequency bins are being omitted because those points represent the baseline and is not useful for analysis.

Graphic output of FTΒ result

Based on the output, we can see the strong signals at x=1.010, which we can turn this onto year, which is 0.99 year (or 11.89 months, depends on the implementation objective).

End note

As we can see FT can help us capture the seasonality and can be used to decompose the time series data. FFT method is also built in various software package and can easy to use regarding any programming languages.

Thanks for reading and happy learning!!! You can view the notebook with full code implementation here.


Seasonality Detection with Fast Fourier Transform (FFT) and Python was originally published in Towards AIβ€Šβ€”β€ŠMultidisciplinary Science Journal on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓