# 1, Time series prediction method

1.Autoregression (AR)
2.Moving Average (MA)
3.Autoregressive Moving Average (ARMA)
4.Autoregressive Integrated Moving Average (ARIMA)
5.Seasonal Autoregressive Integrated Moving-Average (SARIMA)
6.Seasonal Autoregressive Integrated Moving-Average with Exogenous Regressors (SARIMAX)
7.Vector Autoregression (VAR)
8.Vector Autoregression Moving-Average (VARMA)
9.Vector Autoregression Moving-Average with Exogenous Regressors (VARMAX)
10.Simple Exponential Smoothing (SES)
11.Holt Winter's Exponential Smoothing (HWES)

# 2, Usage explanation and python program

## 1.AR

This method is suitable for univariate time series without trend and seasonal components.

Autorepression (AR) method converts the next prediction result in the sequence into a linear function of the previous time step observations.

Symbol of model: the order of model p is taken as the parameter of AR function, i.e. ar §. For example, AR(1) is a first-order Autoregression model.

```from statsmodels.tsa.ar_model import AutoReg
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = AutoReg(data, lags=1)
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)
```

## 2.MA

This method is suitable for univariate time series without trend and seasonal components.

The Moving Average (MA) method converts the prediction result of the next step in the sequence into a linear function of the residual of the average process from the previous time step
Moving Average model is different from calculating the Moving Average of time series
Symbol of model: specify the order of model Q as the parameter of Ma function, i.e. MA(q). For example, MA(1) is a first-order Moving Average model.

```from statsmodels.tsa.arima.model import ARIMA
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = ARIMA(data, order=(0, 0, 1))
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)
```

## 3.ARMA

This method is suitable for univariate time series without trend and seasonal components.

ARIMA method converts the next prediction result in the sequence into a linear function of the observed value and residual in the previous time step.

It combines AR and MA models.

Symbol of model: the order of AR § and MA(q) model is taken as the parameter of ARMA function, such as ARMA(p, q). ARIMA model can be used to develop AR or Ma models.

```from statsmodels.tsa.arima.model import ARIMA
from random import random
# contrived dataset
data = [random() for x in range(1, 100)]
# fit model
model = ARIMA(data, order=(2, 0, 1))
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)
```

## 4.ARIMA

This method is suitable for univariate time series with trend and no seasonal components.

ARIMA method models the next step in the sequence as a linear function of the difference observations and residuals of the previous time step.

ARIMA combines AR and MA models and the differential preprocessing steps of the sequence to make the sequence stable.

Symbol of model: specify the order of AR §, I(d) and MA(q) models as the parameters of ARIMA function, such as ARIMA(p, d, q). ARIMA model can also be used to develop AR, Ma and ARMA models.

```from statsmodels.tsa.arima.model import ARIMA
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = ARIMA(data, order=(1, 1, 1))
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data), typ='levels')
print(yhat)
```

## 5.SARIMA

This method is applicable to univariate time series with trend and / or seasonal components.

SARIMA method takes the next prediction value in the sequence as the linear function of the difference observation value, error, difference seasonal observation value and seasonal error of the previous time step.

SARIMA combines ARIMA models with the ability to perform the same autoregressive, differential and moving average modeling at the seasonal level.

Symbol of model: specify the order of AR §, I(D) and MA(Q) models as the parameters of ARIMA function and seasonal level of AR §, I(D), MA(Q) and m, such as SARIMA(p, d, q)(P, D, Q)m, where "m" is the time step length of each season (seasonal period). SARIMA model can be used to develop AR, Ma, ARMA and ARIMA models.

```from statsmodels.tsa.statespace.sarimax import SARIMAX
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = SARIMAX(data, order=(1, 1, 1), seasonal_order=(0, 0, 0, 0))
model_fit = model.fit(disp=False)
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)

```

## 6.SARIMAX

It is applicable to univariate time series with trend and / or seasonal components and exogenous variables.

SARIMAX model is an extension of SARIMA model, which also includes the modeling of exogenous variables.

Exogenous variables, also known as covariates, can be considered as parallel input sequences, which are observed in the same time step as the original sequence. The primary sequence can be referred to as endogenous data to compare it with exogenous sequences. The observations of exogenous variables are directly included in the model at each time step and are not modeled in the same way as the main endogenous sequence (e.g. as AR, MA, etc.).

