Home Wiki AI Fundamentals Time Series Analysis for Machine Data
AI Fundamentals

Time Series Analysis for Machine Data

What Is Time Series Analysis?

Imagine monitoring an industrial furnace temperature every minute for a week -- you get thousands of readings ordered by time. These readings are not random numbers; they carry hidden patterns: a gradual rise due to insulation wear, daily oscillations from on/off cycles, and random noise from sensor electronics.

A time series is a sequence of data points measured at regular time intervals. Analyzing it means understanding a system's past and predicting its future -- the core of predictive maintenance and industrial planning.

Components of a Time Series

Every time series can be decomposed into three fundamental components:

Trend

The long-term change in the data -- are values rising, falling, or staying flat over months or years?

Consider motor vibration: if the average vibration level slowly increases over 6 months, that upward trend signals gradual bearing wear.

Seasonality

Repeating patterns on a regular cycle -- daily, weekly, or yearly. In factories, electricity consumption drops at night and peaks during the day (daily cycle), while cement demand rises in summer and falls in winter (yearly cycle).

Noise (Residuals)

Random fluctuations that cannot be explained by trend or seasonality -- electrical interference in a sensor, a momentary vibration from a passing truck near the plant.

Observed value = Trend + Seasonality + Noise
y(t) = T(t) + S(t) + R(t)
import numpy as np

# Generate a synthetic time series (365 days)
days = np.arange(365)

# Trend: gradual increase (wear)
trend = 0.05 * days

# Seasonality: weekly cycle
seasonality = 10 * np.sin(2 * np.pi * days / 7)

# Random noise
noise = np.random.normal(0, 2, 365)

# Complete series
vibration = 50 + trend + seasonality + noise
print(f"Day 1: {vibration[0]:.1f} mm/s")
print(f"Day 365: {vibration[364]:.1f} mm/s")

Stationarity

A stationary time series has statistical properties (mean and variance) that do not change over time. Most classical forecasting models require stationary data.

Consider a stable production line: average daily output is about 500 units with +-20 unit fluctuation -- that is stationary. But if a new machine is added and output jumps to 700 units, the series is no longer stationary.

Testing Stationarity -- Augmented Dickey-Fuller (ADF) Test

from statsmodels.tsa.stattools import adfuller

# ADF test: null hypothesis = series is non-stationary
result = adfuller(vibration)
print(f"ADF statistic: {result[0]:.4f}")
print(f"p-value: {result[1]:.4f}")

if result[1] < 0.05:
    print("Series is stationary")
else:
    print("Series is non-stationary -- differencing needed")

Making a Series Stationary

The simplest approach is differencing: instead of using y(t), compute y(t) - y(t-1). This removes the trend component.

ARIMA: Classical Forecasting

ARIMA (AutoRegressive Integrated Moving Average) is the most widely used classical model for time series forecasting. Its name describes its three components:

Component Parameter Purpose
AutoRegressive (AR) p Uses past values to predict
Integrated (I) d Number of differencing steps for stationarity
Moving Average (MA) q Uses past forecast errors

ARIMA(p, d, q) -- for example, ARIMA(2, 1, 1) means: use the last 2 values, difference once, and use the last forecast error.

from statsmodels.tsa.arima.model import ARIMA

# Monthly electricity consumption (24 months)
power_consumption = [
    450, 470, 460, 480, 520, 550, 580, 590,
    560, 530, 490, 460, 470, 490, 480, 500,
    540, 570, 600, 610, 580, 550, 510, 480
]

# Fit ARIMA(1,1,1)
model = ARIMA(power_consumption, order=(1, 1, 1))
fitted = model.fit()

# Forecast next 3 months
forecast = fitted.forecast(steps=3)
print("Consumption forecast:")
for i, val in enumerate(forecast):
    print(f"  Month {25 + i}: {val:.0f} kWh")

