Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Autoregressive-Integrated-Moving-Average Model | Stationary Models
Time Series Analysis

book
Autoregressive-Integrated-Moving-Average Model

The last model we will look at is the autoregressive integrated moving average. This model combines autoregression, moving average and differencing technique(this is not the model we discussed above). It is presented as follows:

It may look quite complicated, but in reality, look, AR - is an autoregressive model with which you are already familiar, MA - is a moving average, which implies a linear combination of errors from the past forecast.

ARIMA uses 3 parameters that we must choose ourselves (p, d, q):

P - is called the order of autoregression. It is the number of immediately preceding values in the series that are used to predict the value at the present time;

D - order of differencing;

Q - the order of the moving average. Allows you to set the model error as a linear combination of previously observed error values.

Note

We will consider differencing techniques in the next chapters in more detail

What is the advantage of this model? It can forecast simple non-stationary processes ( more precisely, processes in which mean and covariance changes over time) and is more efficient when working with short-term predictions.

Let's create an ARIMA model using statsmodels, for this, and we use the class ARIMA():

python
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
from math import sqrt

X = df.values
size = int(len(X) * 0.6)
train, test = X[0:size], X[size:]
history = train.tolist()
predictions = list()

# Making foreasts with ARIMA model
for t in range(len(test)):
model = ARIMA(history, order=(5, 1, 0))
model_fit = model.fit()
output = model_fit.forecast()
predictions.append(output[0])
history.append(test[t])

# Calculate MSE error
rmse = sqrt(mean_squared_error(test, predictions))
print("Test RMSE: %.3f" % rmse)

# Plot results
plt.plot(test)
plt.plot(predictions, color="red")
plt.show()

The results:

Basically, you will use 2 functions: .forecast() and .predict() The first function is for predictions outside of the dataset (which is why we used a loop in the example above), while the .predict() function is used for predictions inside the dataset.

Further, you can experiment with the p, q, and d parameters to get the best results. But even with these parameters, you can see that the model tracks the main trends well.

The code may process for up to 1-minute

Taak

Swipe to start coding

Create an ARIMA model and train it on the pr_air_quality.csv dataset.

  1. Within the for loop, create an ARIMA model using the history data and assign it to the model variable. Then, fit model to the data and save it as model_fit. Then, make forecasts using fitted model_fit.
  2. Calculate RMSE: take the square root (sqrt) of the mean squared error (calculated using the test and predictions).
  3. Visualize the results: display the test values within the first call of the .plot() function and predictions values within the second call.

Please note that the code may take a long time to complete

Oplossing

# Importing libraries
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from math import sqrt
import pandas as pd

# Reading data
df = pd.read_csv("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/pr_air_quality.csv")

# Preparing data
X = df["value"].values
size = int(len(X) * 0.97)
train, test = X[0:size], X[size:]
history = train.tolist()
predictions = list()

# Making foreasts with ARIMA model using a `for` loop
for t in range(len(test)):
model = ARIMA(history, order=(5, 1, 0))
model_fit = model.fit()
output = model_fit.forecast()
predictions.append(output[0])
history.append(test[t])

# Calculate MSE error
rmse = sqrt(mean_squared_error(test, predictions))
print("Test RMSE: %.3f" % rmse)

# Visualize results
plt.plot(test)
plt.plot(predictions, color="black")
plt.show()

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 4
# Importing libraries
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from math import sqrt
import pandas as pd

# Reading data
df = pd.read_csv("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/pr_air_quality.csv")

# Preparing data
X = df["value"].values
size = int(len(X) * 0.97)
train, test = X[0:size], X[size:]
history = train.tolist()
predictions = list()

# Making foreasts with ARIMA model using a `for` loop
for t in range(len(test)):
model = ___(___, order=(5, 1, 0))
model_fit = model.___()
output = model_fit.___()
predictions.append(output[0])
history.append(test[t])

# Calculate MSE error
rmse = ___(mean_squared_error(___, ___))
print("Test RMSE: %.3f" % rmse)

# Visualize results
plt.plot(___)
plt.plot(___, color="black")
plt.show()

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt