A Complete Tutorial on Time Series Filters
A time-series made up of trend cycle, seasonality and irregularities. To correctly forecast the values of any time series, it is essential to remove values that are going out of a decided range and causing unusual fluctuation in the time series. For example, the price series of petrol for a year consists of prices between Rs. 99 to Rs. 100. But suppose for a few days, due to shortage of supply, the price has gone more than Rs.105. This type of fluctuation in any time series can cause uncertainty in any model for the prediction of prices. This fluctuation is not required in the modelling, so we can remove them using the filters. A good filter should be able to remove unit roots and the cyclic components or more formally we can say the filter should be capable of isolating fluctuations of the data at a certain frequency. In this article, we will see different types of filters used in time series modelling and the differences and similarities between them. The important and useful points about the filters that will be discussed in this article are listed below. Table of Contents Band-Pass FilterBaxter-King Filter(BK Filter)Christiano Fitzgerald Filter(CF Filter)SmoothingHodrick Prescott Filter(HP Filter)First Order Difference(FOD)Comparing the Filters Now let us discuss these filters one by one. Band-Pass Filter In time series analysis when we plot the time series, we get a line plot that represents the frequencies consisting of a time series. A band-pass filter allows it to pass frequencies within a certain range and rejects the frequencies going out of the range. The below images demonstrate the band-pass filter with the frequency response at several filter orders for the same sampling rate and cutoff frequencies. The other plot demonstrates the effect of the filter (with order=6) on a sample time series. We are having this overview of band-pass because we have two filters that work on the basis of ideal band-pass filters so it will create a better understanding for those filters. Let’s take a look at those filters which are dependent on the band-pass filter. The Baxter King Filter (BK Filer) BK filter is a linear filter that eliminates the trend and high-frequency irregularity component from a time series by retaining the intermediate cyclic components. A time series can be separated into various frequencies. BK filter extracts frequencies within predefined ranges by eliminating all other frequencies. That is why it can be considered as an ideal band-pass filter. This filter decomposes a time series into three main components. TrendCycleError components Mathematically if y  is a  time series then Where Ꞇ is the trend component, ʗ is the cyclic component and ε is the error component. When we apply a moving average in time series we get a new time series which is a modified version of older time series. Where beta is a fixed component and k is the maximum lag of the time series. When it comes to extracting the cyclic component from the time series using moving average the BK filter uses a weight that adds up to zero. Using weights that add up to zero moving average generates the prosperity of elimination of unstable frequencies and this generates a stationary time series. This filter is derived from the frequency domain where a covariance stationary function can be represented as Since the similarity between the covariance function or integral of a random periodic component and time series it can also be a formula for time series and if the filter is applied to the integral we get. Where frequency response function of the filter is Formally it can be considered as the response of a new function to the old function at a given frequency with respect to the added weight to the random periodic component. And the filter extracts the cyclic component using the following formula. The BK filter consists of two low pass filters because it has two predetermined frequency bands. It can be implemented in python statsmodels statsmodels.tsa.filters.bk_filter.bkfilter package. I have applied the Baxter-King filter to a data macro data where we are having the following information: In the data series inflation rate and unemployment rate are counter cyclic. By applying this filter the bandpass consisting of the filter produces a new series that does not contain fluctuations more than the predetermined frequencies. 10 head of new cycles of the inflation rate and unemployment rate. Results are shown in the graph. The Christiano Fitzgerald Filter This filter is similar to the Baxter King filter(BK filter) which also works on the basis of an ideal band-pass filter. Where the bk filter assumes a symmetric moving average to approximate the band-pass filter on the other hand CF filter assumes that the time series is following randomness without the drift. The cyclic component estimated by the CF filter is as follows. Where pL and pH represent the cut off lengths of the cycle. The CF filter is not symmetric because it puts the weight on each value in the time series. This is also based on the band-pass filters and puts weight on all observations and this filter is appropriate for a series that may follow a random walk. In a random walk, a series contains values in a sequence where the next value is a modification of the previous value. This can be implemented in python using statsmodels.tsa.filters.cf_filter.cffilter module of statsmodals. I have applied to the inflation rate and unemployment rate. 10 head of the cycle after applying the filter. The results of the modified cycle component are presented in the graph. Smoothing Time Series The smoothing techniques are the members of time series forecasting methods or algorithms, which use the weighted average of a past observation to predict the future values or forecast the new value. These techniques are well suited for time-series data having fewer deviations with time. It synchronizes ETS (Error, Trends, Seasonality) components to more computable and smoothed parameters. To know more about smoothing you can check this article. since it is a whole part of time series analysis. We are not discussing it here. We have a general overview of the smoothing time series in the above section because the next section consists of two filters that work on the basis of the smoothing technique. The Hodrick Prescott Filter The Hodrick Prescott filter is a smoothing method filter that obtains a smooth component from the time series trend. As we know from a single line graph of time series we can not measure the trend and cycle presented in the time series this filter allows us to estimate these components. This filter assumes that the trend in time series is having a random probability distribution and is uncorrelated to its cyclic component. Mathematically the trend component estimated by the filter is Here in the formula, the first part is a squared sum of the difference between the time series variable and the trend which can be considered as the goodness of fit. Change in the growth rate of trend is being measured in the second part..the hp filter minimizes the distance between trend and the trend holding time series. The multiplier in formula ???? is a penalty that is positive and given to the growth of the trend so that we can get a smoother (linear) trend and more variation in the cyclic component. We can implement it using statsmodels.tsa.filters.bk_filter.hpfilter module of statsmodel in python. I have applied it to the real gross domestic product series where I tried to extract smoothed trends and cyclic components from the series. The results are shown in the graphs. The First Order Difference It is the most simple filter among all of them. In the output, it gives a time series which is basically a difference between the present variable and the previous time step time variable. This is a commonly used method because it causes the removal of unit root components from a time series. If any observation of time series as time t is. Then the trend will be. And the cyclic component will be. We can clearly see that ultimately we are performing smoothing of the time series. There is no specific module for the implementation of FOD but it can be simply done by using the formula creation in python. I have applied it to the real gross domestic product series using python in this link. Below the graphs will represent the results Comparing the Filters We can see above that the graphs are almost similar but the filters which are using the band-pass filters are extracting smoother cycles than the filters using smoothing. The HP filter is producing a smoother cycle than the FOD filter. We can say that FOD generates relatively lower cyclical components. Any reader who is interested in the thesis can read it and there are many other things available about the filter and detrending the time series using those filters. Final Words Here we have applied all the filters in different time series and seen how they produce new trends and cycles using the old-time series by just putting some weights on the observations or in time series and by smoothing the time series by differencing or square sum differencing methods. In time series analysis we use those filters to remove higher fluctuations in the time series which can be caused by unused changes in any time series or we try to smooth the time series so that any forecasting model can predict or forecast values accurately. References BK filterCF filterHP filterComparison of detrending methods.Google Colab notebook for above codes
A good filter should be able to remove unit roots and the cyclic components or more formally we can say the filter should be capable of isolating fluctuations of the data at a certain frequency.
very high fluctuation is not required in the modelling time series, so we can remove them using the filters.
["AI (Artificial Intelligence)", "Data Science", "Data Scientist", "Deep Learning", "Machine Learning", "Time Series", "Time Series Forecasting", "time series prediction"]
["Go", "TPU", "programming_languages:R", "AI", "Modal", "Machine Learning", "Time Series Forecasting", "RAG", "Colab", "Python", "Time Series", "time series prediction", "programming_languages:Python", "Deep Learning", "Data Science", "Data Scientist", "R", "AI (Artificial Intelligence)"]
["AI", "Colab", "RAG", "TPU", "Python", "R", "Go", "Modal", "programming_languages:Python", "programming_languages:R"]
Yugesh is a graduate in automobile engineering and worked as a data analyst intern. He completed several Data Science projects. He has a strong interest in Deep Learning and writing blogs on data science and machine learning.