delaynet.detrending_methods package#

Submodules#

delaynet.detrending_methods.delta module#

Delta detrending.

delaynet.detrending_methods.delta.delta(ts, window_size: int = 10)[source]#

Delta detrending.

Local mean subtraction. Subtract the local mean, mean([x_{t - w}, …, x_{t + w}]), from each value x_t.

\[x_t' = x_t - \left(2w + 1\right)^{-1} \sum_{k = t - w}^{t + w} x_k\]
Parameters:
  • ts (numpy.ndarray) – Time series to detrend.

  • window_size (int) – Window size to use for calculating the mean. Must be a positive integer.

Returns:

Detrended time series.

Return type:

numpy.ndarray

Raises:

ValueError – If the window_size is not a positive integer.

delaynet.detrending_methods.identity module#

Identity.

delaynet.detrending_methods.identity.identity(ts)[source]#

Identity ‘detrending’ - no detrending.

Parameters:

ts (numpy.ndarray) – Time series to detrend.

Returns:

‘detrended’ time series.

Return type:

numpy.ndarray

delaynet.detrending_methods.second_difference module#

Second difference (2diff) detrending.

delaynet.detrending_methods.second_difference.second_difference(ts)[source]#

Second difference (2diff) detrending.

Parameters:

ts (numpy.ndarray) – Time series to detrend.

Returns:

Detrended time series (length is reduced by 2).

Return type:

numpy.ndarray

delaynet.detrending_methods.z_score module#

Z-Score (ZS) detrending.

delaynet.detrending_methods.z_score.z_score(ts: ndarray, periodicity: int = 1, max_periods: int = -1) ndarray[source]#

Z-Score (ZS) detrending.

The Z-Score (ZS) detrending is a standard score that measures the number of standard deviations a data point is from the mean. It is calculated as:

\[Z = \frac{X - \mu}{\sigma}\]

where \(X\) is the data point, \(\mu\) is the mean, and \(\sigma\) is the standard deviation.

The Z-Score detrending is applied to a time series by computing the mean and standard deviation of a sub time series around each data point. The sub time series is defined by the periodicity and the maximum number of periods to consider before and after the current value. Mean and standard deviation are computed for each sub time series and used to detrend the data point. The current value is removed from the sub time series to avoid bias.

When periodicity > 1, the function compares each point with other points at the same phase of the cycle (same position within each period).

When periodicity == 1:

  • If max_periods == -1, a simple Z-score is applied to the entire time series.

  • If max_periods != -1, a moving window approach is used, where each point is compared with max_periods points before and after it.

For a valid Z-Score, \(2\times\texttt{periodicity}+1 \leq \texttt{len(ts)}\) needs to be satisfied. Also, \(\texttt{max_periods} \times \texttt{periodicity} \geq\texttt{len(ts)}\) results in including all periods.

Parameters:
  • ts (numpy.ndarray) – Time series to detrend.

  • periodicity (int > 0) – Periodicity of the time series - reoccurrence of the same pattern.

  • max_periods (int >= -1) – Maximum number of periods to consider before and after the current value. If -1, all periods are considered.

Returns:

Detrended time series.

Return type:

numpy.ndarray

Raises:
  • ValueError – If the periodicity is not a positive integer.

  • ValueError – If the max_periods is not a positive integer or -1.

  • ValueError – If \(2\times\texttt{periodicity}+1 \leq \texttt{len(ts)}\) is not satisfied.

Module contents#

Detrending methods init, subpackage of delaynet.