The SARIMAX method can also be used to model inclusion models containing exogenous variables, such as ARX, MAX, ARMAX, and ARIMAX.

```from statsmodels.tsa.statespace.sarimax import SARIMAX
from random import random
# contrived dataset
data1 = [x + random() for x in range(1, 100)]
data2 = [x + random() for x in range(101, 200)]
# fit model
model = SARIMAX(data1, exog=data2, order=(1, 1, 1), seasonal_order=(0, 0, 0, 0))
model_fit = model.fit(disp=False)
# make prediction
exog2 = [200 + random()]
yhat = model_fit.predict(len(data1), len(data1), exog=[exog2])
print(yhat)
```

## 7.VAR

This method is suitable for multivariate time series without trend and seasonal components.

VAR method uses AR model to model the next step of each time series. It is a generalization of AR to multiple parallel time series, such as multivariate time series.

Symbol of model: specify the order of AR § model as the parameter of VAR function, such as VAR §.

```from statsmodels.tsa.vector_ar.var_model import VAR
from random import random
# contrived dataset with dependency
data = list()
for i in range(100):
v1 = i + random()
v2 = v1 + random()
row = [v1, v2]
data.append(row)
# fit model
model = VAR(data)
model_fit = model.fit()
# make prediction
yhat = model_fit.forecast(model_fit.y, steps=1)
print(yhat)
```

## 8.VARMA

This method is suitable for multivariate time series without trend and seasonal components.

VARMA method uses ARMA model to model the next step in each time series. It is an extension of ARMA to multiple parallel time series, such as multivariate time series.

Symbol of the model: specify the order of AR § and MA(q) model as the parameters of VARMA function, such as VARMA(p, q). VARMA models can also be used to develop VAR or VMA models.

```from statsmodels.tsa.statespace.varmax import VARMAX
from random import random
# contrived dataset with dependency
data = list()
for i in range(100):
v1 = random()
v2 = v1 + random()
row = [v1, v2]
data.append(row)
# fit model
model = VARMAX(data, order=(1, 1))
model_fit = model.fit(disp=False)
# make prediction
yhat = model_fit.forecast()
print(yhat)
```

## 9.VARMAX

This method is suitable for multivariate time series without trend and seasonal components with exogenous variables.

Vector autoregressive moving average (VARMAX) with exogenous regression is an extension of VARMA model, which also includes the modeling of exogenous variables. It is a multivariate version of the ARMAX method.

Exogenous variables, also known as covariates, can be considered as parallel input sequences, which are observed in the same time step as the original sequence. The main series is called endogenous data to compare it with exogenous sequences. The observations of exogenous variables are directly included in the model at each time step and are not modeled in the same way as the main endogenous sequence (e.g. as AR, MA, etc.).

The VARMAX method can also be used to model inclusion models containing exogenous variables, such as VARX and VMAX.

```from statsmodels.tsa.statespace.varmax import VARMAX
from random import random
# contrived dataset with dependency
data = list()
for i in range(100):
v1 = random()
v2 = v1 + random()
row = [v1, v2]
data.append(row)
data_exog = [x + random() for x in range(100)]
# fit model
model = VARMAX(data, exog=data_exog, order=(1, 1))
model_fit = model.fit(disp=False)
# make prediction
data_exog2 = []
yhat = model_fit.forecast(exog=data_exog2)
print(yhat)
```

## 10.SES

This method is suitable for univariate time series without trend and seasonal components.

The simple exponential smoothing (SES) method takes the prediction result of the next time step as an exponential weighted linear function of the observed value of the previous time step.

```from statsmodels.tsa.holtwinters import SimpleExpSmoothing
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = SimpleExpSmoothing(data)
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)
```

## 11.HWES

This method is applicable to univariate time series with trend and / or seasonal components.

HWES, also known as triple exponential smoothing method, takes the prediction result of the next time step as the exponential weighted linear function of the observed value of the previous time step, taking into account the trend and seasonality.

```from statsmodels.tsa.holtwinters import ExponentialSmoothing
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = ExponentialSmoothing(data)
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)
```

Keywords: Python Machine Learning

Added by YOUAREtehSCENE on Thu, 25 Nov 2021 20:52:40 +0200