Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Autocorrelation | Time Series Processing
Time Series Analysis

book
Autocorrelation

The next characteristic we will analyze is autocorrelation.

Autocorrelation measures how much future values in a time series depend linearly on past values. What examples can we give?

The graph above shows the popularity of the names "Maria" and "Olivia" over 140 years. Olivia's autocorrelation decays much faster than Maria's: this can be explained by the fact that the popularity of the name Olivia was very low until 1980 and then increased very sharply. While the popularity of the name Maria did not have such sharp jumps and developed approximately the same over time.

Let's visualize the autocorrelation:

python
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf


# Read dataset
dataset = pd.read_csv("AirPassengers.csv",
header=0, index_col=0, parse_dates=True,
squeeze=True)


# Plot the autocorrelation
plot_acf(dataset, lags=22)

Let's see how to interpret this chart. The graph shows the last 22 values from the dataset (they are shown as vertical lines). If these lines fall within the shaded blue area, this means that they do not have a significant correlation with the previous values.

As you can see on the graph, the first 13 values are correlated with the previous ones, while the next ones are not.

In summary, autocorrelation is useful for identifying statistically significant relationships between values in a time series.

Uppgift

Swipe to start coding

Visualize the autocorrelation of the following dataset air_quality_no2_long.csv for 30 records.

  1. Import the plot_acf function from statsmodels.graphics.tsaplots.
  2. Visualize the autocorrelation for 30 "value" records of the data DataFrame.

Lösning

# Importing libraries
import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf

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

# Visualize autocorrelation
plot_acf(data["value"], lags=30)

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
# Importing libraries
import pandas as pd
from ___

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

# Visualize autocorrelation
plot_acf(___["___"], lags=___)

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

We use cookies to make your experience better!
some-alt