Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Theil-Sen-Matlab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Keyser, Johannes
Theil-Sen-Matlab
Commits
d862d0e7
Commit
d862d0e7
authored
Mar 22, 2022
by
Keyser, Johannes
Browse files
Options
Downloads
Patches
Plain Diff
Only whitespace changes to improve formatting.
parent
69b34ed5
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
TheilSen.m
+47
-43
47 additions, 43 deletions
TheilSen.m
license.txt
+5
-5
5 additions, 5 deletions
license.txt
with
52 additions
and
48 deletions
TheilSen.m
+
47
−
43
View file @
d862d0e7
function
[
m
,
b
]
=
TheilSen
(
data
)
function
[
m
,
b
]
=
TheilSen
(
data
)
% Performs Theil-Sen robust linear regression on data
% Performs Theil-Sen robust linear regression on data
.
%
%
% [m b] = TheilSen(data)
% [m
,
b] = TheilSen(data)
%
%
% data: A MxD matrix with M observations. The first D-1 columns are the
% data: A MxD matrix with M observations. The first D-1 columns are the
% explanatory variables and the Dth column is the response such that
% explanatory variables and the Dth column is the response such that
...
@@ -12,27 +12,29 @@ function [m, b] = TheilSen(data)
...
@@ -12,27 +12,29 @@ function [m, b] = TheilSen(data)
% b: Estimated offsets.
% b: Estimated offsets.
%
%
%
%
% EXAMPLE
:
% EXAMPLE
% -------
% -------
% n = 100;
% n = 100;
% outN = round(0.2 * n);
% outN = round(0.2 * n);
% noise = randn(n,2)*0.1; noise(randi(n*2,[outN 1]))=randn(outN,1)*5;
% noise = randn(n, 2) * 0.1;
% data = [linspace(0,10,n)' linspace(0,5,n)'] + noise;
% noise(randi(n * 2, [outN, 1])) = randn(outN, 1) * 5;
% Bhat = [ones(n,1) data(:,1)]\data(:,2);
% data = [linspace(0, 10, n)', linspace(0,5,n)'] + noise;
% Bhat = [ones(n, 1), data(:, 1)] \ data(:, 2);
% [m, b] = TheilSen(data);
% [m, b] = TheilSen(data);
% plims = [min(data(:,1)) max(data(:,1))]';
% plims = [min(data(:,
1))
,
max(data(:,
1))]';
% figure
% figure
()
% plot(data(:, 1), data(:, 2), 'k.', ...
% plot(data(:, 1), data(:, 2), 'k.', ...
% plims, plims * m + b, '-r',...
% plims, plims * m + b, '-r',...
% plims, plims * Bhat(2) + Bhat(1), '-b', 'linewidth', 2)
% plims, plims * Bhat(2) + Bhat(1), '-b', 'linewidth', 2)
% legend('Data','TheilSen','Least Squares','location','NW')
% legend('Data', 'Theil-Sen', 'Least Squares', 'location', 'NW')
% title(sprintf('Acual Slope = %2.3g, LS est = %2.3g, TS est = %2.3g',[0.5 Bhat(2) m]))
% title(sprintf('Acual Slope = %2.3g, LS est. = %2.3g, TS est. = %2.3g', ...
% [0.5 Bhat(2) m]))
%
%
%
%
% Source:
% Source:
% Gilbert, Richard O. (1987), "6.5 Sen's Nonparametric Estimator of
% Gilbert, Richard O. (1987), "6.5 Sen's Nonparametric Estimator of
% Slope", Statistical Methods for Environmental Pollution Monitoring,
% Slope", Statistical Methods for Environmental Pollution Monitoring,
% John Wiley and Sons, pp. 217
�
219, ISBN 978-0-471-28878-7
% John Wiley and Sons, pp. 217
-
219, ISBN 978-0-471-28878-7
%
%
%
%
%
%
...
@@ -41,42 +43,44 @@ function [m, b] = TheilSen(data)
...
@@ -41,42 +43,44 @@ function [m, b] = TheilSen(data)
% - updated help
% - updated help
% - speed increase for 2D case
% - speed increase for 2D case
%
%
%
sz
=
size
(
data
);
sz
=
size
(
data
);
if
length
(
sz
)
~=
2
||
sz
(
1
)
<
2
if
length
(
sz
)
~=
2
||
sz
(
1
)
<
2
error
(
'Expecting MxD data matrix with at least 2 observations.'
)
error
(
'Expecting MxD data matrix with at least 2 observations.'
)
end
end
if
sz
(
2
)
==
2
% normal 2
-
D case
if
sz
(
2
)
==
2
% normal 2D case
C
=
nan
(
sz
(
1
));
C
=
nan
(
sz
(
1
));
for
i
=
1
:
sz
(
1
)
for
i
=
1
:
sz
(
1
)
% accumulate slopes
% accumulate slopes
C
(
i
,
i
:
end
)
=
(
data
(
i
,
2
)
-
data
(
i
:
end
,
2
))
.
/(
data
(
i
,
1
)
-
data
(
i
:
end
,
1
));
C
(
i
,
i
:
end
)
=
(
data
(
i
,
2
)
-
data
(
i
:
end
,
2
))
.
/
...
(
data
(
i
,
1
)
-
data
(
i
:
end
,
1
));
end
end
m
=
nanmedian
(
C
(:));
% calculate slope estimate
m
=
nanmedian
(
C
(:));
% calculate slope estimate
if
nargout
==
2
if
nargout
==
2
b
=
nanmedian
(
data
(:,
2
)
-
m
*
data
(:,
1
));
% calculate intercept if requested
% calculate intercept if requested
b
=
nanmedian
(
data
(:,
2
)
-
m
*
data
(:,
1
));
end
end
else
% other cases
else
C
=
nan
(
sz
(
1
),
sz
(
2
)
-
1
,
sz
(
1
));
C
=
nan
(
sz
(
1
),
sz
(
2
)
-
1
,
sz
(
1
));
for
i
=
1
:
sz
(
1
)
for
i
=
1
:
sz
(
1
)
% accumulate slopes
C
(:,
:,
i
)
=
bsxfun
(
@
rdivide
,
data
(
i
,
end
)
-
data
(:,
end
),
...
C
(:,
:,
i
)
=
bsxfun
(
@
rdivide
,
data
(
i
,
end
)
-
data
(:,
end
),
...
bsxfun
(
@
minus
,
data
(
i
,
1
:
end
-
1
),
data
(:,
1
:
end
-
1
))
);
% accumulate slopes
bsxfun
(
@
minus
,
data
(
i
,
1
:
end
-
1
),
data
(:,
1
:
end
-
1
))
);
end
end
Cprm
=
reshape
(
permute
(
C
,[
1
3
2
]),
[],
size
(
C
,
2
),
1
);
% stack layers of C to 2D
% stack layers of C to 2D
Cprm
=
reshape
(
permute
(
C
,
[
1
,
3
,
2
]),
[],
size
(
C
,
2
),
1
);
m
=
nanmedian
(
Cprm
,
1
);
% calculate slope estimate
m
=
nanmedian
(
Cprm
,
1
);
% calculate slope estimate
if
nargout
==
2
if
nargout
==
2
% calculate all intercepts if requested
% calculate all intercepts if requested
b
=
nanmedian
(
bsxfun
(
@
minus
,
data
(:,
end
),
bsxfun
(
@
times
,
m
,
data
(:,
1
:
end
-
1
)))
);
b
=
nanmedian
(
bsxfun
(
@
minus
,
data
(:,
end
),
...
bsxfun
(
@
times
,
m
,
data
(:,
1
:
end
-
1
)))
);
end
end
end
end
This diff is collapsed.
Click to expand it.
license.txt
+
5
−
5
View file @
d862d0e7
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment