π MovingAverage
π MovingAverage
π― Overview
The MovingAverage layer extracts trend components from time series data by computing a moving average over a specified window. This is a crucial component in time series decomposition, separating trend from seasonal patterns.
The layer applies padding at both ends (replicating first and last values) to maintain the temporal dimension, making it ideal for sequence-to-sequence models.
π How It Works
The moving average operates through these steps:
- Padding: Replicates first and last values at both ends to maintain sequence length
- Window Computation: Slides a window of size
kernel_sizeacross the temporal dimension - Averaging: Computes mean of values within each window
- Output: Returns trend component with same shape as input
graph LR
A["Input<br/>(batch, time, features)"] --> B["Pad Front & End<br/>Replicate Edges"]
B --> C["Create Windows<br/>of Size K"]
C --> D["Compute Mean<br/>Per Window"]
D --> E["Output<br/>(batch, time, features)"]
style A fill:#e6f3ff,stroke:#4a86e8
style E fill:#e8f5e9,stroke:#66bb6a
style C fill:#fff9e6,stroke:#ffb74d
π‘ Why Use This Layer?
| Challenge | Traditional Approach | MovingAverage Solution |
|---|---|---|
| Trend Extraction | Manual computation | π― Automatic trend extraction |
| Sequence Length | Output shorter than input | β Preserves temporal dimension |
| Edge Effects | Loses information at boundaries | π Replicates boundary values |
| Decomposition | Complex preprocessing | π§© Integrates seamlessly |
π Use Cases
- Time Series Decomposition: Extract trend from seasonal + residual components
- Signal Smoothing: Reduce noise while preserving temporal structure
- Trend Analysis: Identify long-term patterns in time series
- Preprocessing: Prepare data for forecasting models
- Pattern Recognition: Detect seasonal cycles separate from trends
π Quick Start
Basic Usage
1 2 3 4 5 6 7 8 9 10 11 | |
Time Series Decomposition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
In a Model Pipeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
π API Reference
kerasfactory.layers.MovingAverage
Moving Average layer for time series trend extraction.
Classes
MovingAverage
1 2 3 | |
Extracts the trend component using moving average.
This layer computes a moving average over time series to extract the trend component. It applies padding at both ends to maintain the temporal dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel_size |
int
|
Size of the moving average window. |
required |
name |
str | None
|
Optional name for the layer. |
None
|
Input shape
(batch_size, time_steps, channels)
Output shape
(batch_size, time_steps, channels)
Example
1 2 3 4 5 6 7 8 9 10 | |
Initialize the MovingAverage layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel_size |
int
|
Size of the moving average kernel. |
required |
name |
str | None
|
Optional layer name. |
None
|
**kwargs |
Any
|
Additional keyword arguments. |
{}
|
Source code in kerasfactory/layers/MovingAverage.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
π§ Parameters Deep Dive
kernel_size (int)
- Purpose: Size of the moving average window
- Range: 1 to sequence_length
- Default: None (required)
- Impact: Larger kernel β more smoothing, more trend preservation
- Recommendation: For daily data with weekly patterns, use 7; for monthly patterns, use 25-30
π Performance Characteristics
- Speed: β‘β‘β‘β‘ Very fast - O(n) complexity
- Memory: πΎπΎ Minimal - only input/output tensors
- Accuracy: π―π―π―π― Preserves trends perfectly
- Best For: Quick preprocessing and trend extraction
π¨ Examples
Example 1: Smooth Noisy Signal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Example 2: Multi-Scale Decomposition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Example 3: Trend Extraction for Forecasting
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
π‘ Tips & Best Practices
- Kernel Size Selection: Choose based on the periodicity of your data
- Odd Kernel Sizes: Use odd sizes (3, 5, 7, ...) for symmetric padding
- Memory Efficient: Very memory-efficient even for long sequences
- GPU Friendly: Fully compatible with GPU acceleration
- Differentiable: Gradients flow properly for training
β οΈ Common Pitfalls
- Kernel Size Too Large: May over-smooth and remove important details
- Kernel Size Too Small: May not capture true trend, only high-frequency noise
- Edge Effects: First and last values are replicated - consider this in interpretation
- Data Normalization: Input data should be normalized for best results
π Related Layers
- SeriesDecomposition - Complete decomposition with moving average
- DFTSeriesDecomposition - Frequency-based decomposition
- MultiScaleSeasonMixing - Multi-scale pattern mixing
- MultiScaleTrendMixing - Trend pattern mixing
π Further Reading
- Moving Average Filters - Mathematical foundations
- Time Series Decomposition - Decomposition concepts
- Signal Processing Basics - Signal smoothing
- KerasFactory Layer Explorer - Browse all available layers