Loading [MathJax]/extensions/tex2jax.js
scipy 1.10.1 Pypi GitHub Homepage
Other Docs

NotesParametersReturnsBackRef
theilslopes(y, x=None, alpha=0.95, method='separate')

theilslopes implements a method for robust linear regression. It computes the slope as the median of all slopes between paired values.

Notes

The implementation of theilslopes follows . The intercept is not defined in , and here it is defined as median(y) - slope*median(x), which is given in . Other definitions of the intercept exist in the literature such as median(y - slope*x) in . The approach to compute the intercept can be determined by the parameter method. A confidence interval for the intercept is not given as this question is not addressed in .

For compatibility with older versions of SciPy, the return value acts like a namedtuple of length 4, with fields slope, intercept, low_slope, and high_slope, so one can continue to write

slope, intercept, low_slope, high_slope = theilslopes(y, x)

Parameters

y : array_like

Dependent variable.

x : array_like or None, optional

Independent variable. If None, use arange(len(y)) instead.

alpha : float, optional

Confidence degree between 0 and 1. Default is 95% confidence. Note that alpha is symmetric around 0.5, i.e. both 0.1 and 0.9 are interpreted as "find the 90% confidence interval".

method : {'joint', 'separate'}, optional

Method to be used for computing estimate for intercept. Following methods are supported,

  • 'joint': Uses np.median(y - slope * x) as intercept.

The default is 'separate'.

Returns

result : ``TheilslopesResult`` instance

The return value is an object with the following attributes:

slope

slope

intercept

intercept

low_slope

low_slope

high_slope

high_slope

Computes the Theil-Sen estimator for a set of points (x, y).

See Also

siegelslopes

a similar technique using repeated medians

Examples

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, num=150)
y = x + np.random.normal(size=x.size)
y[11:15] += 10  # add outliers
y[-5:] -= 7
Compute the slope, intercept and 90% confidence interval. For comparison, also compute the least-squares fit with `linregress`:
res = stats.theilslopes(y, x, 0.90, method='separate')
lsq_res = stats.linregress(x, y)
Plot the results. The Theil-Sen regression line is shown in red, with the dashed red lines illustrating the confidence interval of the slope (note that the dashed red lines are not the confidence interval of the regression as the confidence interval of the intercept is not included). The green line shows the least-squares fit for comparison.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'b.')
ax.plot(x, res[1] + res[0] * x, 'r-')
ax.plot(x, res[1] + res[2] * x, 'r--')
ax.plot(x, res[1] + res[3] * x, 'r--')
ax.plot(x, lsq_res[1] + lsq_res[0] * x, 'g-')
plt.show()
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.stats._mstats_basic:theilslopes scipy.stats._mstats_basic:sen_seasonal_slopes

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

siegelslopessiegelslopesscipy.stats._mstats_basic:theilslopes_mstats_basic:theilslopesscipy.stats._mstats_basic:sen_seasonal_slopes_mstats_basic:sen_seasonal_slopes

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/stats/_stats_mstats_common.py#213
type: <class 'function'>
Commit: