fracdiff.fdiff#

fracdiff.fdiff(a, n=1.0, axis=-1, prepend=None, append=None, window=10, mode='same')[source]#

Calculate the n-th differentiation along the given axis.

Extention of numpy.diff to fractional differentiation.

Parameters:
  • a (array_like) – The input array.

  • n (float, default=1.0) – The order of differentiation. If n is an integer, returns the same output with numpy.diff.

  • axis (int, default=-1) – The axis along which differentiation is performed, default is the last axis.

  • prepend (array_like, optional) – Values to prepend to a along axis prior to performing the differentiation. Scalar values are expanded to arrays with length 1 in the direction of axis and the shape of the input array in along all other axes. Otherwise the dimension and shape must match a except along axis.

  • append (array_like, optional) – Values to append.

  • window (int, default=10) – Number of observations to compute each element in the output.

  • mode ({"same", "valid"}, default="same") –

    “same” (default) :

    At the beginning of the time series, return elements where at least one coefficient of fracdiff is used. Output size along axis is \(L_{\mathrm{in}}\) where \(L_{\mathrm{in}}\) is the length of a along axis (plus the lengths of append and prepend). Boundary effects may be seen at the at the beginning of a time-series.

    ”valid” :

    Return elements where all coefficients of fracdiff are used. Output size along axis is \(L_{\mathrm{in}} - \mathrm{window} + 1\) where where \(L_{\mathrm{in}}\) is the length of a along axis (plus the lengths of append and prepend). Boundary effects are not seen.

Returns:

fdiff – The fractional differentiation. The shape of the output is the same as a except along axis.

Return type:

numpy.ndarray

Examples

This returns the same result with numpy.diff for integer n.

>>> from fracdiff import fdiff
>>> a = np.array([1, 2, 4, 7, 0])
>>> (np.diff(a) == fdiff(a)).all()
True
>>> (np.diff(a, 2) == fdiff(a, 2)).all()
True

This returns fractional differentiation for noninteger n.

>>> fdiff(a, 0.5, window=3)
array([ 1.   ,  1.5  ,  2.875,  4.75 , -4.   ])

Mode “valid” returns elements for which all coefficients are convoluted.

>>> fdiff(a, 0.5, window=3, mode="valid")
array([ 2.875,  4.75 , -4.   ])
>>> fdiff(a, 0.5, window=3, mode="valid", prepend=[1, 1])
array([ 0.375,  1.375,  2.875,  4.75 , -4.   ])

Differentiation along desired axis.

>>> a = np.array([[  1,  3,  6, 10, 15],
...               [  0,  5,  6,  8, 11]])
>>> fdiff(a, 0.5, window=3)
array([[1.   , 2.5  , 4.375, 6.625, 9.25 ],
       [0.   , 5.   , 3.5  , 4.375, 6.25 ]])
>>> fdiff(a, 0.5, window=3, axis=0)
array([[ 1. ,  3. ,  6. , 10. , 15. ],
       [-0.5,  3.5,  3. ,  3. ,  3.5]])