This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
madc.m
executable file
·79 lines (62 loc) · 1.59 KB
/
madc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function result=madc(x)
%MADC is a scale estimator given by the Median Absolute Deviation
% with finite sample correction factor.
% It is defined as
% mad(x)= b_n 1.4826 med(|x_i - med(x)|)
% with b_n a small sample correction factor to make the mad unbiased at the
% normal distribution. It can resist 50% outliers.
% If x is a matrix, the scale estimate is computed on the columns of x. The
% result is then a row vector. If x is a row or a column vector,
% the output is a scalar.
%
% Required input argument:
% x: either a data matrix with n observations in rows, p variables in columns
% or a columnn vector of length n.
%
% I/O: result=mad(x);
%
% This function is part of LIBRA: the Matlab Library for Robust Analysis,
% available at:
% http://wis.kuleuven.be/stat/robust.html
%
% Written by S.Verboven
% Last revised: 22/12/03
[n,p]=size(x);
if n==1 & p==1
result=0; %when X is a one by one matrix, all scale estimators must be equal to 0
return
elseif n==1
x=x'; %we only want to work with column vectors
n=p;
p=1;
end
bn=fcorfac(x); %calculating finite sample correction
t_0=median(x);
result=median(abs(x - repmat(median(x),n,1)))*1.4826*bn;
if ~all(result)
result(result==0)=t_0(result==0);
end
%---------
function bn=fcorfac(Z)
[n,p]=size(Z);
switch n
case 2
bn=1.196;
case 3
bn=1.495;
case 4
bn=1.363;
case 5
bn=1.206;
case 6
bn=1.200;
case 7
bn=1.140;
case 8
bn=1.129;
case 9
bn=1.107;
end
if n>9
bn=n/(n-0.8);
end