# Time Series- Basic Concepts before you start

We always want to know what will happen in future, and if we can predict the important information of future it is even more interesting.

Time Series data is a set of observations taken during a time period. Time-Series in machine learning is a predictive tool to forecast upcoming information. It involves working on time, like year, month, day and hour to derive important information and take decisions based on it.

Examples can be stock market data, weather data that is recorded per day, vital stats data like steps.

Forecasting or Predicting Time Series data is a challenging task and is more complex than machine learning.

In Machine learning the dependent feature was predicted based on a number of features that are independent while in time series we have to predict future observation depending on past observations, that is, independent features are observations in time series only.

**Prediction methodologies can be various like**

1) Mean of past observations, which gives same weightage to all past data.

2) Weighted mean of past observations in which more weightage is given to recent observation and less weightage is given to older data.

**Also factors like below make it more challenging:**

1) **Seasonality:** Seasonality means data observations based on season like winters, summers, or festival. The best example of seasonality is the sales of Air Conditioners which is on peak in summer season in North India. We need to capture the seasonality of data to predict or forecast the future sale.

2) **Trend: **Trend example can be of a company having an upward or downward trend based on some news, merger or annual results, this is basically trend of the company which can be going on for some time and will continue so for more time. It may take longer periods than season for trend to set up. Another example can be consumption of electricity in India which going up because of more and more gadgets at home, it may come down in sometime if there is again a new gadgets coming in which consume less of energy. Trends dies out after a long time.

3) **Irregularity:** Unpredicted observations can be because of anything unpredictable like natural calamity, a war or a pandemic like COVID -19 which made markets to crash in the month of March 2020.

**Stationary Data:**

Time series data needs to be stationary before someone can start working on them.

Now what do we mean by stationary data? Stationary data means, it should have constant mean and constant variance.

If data is not stationary there are many methods to make it stationary, few are as below:

1) De-Trending: To remove the trend component out of data observations, seeing if it is linear, exponential or constant.

2) Differencing: To remove seasonality from data, like if data is seasonal month-wise, it can be done by subtracting the data lag-wise with 12 as lag difference.

3) Logging: Doing Log of Observations can help in making the trend linear from exponential.

**Types of Time Series**

It is important to know what kind of time series data we are seeing, for example below is example of simple 2 types of time series data **additive and multiplicative**.

By knowing about our data we decide that how it will be decomposed into various factors like trend seasonality as we see different graphs here for additive and multiplicative data.

In Additive data component of Seasonality, Trend and irregularity are additive in nature, Value= Base + Trend X+ Seasonality + Irregularity.

While in multiplicative these components are multiplicative in nature i.e,

Value= Base X Trend X Seasonality X Irregularity.

**Data Smoothing**

Data smoothing uses algorithms to remove any noise from data to recognize patterns.

**Smoothing by Rolling Stats**

Rolling Stats can exhibit trend in the data. Moving average, if window is 2 and we apply it to data below then at t1 itâ€™s NULL and at t2 its (20+25)/2=22.5

If mean at a particular time is (25+40)/2, it should be almost similar to (35+30)/2 if it is stationary.

Similarly it applies to variance.

If we see here blue line is the Non-Stationary normal time series data given. While green and orange lines are moving average with windows 2 and 3 respectively.

Observe the smoother curves. Which means mean and variance can be constant. Hence it is used in making the data stationary.

**Simple Exponential Smoothing:**

It will take different observations in time data and it will try to give weightage to past observations with assumption that latest observation will be given more weightage than older observations.

Formula is

y t = alpha * x t + (1-alpha) * y t-1 | alpha = 0-1,

Above is plotting done with black line as original dataset. Blue line is one when recent data is given more weightage, red plot is when old observations are given more weightage than the recent one.

It is important to note the smoothing effect. It is used when there is no seasonality or trend in data and the data is less.

*I will be giving link of my kaggle notebook at the end of article for the plot above.*

**Double Exponential Smoothing (Holt Exponential Smoothing):**

With normal exponential smoothing, double exponential smoothing also takes care of trend by adding a parameter b which is trend parameter.

y t = alpha * x t / C t-L + (1-alpha) * (y t-1 + b t-1) | alpha = 0-1

b t = beta * (y t - y t -1)+(1-beta) * b t-1 | beta = 0-1

b is trend component of time series.

Above graph was made with black line as original data. Blue line is when linear trend is taken into consideration, while red is the one when exponential trend is taken into account.

It is used when there is no seasonality in data but there is some trend.

**Triple Exponential Smoothing (Holt-Winters Exponential Smoothing)**

With normal exponential smoothing, double exponential smoothing, triple exponential smoothing also takes care of seasonality by putting in C parameter in equation, it is important to know L here which is a variable to define period of seasonality.

y t = alpha * x t / C t-L + (1-alpha) * (y t-1 + b t-1) | alpha = 0-1

b t = beta * (y t - y t -1)+(1-beta) * b t-1 | beta = 0-1

C t = gamma * x t /y t + (1-gamma) C t-L

L is period of seasonality like after each 6 months sales pattern will repeat then L =6.

C t will capture seasonality component of data.

When we know about these methods it will be easy for us to decompose data and work on it.

It is used when there is both seasonality and trend in data.

There are several test to conclude that your data is stationary or no like, **Adfuller test.**

Adfuller or Augmented Dickey-Fuller Test, tests the null hypothesis like value of p to be more than 0.05, the data has a unit root and is non-stationary. If less than equal to 0.05 that it doesnâ€™t have a unit root and is stationary.

The forecasting or prediction on it is done taking into consideration many things like:

**We need to consider while doing a time Series problem.**

1) Business hours

2) Business quarter

3) Hour of the day

4) Weekend

5) Festival

6) Daylight Saving

7) Leap Year

8) Season

We need to know the business hour trend, weekly trend, festival season trend, Season trend based on what kind of data we are handling, its domain and type. By doing so we can easily decompose data, for own use.

**Conclusion**

These are basic things to start with time-series forecasting and what are the things to keep in mind before forecasting.

There are many more things to consider like what can be the window for better forecasting, which is determined by ACF, PACF and ARMA models which capture lags in time-series data for a long trend, small trend or so on.

There are various models of time-series prediction which we will discuss further in my next blog.

Thanks for reading!

Reference for kaggle work is here :https://www.kaggle.com/namratakapoor1/time-series-basic