File Exchange

## Theil–Sen estimator

version 1.0.0.0 (1.35 KB) by
Robust regression for slope estimation between 1dimensional X and y

Updated 21 Dec 2011

the Theil–Sen estimator, also known as Sen's slope estimator,slope selection,the single median method, or the Kendall robust line-fit method, is a method for robust linear regression that chooses the median slope among all lines through pairs of two-dimensional sample points. It is named after Henri Theil and Pranab K. Sen, who published papers on this method in 1950 and 1968 respectively. It can be computed efficiently, and is insensitive to outliers; it can be significantly more accurate than simple linear regression for skewed and heteroskedastic data, and competes well against simple least squares even for normally distributed data. It has been called "the most popular nonparametric technique for estimating a linear trend".

PS: Example code on how to use can be found in .mfile

Note: This code can only be used for the twodimensional case.

This was a response to ==>
http://www.mathworks.nl/support/solutions/en/data/1-FERW6X/index.html?product=ML&solution=1-FERW6X

### Cite As

Arnout Tilgenkamp (2021). Theil–Sen estimator (https://www.mathworks.com/matlabcentral/fileexchange/34308-theil-sen-estimator), MATLAB Central File Exchange. Retrieved .

### Comments and Ratings (8)

James Harynuk

@Nommy, please see updated function here:

https://www.mathworks.com/matlabcentral/fileexchange/71205-theil-sen-regression-with-intercept

Apologies to Arnout, for some reason the submission system would not let me put in an acknowledgement to anything. Hopefully the Mathworks Gods can fix.

Nommy

@ James Harynuk , can you please clarify something to me,, so if I use this function b = Theil_Sen_Regress(x,y) or even the one you recommended, what is x and y, when explaining to someone new to matlab?

James Harynuk

This does require the stats toolbox, and it seems to me that it (even with Felipe's edit), forces the intercept through zero, which isn't great. Why not the following:

function [s, b] = tsreg(x,y)

comb = combnk(1:length(x),2); %get all combinations of data point

s = median(diff(y(comb),1,2)./diff(x(comb),1,2)); % calculate median slope

b = median(y-s*x); % given median slope, calculate median y-intercept
end

Liu lb

Great!

t

t

good

Patricia

It requires the Statistics Toolbox

Felipe G. Nievinski

Line 12:

theil=diff(y(Comb),1,2)./diff(x(Comb),1,2);

can be replaced by:

theil=deltay./deltax;

(otherwise the value assigned to deltax and deltay would be unused.

##### MATLAB Release Compatibility
Created with R2011a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!