-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp
317 lines (314 loc) · 13.9 KB
/
temp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#RJMCMC抽样
w=NULL;mu=NULL;Isigma=NULL;k_arr=NULL;
for(chou in 1:20000)
{
#更新w
par=rep(0,k);#replicates the values in x
for(i in 1:k)
{par[i]=delta+N[i];}
w0=t(rdirichlet(1,par));
#Dirichlet distribution
#rdirichlet(n, alpha)
#n number of random variables to be generated.
#alpha vector of Dirichlet hyper parameter.
#更新mu
for(j in 1:k)
{
mub=(Isigma0[j]*sum((ifelse(z==j,1,0))*y)+kai*kesi)/(Isigma0[j]*N[j]+kai);
sigmab=1/(Isigma0[j]*N[j]+kai);
mu0[j]=rnorm(1,mub,sigmab);
}
#更新Isigma
for(j in 1:k)
{
Isigma0[j]=rgamma(1,alpha+N[j]/2,1/(beta+sum((ifelse(z==j,1,0))*((y-mu0[j])^2))/2));
}
#更新z,N
#Z为潜变量
p=matrix(0,n,k);
for(j in 1:k)
{
p[,j]=w0[j]*sqrt(Isigma0[j])*exp(-(y-mu0[j])^2*Isigma0[j]/2);
}
z=max.col(p);
#max.col
#Find the maximum position for each row of a matrix,
for(i in 1:k)
{
N[i]=sum(ifelse(z==i,1,0));
}
#更新beta
temp=0;
for(j in 1:k)
{
temp=temp+Isigma0[j];
}
beta=rgamma(1,g+k*alpha,h+temp);
#更新k
k_sel=ceiling(k*runif(1));#随机取一个数用于合并或拆分
if (k>1) #合并
{
u=runif(1);#抽0~1的一个随机数
if (u<die[k])#判断是否合并
{
mu_sort=sort(mu0);#均值按升序排列
k_new=which(sort.list(mu0)==k_sel);#找到选取的点在升序中的位置
if(k_new==1)#选取在升序中与选取点邻近的点
{
k_old=sort.list(mu0)[k_new+1];
}else{
k_old=ifelse(k_new==k,sort.list(mu0)[k_new-1],ifelse(u<0.5,sort.list(mu0)[k_new-1],sort.list(mu0)[k_new+1]));
}
w_j=w0[k_sel]+w0[k_old];#更新w
mu_j=(w0[k_sel]*mu0[k_sel]+w0[k_old]*mu0[k_old])/w_j;#更新mu
Isigma_j=((w0[k_sel]*(mu0[k_sel]^2+Isigma0[k_sel]^(-1))+w0[k_old]*(mu0[k_old]^2+Isigma0[k_old]^(-1)))/w_j-mu_j^2)^(-1);#更新Isigma
if(max(k_sel,k_old)==k)
{
w_0=rep(0,(k-1));
w_0[1:(max(k_sel,k_old)-1)]=w0[1:(max(k_sel,k_old)-1)];
w_0[min(k_sel,k_old)]=w_j;#调整w
mu_0=rep(0,(k-1));
mu_0[1:(max(k_sel,k_old)-1)]=mu0[1:(max(k_sel,k_old)-1)];
mu_0[min(k_sel,k_old)]=mu_j;#调整mu
Isigma_0=rep(0,(k-1));
Isigma_0[1:(max(k_sel,k_old)-1)]=Isigma0[1:(max(k_sel,k_old)-1)];
Isigma_0[min(k_sel,k_old)]=Isigma_j;#调整Isigma
}
else{
w_0=rep(0,(k-1));
w_0[1:(max(k_sel,k_old)-1)]=w0[1:(max(k_sel,k_old)-1)];
w_0[min(k_sel,k_old)]=w_j;
w_0[max(k_sel,k_old):(k-1)]=w0[(max(k_sel,k_old)+1):k];#调整w
mu_0=rep(0,(k-1));
mu_0[1:(max(k_sel,k_old)-1)]=mu0[1:(max(k_sel,k_old)-1)];
mu_0[min(k_sel,k_old)]=mu_j;
mu_0[max(k_sel,k_old):(k-1)]=mu0[(max(k_sel,k_old)+1):k];#调整mu
Isigma_0=rep(0,(k-1));
Isigma_0[1:(max(k_sel,k_old)-1)]=Isigma0[1:(max(k_sel,k_old)-1)];
Isigma_0[min(k_sel,k_old)]=Isigma_j;
Isigma_0[max(k_sel,k_old):(k-1)]=Isigma0[(max(k_sel,k_old)+1):k];#调整Isigma
}
z_0=z;temp_1=which(z==k_sel);Palloc=1;
if(length(temp_1)!=0)
{
z_0[temp_1]=min(k_sel,k_old);
Palloc_1=prod((w0[k_sel]*sqrt(Isigma0[k_sel])*exp(-Isigma0[k_sel]*++
(y[temp_1]-mu0[k_sel])^2/2)/++
(w0[k_sel]*sqrt(Isigma0[k_sel])*++
exp(-Isigma0[k_sel]*(y[temp_1]-mu0[k_sel])^2/2)+++
w0[k_old]*sqrt(Isigma0[k_old])*exp(-Isigma0[k_old]*(y[temp_1]-mu0[k_old])^2/2))));
Palloc=Palloc*Palloc_1;
}
temp_2=which(z==k_old);
if(length(temp_2)!=0)
{
z_0[temp_2]=min(k_sel,k_old);
Palloc_2=prod((w0[k_old]*sqrt(Isigma0[k_old])*exp(-Isigma0[k_old]*++
(y[temp_2]-mu0[k_old])^2/2)/++
(w0[k_sel]*sqrt(Isigma0[k_sel])*++
exp(-Isigma0[k_sel]*(y[temp_2]-mu0[k_sel])^2/2)+++
w0[k_old]*sqrt(Isigma0[k_old])*exp(-Isigma0[k_old]*(y[temp_2]-mu0[k_old])^2/2))));
Palloc=Palloc*Palloc_2;
}
if (max(k_sel,k_old)<k)#调整z
{
for (i in (max(k_sel,k_old)+1):k)
{
temp=which(z==i);
if (length(temp)!=0) z_0(temp)=i-1;
}
}
u1=rbeta(1,2,2);u2=rbeta(1,2,2);u3=rbeta(1,1,1);#计算合并的接受概率
tempratio=w_j^(sum(ifelse(z==k_sel,1,0))+sum(ifelse(z==k_old,1,0)))*beta(1,(k-1))*gamma(alpha)*(Isigma0[k_sel]*Isigma0[k_old]/++
Isigma_j)^(1-alpha)/k/sqrt(kai/2/pi)/exp(-kai*(mu0[k_sel]^2+mu0[k_old]^2-mu_j^2)/2)/beta^alpha/++
exp(-beta*(Isigma0[k_sel]+Isigma0[k_old]-Isigma_j))/w0[k_sel]^(sum(ifelse(z==k_sel,1,0)))/w0[k_old]^(sum(ifelse(z==k_old,1,0)));
tempratio=ifelse(is.na(tempratio),100,tempratio);
ac_c=tempratio*bor[k-1]*Palloc*dbeta(u1,2,2)*dbeta(u2,2,2)*dbeta(u3,1,1)/die[k]/++
(w_j*abs(mu0[k_sel]-mu0[k_old])*Isigma_j)*(u2*(1-u2^2)*u3*(1-u3)*Isigma0[k_sel]*Isigma0[k_old]);
u=runif(1);
if(u<min(1,ac_c))#判断是否接受新的参数
{
k=k-1;mu0=mu_0;w0=w_0;Isigma0=Isigma_0;z=z_0;
}
}
else{ #进行拆分
u1=rbeta(1,2,2);u2=rbeta(1,2,2);u3=rbeta(1,1,1);
w_j1=w0[k_sel]*u1;w_j2=w0[k_sel]*(1-u1);#更新w
mu_j1=mu0[k_sel]-u2*sqrt(w_j2/w_j1)/sqrt(Isigma0[k_sel]);#更新mu
mu_j2=mu0[k_sel]+u2*sqrt(w_j1/w_j2)/sqrt(Isigma0[k_sel]);
Isigma_j1=1/(u3*(1-u2^2)*w0[k_sel]/(w_j1*Isigma0[k_sel]));#更新Isigma
Isigma_j2=1/((1-u3)*(1-u2^2)*w0[k_sel]/(w_j2*Isigma0[k_sel]));
label_var_pre=rep(0,(k-1));
label_var_pre[1:(k_sel-1)]=mu0[1:(k_sel-1)];
label_var_pre[k_sel:(k-1)]=mu0[(k_sel+1):k];
label_var_pre_sort=sort(label_var_pre);#升序排列
new_com_1=p_w*w_j1+q_w*mu_j1+(1-p_w-q_w)*Isigma_j1;
new_com_2=p_w*w_j2+q_w*mu_j2+(1-p_w-q_w)*Isigma_j2;
if (sum(ifelse(label_var_pre_sort<new_com_1,1,0))==sum(ifelse(label_var_pre_sort<new_com_2,1,0)))
{
if (k_sel<k)
{
w_0=rep(0,(k+1));
w_0[1:(k_sel-1)]=w0[1:(k_sel-1)];w_0[k_sel]=w_j1;w_0[k_sel+1]=w_j2;w_0[(k_sel+2):(k+1)]=w0[(k_sel+1):k];
mu_0=rep(0,(k+1));
mu_0[1:(k_sel-1)]=mu0[1:(k_sel-1)];mu_0[k_sel]=mu_j1;mu_0[k_sel+1]=mu_j2;mu_0[(k_sel+2):(k+1)]=mu0[(k_sel+1):k];
Isigma_0=rep(0,(k+1));
Isigma_0[1:(k_sel-1)]=Isigma0[1:(k_sel-1)];Isigma_0[k_sel]=Isigma_j1;Isigma_0[k_sel+1]=Isigma_j2;Isigma_0[(k_sel+2):(k+1)]=Isigma0[(k_sel+1):k];
z_0=z;
for(i in (k_sel+1):k)
{
temp=which(z==i);
if(length(temp)!=0) z_0[temp]=i+1;
}
}else
{
w_0=rep(0,(k+1));
w_0[1:(k_sel-1)]=w0[1:(k_sel-1)];w_0[k_sel]=w_j1;w_0[k_sel+1]=w_j2;
mu_0=rep(0,(k+1));
mu_0[1:(k_sel-1)]=mu0[1:(k_sel-1)];mu_0[k_sel]=mu_j1;mu_0[k_sel+1]=mu_j2;
Isigma_0=rep(0,(k+1));
Isigma_0[1:(k_sel-1)]=Isigma0[1:(k_sel-1)];Isigma_0[k_sel]=Isigma_j1;Isigma_0[k_sel+1]=Isigma_j2;
z_0=z;
}
temp=which(z==k_sel);U=runif(length(temp));
z_0[temp]=k_sel+ifelse((U<(w_j2*sqrt(Isigma_j2)*exp(-Isigma_j2*(y[temp]-mu_j2)^2/2)/(w_j1*sqrt(Isigma_j1)*exp(-Isigma_j1*++
(y[temp]-mu_j1)^2/2)+w_j2*sqrt(Isigma_j2)*exp(-Isigma_j2*(y[temp]-mu_j2)^2/2)))),1,0);
Palloc=1;temp_1=which(z_0==k_sel);
if(length(temp_1)!=0)
{
Palloc_1=prod((w_j1*sqrt(Isigma_j1)*exp(-Isigma_j1*(y[temp_1]-mu_j1)^2/2)/++
(w_j1*sqrt(Isigma_j1)*exp(-Isigma_j1*(y[temp_1]-mu_j1)^2/2)+++
w_j2*sqrt(Isigma_j2)*exp(-Isigma_j2*(y[temp_1]-mu_j2)^2/2))));
Palloc=Palloc*Palloc_1;
}
temp_2=which(z_0==(k_sel+1));
if(length(temp_2)!=0)
{
Palloc_2=prod((w_j2*sqrt(Isigma_j2)*exp(-Isigma_j2*(y[temp_2]-mu_j2)^2/2)/++
(w_j1*sqrt(Isigma_j1)*exp(-Isigma_j1*(y[temp_2]-mu_j1)^2/2)+++
w_j2*sqrt(Isigma_j2)*exp(-Isigma_j2*(y[temp_2]-mu_j2)^2/2))));
Palloc=Palloc*Palloc_2;
}
temp=which(z==k_sel);
tempratio=w0[k_sel]^(sum(ifelse(z==k_sel,1,0))+sum(ifelse(z==(k_sel+1),1,0)))*beta(1,k)*gamma(alpha)*(Isigma_j1*Isigma_j2/++
Isigma0[k_sel])^(1-alpha)/(k+1)/sqrt(kai/2/pi)/exp(-kai*(mu_j1^2+mu_j2^2-mu0[k_sel]^2)/2)/beta^alpha/++
exp(-beta*(Isigma_j1+Isigma_j2-Isigma0[k_sel]))/w_j1^(sum(ifelse(z_0==k_sel,1,0)))/w_j2^(sum(ifelse(z==(k_sel+1),1,0)));
tempratio=ifelse(is.na(tempratio),1/100,tempratio);
ac_s=d_k[k+1]/(b_k[k]*Palloc*dbeta(u1,2,2)*dbeta(u2,2,2)*dbeta(u3,1,1))*w0[k_sel]*abs(mu_j1-mu_j2)*Isigma0[k_sel]/(u2*++ #计算拆分的接受概率
(1-u2^2)*u3*(1-u3)*Isigma_j1*Isigma_j2)/tempratio;
u=runif(1);
if(u<min(1,ac_s))#判断是否接受新的参数
{
k=k+1;mu0=mu_0;w0=w_0;Isigma0=Isigma_0;z=z_0;
}
}
}
}else #k=1,直接进行拆分
{
u1=rbeta(1,2,2);u2=rbeta(1,2,2);u3=rbeta(1,1,1);w_1=w0*u1;w_2=w0*(1-u1);
mu_1=mu0-u2*sqrt(w_2/w_1)/sqrt(Isigma0);mu_2=mu0+u2*sqrt(w_1/w_2)/sqrt(Isigma0);
Isigma_1=1/(u3*(1-u2^2)*w0/(w_1*Isigma0));Isigma_2=1/((1-u3)*(1-u2^2)*w0/(w_2*Isigma0));
mu_0=rep(0,2);
mu_0[1]=mu_1;mu_0[2]=mu_2;
Isigma_0=rep(0,2);
Isigma_0[1]=Isigma_1;Isigma_0[2]=Isigma_2;
w_0=rep(0,2);
w_0[1]=w_1;w_0[2]=w_2;
U=runif(length(y));
z_0=1+ifelse((U<(w_0[2]*sqrt(Isigma_0[2])*exp(-Isigma_0[2]*(y-mu_0[2])^2/2)/++
(w_0[1]*sqrt(Isigma_0[1])*exp(-Isigma_0[1]*(y-mu_0[1])^2/2)+++
w_0[2]*sqrt(Isigma_0[2])*exp(-Isigma_0[2]*(y-mu_0[2])^2/2)))),1,0);
Palloc=1;
for(i in 1:2)
{
temp=which(z_0==i);
if(length(temp)!=0)
{
Palloc_0=prod((w_0[i]*sqrt(Isigma_0[i])*exp(-Isigma_0[i]*++
(y[temp]-mu_0[i])^2/2)/(w_0[1]*sqrt(Isigma_0[1])*exp(-Isigma_0[1]*++
(y[temp]-mu_0[1])^2/2)+w_0[2]*sqrt(Isigma_0[2])*exp(-Isigma_0[2]*++
(y[temp]-mu_0[2])^2/2))));
Palloc=Palloc*Palloc_0;
}
}
tempratio=beta(1,k)*gamma(alpha)*(Isigma_1*Isigma_2/Isigma0)^(1-alpha)/(k+1)/sqrt(kai/2/pi)/exp(-kai*(mu_1^2+mu_2^2-mu0^2)/2)/++
beta^alpha/exp(-beta*(Isigma_1+Isigma_2-Isigma0))/w_1^(sum(ifelse(z_0==1,1,0)))/w_2^(sum(ifelse(z_0==2,1,0)));
tempratio=ifelse(is.na(tempratio),1/100,tempratio);
ac_s=die[k+1]/(bor[k]*Palloc*dbeta(u1,2,2)*dbeta(u2,2,2)*dbeta(u3,1,1))*++
w0*abs(mu_1-mu_2)*Isigma0/(u2*(1-u2^2)*u3*(1-u3)*Isigma_1*Isigma_2)/tempratio;
u=runif(1);
if(u<min(1,ac_s))
{k=k+1;mu0=mu_0;w0=w_0;Isigma0=Isigma_0;z=z_0;}
}
###########################
#生成或删除一个成分
N=rep(0,k);#计算属于第i类点的个数
for(i in 1:k){
N[i]=sum(ifelse(z==i,1,0));
}
k_empty=which(N==0);
if(k>1) {#生成或删除一个成分
u=runif(1);
if(u<bor[k]){#<--生成一个成分
w_birth=rbeta(1,1,k);mu_birth=rnorm(1,kesi,sqrt(1/kai));Isigma_birth=rgamma(alpha,1/beta);
B=1/beta(k*delta,delta)*w_birth^(delta-1)*(1-w_birth)^(length(y)+k*delta-k)*(k+1)*++
die[k+1]/(length(k_empty)+1)/bor[k]/dbeta(w_birth,1,k)*(1-w_birth)^k;
u=runif(1);
if(u<min(1,B))#判断是否接受
{
k=k+1;
w_0=rep(0,(k+1));
w_0[1:k]=w0*(1-w_birth);w_0[k+1]=w_birth;w0=w_0;
mu_0=rep(0,(k+1));
mu_0[1:k]=mu0;mu_0[k+1]=mu_birth;mu0=mu_0;
Isigma_0=rep(0,(k+1));
Isigma_0[1:k]=Isigma0;Isigma_0[k+1]=Isigma_birth;Isigma0=Isigma_0;
}
}#生成一个成分-->
else{#<--删除一个成分
if(length(k_empty)!=0){
k_death=k_empty[ceiling(length(k_empty)*runif(1))];
B_B=beta((k-1)*delta,delta)/w0[k_death]^(delta-1)/(1-w0[k_death])^(length(y)+(k-1)*(delta-1))/k*++
bor[k-1]*length(k_empty)/die[k]*dbeta(w0[k_death],1,k-1)/(1-w0[k_death])^(k-1);
u=runif(1);
if(u<min(1,B_B))
{
w_0=rep(0,(k-1));
w_0[1:(k_death-1)]=w0[1:(k_death-1)];
w_0[k_death:(k-1)]=w0[(k_death+1):k];
w_0=w_0/(1-w0[k_death]);
mu_0=rep(0,(k-1));
mu_0[1:(k_death-1)]=mu0[1:(k_death-1)];
mu_0[k_death:(k-1)]=mu0[(k_death+1):k];
Isigma_0=rep(0,(k-1));
Isigma_0[1:(k_death-1)]=Isigma0[1:(k_death-1)];
Isigma_0[k_death:(k-1)]=Isigma0[(k_death+1):k];
if(k_death<k)
{
for(i in (k_death+1):k)
{
temp=which(z==i);
if(length(temp)!=0) z[temp]=i-1;
}
}
k=k-1;
}
}
} #删除一个成分-->
}#生成或删除一个成分-->
else{#<--当k=1时,直接生成一个成分
w_birth=rbeta(1,1,k);mu_birth=rnorm(1,kesi,sqrt(1/kai));Isigma_birth=rgamma(alpha,1/beta);
B=1/beta(k*delta,delta)*w_birth^(delta-1)*(1-w_birth)^(length(y)+k*delta-k)*(k+1)*++
die[k+1]/(length(k_empty)+1)/bor[k]/dbeta(w_birth,1,k)*(1-w_birth)^k;
u=runif(1);
if(u<min(1,B)){#<--判断是否接受
k=k+1;
w_0=rep(0,(k+1));
w_0[1:k]=w0*(1-w_birth);w_0[k+1]=w_birth;w0=w_0;
mu_0=rep(0,(k+1));
mu_0[1:k]=mu0;mu_0[k+1]=mu_birth;mu0=mu_0;
Isigma_0=rep(0,(k+1));
Isigma_0[1:k]=Isigma0;Isigma_0[k+1]=Isigma_birth;Isigma0=Isigma_0;
}#判断是否接受-->
}#当k=1时,直接生成一个成分-->
}