Skip to main content

Time Series Decomposition

Time series decomposition is the process of breaking a time series into its constituent components: trend (long-term direction), seasonal (repeating cyclical patterns), and residual (random fluctuations remaining after trend and seasonality are removed). Understanding these components helps you choose the right forecasting model and diagnose data quality issues.

FunctionDescription
ts_mstl_decomposition_byMultiple Seasonal-Trend decomposition (MSTL)
ts_detrend_byRemove trend using multiple methods
ts_detect_peaks_byDetect local maxima with prominence
ts_analyze_peak_timing_byAnalyze peak timing variability
Showing 4 of 4

ts_mstl_decomposition_by

Multiple Seasonal-Trend decomposition using Loess (MSTL). Decomposes a time series into trend, one or more seasonal components, and residual.

Parameters

ParameterTypeRequiredDescription
sourceVARCHARYesSource table name
group_colIDENTIFIERYesGroup column for multi-series (unquoted)
date_colIDENTIFIERYesDate column (unquoted)
value_colIDENTIFIERYesValue column (unquoted)
seasonal_periodsINTEGER[]YesSeasonal periods to decompose
paramsMAPNoConfiguration (use MAP{} for defaults)

Output

FieldTypeDescription
group_col(input)Series identifier
trendDOUBLE[]Trend component
seasonalDOUBLE[][]Seasonal components (one per period)
remainderDOUBLE[]Remainder after removing trend and seasonality
periodsINTEGER[]Seasonal periods used

Examples

-- Single seasonality (weekly)
SELECT * FROM ts_mstl_decomposition_by(
'sales_data',
product_id,
date,
sales,
[7],
MAP{}
);

-- Multiple seasonalities (daily + weekly for hourly data)
SELECT * FROM ts_mstl_decomposition_by(
'hourly_demand',
sensor_id,
timestamp,
demand,
[24, 168],
MAP{}
);

-- Weekly + yearly for daily data
SELECT * FROM ts_mstl_decomposition_by(
'daily_sales',
product_id,
date,
revenue,
[7, 365],
MAP{}
);

Interpretation Guide

ComponentWhat It ShowsBusiness Use
TrendLong-term directionGrowth/decline analysis
SeasonalRepeating patternsCapacity planning, promotions
ResidualRandom fluctuationsAnomaly detection, forecast uncertainty

Residual Analysis:

  • Large residuals indicate unusual events or data quality issues
  • Patterns in residuals suggest missing seasonality
  • Residual variance indicates forecast uncertainty

Common Seasonal Periods

Data FrequencyCommon Periods
Daily7 (weekly), 365 (yearly)
Hourly24 (daily), 168 (weekly)
Weekly52 (yearly)
Monthly12 (yearly)
Quarterly4 (yearly)

Decomposition Workflow

-- 1. Detect seasonality first
SELECT id, (periods).primary_period
FROM ts_detect_periods_by('sales_data', product_id, date, sales, MAP{});

-- 2. Use detected periods for decomposition
SELECT * FROM ts_mstl_decomposition_by(
'sales_data',
product_id,
date,
sales,
[7, 365],
MAP{}
);

ts_detrend_by

Remove trend from grouped time series.

ts_detrend_by(
source VARCHAR,
group_col IDENTIFIER,
date_col IDENTIFIER,
value_col IDENTIFIER,
method VARCHAR
)TABLE

Methods:

MethodDescriptionBest For
'linear'Linear trend via least squaresSimple linear trends
'quadratic'Quadratic polynomialCurved trends
'cubic'Cubic polynomialComplex curves
'auto'Automatic selectionUnknown trend type

Returns:

ColumnTypeDescription
group_col(input)Series identifier
trendDOUBLE[]Extracted trend component
detrendedDOUBLE[]Detrended values
methodVARCHARMethod used
coefficientsDOUBLE[]Polynomial coefficients
rssDOUBLEResidual sum of squares
n_paramsBIGINTNumber of parameters

Example:

-- Linear detrending
SELECT * FROM ts_detrend_by('sales', product_id, date, revenue, 'linear');

-- Automatic method selection
SELECT * FROM ts_detrend_by('sales', product_id, date, revenue, 'auto');

Use Cases:

  • Preparing data for stationarity tests
  • Isolating seasonal patterns
  • Removing growth effects for comparison

ts_detect_peaks_by

Detect peaks (local maxima) in grouped time series data with prominence calculation.

ts_detect_peaks_by(
source VARCHAR,
group_col COLUMN,
date_col COLUMN,
value_col COLUMN,
params MAP
)TABLE(id, peaks)

Params MAP Options:

KeyTypeDefaultDescription
min_distanceVARCHAR'1.0'Minimum distance between peaks
min_prominenceVARCHAR'0.0'Minimum peak prominence threshold
smooth_firstVARCHAR'false'Smooth data before peak detection

Returns peaks STRUCT:

FieldTypeDescription
peaks[]STRUCT[]Array of {index, time, value, prominence}
n_peaksINTEGERNumber of peaks detected
inter_peak_distances[]DOUBLE[]Distances between consecutive peaks
mean_periodDOUBLEAverage inter-peak distance

Example:

-- Detect all peaks
SELECT id, (peaks).n_peaks, (peaks).mean_period
FROM ts_detect_peaks_by('daily_data', series_id, date, value, MAP{});

-- Detect significant peaks only (prominence > 2.0)
SELECT id, (peaks).n_peaks
FROM ts_detect_peaks_by('sales', product_id, date, revenue,
MAP{'min_prominence': '2.0'});

Use Cases:

  • Finding demand spikes
  • Identifying seasonal peaks
  • Anomaly detection

ts_analyze_peak_timing_by

Analyze peak timing regularity across seasonal cycles.

ts_analyze_peak_timing_by(
source VARCHAR,
group_col COLUMN,
date_col COLUMN,
value_col COLUMN,
period DOUBLE,
params MAP
)TABLE(id, timing)

Returns timing STRUCT:

FieldTypeDescription
n_peaksINTEGERNumber of peaks analyzed
peak_times[]DOUBLE[]Peak positions
variability_scoreDOUBLELower = more stable
is_stableBOOLEANConsistent peak timing?

Example:

-- Analyze weekly peak timing
SELECT id, (timing).is_stable, (timing).variability_score
FROM ts_analyze_peak_timing_by('sales', product_id, date, value, 7.0, MAP{});

Interpretation:

  • is_stable = TRUE: Peaks occur at predictable times (good for planning)
  • is_stable = FALSE: Peak timing varies (need flexible capacity)

🍪 Cookie Settings