Skip to content

ankane/stl-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

152 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

STL C++

Seasonal-trend decomposition for C++

Build Status

Installation

Add the header to your project (supports C++20 and greater).

There is also support for CMake and FetchContent:

include(FetchContent)

FetchContent_Declare(stl GIT_REPOSITORY https://github.com/ankane/stl-cpp.git GIT_TAG v0.3.0)
FetchContent_MakeAvailable(stl)

target_link_libraries(app PRIVATE stl::stl)

Getting Started

Include the header

#include "stl.hpp"

Decompose a time series

std::vector<float> series{
    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
};
size_t period = 7; // period of seasonal component

stl::Stl fit{series, period};

Get the components

const std::vector<float>& seasonal = fit.seasonal();
const std::vector<float>& trend = fit.trend();
const std::vector<float>& remainder = fit.remainder();

Robustness

Use robustness iterations

stl::Stl fit{series, period, {.robust = true}};

Get robustness weights

const std::vector<float>& weights = fit.weights();

Multiple Seasonality

Specify multiple periods

stl::Mstl fit{series, {7, 365}};

Parameters

Set STL parameters

stl::StlParams 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
};

stl::Stl fit{series, period, params};

Set MSTL parameters

stl::MstlParams 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::Mstl fit{series, period, params};

Strength

Get the seasonal strength for STL

double strength = fit.seasonal_strength();

Get the seasonal strength for MSTL

std::vector<double> strength = fit.seasonal_strength();

Get the trend strength

double strength = fit.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-cpp.git
cd stl-cpp
cmake -S . -B build
cmake --build build
build/test

About

Seasonal-trend decomposition for C++

Resources

License

Unlicense, MIT licenses found

Licenses found

Unlicense
UNLICENSE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

 
 
 

Contributors