{ } Raw JSON

bundles / scipy 1.17.1 / scipy / stats / _mstats_basic / sen_seasonal_slopes

function

scipy.stats._mstats_basic:sen_seasonal_slopes

source: /scipy/stats/_mstats_basic.py :1320

Signature

def   sen_seasonal_slopes ( x )

Summary

Computes seasonal Theil-Sen and Kendall slope estimators.

Extended Summary

The seasonal generalization of Sen's slope computes the slopes between all pairs of values within a "season" (column) of a 2D array. It returns an array containing the median of these "within-season" slopes for each season (the Theil-Sen slope estimator of each season), and it returns the median of the within-season slopes across all seasons (the seasonal Kendall slope estimator).

Parameters

x : 2D array_like

Each column of x contains measurements of the dependent variable within a season. The independent variable (usually time) of each season is assumed to be np.arange(x.shape[0]).

Returns

result : ``SenSeasonalSlopesResult`` instance

The return value is an object with the following attributes:

intra_slope

intra_slope

inter_slope

inter_slope

Notes

The slopes within season are:

for pairs of distinct integer indices of .

Element of the returned intra_slope array is the median of the over all ; this is the Theil-Sen slope estimator of season . The returned inter_slope value, better known as the seasonal Kendall slope estimator, is the median of the over all .

Examples

Suppose we have 100 observations of a dependent variable for each of four seasons:
import numpy as np
rng = np.random.default_rng()
x = rng.random(size=(100, 4))
We compute the seasonal slopes as:
from scipy import stats
intra_slope, inter_slope = stats.mstats.sen_seasonal_slopes(x)
If we define a function to compute all slopes between observations within a season:
def dijk(yi):
    n = len(yi)
    x = np.arange(n)
    dy = yi - yi[:, np.newaxis]
    dx = x - x[:, np.newaxis]
    # we only want unique pairs of distinct indices
    mask = np.triu(np.ones((n, n), dtype=bool), k=1)
    return dy[mask]/dx[mask]
then element ``i`` of ``intra_slope`` is the median of ``dijk[x[:, i]]``:
i = 2
np.allclose(np.median(dijk(x[:, i])), intra_slope[i])
and ``inter_slope`` is the median of the values returned by ``dijk`` for all seasons:
all_slopes = np.concatenate([dijk(x[:, i]) for i in range(x.shape[1])])
np.allclose(np.median(all_slopes), inter_slope)
Because the data are randomly generated, we would expect the median slopes to be nearly zero both within and across all seasons, and indeed they are:
intra_slope.data
inter_slope

See also

scipy.stats.theilslopes

non-seasonal slopes for non-masked arrays

theilslopes

the analogous function for non-seasonal data

Aliases

  • scipy.stats._mstats_basic.sen_seasonal_slopes