package stl
implements Seasonal-Trend Decompositions by LOESS of time series. It is a direct implementation of Cleveland et al (1990), which was written in Fortran '77.
This package does not implement the KD-Tree indexing for nearest neighbour search for the LOESS component as implemented in the original Netlib library. Instead, a straightforwards algorithm is implemented, focusing on clarity and understandability.
There were some parts that were "inlined" and unrolled manually for performance purposes - the tricube function and local neighbour functions for example. The original more mathematical implemetnations have been left in there for future reference.
Additionally, because multiplicative models are common in time series decompositions, Box-Cox transform functions (and generator) have also been provided.
This package has very minimal dependencies. These are the listing of the dependencies:
gorgonia.org/tensor
- used for storing the matrix data in the subcycle smoothing bits of the codegithub.com/pkg/error
- general errors management packagegithub.com/chewxy/tightywhities
- used in plotting ASCII charts in the examples/test.gorgonia.org/dawson
- used in tests to compare floating point numbers.
This package supports modules
Automated testing only tests up from Go 1.8 onwards. While I'm fairly confident that this package will work on those lower versions as well
import (
"encoding/csv"
"os"
"fmt"
"github.com/chewxy/stl"
)
func main() {
var data []float64
f, _ := os.Open("testdata/co2.csv")
r := csv.NewReader(f)
r.Read() // read header
for rec, err := r.Read(); err == nil; rec, err = r.Read() {
// here we're ignoring errors because we know the file to be correct
if co2, err := strconv.ParseFloat(rec[0], 64); err == nil {
data = append(data, co2)
}
}
// The main function:
res := stl.Decompose(data, 12, 35, stl.Additive(), stl.WithRobustIter(2), stl.WithIter(2))
fmt.Printf("%v", res.Seasonal)
fmt.Printf("%v", res.Trens)
fmt.Printf("%v", res.Resid)
This package is licenced with a MIT licence. I thank Rob Hyndman for writing a very excellent guide to STL, both in the R standard lib and in principle.