bundles / scipy 1.17.1 / scipy / stats / _mstats_basic / sen_seasonal_slopes
function
scipy.stats._mstats_basic:sen_seasonal_slopes
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_likeEach column of
xcontains measurements of the dependent variable within a season. The independent variable (usually time) of each season is assumed to benp.arange(x.shape[0]).
Returns
result: ``SenSeasonalSlopesResult`` instanceThe 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))✓
from scipy import stats intra_slope, inter_slope = stats.mstats.sen_seasonal_slopes(x)✓
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]✓
i = 2 np.allclose(np.median(dijk(x[:, i])), intra_slope[i])✓
all_slopes = np.concatenate([dijk(x[:, i]) for i in range(x.shape[1])]) np.allclose(np.median(all_slopes), inter_slope)✓
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