0
0
ML Pythonml~5 mins

Autocorrelation analysis in ML Python

Choose your learning style9 modes available
Introduction
Autocorrelation analysis helps us find patterns in data by checking if values repeat over time or space.
To see if daily temperatures follow a pattern over weeks.
To check if stock prices depend on their past values.
To find repeating signals in sensor data like heartbeats.
To detect seasonality in sales data over months.
To understand if errors in a model are related over time.
Syntax
ML Python
import numpy as np

def autocorrelation(x, lag=1):
    n = len(x)
    x_mean = np.mean(x)
    numerator = np.sum((x[:n-lag] - x_mean) * (x[lag:] - x_mean))
    denominator = np.sum((x - x_mean) ** 2)
    if denominator == 0:
        return float('nan')
    return numerator / denominator
The function calculates autocorrelation for a given lag (how far apart values are).
Lag 1 means comparing each value with the next one.
Examples
Calculates autocorrelation between each number and the next one.
ML Python
autocorrelation([1, 2, 3, 4, 5], lag=1)
Calculates autocorrelation between each number and the one two steps ahead.
ML Python
autocorrelation([1, 2, 3, 4, 5], lag=2)
Returns NaN because all values are the same, so no variation.
ML Python
autocorrelation([5, 5, 5, 5, 5], lag=1)
Sample Model
This program calculates how daily sales relate to the previous day (lag 1) and two days before (lag 2).
ML Python
import numpy as np

def autocorrelation(x, lag=1):
    n = len(x)
    x_mean = np.mean(x)
    numerator = np.sum((x[:n-lag] - x_mean) * (x[lag:] - x_mean))
    denominator = np.sum((x - x_mean) ** 2)
    if denominator == 0:
        return float('nan')
    return numerator / denominator

# Example data: daily sales over 7 days
sales = np.array([10, 12, 13, 12, 11, 13, 14])

# Calculate autocorrelation for lag 1 and lag 2
ac_lag1 = autocorrelation(sales, lag=1)
ac_lag2 = autocorrelation(sales, lag=2)

print(f"Autocorrelation at lag 1: {ac_lag1:.3f}")
print(f"Autocorrelation at lag 2: {ac_lag2:.3f}")
OutputSuccess
Important Notes
Autocorrelation values range from -1 to 1, where 1 means perfect positive correlation.
A value near 0 means no correlation at that lag.
High autocorrelation at certain lags can show repeating patterns or cycles.
Summary
Autocorrelation checks if data points relate to past points at different distances.
It helps find patterns like seasonality or trends in time series data.
You can calculate it easily with a simple formula comparing shifted data.