# Trend, Seasonality, Moving Average, Auto Regressive Model : My Journey to Time Series Data with Interactive Code

So recently I have been working with Time Series Data. And I wanted to review what a Time series is as well as make my understanding more concert on Time Series Data.

Please note this post is for my future self and for me to gain more deeper understanding of Time Series.

**Definition**

There are multiple of different sources defining the term ‘Time Series’ , so here I’ll try to give a general definition that is easy for my self to understand.

Atime seriesis a series of data points indexed (or listed or graphed) in time order.

As seen above, wiki gives very straight forward definition, any data in a sequential time order. Now lets take a look at the definition from investopedia.

A time series is a sequence of numerical data points in successive order.

As seen above, we can get a general idea of what a time series data can be. It can be any data recored over time in sequential order. From the start we can think of stock prices, however videos, languages, songs, and MRI Scans can be thought of Time Series data as well.

**Types of Time Series Data**

Above is an example image of Stock Prices, and we can observe that in the x-axis we have the time index, and in the y-axis we have the stock prices of different markets.

Another great example of Time Series data in computer vision world would be video (above is a GIF), since we capture frame by frame in one time sequence. And we can clearly see MRI scan can be named as Time Series Data.

**Goals of Time Series**

One obvious example use case of Time Series is predicting stock prices. (Well if this was so easy, a lot of Data Scientist would be rich.) But I wanted to add some different uses cases, we can even use GAN’s to predict the next frame of a given video, or MRI Scan. Imagine where a GAN takes in few sequences of your MRI Scan and generates a MRI scan if you had cancer inside your body. (That would be pretty cool.). So in general we want to forecast / predict the next value when it comes to Time Series.

**Trend / Seasonality / Noise**

Before we move on we need to discuss something important, most time series data can be described by three components. And those are trend, seasonality and bias.

**Trend** → a general systematic linear or (most often) nonlinear component that changes over time and does not repeat**Seasonality** → a general systematic linear or (most often) nonlinear component that changes over time and does repeat**Noise** → a non-systematic component that is nor Trend/Seasonality within the data

**Right Image **→ Python Code to Create the Visualization**Red Line → **Apple Stock Prices from 1985 **Blue Line → **Trend for Apple Stock Price**Green Line →** Residual (Noise) for Apple Stock Price**Orange Line** → Seasonal (Yearly) trend for Apple Stock Price

With simple code and statsmodel library we can easily see how each components related to one another. We can observe that there is a seasonal increase every year, as well as general trend for apple stock price is increasing.

**Approach to Predict / Forecast**

**Left/Right Image **→ Standard Window Avg / Exponentially Moving Avg

There are lot of different methods that we can use when we want perform forecasting. I will mention the methods that I have found during my research.

**Standard / Exponentially Moving Average** → calculation to analyze data points by creating series of averages of different subsets of the full data set

**Auto Regression**** **→ is a representation of a type of random process; as such, it is used to describe certain time-varying processes in nature, economics, etc

