Skip to main content

A python library for timeseries smoothing and outlier detection in a vectorized way.

Project description

tsmoothie

A python library for timeseries smoothing and outlier detection in a vectorized way.

Overview

tsmoothie computes, in a fast and efficient way, the smoothing of single or multiple timeseries.

The smoothing tecniques available are:

  • Exponential Smoothing
  • Convolutional Smoothing with various window types (constant, hanning, hamming, bartlett, blackman)
  • Polynomial Smoothing
  • Spline Smoothing of various kind (linear, cubic, natural cubic)
  • Gaussian Smoothing
  • Binner Smoothing
  • LOWESS
  • Kalman Smoothing with customizable components (level, trend, seasonality, long seasonality)

tsmoothie provides the calculation of intervals as result of the smoothing process. This can be useful to identify outliers and anomalies in timeseries.

The interval types available are:

  • sigma intervals
  • confidence intervals
  • predictions intervals
  • kalman intervals

The adoption of this type of intervals depends on the smoothing method used.

tsmoothie can also carry out a sliding smoothing approach. This is possible splitting the timeseries into equal sized pieces and smoothing them independently. As always, this functionality is implemented in a vectorized way through the WindowWrapper class.

Media

Blog Posts:

  • Timeseries Smoothing for better clustering (cooming soon)
  • Timeseries Smoothing for better forecasting (cooming soon)

Installation

pip install tsmoothie

The module depends only on NumPy, SciPy and simdkalman. Python 3.5 or above is supported.

Usage

Below a couple of examples of how tsmoothie works. Full examples are available in the notebooks folder.

# import libraries
import numpy as np
import matplotlib.pyplot as plt
from tsmoothie.utils_func import sim_randomwalk
from tsmoothie.smoother import LowessSmoother

# generate 3 randomwalks of lenght 200
np.random.seed(123)
data = sim_randomwalk(n_series=3, timesteps=200, 
                      process_noise=10, measure_noise=30)

# operate smoothing
smoother = LowessSmoother(smooth_fraction=0.1, iterations=1)
smoother.smooth(data)

# generate intervals
low, up = smoother.get_intervals('prediction_interval')

# plot the smoothed timeseries with intervals
plt.figure(figsize=(18,5))

for i in range(3):

    plt.subplot(1,3,i+1)
    plt.plot(smoother.smooth_data[i], linewidth=3, color='blue')
    plt.plot(smoother.data[i], '.k')
    plt.title(f"timeseries {i+1}"); plt.xlabel('time')

    plt.fill_between(range(len(smoother.data[i])), low[i], up[i], alpha=0.3)

Randomwalk Smoothing

# import libraries
import numpy as np
import matplotlib.pyplot as plt
from tsmoothie.utils_func import sim_seasonal_data
from tsmoothie.smoother import LowessSmoother

# generate 3 periodic timeseries of lenght 300
np.random.seed(123)
data = sim_seasonal_data(n_series=3, timesteps=300, 
                         freq=24, measure_noise=30)

# operate smoothing
smoother = LowessSmoother(smooth_fraction=0.05, iterations=1)
smoother.smooth(data)

# generate intervals
low, up = smoother.get_intervals('prediction_interval')

# plot the smoothed timeseries with intervals
plt.figure(figsize=(18,5))

for i in range(3):

    plt.subplot(1,3,i+1)
    plt.plot(smoother.smooth_data[i], linewidth=3, color='blue')
    plt.plot(smoother.data[i], '.k')
    plt.title(f"timeseries {i+1}"); plt.xlabel('time')

    plt.fill_between(range(len(smoother.data[i])), low[i], up[i], alpha=0.3)

Sinusoidal Smoothing

References

  • Polynomial, Spline, Gaussian and Binner smoothing are carried out building a regression on custom basis expansions. These implementations are based on the amazing intutions of Matthew Drury available here
  • Time Series Modelling with Unobserved Components, Matteo M. Pelagatti

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

tsmoothie-0.1.5.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

tsmoothie-0.1.5-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file tsmoothie-0.1.5.tar.gz.

File metadata

  • Download URL: tsmoothie-0.1.5.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for tsmoothie-0.1.5.tar.gz
Algorithm Hash digest
SHA256 e0dfac7cc67ee946e8eee37594c7a8acf923bee67ea9b86996685e96da93107c
MD5 a0085a8222def6e28d5c06637f89348c
BLAKE2b-256 faca90eac4dcb42ee970f52f4411f16ac3037a0c43ac28e9e438cd12f67e0d8f

See more details on using hashes here.

File details

Details for the file tsmoothie-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: tsmoothie-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for tsmoothie-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 39786bdaa0d2d7c3ab3e526ecf0fcc501c4bf51c7051c7f085e98249ce7ae178
MD5 fbc06df1aec7348ab620248f042af9c6
BLAKE2b-256 5e3953e4e5fcb7b9781d5ff96c600ebb5b2e523a5cc67972dc354ed8fb426ec2

See more details on using hashes here.

Supported by

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