-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenStates.r
334 lines (267 loc) · 12.3 KB
/
GenStates.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
library(mvtnorm)
library(Matrix)
set.seed(3809)
## input
## swlamda switching rate, ou-bm or bm-ou predined swlamda=c(0.1,0.5)
## deltat time distance between two sampling/observation points
## uplen the length of updating fragment
## start_time the starting time of updating fragment
## osp_state the old states at all sampling/observation points
## ostate the old state at all switching and sampling/observation points
## otime the old time at all switching and sampling/observation points
## osw_ind the old indicator 'SW' or 'SP' for all switching and sampling/observation points
## opon_ind the old ndicator of all potential switching and actual switching 'OB' 'BO' 'pn'
## opon_time the old time at all potential switching and actual switching points.
## output
## pro_state is the proposed states at all switching and sampling/observation points
## pro_time is the proposed time at all switching and sampling/observation points
## pro_sw_ind is the indicator 'SW' or 'SP' for all switching and sampling/observation points
## pro_pon_ind is the indicator of all potential switching and actual switching 'OB' 'BO' 'pn'
## pro_pon_time is the proposed time at all potential switching and actual switching points.
GenStates<- function(swlamda,deltat, uplen,start_time, osp_state,ostate,otime,osw_ind, opon_ind,opon_time)
{
nai = dim(osp_state)[2]
## update start from 4 and update at 6 and update end at 8, states for 4 and 8 are fixed
end_time = start_time+(uplen-1)*deltat
up_start = which(otime == start_time )
up_end = which(otime == end_time )
upNsamp = uplen
upstate = ostate[ up_start, ]
endstate = ostate[ up_end, ]
#Kappa = 3.5
iterkappa = 1 ## count how many iterations to get the right state at the end point
iterbreak = 1 ## if it is iterbreak we reject the new states and use the old states
repeat
{
pon_ind = c() ## 'pn' , 'sw' , 'BO' , 'OB'
pon_probAll = c()
pon_list = c()
pstate<- upstate #rep(1,nai) # state 1 attract to black dot state 2 unattract to black dot (brownian motion)
pall_s <- pstate
psw_ind <- c('SP')
pall_t <- c(0) ## uptime
ouindex=which(pstate==1); n_ou = length(ouindex)
bmindex=which(pstate==2); n_bm = length(bmindex)
psamp = 1
pswt_old = 0
#rest T ,how far from current time to the grid time point
resT = deltat
## propose first switch time
#Kappa = nai*max(swlamda) #nai*swlamda[1]
P_ac_sw = ( n_ou*swlamda[1] + n_bm*swlamda[2] )/Kappa
npsw = 0
repeat{
ac_sw = sample( c(0,1), 1, FALSE, prob=c(1-P_ac_sw,P_ac_sw) )
npsw=npsw+1
if(ac_sw==1) break
}
#pswt = sum( rexp(npsw,Kappa) ) #switch time
pswt = 0
po_t = rexp(npsw,Kappa)
pswt = sum(po_t)
resT = pswt - deltat
move2 = pswt
move1 = deltat
## another way of propose pontential switching points
# Kappa = nai*swlamda[2]
# n_posw = rpois(1, Kappa*deltat)
# t_po_sw = sort( runif(n_posw,min=0,max=deltat) )
# P_ac_sw = ( n_ou*swlamda[1] + n_bm*swlamda[2] )/Kappa
# ?? = sample(t_po_sw,size=1,prob=c(0.1,0.1,0.9) )
repeat
{
##case 1, switch animal state
if(resT<0) ## switching happen first , before sampling
{
if(psamp == upNsamp)
{
break
}
### move to switch point
t = move2
### make the move
ouindex=which(pstate==1)
bmindex=which(pstate==2)
## pick the switch animal according to the switching probability - swlamda[pstate]/de
de=sum(swlamda[pstate])
lnai = c(1:nai)
lppstate = swlamda[pstate]/de
SwitchAnimal = sample( lnai,1,FALSE, prob = lppstate )
## switch animal state
pstate[SwitchAnimal] = 3 - pstate[SwitchAnimal]
######### record all pontential and actual switching time
if(pstate[SwitchAnimal] ==1 ){
pon_ind = c( pon_ind, rep('pn', npsw-1 ),'BO' )
#pon_probAll = c( pon_probAll, rep((1-de/Kappa), npsw-1 ), length(bmindex)*swlamda[2]/Kappa )
pon_probAll = c( pon_probAll, rep((1-de/Kappa), npsw-1 ), swlamda[2]/Kappa )
}else{
pon_ind = c( pon_ind, rep('pn', npsw-1 ),'OB' )
#pon_probAll = c( pon_probAll, rep((1-de/Kappa), npsw-1 ), length(ouindex)*swlamda[1]/Kappa)
pon_probAll = c( pon_probAll, rep((1-de/Kappa), npsw-1 ), swlamda[1]/Kappa)
}
pon_list = c( pon_list, po_t ) #pon_swt
######### ######### ######### ######### ######### ######### #########
## demo state and switch time
psw_ind= rbind(psw_ind,'SW')
pall_s = rbind(pall_s,pstate)
pall_t = cbind(pall_t,pswt+ sum(pswt_old) )
pswt_old = c(pswt_old,pswt)
## update index
ouindex=which(pstate==1); n_ou = length(ouindex)
bmindex=which(pstate==2); n_bm = length(bmindex)
## propose next switching time
#pswt=rexp(1, sum( length(ouindex)*swlamda[1],length(bmindex)*swlamda[2]) )
#Kappa = nai*max(swlamda)#[2]#nai*swlamda[1]
P_ac_sw = ( n_ou*swlamda[1] + n_bm*swlamda[2] )/Kappa
npsw = 0
repeat{
ac_sw = sample( c(0,1), 1, FALSE, prob=c(1-P_ac_sw,P_ac_sw) )
npsw=npsw+1
if(ac_sw==1) break
}
po_t = rexp(npsw,Kappa)
pswt = sum(po_t)
#pon_swt = cumsum(po_t)
#po_resT = pon_swt - abs(resT)
move1 = abs(resT)
move2 = pswt
resT = pswt - abs(resT)
}
###case 2, do not switch animal state
if(resT > 0) #sampling happan first resT>0
{
### move to sampling point
t = move1
ouindex=which(pstate==1)
bmindex=which(pstate==2)
### recording location and state information#
if(psamp == upNsamp)
{
break
} else
{
# demo state and sampling time
pdemot = psamp*2
psamp = psamp+1
psw_ind= rbind(psw_ind,'SP')
pall_s= rbind(pall_s,pstate)
pall_t = cbind(pall_t,pdemot)
}
move2 = abs(resT)
move1 = deltat
resT = resT - deltat
if(psamp == upNsamp)
{
## correct pontential sw time
pon_listR= cumsum(pon_list)
last_pon = cumsum(po_t) + pon_listR[ length(pon_listR) ]
last_pon_ind = which( ( last_pon - (upNsamp-1)*deltat ) < 0 )
pon_listR = c(pon_listR,last_pon[last_pon_ind] )
pon_indR = c(pon_ind, rep( 'pn', length(last_pon_ind) ) )
pon_probAll = c(pon_probAll, rep( (1-sum(swlamda[pstate])/Kappa), length(last_pon_ind) ) )
}
}
}
iterkappa = iterkappa +1
if( identical( pall_s[dim(pall_s)[1],] , endstate) )
{
break
}
## if we cannot make the proposed state the same as the end time point in 5000 iterations,
## we just reject the proposed states and use the old one.
if(iterkappa == 3000)
{
iterbreak = 2
break
}
##########################################################################################
}
if( iterbreak == 1 )
{
pall_t = pall_t + start_time
otime_end= dim(otime)[2]
## put proposed fregments of switching time into the old time sequence
if(up_end < otime_end)
{
pall_time = cbind( t(otime[1:(up_start-1)]),pall_t,t(otime[(up_end+1):dim(otime)[2]] ) )
pall_state = rbind( ostate[1:(up_start-1),], pall_s ,ostate[(up_end+1):dim(otime)[2],] )
nsw_ind = c( osw_ind[1:(up_start-1)] , psw_ind , osw_ind[(up_end+1):dim(otime)[2]] )
} else if(up_end == otime_end)
{
pall_time = cbind( t(otime[1:(up_start-1)]), pall_t )
pall_state = rbind( ostate[1:(up_start-1),], pall_s )
nsw_ind = c( osw_ind[1:(up_start-1)] , psw_ind )
}
if( missing(opon_ind) )
{
## add sampling/observation time and indicator to the potential switching list
## we need the sampling/obsesrvation indicator to find out which fragment we are updating
all_pont = c( pon_listR + start_time , seq(start_time,end_time,by=deltat ) )
## we need to sort the vector after we combined pontential switching time and sampling/observation time
ooo = sort(all_pont, index.return=TRUE)
pro_pon_time = ooo$x
all_pon_ind = c(pon_indR , rep('sp',uplen) )
all_pro_pon_probAll = c(pon_probAll, rep('sp',uplen))
## order all indicators as the index we derived from the order index of all potential time point
pro_pon_ind = all_pon_ind[ooo$ix]
pro_pon_probAll = all_pro_pon_probAll[ooo$ix]
} else{
## add sampling/observation time and indicator to the potential switching list
## we need the sampling/obsesrvation indicator to find out which fragment we are updating
if(up_end < otime_end)
{
all_pont = c( pon_listR + start_time , seq(start_time,end_time,by=deltat ) )
all_pon_ind = c(pon_indR , rep('sp',uplen) )
all_pon_probAll = c(pon_probAll, rep('sp',uplen))
ooo = sort(all_pont, index.return=TRUE)
pon_start = which( opon_time ==start_time); pon_end = which(opon_time==end_time)
if(pon_start==1){ ## if the updating start from time =2 which are the firt point, we can ignore the old time.
pro_pon_ind = c( all_pon_ind[ooo$ix], opon_ind[(pon_end+1):length(opon_ind)] )
pro_pon_time = c( ooo$x, opon_time[(pon_end+1):length(opon_time)] )
pro_pon_probAll = c( all_pon_probAll[ooo$ix], opon_probAll[(pon_end+1):length(opon_probAll)] )
}else{## if the updating do not start from time =2 which are the firt point, we need to add the old time back to the list.
pro_pon_ind = c( opon_ind[1:(pon_start-1)], all_pon_ind[ooo$ix], opon_ind[(pon_end+1):length(opon_ind)] )
pro_pon_time = c( opon_time[1:(pon_start-1)], ooo$x, opon_time[(pon_end+1):length(opon_time)] )
pro_pon_probAll = c( opon_probAll[1:(pon_start-1)], all_pon_probAll[ooo$ix], opon_probAll[(pon_end+1):length(opon_probAll)] )
}
}else if( up_end == otime_end )
{
all_pont = c( pon_listR + start_time , seq(start_time,end_time,by=deltat ) )
all_pon_ind = c(pon_indR , rep('sp',uplen) )
all_pon_probAll = c(pon_probAll, rep('sp',uplen) )
ooo = sort(all_pont, index.return=TRUE)
pon_start = which( opon_time ==start_time)
pro_pon_ind = c( opon_ind[1:(pon_start-1)], all_pon_ind[ooo$ix] ) #cbind( opon_ind[1:(pon_start-1)], all_pon_ind[ooo$ix] )
pro_pon_time = c( opon_time[1:(pon_start-1)], ooo$x ) #cbind( opon_time[1:(pon_start-1)], ooo$x )
pro_pon_probAll = c( pon_probAll[1:(pon_start-1)], all_pon_probAll[ooo$ix] )
}
}
} else {
pall_state =0 ; pall_time=0; nsw_ind = 0; pro_pon_ind = 0; pro_pon_time =0; pro_pon_probAll=0
}
return( list('pro_state'=pall_state,'pro_time'=pall_time,'pro_sw_ind'=nsw_ind,'pro_pon_ind'=pro_pon_ind,'pro_pon_time'=pro_pon_time,'pro_pon_probAll'=pro_pon_probAll,'iterbreak'=iterbreak,'iterkappa'=iterkappa) )
}
# Kappa = max(swlamda)
# pro_all_time=c(0)
# repeat
# {
# # ouindex=which(state==1)
# # bmindex=which(state==2)
# pon_sw_t = rexp( 1, Kappa*nai )
# pro_all_time = cbind( pro_all_time, + pon_sw_t )
# if(pon_sw_t > mxtime)
# {
# break
# } else {
# P_ac_sw = ( n_ou*swlamda[state] + n_bm*swlamda[state] )/Kappa*nai
# de=sum(swlamda[state])
# SwitchAnimal =sample( c(1,2,3,4,5,6),1,FALSE, prob=c( swlamda[state[1]]/de,swlamda[state[2]]/de,swlamda[state[3]]/de,swlamda[state[4]]/de,swlamda[state[5]]/de,swlamda[state[6]]/de) )
# state[SwitchAnimal] = 3 - state[SwitchAnimal]
# }
# }
# all_pon = pro_st$pro_pon_ind[ which( pro_st$pro_pon_ind != 'sp') ]
# all_pon[which(all_pon=='pn')] = 0
# all_pon[which(all_pon=='OB')] = -1
# all_pon[which(all_pon=='BO')] = 1
# all_pon = as.numeric(all_pon)
# all_pon =cumsum(all_pon)