How to Choose p, d, q?

  • d: Start with d=0 and test stationarity. If non-stationary, try d=1 then d=2.
  • p and q: Use autocorrelation function (ACF) and partial autocorrelation function (PACF) plots, or use auto_arima for automatic selection.

LSTM Networks: Deep Learning Forecasting

When temporal relationships are too complex for ARIMA -- such as predicting equipment failure from dozens of sensors -- we turn to LSTM (Long Short-Term Memory) networks.

Think of an LSTM as an experienced engineer who remembers important events from the distant past (a vibration spike 3 months ago) while ignoring irrelevant details (a brief fluctuation lasting seconds).

Why LSTM Instead of Standard RNN?

Standard Recurrent Neural Networks (RNNs) suffer from the vanishing gradient problem -- they forget old information. LSTM solves this with three gates:

Gate Function
Forget Gate Decides what to erase from memory
Input Gate Decides what to add to memory
Output Gate Decides what to send as output
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

# Prepare data: sliding windows of length 10
def create_sequences(data, window=10):
    X, y = [], []
    for i in range(len(data) - window):
        X.append(data[i:i+window])
        y.append(data[i+window])
    return np.array(X), np.array(y)

# Vibration data (normalized to 0-1)
vibration_data = np.random.rand(500).astype(np.float32)
X, y = create_sequences(vibration_data, window=10)
X = X.reshape(-1, 10, 1)  # (samples, timesteps, features)

# Build LSTM model
model = Sequential([
    LSTM(32, input_shape=(10, 1)),
    Dense(1)
])
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, epochs=10, batch_size=32, verbose=0)

# Predict
prediction = model.predict(X[-1:])
print(f"Next prediction: {prediction[0][0]:.4f}")

ARIMA vs. LSTM Comparison

Criterion ARIMA LSTM
Data type Univariate Multivariate
Data requirement Hundreds of points Thousands to millions
Interpretability High -- clear coefficients Low -- black box
Non-linear patterns Cannot capture Captures well
Training speed Seconds Minutes to hours
Best for Short-term, simple data Complex patterns, multiple sensors

Industrial Applications

Demand Forecasting

A cement plant needs to know next month's expected demand to determine raw material quantities and schedule shipments. A seasonal ARIMA (SARIMA) model captures yearly seasonality and produces reasonably accurate forecasts.

Degradation Modeling

Consider an industrial pump: we monitor vibration, temperature, and pressure daily. A multivariate LSTM learns the relationship between these sensors and predicts the Remaining Useful Life (RUL).

# Simplified example: estimating remaining useful life
sensors = {
    "vibration_trend": "rising",
    "temperature_trend": "stable",
    "pressure_trend": "slightly declining"
}

# Rule: rising vibration + declining pressure -> bearing wear
if sensors["vibration_trend"] == "rising" and "declining" in sensors["pressure_trend"]:
    print("Warning: possible bearing wear detected")
    print("Estimated remaining life: 45-60 days")
    print("Action: schedule maintenance within 30 days")

Anomaly Detection

Build an ARIMA or LSTM model that predicts the next value. If the actual value deviates from the prediction by more than 3 standard deviations, it is an anomaly worth investigating.

Production Quality Monitoring

Time series analysis of manufactured part dimensions reveals gradual drift before parts fall outside tolerances.

Practical Tips for Engineers

  1. Plot first -- Visualize the data before fitting any model. The eye catches patterns quickly.
  2. Check stationarity -- Run the ADF test before applying ARIMA.
  3. ARIMA as baseline -- Start with ARIMA as a baseline before moving to LSTM.
  4. Data size matters -- LSTM needs thousands of data points minimum. If your data is small, ARIMA is better.
  5. Normalization is essential -- Before LSTM, scale data to [0, 1] using MinMaxScaler.
  6. Time-based validation -- Do not use random cross-validation. Split data chronologically: older data for training, newer data for testing.
time-series ARIMA LSTM trend seasonality forecasting السلاسل الزمنية الاتجاه الموسمية التنبؤ نموذج ARIMA الذاكرة الطويلة