Local Linear Trend Extraction for Time Series
Project description
๐ AutoTrend: Local Linear Trend Extraction
AutoTrend is a lightweight, iterative method for extracting local linear trends from time series data. Unlike traditional sliding window approaches that fit a model at every point, AutoTrend achieves computational efficiency by training a single linear regression model per focus region and extending the trend forward, measuring prediction errors without repeated model fitting.
๐ Demo: Google Colab
๐ Quick Start
import numpy as np
from autotrend import decompose_llt, plot_full_decomposition
# Generate or load your time series
sequence = np.sin(np.linspace(0, 50, 500)) + np.linspace(0, 5, 500)
# Run LLT decomposition
result = decompose_llt(
seq=sequence,
max_models=5,
window_size=10,
error_percentile=40
)
# Visualize results
plot_full_decomposition(sequence, result)
# Access results
print(f"Number of iterations: {result.get_num_iterations()}")
print(f"Trend segments: {result.get_trend_segments()}")
Output:
result.trend_marks: Array indicating which iteration labeled each pointresult.prediction_marks: Predicted values for each pointresult.models: List of LinearRegression models from each iterationresult.process_logs: Detailed logs for visualization
๐ก Core Concept
The Problem
Traditional sliding window regression methods fit a new model at every time point, leading to high computational costs. Change point detection methods often require complex algorithms and parameter tuning.
The Solution
AutoTrend uses an iterative, focus-based approach:
- Single Model per Region: Train one linear regression model at the start of each focus region
- Trend Extension: Extend the trend line forward without retraining
- Error-Based Refinement: Identify high-error points and focus on them in the next iteration
- Adaptive Segmentation: Automatically discover trend boundaries based on prediction error
Key Advantages
โ
Computationally Efficient: Minimal model training compared to full sliding windows
โ
Adaptive: Automatically discovers trend boundaries without predefined change points
โ
Interpretable: Clear linear segments with explicit slopes and intercepts
โ
Flexible: Adjustable error thresholds and iteration limits
โ
Lightweight: No complex optimization or parameter search required
โ๏ธ Algorithm Overview
Input
- Sequence: Univariate time series
y = [yโ, yโ, ..., yโ] - Parameters:
window_size: Size of training window (default: 10)max_models: Maximum iterations (default: 5)error_percentile: Error threshold percentile (default: 40)percentile_step: Increment per iteration (default: 0)update_threshold: Whether to update threshold each iteration (default: False)
Process
Step 1: Initialization
Define initial focus targets covering all predictable points:
focus_targets = [window_size, window_size+1, ..., T-1]
Step 2: Train Linear Model
For each iteration, train a model on the first window of the focus region:
X_train = [0, 1, ..., window_size-1]
y_train = sequence[start:end]
model = LinearRegression().fit(X_train, y_train)
Step 3: Extend Trend and Measure Error
Predict forward using the trained model's trend offset:
ฮ = ลท_window_size - y_start
ลท_t = y_(t-window_size) + ฮ
error_t = |y_t - ลท_t|
Step 4: Segment by Error Threshold
threshold = percentile(errors, error_percentile)
low_error_points = {t | error_t โค threshold}
high_error_points = {t | error_t > threshold}
- Low error points: Assigned to current iteration, marked as resolved
- High error points: Become focus targets for next iteration
Step 5: Iterate
Repeat Steps 2-4 on high-error regions until:
- All points meet the error criterion, OR
- Maximum iterations reached
Output
LLTResult(
trend_marks: np.ndarray, # Iteration labels for each point
prediction_marks: np.ndarray, # Predicted values
models: List[LinearRegression], # Trained models per iteration
process_logs: List[Tuple] # Detailed iteration logs
)
๐ Directory Structure
autotrend/
โโโ autotrend/
โ โโโ __init__.py # Main package exports
โ โโโ core/
โ โ โโโ __init__.py
โ โ โโโ local_linear_trend.py # Core LLT algorithm
โ โ โโโ utility.py # Helper functions (extract_ranges, split_by_gap)
โ โโโ data/
โ โ โโโ __init__.py
โ โ โโโ sythn_data/
โ โ โ โโโ __init__.py
โ โ โ โโโ generate_simple_wave.py # Stationary sine wave generator
โ โ โ โโโ generate_nonstationary_wave.py # Amplitude-modulated wave generator
โ โ โ โโโ generate_piecewise_linear.py # Piecewise linear sequence generator
โ โ โโโ datasets/ # Future: Real-world dataset loaders
โ โโโ visualization/
โ โ โโโ __init__.py
โ โ โโโ plot.py # Main plotting module
โ โ โโโ plot_error.py # Error analysis visualization
โ โ โโโ plot_slope.py # Slope comparison plots
โ โ โโโ plot_full_decomposition.py # Full decomposition view
โ โ โโโ plot_iteration_grid.py # Iteration grid visualization
โ โ โโโ plot_model_statistics.py # Model statistics plots
โ โโโ decomposition/
โ โโโ __init__.py # Future: Trend-seasonal decomposition
โโโ demo/
โ โโโ demo_utils.py # Demo configuration and utilities
โ โโโ simple_wave.py # Sine wave demo
โ โโโ piecewise_linear.py # Piecewise linear demo
โ โโโ run_all.py # Run all demos
โโโ output/ # Generated plots and logs
โ โโโ simple_wave/
โ โโโ piecewise_linear/
โโโ README.md
โโโ requirements.txt
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autotrend-0.1.0.tar.gz.
File metadata
- Download URL: autotrend-0.1.0.tar.gz
- Upload date:
- Size: 19.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ecccfc84441f5c08d9ef5d87ac05501586b1f7313b72a30621beefc628732d7
|
|
| MD5 |
b42dd94a14fa4dea0cacad24ff26fed4
|
|
| BLAKE2b-256 |
bd3b6d133fc49215318546363e323beed687f05e17fa72a41dceb5e05e63f13e
|
File details
Details for the file autotrend-0.1.0-py3-none-any.whl.
File metadata
- Download URL: autotrend-0.1.0-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cca3647b59824bc4813f6a9df30288ae2327fc486bac6d48b9c4d5c07a664ee8
|
|
| MD5 |
645d6ea9c662c3e0074cab20c291b560
|
|
| BLAKE2b-256 |
f2206aa76bc32a2679310b996bf48d81538bb3f332f61684a974d587ae60753c
|