-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMM_estimatefast.R
262 lines (230 loc) · 6.35 KB
/
MM_estimatefast.R
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#Function of MM-Estimate
rhox = function(x, c = 3.45){ #bisquare function rho
if(abs(x)<=c){
rhox1 = 1-(1-(x/c)^(2))^(3)
}else{
rhox1 = 1
}
return(rhox1)
}
phi = function(x,c = 3.45){ #first derivative of rho
if(abs(x)>=c){
phix = 0
}else{
phix = (6/c^(2))*x*(1-(x/c)^(2))^(2)
}
return(phix)
}
w = function(u){ #weight function
return(phi(u)/u)
}
dw = function(x, c = 3.45){
if(abs(x)>=c){
dw1 = 0
}else{
dw1 = -(24/c^(4))*x*(1-(x/c)^(2))
}
return(dw1)
}
#From Maronna(2010):
#q=1,c=1.56;q=2,c=2.66;q=3,c=3.45;q=4,c=4.10;q=5,c=4.65;q=10,c=6.77
#use the following function cacu_c and also obtain a proper c
#Choose the c value
cacu_c = function(q, Sigma = diag(q)){
c0 = 1:1000/100
b=0.5
u = rmvn(1000,mu=rep(0,q),Sigma = Sigma)
Mr = sqrt(diag(u%*%t(u)))
for(i in 1:1000){
c = c0[i] #use the S-estimator to calculate the scale c
V1 = (1/1000)*sum(sapply(Mr/c,rhox,c=1))-b
if(V1<0){
break
}
}
return(c)
}
#MM-Estimate by Maronna(2010)
#The first function needs to adjust parameters , but when using in simulations, it may be faster.
MM_estimate1X = function(y, x, B0, Sigma0, err = 0.00001){
#This algorithm mainly base on Kudraszow and Marronna(2010),section 7
n = nrow(y)
q = ncol(y)
p = ncol(x)
#if(det(Sigma0)==0){ #avoid singular
#Sigma0 = Sigma0+err*diag(q)
#}
Gamma0 = Sigma0/det(Sigma0)^(1/q)
#det(Gamma0)=1
b = 0.5 #0.5BDP
##Step1:M-estimate for scale
s0 = (1:1000)/100 #for simplicity we assume s ranges from 0 to 10, which can be adjusted
#calculate the Mahalanobis distance without scale,using for estimating the scale parameter s
res = y-x%*%B0
Mr = sqrt(diag(res%*%solve(Gamma0)%*%t(res)))
#Step 1: Screening the scale s
for(i in 1:1000){
s = s0[i] #use S-estimator to estimate the scale s
V1 = (1/n)*sum(sapply(Mr/s,rhox))-b
if(V1<0){
break
}
}
##Step2: Estimate of B by WLS
Sigma0 = s^(2)*Gamma0
Mr = sqrt(diag(res%*%solve(Sigma0)%*%t(res)))
weight0 = sapply(Mr,w)
A2 = t(x)%*%diag(weight0)%*%x
B2 = t(x)%*%diag(weight0)%*%y
B1 = solve(A2)%*%B2
u = y-x%*%B1
##Step3: Estimate of Gamma and finally Sigma
C1 = t(u)%*%diag(weight0)%*%u
Gamma1 = C1/det(C1)^(1/q)
Sigma1 = s^(2)*Gamma1
##Step4: Stop until Mahalanobis distance converges
loop = 1
Loop = 1000 #max repeat times
index = 0 #whether the algorithm converge
while(loop<Loop){
B0 = B1
Mr0 = Mr
#repeat
res = y-x%*%B0
#Sigma0 = s^(2)*Gamma0
Sigma0=Sigma1
Mr = sqrt(diag(res%*%solve(Sigma0)%*%t(res)))
##Step2: Estimate of B by WLS
weight0 = sapply(Mr,w)
A2 = t(x)%*%diag(weight0)%*%x
B2 = t(x)%*%diag(weight0)%*%y
B1 = solve(A2)%*%B2
u = y-x%*%B1
##Step3: Estimate of Gamma
C1 = t(u)%*%diag(weight0)%*%u
Gamma1 = C1/det(C1)^(1/q)
Sigma1 = s^(2)*Gamma1
if(t(Mr-Mr0)%*%(Mr-Mr0)<err){
index = 1
break
}else{
#cat("Loop",loop,"times.\r")
loop = loop + 1
}
}
return(list(Beta = B1, Sigma = Sigma1,s=s, index = index))
}
#The second function does not need to adjust parameters, using for real data analysis is proper, but it also needs initial values which are affine-equivalent(eg. LTS)
MM_estimate2X = function(y, x, B0, Sigma0, err = 0.00001){
#This algorithm mainly base on Kudraszow and Marronna(2010),section 7
n = nrow(y)
q = ncol(y)
p = ncol(x)
cacu_c = function(q, Sigma){ #c ranges from 0 to 10, can be adjusted.
c0 = 1:1000/100
b=0.5
u = rmvn(1000,mu=rep(0,q),Sigma = Sigma)
Mr = sqrt(diag(u%*%t(u)))
for(i in 1:1000){
c = c0[i] #use S-estimator to calculate the scale s
V1 = (1/1000)*sum(sapply(Mr/c,rhox,c=1))-b
if(V1<0){
break
}
}
return(c)
}
c = cacu_c(q, Sigma0)
rhox = function(x, c){ #bisquare function rho
if(abs(x)<=c){
rhox1 = 1-(1-(x/c)^(2))^(3)
}else{
rhox1 = 1
}
return(rhox1)
}
phi = function(x, c){ #first derivative of rho
if(abs(x)>=c){
phix = 0
}else{
phix = (6/c^(2))*x*(1-(x/c)^(2))^(2)
}
return(phix)
}
w = function(u){ #weight function
return(phi(u)/u)
}
dw = function(x, c){
if(abs(x)>=c){
dw1 = 0
}else{
dw1 = -(24/c^(4))*x*(1-(x/c)^(2))
}
return(dw1)
}
#if(det(Sigma0)==0){ #avoid singular
#Sigma0 = Sigma0+err*diag(q)
#}
Gamma0 = Sigma0/det(Sigma0)^(1/q)
#det(Gamma0)=1
b = 0.5 #0.5BDP
##Step1:M-estimate for scale
s0 = (1:1000)/100 #for simplicity we assume s ranges from 0 to 10, which can be adjusted
#calculate the Mahalanobis distance without scale,using for estimating the scale parameter s
res = y-x%*%B0
Mr = sqrt(diag(res%*%solve(Gamma0)%*%t(res)))
#Step 1: Screening the scale s
for(i in 1:1000){
s = s0[i] #use S-estimator to estimate the scale s
V1 = (1/n)*sum(sapply(Mr/s,rhox))-b
if(V1<0){
break
}
}
##Step2: Estimate of B by WLS
Sigma0 = s^(2)*Gamma0
Mr = sqrt(diag(res%*%solve(Sigma0)%*%t(res)))
weight0 = sapply(Mr,w)
A2 = t(x)%*%diag(weight0)%*%x
B2 = t(x)%*%diag(weight0)%*%y
B1 = solve(A2)%*%B2
u = y-x%*%B1
##Step3: Estimate of Gamma and finally Sigma
C1 = t(u)%*%diag(weight0)%*%u
Gamma1 = C1/det(C1)^(1/q)
Sigma1 = s^(2)*Gamma1
##Step4: Stop until Mahalanobis distance converges
loop = 1
Loop = 1000 #max repeat times
index = 0 #whether the algorithm converge
while(loop<Loop){
#Gamma0 = Gamma1
#C=solve(Gamma0)
#u = y-x%*%B0
B0 = B1
Mr0 = Mr
#repeat
res = y-x%*%B0
#Sigma0 = s^(2)*Gamma0
Sigma0=Sigma1
Mr = sqrt(diag(res%*%solve(Sigma0)%*%t(res)))
##Step2: Estimate of B by WLS
weight0 = sapply(Mr,w)
A2 = t(x)%*%diag(weight0)%*%x
B2 = t(x)%*%diag(weight0)%*%y
B1 = solve(A2)%*%B2
u = y-x%*%B1
##Step3: Estimate of Gamma
C1 = t(u)%*%diag(weight0)%*%u
Gamma1 = C1/det(C1)^(1/q)
Sigma1 = s^(2)*Gamma1
if(t(Mr-Mr0)%*%(Mr-Mr0)<err){
index = 1
break
}else{
#cat("Loop",loop,"times.\r")
loop = loop + 1
}
}
return(list(Beta = B1, Sigma = Sigma1,s=s, index = index))
}