Local Linear Trend Extraction for Time Series
A lightweight, iterative method for extracting local linear trends from time series data with computational efficiency and automatic trend boundary discovery.
pip install autotrend
Minimal model training compared to full sliding windows
Automatically discovers trend boundaries without predefined change points
Clear linear segments with explicit slopes and intercepts
Adjustable error thresholds and iteration limits
pip install autotrend
Or install from source:
git clone https://github.com/chotanansub/autotrend.git
cd autotrend
pip install -e .
import numpy as np
from autotrend import decompose_llt
# 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
result.plot_full_decomposition()
# Access results
print(f"Number of iterations: {result.get_num_iterations()}")
print(f"Trend segments: {result.get_trend_segments()}")
The Problem: Traditional sliding window regression methods fit a new model at every time point, leading to high computational costs.
The Solution: AutoTrend uses an iterative, focus-based approach:
from autotrend import decompose_llt
result = decompose_llt(
seq, # 1D input sequence
max_models=10, # Maximum number of iterations
window_size=5, # Length of training window
error_percentile=40, # Error threshold percentile
percentile_step=0, # Increment per iteration
update_threshold=False, # Update threshold each iteration
verbose=2, # Verbosity level (0-2)
store_sequence=True # Store sequence for plotting
)
from autotrend import DecomposeLLT
# Create decomposer with custom parameters
decomposer = DecomposeLLT(
max_models=10,
window_size=20,
error_percentile=40,
update_threshold=True,
percentile_step=2
)
# Fit to sequence
result = decomposer.fit(sequence)
# Or fit and plot in one call
result = decomposer.fit_plot(
sequence,
plot_types=['full_decomposition'],
show=True
)
# Access components
trends = result.trend_marks # Iteration labels for each point
predictions = result.prediction_marks # Predicted values
models = result.models # LinearRegression models
# Get summary information
n_iterations = result.get_num_iterations()
segments = result.get_trend_segments()
indices, preds = result.get_predictions_by_iteration(iteration=1)
# Plotting methods
result.plot_error()
result.plot_slopes()
result.plot_full_decomposition()
result.plot_iteration_grid()
result.plot_statistics()
result.plot_all(output_dir="results/")
AutoTrend provides comprehensive visualization tools:
# Generate animation
from autotrend import animate_error_threshold
animate_error_threshold(
result,
output_path='animation.gif',
sliding_mode=True,
fps=5
)
Built-in synthetic data generators for testing:
from autotrend import (
generate_simple_wave,
generate_nonstationary_wave,
generate_piecewise_linear
)
# Simple sine wave
wave = generate_simple_wave(
length=500,
frequency=5.0,
add_noise=True
)
# Non-stationary wave with amplitude modulation
nonstat = generate_nonstationary_wave(
add_noise=True,
noise_strength=2
)
# Piecewise linear segments
piecewise = generate_piecewise_linear(
trends=['increase', 'decrease', 'steady'],
total_length=300,
min_seg_len=50,
max_seg_len=150
)
| Parameter | Description | Default |
|---|---|---|
window_size |
Size of training window. Smaller values capture finer trends. | 5 |
max_models |
Maximum iterations. More iterations allow finer refinement. | 10 |
error_percentile |
Threshold percentile (0-100). Lower values are more strict. | 40 |
percentile_step |
Increment per iteration. Enables adaptive thresholding. | 0 |
update_threshold |
Whether to update threshold each iteration. | False |
verbose |
Verbosity level: 0 (silent), 1 (progress), 2 (detailed). | 2 |
See the examples directory for complete usage examples:
01_quick_start.py - Minimal example02_basic_usage.py - API patterns and customizationRun demos:
python demo/run_all.py
For detailed algorithm description, see the full README.
Key Steps:
Contributions are welcome! Please see our GitHub issues page.
AutoTrend is released under the MIT License. See LICENSE for details.