npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

arima-ts

v0.0.1

Published

ARIMA, SARIMA, SARIMAX and AutoARIMA models for time series analysis and forecasting

Downloads

107

Readme

Moving Average Pt2 Unfinished Project

Please check PT1 https://github.com/IbrahimShamma99/moving-average-algorithms

ARIMA. Time-series forecasting in browsers and Node.js

Emscripten port of the native C package ctsa for time series analysis and forecasting

This CommonJS module includes:

  • ARIMA (Autoregressive Integrated Moving Average)
  • SARIMA (Seasonal ARIMA)
  • SARIMAX (Seasonal ARIMA with exogenous variables)
  • AutoARIMA (ARIMA with automatic parameters)

Installation of the ARIMA module

npm install arima-ts

Initialization

const ARIMA = require('moving-average-arima')
const arima = new ARIMA(options)

Where the options object can include:

  • auto - automatic moving-average-arima (default: false)
  • p, d, q params for ARIMA (default: p: 1, d: 0, q: 1)
  • P, D, Q, s seasonal params (default: 0s). Setting them to non-zero values makes the ARIMA model seasonal
  • method - ARIMA method (default: 0, described below)
  • optimizer - optimization method (default: 6, described below)
  • transpose - transpose exogenous array when fitting SARIMAX (default: false)
  • verbose - verbose output (default: true)

Also for AutoARIMA only:

  • approximation - approximation method (default: 1),
  • search - search method (default: 1)
  • p, d, q, P, D, Q params define max values for a search algorithm

Train/Predict

ARIMA, SARIMA, AutoARIMA:

arima.train(ts) // Or arima.fit(ts)
arima.predict(10) // Predict 10 steps forward

SARIMAX:

arima.train(ts, exog) // or arima.fit(ts, exog)
arima.predict(10, exognew) // Predict 10 steps forwars using new exogenous variables

Running in browsers

As Chrome prevents compilation of wasm modules >4kB. There are two ways to overcome this:

  • Load arima in a Web Worker
  • Use the arima/async module

Example of async loading:


const ARIMAPromise = require('moving-average-arima/async')

ARIMAPromise.then(ARIMA => {
  const ts = Array(10).fill(0).map((_, i) => i + Math.random() / 5)
  const arima = new ARIMA({ p: 2, d: 1, q: 2, P: 0, D: 0, Q: 0, S: 0, verbose: false }).train(ts)
  const [pred, errors] = arima.predict(10)
})

All following examples use synchronous compilation (Node.js, Firefox). They will not work in Chrome.

Example: ARIMA

// Load package
const { arimaModule: ARIMA } = require("moving-average-arima");

// Synthesize timeseries
const ts = Array(24).fill(0).map((_, i) => i + Math.random() / 5)

// Init arima and start training
const arima = new ARIMA({
  p: 2,
  d: 1,
  q: 2,
  verbose: false
}).train(ts)

// Predict next 12 values
const [pred, errors] = arima.predict(12)

Example: SARIMA

// Init sarima and start training
const sarima = new ARIMA({
  p: 2,
  d: 1,
  q: 2,
  P: 1,
  D: 0,
  Q: 1,
  s: 12,
  verbose: false
}).train(ts)

// Predict next 12 values
const [pred, errors] = sarima.predict(12)

Example: SARIMAX

// Generate timeseries using exogenous variables
const f = (a, b) => a * 2 + b * 5;
const exog = Array(30).fill(0).map(x => [Math.random(), Math.random()]);
const exognew = Array(10).fill(0).map(x => [Math.random(), Math.random()]);
const ts = exog.map(x => f(x[0], x[1]) + Math.random() / 5);

// Init and fit sarimax
const sarimax = new ARIMA({
  p: 1,
  d: 0,
  q: 1,
  transpose: true,
  verbose: false
}).fit(ts, exog);

// Predict next 12 values using exognew
const [pred, errors] = sarimax.predict(12, exognew);

Example: AutoARIMA

const autoarima = new ARIMA({ auto: true }).fit(ts)
const [pred, errors] = autoarima.predict(12)

ARIMA Method (method)

0 - Exact Maximum Likelihood Method (Default)
1 - Conditional Method - Sum Of Squares
2 - Box-Jenkins Method

Optimization Method (optimizer)

Method 0 - Nelder-Mead
Method 1 - Newton Line Search
Method 2 - Newton Trust Region - Hook Step
Method 3 - Newton Trust Region - Double Dog-Leg
Method 4 - Conjugate Gradient
Method 5 - BFGS
Method 6 - Limited Memory BFGS (Default)
Method 7 - BFGS Using More Thuente Method

Old functional API (still works)

The old interface of the arima package was only one function that took 3 arguments:

  • a 1D array with observations over time
  • a number of time steps to predict
  • a javascript object with ARIMA parameters p, d, q and other options

It returned two vectors - predictions and mean square errors.

const { arimaModule: arima } = require("moving-average-arima");

const [pred, errors] = arima(ts, 20, {
  method: 0, // ARIMA method (Default: 0)
  optimizer: 6, // Optimization method (Default: 6)
  p: 1, // Number of Autoregressive coefficients
  d: 0, // Number of times the series needs to be differenced
  q: 1, // Number of Moving Average Coefficients
  verbose: true // Output model analysis to console
})

Web demo

You can try ARIMA online in the StatSim Forecast app: https://statsim.com/forecast/. It uses the arima package under the hood and applies random search to find the best values of p, d and q.