-
Notifications
You must be signed in to change notification settings - Fork 12
/
robust_func.m
105 lines (97 loc) · 3.08 KB
/
robust_func.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function o = robust_func(z, functype, outtype, delta, b, c)
% functype: 'huber', 'bisquare', 'cauchy', 'welsch', 'ggw'
% outtype: 'rho', 'psi', 'wgt'
switch lower(functype)
case('l1')
switch lower(outtype)
case('rho')
o = abs(z);
case('psi')
o = sign(z);
case('wgt')
o = 1 ./ max(abs(z),0.000001);
otherwise
warning('error outtype');
end
case('huber')
k = delta;
switch lower(outtype)
case('rho')
k_mask = abs(z)<k;
o = zeros(size(z));
o(k_mask) = 0.5*(z(k_mask).^2);
o(~k_mask) = k * (abs(z(~k_mask))-k/2);
case('psi')
k_mask = abs(z)<k;
o = zeros(size(z));
o(k_mask) = z(k_mask);
o(~k_mask) = k * sign(z(~k_mask));
case('wgt')
o = min(1, k ./ abs(z));
otherwise
warning('error outtype');
end
case('bisquare')
k = delta;
switch lower(outtype)
case('rho')
k_mask = abs(z)<k;
o = zeros(size(z));
o(k_mask) = 1/6*k*k*(1-(1-(z(k_mask)/k).^2).^3);
o(~k_mask) = 1/6*k*k;
case('psi')
k_mask = abs(z)<k;
o = zeros(size(z));
o(k_mask) = z(k_mask) .* (1-(z(k_mask)/k).^2).^2;
case('wgt')
k_mask = abs(z)<k;
o = zeros(size(z));
o(k_mask) = (1-(z(k_mask)/k).^2).^2;
otherwise
warning('error outtype');
end
case('cauchy')
k = delta;
switch lower(outtype)
case('rho')
o = 0.5*log(1+(z/k).^2);
case('psi')
o = 1*z ./ (1+(z/k).^2);
case('wgt')
o = 1 ./ (1+(z/k).^2);
otherwise
warning('error outtype');
end
case('welsch')
k = delta;
switch lower(outtype)
case('rho')
o = 1/k/k * exp(-0.5*(z/k).^2);
case('psi')
o = z .* exp(-0.5*(z/k).^2);
case('wgt')
o = exp(-0.5*(z/k).^2);
otherwise
warning('error outtype');
end
case('ggw')
a = delta;
switch lower(outtype)
case('rho')
o = 1;
case('psi')
c_mask = abs(z)<=c;
o = zeros(size(z));
o(c_mask) = z(c_mask);
o(~c_mask) = z(~c_mask).*exp(-0.5*((z(~c_mask)-c).^b));
case('wgt')
c_mask = abs(z)<=c;
o = zeros(size(z));
o(c_mask) = 1;
o(~c_mask) = exp(-0.5*((z(~c_mask)-c).^b));
otherwise
warning('error outtype');
end
otherwise
warning('error functype');
end