Time series tutorial for beginners
Published:
Before we start
This article is for those who just started working with time series forecasting tasks and get a little confused about where to start. I will present with brief introduction of what model tools we have together with a short code of how to implement them.
There are also a lot of great resources. If you want to dive deeper into the deep models, you can also refer to this medium article.
What is time series forecasting?
Tasks in time series forecasting
Train your first time-series model
Auto regression
ARIMA
Below is a simple example.
First let us create out dataset and add some noise to make it a harder task.
import numpy as np
# Generate synthetic time series data
np.random.seed(0)
time_steps = 100
t = np.arange(time_steps)
data = np.sin(0.1 * t) + np.random.normal(scale=0.1, size=time_steps)
There are a lot of packages for AMIRA implementation in Python. Here we use pmdarima
.
import pandas as pd
from pmdarima import auto_arima
# Create a pandas DataFrame
df = pd.DataFrame(data, columns=['Value'])
# Split data into train and test sets
train_size = int(len(df) * 0.8)
train, test = df.iloc[:train_size], df.iloc[train_size:]
# Fit ARIMA model
model = auto_arima(train, start_p=1, start_q=1, max_p=3, max_q=3, seasonal=False, trace=False)
model.fit(train)
# Make predictions
predictions = model.predict(n_periods=len(test))
# Evaluate the model
mse = mean_squared_error(test, predictions)
print(f"Mean Squared Error: {mse}")
We can visualize the prediction and see how it works! (Plotly is just so much better than matplotlib.)
import plotly.graph_objs as go
import plotly.io as pio
# Create traces
trace_original = go.Scatter(x=df.index, y=df['Value'], mode='lines', name='Original Data')
trace_predictions = go.Scatter(x=test.index, y=predictions, mode='lines', name='Predictions', line=dict(color='red'))
# Layout
layout = go.Layout(title='Autoregression Predictions', xaxis=dict(title='Time'), yaxis=dict(title='Value'))
# Create figure
fig = go.Figure(data=[trace_original, trace_predictions], layout=layout)
# Show plot
pio.show(fig)
Multi dimensions
Elastic Net (extension of Lasso)
Continue into deep learning models
Outputs and inputs
Ways to align desired output dimension
Introduction to deep-learning based models
While deep learning with gigantic models shows advanced performance in Computer Vision and Natural Language processing, this seems to be not the case for time series forecasting. Before we go deeper into deep learning, I must warn you that this would require great effort in hyperparameter tuning and sometimes it might even downperform the linear regressions! Try ElasticNet (even a persistent model for autoregression tasks) first to have a good baseline on a specific task!