Skip to main content

Local Linear Trend Extraction for Time Series

Project description

๐Ÿ“ˆ AutoTrend: Local Linear Trend Extraction

Python License

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


๐Ÿ“ฆ Installation

pip install autotrend

Or install from source:

git clone https://github.com/chotanansub/autotrend.git
cd autotrend
pip install -e .

๐Ÿš€ Quick Start

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()}")

Output:

  • result.trend_marks: Array indicating which iteration labeled each point
  • result.prediction_marks: Predicted values for each point
  • result.models: List of LinearRegression models from each iteration
  • result.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:

  1. Single Model per Region: Train one linear regression model at the start of each focus region
  2. Trend Extension: Extend the trend line forward without retraining
  3. Error-Based Refinement: Identify high-error points and focus on them in the next iteration
  4. 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: 5)
    • max_models: Maximum iterations (default: 10)
    • 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
โ”‚   โ”‚   โ”œโ”€โ”€ llt_algorithm.py           # Core LLT implementation
โ”‚   โ”‚   โ”œโ”€โ”€ llt_result.py              # Result dataclass with plotting methods
โ”‚   โ”‚   โ”œโ”€โ”€ decompose_llt_class.py     # Object-based API (DecomposeLLT)
โ”‚   โ”‚   โ”œโ”€โ”€ functional_api.py          # Functional API (decompose_llt)
โ”‚   โ”‚   โ””โ”€โ”€ 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/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ demo_runner.py                 # Demo configuration and utilities
โ”‚   โ”œโ”€โ”€ cases/
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ simple_wave.py             # Sine wave demo
โ”‚   โ”‚   โ”œโ”€โ”€ nonstationary.py           # Non-stationary wave demo
โ”‚   โ”‚   โ””โ”€โ”€ piecewise_linear.py        # Piecewise linear demo
โ”‚   โ””โ”€โ”€ run_all.py                     # Run all demos
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ 01_quick_start.py
โ”‚   โ””โ”€โ”€ 02_basic_usage.py
โ”œโ”€โ”€ output/                            # Generated plots and logs
โ”‚   โ”œโ”€โ”€ simple_wave/
โ”‚   โ”œโ”€โ”€ nonstationary_wave/
โ”‚   โ””โ”€โ”€ piecewise_linear/
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ setup.py
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ MANIFEST.in
โ”œโ”€โ”€ update_package.sh
โ””โ”€โ”€ .gitignore

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

autotrend-0.2.1.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

autotrend-0.2.1-py3-none-any.whl (29.1 kB view details)

Uploaded Python 3

File details

Details for the file autotrend-0.2.1.tar.gz.

File metadata

  • Download URL: autotrend-0.2.1.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for autotrend-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1bc59570edadce52b6856c01fbbd6445b04c0d8880cbbbbc1647f83067f7ed3b
MD5 4178a84297980be4e26d9a5ec42abf77
BLAKE2b-256 909fb19de9e5ffc748b84174caa2950a637b3943ce3a148d35a0cd5bbdda8f88

See more details on using hashes here.

File details

Details for the file autotrend-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: autotrend-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 29.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for autotrend-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1494c639c47a9ff9eade6f445be774f4451b1e6af52f9b40ed54910e7315e0c1
MD5 59e15cf4101e973570f57d49d4b66563
BLAKE2b-256 64818289441dc9ffbfc26fb6e2fd24d32b4f6578d16d46b5b7a521d61d47ece4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page