Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Created April 3, 2023 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/e5079eaf9d77a87a19615f5f068e7957 to your computer and use it in GitHub Desktop.
Save danielhaim1/e5079eaf9d77a87a19615f5f068e7957 to your computer and use it in GitHub Desktop.
ARIMA

ARIMA (Autoregressive Integrated Moving Average)

ARIMA (Autoregressive Integrated Moving Average) is a time series forecasting method that combines autoregression, differencing, and moving average techniques to model time series data. ARIMA models are commonly used in fields such as finance, economics, and engineering to make predictions about future values based on historical data.

Entropy

ARIMA, which stands for AutoRegressive Integrated Moving Average, is a time series forecasting method that models the relationships among data points to predict future values. The ARIMA model is specified by three parameters: p, d, and q.

Here's an example of how to use ARIMA to forecast the next value in a time series:

Sequence: 1, 2, 4, 7, 11

  1. Stationarize the time series by differencing: ARIMA requires the time series to be stationary, which means that its statistical properties (such as mean and variance) are constant over time. If the time series is not stationary, it can be transformed to become stationary through a process called differencing. To difference the time series, subtract each value from the previous value:
$$d(t) = x(t) - x(t-1) d = [1, 1, 2, 3, 4]$$
  1. Determine the values of p, d, and q for the ARIMA model: The values of p, d, and q can be determined by analyzing the autocorrelation function (ACF) and partial autocorrelation function (PACF) plots of the differenced time series. The ACF shows the correlation between a data point and the previous data points, while the PACF shows the correlation between a data point and the previous data points after accounting for the influence of intervening points.

In this example, the ACF shows a significant correlation with the first lag and a gradual decrease in correlation with subsequent lags, suggesting an AR(1) model. The PACF also shows a significant correlation with the first lag and no significant correlations with subsequent lags, suggesting an MA(0) model. Therefore, the values of p and q are 1 and 0, respectively.

$$p = 1 (based on the autocorrelation plot) d = 1 (already differenced) q = 0 (based on the partial autocorrelation plot)$$
  1. Fit the ARIMA model to the data and make a prediction for the next value: The ARIMA model can be fit to the differenced time series using maximum likelihood estimation (MLE). The fitted model can then be used to make a prediction for the next value by applying the inverse differencing formula to the forecasted difference value:
$$ARIMA(1,1,0): y(6) = 11 + 1 * (11 - 7) = 15$$

Therefore, the predicted number for the next value in the time series is 15.

Usage

Here's an example of how to use ARIMA to fit a model to a time series and make predictions:

const tslib = require('tslib');
const regression = tslib.__importStar(require('regression'));
const arima = tslib.__importStar(require('arima'));

const data = [[1, 10], [2, 23], [3, 17], [4, 29], [5, 30], [6, 34], [7, 45]];
const result = arima.fit(data, { method: 'MLE', order: [1, 1, 0] });

console.log('ARIMA model:', result.arima);
const nextValue = arima.predict(result.arima, 8);
console.log('Next value prediction:', nextValue);

In this example, we have an array of data points representing a time series. We use the fit method to fit an ARIMA model to the data with an order of (1,1,0), which specifies an autoregressive component of order 1, a differencing component of order 1, and no moving average component. We then use the predict method to make a prediction for the next value in the time series.

API Reference

ARIMA has two main methods:

fit(data: number[][], options?: { method?: 'MLE' | 'CSS', order?: [number, number, number], verbose?: boolean })

Fits an ARIMA model to the given time series data.

Parameters:

  • data - An array of data points for the time series. Each data point should be an array with two elements: the time index and the value of the time series at that index.
  • options - An optional object containing the following properties: -- method - The method to use for fitting the model. Valid values are 'MLE' (maximum likelihood estimation) and 'CSS' (conditional sum of squares). Defaults to 'MLE'. -- order - An array of three integers specifying the order of the ARIMA model. The first element is the order of the autoregressive component, the second element is the order of the differencing component, and the third element is the order of the moving average component. Defaults to [0, 0, 0]. -- verbose - A boolean value indicating whether to output diagnostic information during the fitting process. Defaults to false.

Returns:

An object containing the following properties:

  • arima - The fitted ARIMA model.
  • residuals - The residuals of the fitted model.
  • stderr - The standard error of the fitted model.
  • order - The order of the fitted model.

predict(model: any, steps: number)

Predicts the next steps values in the time series using the given ARIMA model.

Parameters:

  • model - The ARIMA model to use for prediction.
  • steps - The number of steps to predict into the future.

Returns:

An array of predicted values.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment