6 releases

0.3.1 Jul 11, 2024
0.3.0 Sep 25, 2023
0.2.2 Jun 20, 2023
0.2.1 Dec 16, 2021
0.1.0 Oct 15, 2021

#89 in Math

Download history 120/week @ 2024-08-02 31/week @ 2024-08-09 99/week @ 2024-08-16 14/week @ 2024-08-23 152/week @ 2024-08-30 177/week @ 2024-09-06 84/week @ 2024-09-13 503/week @ 2024-09-20 158/week @ 2024-09-27 947/week @ 2024-10-04 1212/week @ 2024-10-11 310/week @ 2024-10-18 419/week @ 2024-10-25 543/week @ 2024-11-01 1000/week @ 2024-11-08 670/week @ 2024-11-15

2,716 downloads per month
Used in 7 crates (2 directly)

Unlicense OR MIT

38KB
831 lines

STL Rust

Seasonal-trend decomposition for Rust

🎉 Zero dependencies

Build Status

Installation

Add this line to your application’s Cargo.toml under [dependencies]:

stlrs = "0.3"

Getting Started

Decompose a time series

use stlrs::Stl;

let series = vec![
    5.0, 9.0, 2.0, 9.0, 0.0, 6.0, 3.0, 8.0, 5.0, 8.0,
    7.0, 8.0, 8.0, 0.0, 2.0, 5.0, 0.0, 5.0, 6.0, 7.0,
    3.0, 6.0, 1.0, 4.0, 4.0, 4.0, 3.0, 7.0, 5.0, 8.0
];
let period = 7; // period of the seasonal component

let res = Stl::fit(&series, period).unwrap();

Get the components

res.seasonal();
res.trend();
res.remainder();

Robustness

Use robustness iterations

let res = Stl::params().robust(true).fit(&series, period).unwrap();

Get robustness weights

res.weights();

Multiple Seasonality

Specify multiple periods

use stlrs::Mstl;

let res = Mstl::fit(&series, &[7, 365]).unwrap();

Parameters

Set STL parameters

Stl::params()
    .seasonal_length(7)     // length of the seasonal smoother
    .trend_length(15)       // length of the trend smoother
    .low_pass_length(7)     // length of the low-pass filter
    .seasonal_degree(0)     // degree of locally-fitted polynomial in seasonal smoothing
    .trend_degree(1)        // degree of locally-fitted polynomial in trend smoothing
    .low_pass_degree(1)     // degree of locally-fitted polynomial in low-pass smoothing
    .seasonal_jump(1)       // skipping value for seasonal smoothing
    .trend_jump(2)          // skipping value for trend smoothing
    .low_pass_jump(1)       // skipping value for low-pass smoothing
    .inner_loops(2)         // number of loops for updating the seasonal and trend components
    .outer_loops(0)         // number of iterations of robust fitting
    .robust(false)          // if robustness iterations are to be used

Set MSTL parameters

Mstl::params()
    .iterations(2)                   // number of iterations
    .lambda(0.5)                     // lambda for Box-Cox transformation
    .seasonal_lengths(&[11, 15])     // lengths of the seasonal smoothers
    .stl_params(Stl::params())       // STL params

Strength

Get the seasonal strength

res.seasonal_strength();

Get the trend strength

res.trend_strength();

Credits

This library was ported from the Fortran implementation.

References

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/stl-rust.git
cd stl-rust
cargo test

No runtime deps