pmdarima.utils.diff

pmdarima.utils.diff(x, lag=1, differences=1)[source][source]

Difference an array.

A python implementation of the R diff function [1]. This computes lag differences from an array given a lag and differencing term.

If x is a vector of length \(n\), lag=1 and differences=1, then the computed result is equal to the successive differences x[lag:n] - x[:n-lag].

Parameters:

x : array-like, shape=(n_samples, [n_features])

The array to difference.

lag : int, optional (default=1)

An integer > 0 indicating which lag to use.

differences : int, optional (default=1)

An integer > 0 indicating the order of the difference.

Returns:

res : np.ndarray, shape=(n_samples, [n_features])

The result of the differenced arrays.

References

[R44]https://stat.ethz.ch/R-manual/R-devel/library/base/html/diff.html

Examples

Where lag=1 and differences=1:

>>> x = c(10, 4, 2, 9, 34)
>>> diff(x, 1, 1)
array([ -6.,  -2.,   7.,  25.], dtype=float32)

Where lag=1 and differences=2:

>>> x = c(10, 4, 2, 9, 34)
>>> diff(x, 1, 2)
array([  4.,   9.,  18.], dtype=float32)

Where lag=3 and differences=1:

>>> x = c(10, 4, 2, 9, 34)
>>> diff(x, 3, 1)
array([ -1.,  30.], dtype=float32)

Where lag=6 (larger than the array is) and differences=1:

>>> x = c(10, 4, 2, 9, 34)
>>> diff(x, 6, 1)
array([], dtype=float32)

For a 2d array with lag=1 and differences=1:

>>> import numpy as np
>>>
>>> x = np.arange(1, 10).reshape((3, 3)).T
>>> diff(x, 1, 1)
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)