**Linear****/****Polynomial Regression** → regression analysis in which the relationship between the independent variable *x* and the dependent variable *y* is modelled as an *n*th degree polynomial (or 1 degree for linear)

**ARMA**** **→ model that provide a parsimonious description of a (weakly) stationary stochastic process in terms of two polynomials, one for the autoregression and the second for the moving average.

**ARIMA (Autoregressive integrated moving average)**** **→ is a generalization of an autoregressive moving average (ARMA) model. Both of these models are fitted to time series data either to better understand the data or to predict future points in the series (forecasting)

**Seasonal ARIMA**** **→ seasonal AR and MA terms predict *xt *using data values and errors at times with lags that are multiples of *S* (the span of the seasonality)

**ARIMAX** → An ARIMA model with covariate on the right hand side

**Recurrent Neural Network (LSTM) **→ a class of artificial neural network where connections between nodes form a directed graph along a sequence in which allows it to exhibit dynamic temporal behavior for a time sequence.

As seen above even a simple Auto Regressive can fit the stock price quite perfectly. Please click here to read about the difference between ARMA, ARIMA and ARIMAX.

**Interactive Code**

*For Google Colab, you would need a google account to view the codes, also you can’t run read only scripts in Google Colab so make a copy on your play ground. Finally, I will never ask for permission to access your files on Google Drive, just FYI. Happy Coding!*

To access the code used for this post, please click here.

**Final Words**

I hope to continue my research to gain a deeper understanding about Time Series. Since I think its very common problem in every industry.

If any errors are found, please email me at jae.duk.seo@gmail.com, if you wish to see the list of all of my writing please view my website here.

Meanwhile follow me on my twitter here, and visit my website, or my Youtube channel for more content. I also implemented Wide Residual Networks, please click here to view the blog post.

**Reference**

- Time series. (2018). En.wikipedia.org. Retrieved 2 June 2018, from https://en.wikipedia.org/wiki/Time_series
- Staff, I. (2006). Time Series. Investopedia. Retrieved 2 June 2018, from https://www.investopedia.com/terms/t/timeseries.asp
- How To Identify Patterns in Time Series Data: Time Series Analysis . (2018). Statsoft.com. Retrieved 2 June 2018, from http://www.statsoft.com/Textbook/Time-Series-Analysis
- statsmodels.tsa.seasonal.seasonal_decompose — statsmodels 0.9.0 documentation. (2018). Statsmodels.org. Retrieved 2 June 2018, from http://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.seasonal_decompose.html
- StatsModels: Statistics in Python — statsmodels 0.9.0 documentation. (2018). Statsmodels.org. Retrieved 2 June 2018, from https://www.statsmodels.org/stable/index.html
- Deshpande, B. (2018). Time series forecasting: understanding trend and seasonality. Simafore.com. Retrieved 2 June 2018, from http://www.simafore.com/blog/bid/205420/Time-series-forecasting-understanding-trend-and-seasonality
- Moving average. (2018). En.wikipedia.org. Retrieved 2 June 2018, from https://en.wikipedia.org/wiki/Moving_average
- Linear regression. (2018). En.wikipedia.org. Retrieved 2 June 2018, from https://en.wikipedia.org/wiki/Linear_regression
- Polynomial regression. (2018). En.wikipedia.org. Retrieved 2 June 2018, from https://en.wikipedia.org/wiki/Polynomial_regression
- Autoregressive–moving-average model. (2018). En.wikipedia.org. Retrieved 2 June 2018, from https://en.wikipedia.org/wiki/Autoregressive%E2%80%93moving-average_model
- Recurrent neural network. (2018). En.wikipedia.org. Retrieved 2 June 2018, from https://en.wikipedia.org/wiki/Recurrent_neural_network
- Autoregressive model. (2018). En.wikipedia.org. Retrieved 2 June 2018, from https://en.wikipedia.org/wiki/Autoregressive_model
- The ARIMAX model muddle | Rob J Hyndman. (2010). Robjhyndman.com. Retrieved 2 June 2018, from https://robjhyndman.com/hyndsight/arimax/
- 4.1 Seasonal ARIMA models | STAT 510. (2018). Newonlinecourses.science.psu.edu. Retrieved 2 June 2018, from https://newonlinecourses.science.psu.edu/stat510/node/67/
- statsmodels.tsa.ar_model.AR.predict — statsmodels 0.9.0 documentation. (2018). Statsmodels.org. Retrieved 2 June 2018, from http://www.statsmodels.org/dev/generated/statsmodels.tsa.ar_model.AR.predict.html#statsmodels.tsa.ar_model.AR.predict
- Python, A. (2018). Autoregressive model using statsmodels in Python. Stack Overflow. Retrieved 2 June 2018, from https://stackoverflow.com/questions/28094538/autoregressive-model-using-statsmodels-in-python
- Anon, (2018). [online] Available at: https://www.researchgate.net/figure/Raman-spectra-of-the-MCC-101-raw-material-with-123-water-content-top-and-of-the-MCC_fig11_8508180 [Accessed 2 Jun. 2018].