Skip to content

strength_of_trend

timecave.data_characteristics.strength_of_trend(ts)

Compute the strength of trend of a time series.

This function computes the strength of trend of a given time series using the method employed by Cerqueira et. al (2020) (i.e. the ratio between the time series' standard deviation and that of the differenced time series).

Parameters:

Name Type Description Default
ts ndarray | Series

Univariate time series.

required

Returns:

Type Description
float

Strength of trend.

Raises:

Type Description
TypeError

If ts is neither a Numpy array nor a Pandas series.

Notes

Let \(\sigma\) be the standard deviation of a given time series. The strength of trend of a series is defined by Cerqueira et al [1] as:

\[ SOT = \frac{\sigma_{ts}}{\sigma_{diff}} \]

where \(ts\) stands for the time series itself and \(diff\) denotes the differenced time series.

References

1

Cerqueira, V., Torgo, L., Mozetiˇc, I., 2020. Evaluating time series forecasting models: An empirical study on performance estimation methods. Machine Learning 109, 1997–2028.

Examples:

>>> import numpy as np
>>> from timecave.data_characteristics import strength_of_trend
>>> rng = np.random.default_rng(seed=1);
>>> noise = rng.uniform(low=0, high=0.01, size=10);
>>> constant_series = np.ones(10);
>>> strength_of_trend(constant_series + noise)
0.5717034302917938

For a series with a strong trend, this value will be larger:

>>> series_trend = np.arange(0, 10);
>>> strength_of_trend(series_trend + noise)
543.4144869043147

For pure trends, the strength of trend is infinite:

>>> strength_of_trend(series_trend)
inf

If the time series is neither an array nor a series, an exception is thrown:

>>> strength_of_trend([0, 1, 2])
Traceback (most recent call last):
...
TypeError: Time series must be either a Numpy array or a Pandas series.
Source code in timecave/data_characteristics.py
def strength_of_trend(ts: np.ndarray | pd.Series) -> float:

    """
    Compute the strength of trend of a time series.

    This function computes the strength of trend of a given time series using the method
    employed by Cerqueira et. al (2020) (i.e. the ratio between the time series' standard deviation and
    that of the differenced time series).

    Parameters
    ----------
    ts : np.ndarray | pd.Series
        Univariate time series.

    Returns
    -------
    float
        Strength of trend.

    Raises
    ------
    TypeError
        If `ts` is neither a Numpy array nor a Pandas series.

    Notes
    -----
    Let $\sigma$ be the standard deviation of a given time series. The strength of trend of a series is defined \
    by Cerqueira et al [[1]](#1) as:

    $$
    SOT = \\frac{\sigma_{ts}}{\sigma_{diff}}
    $$

    where $ts$ stands for the time series itself and $diff$ denotes the differenced time series.

    References
    ----------
    ##1
    Cerqueira, V., Torgo, L., Mozetiˇc, I., 2020. Evaluating time series forecasting
    models: An empirical study on performance estimation methods.
    Machine Learning 109, 1997–2028.

    Examples
    --------
    >>> import numpy as np
    >>> from timecave.data_characteristics import strength_of_trend
    >>> rng = np.random.default_rng(seed=1);
    >>> noise = rng.uniform(low=0, high=0.01, size=10);
    >>> constant_series = np.ones(10);
    >>> strength_of_trend(constant_series + noise)
    0.5717034302917938

    For a series with a strong trend, this value will be larger:

    >>> series_trend = np.arange(0, 10);
    >>> strength_of_trend(series_trend + noise)
    543.4144869043147

    For pure trends, the strength of trend is infinite:

    >>> strength_of_trend(series_trend)
    inf

    If the time series is neither an array nor a series, an exception is thrown:

    >>> strength_of_trend([0, 1, 2])
    Traceback (most recent call last):
    ...
    TypeError: Time series must be either a Numpy array or a Pandas series.
    """

    _check_type(ts);

    if(isinstance(ts, np.ndarray) is True):

        diff_ts = np.diff(ts);

    else:

        diff_ts = ts.diff().dropna();

    ts_std = ts.std();
    diff_std = diff_ts.std();

    if(diff_std == 0):

        SOT = np.inf;

    else:

        SOT = ts_std / diff_std;

    return SOT;