-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntl_gibbs.jl
702 lines (592 loc) · 21.2 KB
/
ntl_gibbs.jl
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
###########################################################################
# Utilities for Gibbs updates
###########################################################################
# using StatsBase
# using Distributions
function logp_partition(PP::Vector{Int},T::Vector{Int},Psi::Vector{Float64},
alpha::Float64,ia_dist::DiscreteDistribution,is_partition::Bool)
"""
- `PP`: vector of partition block sizes ordered by arrival time
- `T`: vector of arrival times
- `Psi`: vector of beta random variables (can be log(Psi))
- `alpha`: 'discount parameter' in size-biased reinforcement
- `ia_dist`: distribution object corresponding to i.i.d. interarrivals
- `is_partition`: Boolean flag for computing binomial coefficients
"""
if all(Psi .<= 0)
# warn("Psi in log space.")
log_Psi = Psi
log_Psi_c = log.(1 - exp.(log_Psi))
elseif all(0 .<= Psi .<= 1)
log_Psi = log.(Psi)
log_Psi_c = log.(1 - Psi)
else
error("Invalid Ψ.")
end
# shift distributions with non-zero mass on zero
zero_shift = Int(minimum(ia_dist) == 0)
PP_bar = cumsum(PP)
ia = T[2:end] .- T[1:(end-1)]
K = size(Psi,1)
idx = 1:(K-1)
N = sum(PP)
log_p = dot((PP[2:end] .- alpha .- 1),log_Psi[2:end]) + dot((PP_bar[1:(end-1)] .- idx.*alpha .- 1), log_Psi_c[2:end])
# include arrival times
log_p += sum(logpdf.(ia_dist, ia .- zero_shift))
N - T[end] > 0 ? log_p += log(1 - cdf(ia_dist, N-T[end]-zero_shift)) : nothing
log_p += -sum([lbeta(1 - alpha,T[j] - 1 - (j-1)*alpha) for j in 2:K])
# include binomial coefficients if for a partition
if is_partition
log_p += sum([lbinom(PP_bar[j] - T[j],PP[j] - 1) for j in 2:K])
end
return log_p
end
function logp_partition(PP::Vector{Int},T::Vector{Int},
alpha::Float64,ia_dist::DiscreteDistribution,is_partition::Bool)
f = (x,y) -> ia_dist
logp_partition(PP,T,alpha,f,is_partition)
end
function logp_partition(PP::Vector{Int},T::Vector{Int},
alpha::Float64,ia_dist::Function,is_partition::Bool)
"""
- `PP`: vector of partition block sizes ordered by arrival time
- `T`: vector of arrival times
- `alpha`: 'discount parameter' in size-biased reinforcement
- `ia_dist`: function that creates an interarrival distribution at fixed parameters
- `is_partition`: flag for computing binomial coefficients
"""
# shift distributions with non-zero mass on zero
zero_shift = Int(minimum(ia_dist(1,1)) == 0)
PP_bar = cumsum(PP)
# pop!(PP_bar)
ia = T[2:end] .- T[1:(end-1)]
K = length(PP)
idx = 1:(K-1)
N = sum(PP)
log_p = log_CPPF(PP,T,alpha)
if N - T[end] > 0
p_gt = 1 - cdf(ia_dist(T[end],K), N-T[end]-zero_shift)
abs(p_gt)<=eps(one(typeof(p_gt))) || p_gt < 0. ? log_p = -Inf : log_p += log(p_gt)
# log_p += log(1 - cdf(ia_dist(T[end],K), N-T[end]-zero_shift))
end
log_p += sum( [logpdf(ia_dist(T[j-1],j-1),T[j]-T[j-1]-zero_shift) for j in 2:K ])
# include binomial coefficients if for a partition
if is_partition
log_p += sum([lbinom(PP_bar[j] - T[j],PP[j] - 1) for j in 2:K])
end
return log_p
end
function logp_pred_partition(PP_train::Vector{Int},PP_test::Vector{Int},
T::Vector{Int},
alpha::Float64,ia_dist::DiscreteDistribution,
is_partition_train::Bool,is_partition_test::Bool)
f = (x,y) -> ia_dist
logp_pred_partition(PP_train,PP_test,T,alpha,f,is_partition_train,is_partition_test)
end
function logp_pred_partition(PP_train::Vector{Int},PP_test::Vector{Int},
T::Vector{Int},
alpha::Float64,ia_dist::Function,
is_partition_train::Bool,is_partition_test::Bool)
zero_shift = Int(minimum(ia_dist(1,1)) == 0)
K_train = length(PP_train)
K_test = length(PP_test)
T_train = T[1:K_train]
PP_train_bar = cumsum(PP_train)
N_train = PP_train_bar[end]
logp_train = log_CPPF(PP_train,T_train,alpha)
# is_partition_train ? logp_train += sum([lbinom(PP_train_bar[j] - T_train[j],PP_train[j] - 1) for j in 2:length(PP_train)]) : nothing
PP_test_bar = cumsum(PP_test)
logp_test = log_CPPF(PP_test,T,alpha)
# is_partition_test ? logp_test += sum([lbinom(PP_test_bar[j] - T_test[j],PP_test[j] - 1) for j in 2:length(PP_test)]) : nothing
if is_partition_test
# predicted part of partition is as usual
logp_test += sum([lbinom(PP_test_bar[j] - T[j],PP_test[j] - 1) for j in (K_train+1):length(PP_test)])
# conditioned part of partition is constrained
for j in 1:K_train
( PP_test[j] - N_train > 0 && PP_test_bar[j] - PP_train_bar[j] > 0 )?
logp_test += lbinom(PP_test_bar[j] - N_train,PP_test[j] - PP_train[j]) : nothing
end
end
N = PP_test_bar[end]
if N - T[end] > 0
p_gt = 1 - cdf(ia_dist(T[end],K_test), N-T[end]-zero_shift)
abs(p_gt)<=eps(one(typeof(p_gt))) || p_gt < 0. ? logp_test = -Inf : logp_test += log(p_gt)
end
logp_test += sum( [logpdf(ia_dist(T[j-1],j-1),T[j]-T[j-1]-zero_shift) for j in (K_train+1):K_test])
return logp_test - logp_train
end
# memoize this?
function lbinom(n::Int,k::Int)
"""
computes log of binomial coefficient {`n` choose `k`} using `lgamma`
"""
ret = lgamma(n+1) - lgamma(k+1) - lgamma(n - k + 1)
return ret
end
function tally_ints(Z::Vector{Int},K::Int)
"""
counts occurrences in `Z` of integers 1 to `K`
- `Z`: vector of integers
- `K`: maximum value to count occurences in `Z`
"""
ret = zeros(Int,K)
n = size(Z,1)
idx_all = 1:n
idx_j = trues(n)
for j in 1:K
for i in idx_all[idx_j]
if Z[i]==j
ret[j] += 1
idx_j[i] = false
end
end
end
return ret
end
function initialize_alpha(prior_dist::ContinuousUnivariateDistribution)
return [1 - rand(prior_dist)]
end
function update_ntl_alpha!(alpha::Vector{Float64},PP::Vector{Int},T::Vector{Int},log_prior::Function,w::Float64)
"""
Slice-sampling update (with sampler parameter `w`) of NTL discount parameter
`alpha`, conditioned on arrival times `T` and arrival-ordered block/vertex
counts `PP`.
"""
alpha_trans = log(1 - alpha[1])
ss = x -> ntl_alpha_trans_logpdf(x,PP,T,log_prior)
alpha_trans_new = slice_sampling(ss,w,alpha_trans)
alpha[1] = 1 - exp(alpha_trans_new)
end
function ntl_alpha_logpdf(alpha::Float64,PP::Vector{Int},T::Vector{Int},log_prior::Function)
"""
calculate unnormalized log-pdf proportional to `alpha` (discount in NTL)
(Psi_j's are marginalized)
log_prior is a function that returns the (possibly unnormalized) prior log-probability
of `alpha`
"""
# PP_bar = cumsum(PP)
logp = log_prior(1 - alpha) + lgamma(PP[1] - alpha) - lgamma(sum(PP) - size(PP,1)*alpha) # prior is specified as a distribution on (0,Inf); alpha ∈ (-Inf,1)
for j in 2:size(PP,1)
logp += lgamma(PP[j] - alpha) - lbeta(1 - alpha,T[j] - 1 - (j-1)*alpha)
# logp += lbeta(PP[j] - alpha, PP_bar[j-1] - (j-1)*alpha) - lbeta(1-alpha,T[j] - 1 - (j-1)*alpha)
end
return logp
end
function ntl_alpha_trans_logpdf(alpha_trans::Float64,PP::Vector{Int},T::Vector{Int},log_prior::Function)
"""
calculate unnormalized log-pdf proportional to `alpha_trans`,
the transformed discount (alpha) in NTL such that alpha = 1 - exp(alpha_trans)
(Psi_j's are marginalized)
log_prior is a function that returns the (possibly unnormalized) prior log-probability
of `alpha`
"""
return ntl_alpha_logpdf(1 - exp(alpha_trans),PP,T,log_prior) + alpha_trans
end
function seq2part(Z::Vector{Int})
"""
- `Z`: vector of labels corresponding to clusters/blocks in a partition
"""
# transform Z into an ordered partition and get arrival times
K = maximum(Z)
PP = tally_ints(Z,K)
# T = zeros(Int,K)
# for j in 1:K
# T[j] = findfirst(Z.==j)
# end
return PP
end
function get_arrivals(Z::Vector{Int})
"""
- `Z`: vector of labels corresponding to clusters/blocks in a partition
"""
K = maximum(Z)
T = zeros(Int,K)
for j in 1:K
T[j] = findfirst(Z.==j)
end
return T
end
function logp_label_sequence(Z::Vector{Int},Psi::Vector{Float64},
alpha::Float64,ia_dist::DiscreteDistribution)
"""
- `Z`: vector of labels corresponding to clusters/blocks in a partition
- `Psi`: vector of beta random variables (can be log(Psi))
- `alpha`: 'discount parameter' in size-biased reinforcement
- `ia_dist`: distribution object corresponding to i.i.d. interarrivals
"""
PP = seq2part(Z)
T = get_arrivals(Z)
log_p = logp_partition(PP,T,Psi,alpha,ia_dist,false)
return log_p
end
function cluster_rm!(x::Vector{Vector{Float64}},k::Int)
"""
removes column k from vector of vectors x (e.g. corresponding cluster params)
"""
deleteat!(x,k)
end
function cluster_rm!(x::Vector{Float64},k::Int)
"""
removes k-th entry from x
"""
deleteat!(x,k)
end
function cluster_add!(x::Vector{Vector{Float64}},x_new::Vector{Float64},k::Int)
"""
inserts `x_new` into `x` at entry `k`
"""
insert!(x,k,x_new)
end
function cluster_add!(x::Vector{Float64},x_new::Float64,k::Int)
"""
inserts `x_new` into `x` at entry `k`
"""
insert!(x,k,x_new)
end
function cycle_elements_left!(V::Vector,start_idx::Int,end_idx::Int)
"""
- `V`: Vector whose elements will be cycled
- `start_idx`: start of cycle (will be moved to end)
- `end_idx`: end of cycle
"""
st = V[start_idx]
for i in start_idx:(end_idx-1)
V[i] = V[i+1]
end
V[end_idx] = st
return V
end
function cycle_elements_left!(X::Array,start_idx::Int,end_idx::Int)
"""
- `X`: Array whose columns will be cycled
- `start_idx`: start of cycle (will be moved to end)
- `end_idx`: end of cycle
"""
for i in 1:size(X,1)
X[i,:] = cycle_elements_left!(X[i,:],start_idx,end_idx)
end
return X
end
function cycle_elements_right!(V::Vector,start_idx::Int,end_idx::Int)
"""
- `V`: Vector whose elements will be cycled
- `start_idx`: start of cycle (will be moved to end)
- `end_idx`: end of cycle
"""
ed = V[end_idx]
for i in end_idx:-1:(start_idx+1)
V[i] = V[i-1]
end
V[start_idx] = ed
return V
end
function cycle_elements_right!(X::Array,start_idx::Int,end_idx::Int)
"""
- `X`: Array whose columns will be cycled
- `start_idx`: start of cycle (will be moved to end)
- `end_idx`: end of cycle
"""
for i in 1:size(X,1)
X[i,:] = cycle_elements_right!(X[i,:],start_idx,end_idx)
end
return X
end
function log_cppf_counts(PP::Vector{Int},alpha::Float64)
"""
helper function for `log_CPPF` and `update_label_sequence`
"""
gt1 = PP .> 1
ret = sum( lgamma.(PP[gt1] .- alpha) ) - sum(gt1)*lgamma(1 - alpha)
return ret
end
function log_cppf_arrivals(T::Vector{Int},alpha::Float64)
"""
helper function for `log_CPPF` and `update_label_sequence`
"""
K = size(T,1)
ret = sum( lgamma.(T .- (1:K).*alpha) ) - sum( lgamma.(T[2:end] .- 1 .- (1:(K-1)).*alpha) )
return ret
end
function log_cppf_arrivals(T::Vector{Int},arrival_offset::Int,alpha::Float64)
"""
helper function for computing predictive log-probabilities
`arrival_offset=K` indicates that the first element of `T` corresponds to the
K-th arrival
"""
K_end = arrival_offset - 1 + size(T,1)
return sum( lgamma.(T .- (arrival_offset:K_end).*alpha) ) - sum( lgamma.(T .- 1 .- (arrival_offset-1):(K_end-1).*alpha) )
end
function log_CPPF(PP::Vector{Int},T::Vector{Int},alpha::Float64)
"""
- `PP`: arrival-ordered vector of partition block sizes
- `T`: arrival times
- `alpha`: 'discount' parameter
"""
n = sum(PP)
K = size(T,1)
logp = -lgamma(n - K*alpha) + log_cppf_arrivals(T,alpha) + log_cppf_counts(PP,alpha)
return logp
end
function log_CPPF(Z::Vector{Int},alpha::Float64)
"""
- `Z`: vector of labels corresponding to clusters/blocks in a partition
- `alpha`: 'discount' parameter
"""
T = get_arrivals(Z)
PP = seq2part(Z)
logp = log_CPPF(PP,T,alpha)
return logp
end
function get_num_blocks(Z::Vector{Int})
"""
- `Z`: vector of labels corresponding to clusters/blocks in a partition
"""
n = size(Z,1)
K = zeros(Int64,n)
K[1] = 1
max_z = 1
for i in 2:n
if Z[i] > max_z
K[i] = K[i-1] + 1
max_z += 1
end
end
return K
end
function update_psi_parameters_sequence!(Psi::Vector{Float64},Z::Vector{Int},alpha::Float64)
"""
- `Psi`: vector of current values of Psi
- `Z`: vector of labels corresponding to clusters/blocks in a partition
- `alpha`: 'discount' parameter
"""
PP = seq2part(Z)
update_psi_parameters_partition!(Psi,PP,alpha)
# Psi[:] = [pu[i] for i in 1:size(Psi,1)]
return Psi
end
function update_psi_parameters_partition!(Psi::Vector{Float64},PP::Vector{Int},alpha::Float64)
"""
- `Psi`: vector of current values of Psi
- `PP`: arrival-ordered vector of partition block sizes
- `alpha`: 'discount' parameter
"""
K = size(PP,1)
PP_bar = cumsum(PP)
Psi[1] = 1
for j in 2:K
Psi[j] = rand(Beta(PP[j]-alpha,PP_bar[j-1]-(j-1)*alpha))
end
return Psi
end
function log_sum_exp_weights(logw::Vector{Float64})
"""
-`logw`: log of weights to be combined for a discrete probability distribution
"""
maxlogw = maximum(logw)
shift_logw = logw - maxlogw
p = exp.(shift_logw)./sum(exp.(shift_logw))
return p
end
function initialize_arrival_times(PP::Vector{Int},alpha::Float64,ia_dist::DiscreteDistribution)
f = (x,y)->ia_dist
initialize_arrival_times(PP,alpha,f)
end
function initialize_arrival_times(PP::Vector{Int},alpha::Float64,ia_dist::Function)
"""
- `PP`: arrival-ordered vector of partition block sizes
- `alpha`: 'discount' parameter
- `ia_dist`: function that returns pdf corresponding to interarrivals
"""
zero_shift = Int(minimum(ia_dist(1,1)) == 0)
K = size(PP,1)
PP_bar = cumsum(PP)
n = PP_bar[end]
typeof(ia_dist(1,1))==CRPinterarrival ? crp = true : crp = false
crp ? crp_dist = ia_dist(1,1) : nothing
T = zeros(Int,K)
T[1] = 1
for j in 2:K
# determine support of interarrival
supp = 1:(PP_bar[j-1] - T[j-1] + 1)
# calculate pmf of conditional distribution
log_p = zeros(Float64,size(supp,1))
if crp
log_p += crp_logpdf(crp_dist.theta,crp_dist.alpha,T[j-1],j-1,supp.-zero_shift)
else
log_p += logpdf.(ia_dist(T[j-1],j-1),supp.-zero_shift)
end
log_p += lbinom.(PP_bar[j] .- T[j-1] .- supp, PP[j] - 1)
log_p += lgamma.(T[j-1] .+ supp .- j*alpha) .- lgamma.(T[j-1] .+ supp .- 1 .- (j-1)*alpha)
# sample an update
p = log_sum_exp_weights(log_p)
T[j] = T[j-1] + wsample(supp,p)
# println("initialized T_",j,"=",T[j])
end
return T
end
function sample_interarrival(j::Int,T_jm1::Int,T_jp1::Int,ia_dist::Function,
zero_shift::Int,PP_bar_jm1::Int,PP_j::Int,alpha::Float64)
"""
Utility function for arrival time updates
"""
delta2 = T_jp1 - T_jm1
# check to see if interarrivals are Geometric or CRP to short-cut some computtions
typeof(ia_dist(1,1))==Distributions.Geometric{Float64} ? geom = true : geom = false
typeof(ia_dist(1,1))==CRPinterarrival ? crp = true : crp = false
crp ? crp_dist = ia_dist(1,1) : nothing
ia_dist(1,1)==ia_dist(10,10) ? iid = true : iid = false
# determine support
supp = 1:min(delta2 - 1, PP_bar_jm1 - T_jm1 + 1)
if size(supp,1) > 1
# calculate pmf of conditional distribution
log_p = zeros(Float64,size(supp,1))
if crp
log_p += crp_logpdf(crp_dist.theta,crp_dist.alpha,T_jm1,j-1,supp.-zero_shift)
elseif !geom && iid # geometric interarrival prior is memoryless => doesn't contribute
tmp = logpdf.(ia_dist(T_jm1,j-1),supp.-zero_shift)
log_p += tmp .+ tmp[end:-1:1] # iid interarrivals are symmetric
end
log_p += lbinom.(PP_bar_jm1 .+ PP_j .- T_jm1 .- supp, PP_j - 1)
log_p += lgamma.(T_jm1 .+ supp .- j*alpha) .- lgamma.(T_jm1 .+ supp .- 1 .- (j-1)*alpha)
if !iid
for s in supp
if crp
log_p[s] += crp_logpdf(crp_dist.theta,crp_dist.alpha,T_jm1+s,j,delta2 - s - zero_shift)
else
log_p[s] += logpdf(ia_dist(T_jm1+s,j),delta2 - s - zero_shift)
end
end
end
# sample an update
p = log_sum_exp_weights(log_p)
return wsample(supp,p)
else
return 1
end
end
function sample_final_arrival(T_Km1::Int,K::Int,n::Int,ia_dist::Function,
zero_shift::Int,PP_bar_Km1::Int,PP_K::Int,alpha::Float64)
if T_Km1==(n-1)
TK = n
else
typeof(ia_dist(1,1))==Distributions.Geometric{Float64} ? geom = true : geom = false
typeof(ia_dist(1,1))==CRPinterarrival ? crp = true : crp = false
crp ? crp_dist = ia_dist(1,1) : nothing
supp = 1:min(n - T_Km1, PP_bar_Km1 - T_Km1 + 1)
log_p = zeros(Float64,size(supp,1))
if crp
log_p += crp_logpdf(crp_dist.theta,crp_dist.alpha, T_Km1, K-1, supp.-zero_shift)
elseif !geom
log_p += logpdf.(ia_dist(T_Km1,K-1), supp.-zero_shift)
end
log_p += lbinom.(n .- T_Km1 .- supp, PP_K - 1)
log_p += lgamma.(T_Km1 .+ supp .- K*alpha) .- lgamma.(T_Km1 .+ supp .- 1 .- (K-1)*alpha)
if !geom
for s in supp
if crp
p_gt = 1. - crp_cdf(crp_dist.theta,crp_dist.alpha,T_Km1+s,K,n-(T_Km1+s-zero_shift))
else
p_gt = 1. - cdf(ia_dist(T_Km1+s,K),n-(T_Km1+s-zero_shift)) # this can be arbitrarily close to zero, need to handle numerical instability
end
abs(p_gt)<=eps(one(typeof(p_gt))) || p_gt < 0. ? log_p[s] = -Inf : log_p[s] += log(p_gt)
end
end
p = log_sum_exp_weights(log_p)
TK = T_Km1 + wsample(supp,p)
end
return TK
end
function update_arrival_times!(T::Vector{Int},PP::Vector{Int},alpha::Float64,ia_dist::Function)
"""
Takes advantage of multiple threads if possible.
- `PP`: arrival-ordered vector of partition block sizes
- `T`: current arrival times (to be updated)
- `alpha`: 'discount' parameter
- `ia_dist`: function that creates a distribution object corresponding to interarrival distribution
"""
update_arrival_times_st!(T,PP,alpha,ia_dist)
end
function update_arrival_times!(T::Vector{Int},PP::Vector{Int},alpha::Float64,ia_dist::DiscreteDistribution)
f = (x,y)->ia_dist
update_arrival_times!(T,PP,alpha,f)
end
function update_arrival_times_mt!(T::Vector{Int},PP::Vector{Int},alpha::Float64,ia_dist::Function)
"""
multi-threaded version
- `PP`: arrival-ordered vector of partition block sizes
- `T`: current arrival times (to be updated)
- `alpha`: 'discount' parameter
- `ia_dist`: function that creates a distribution object corresponding to interarrival distribution
"""
zero_shift = Int(minimum(ia_dist(1,1)) == 0)
K = size(T,1)
PP_bar = cumsum(PP)
n = PP_bar[end]
evens = 2:2:(K-1)
odds = 3:2:(K-1)
for j in evens
T[j] = T[j-1] + sample_interarrival(j,T[j-1],T[j+1],ia_dist,zero_shift,PP_bar[j-1],PP[j],alpha)
end
for j in odds
T[j] = T[j-1] + sample_interarrival(j,T[j-1],T[j+1],ia_dist,zero_shift,PP_bar[j-1],PP[j],alpha)
end
T[K] = sample_final_arrival(T[K-1],K,n,ia_dist,zero_shift,PP_bar[K-1],PP[K],alpha)
end
function update_arrival_times_st!(T::Vector{Int},PP::Vector{Int},alpha::Float64,ia_dist::Function)
"""
single-threaded version
- `PP`: arrival-ordered vector of partition block sizes
- `T`: current arrival times (to be updated)
- `alpha`: 'discount' parameter
- `ia_dist`: function that creates a distribution object corresponding to interarrival distribution
"""
zero_shift = Int(minimum(ia_dist(1,1)) == 0)
K = size(T,1)
PP_bar = cumsum(PP)
n = PP_bar[end]
for j in 2:(K-1)
T[j] = T[j-1] + sample_interarrival(j,T[j-1],T[j+1],ia_dist,zero_shift,PP_bar[j-1],PP[j],alpha)
end
T[K] = sample_final_arrival(T[K-1],K,n,ia_dist,zero_shift,PP_bar[K-1],PP[K],alpha)
return T
end
function swap_elements!(x::Vector,i::Int,j::Int)
"""
swap elements `i` and `j` of `x` in place
"""
x[i],x[j] = x[j],x[i]
return x
end
function update_block_order!(perm::Vector{Int},PP::Vector{Int},T::Vector{Int},alpha::Float64)
"""
- `perm`: permutation of order of entries in `PP` (to be updated)
- `PP`: partition of arrival-ordered block sizes
- `T`: arrival times
- `alpha`: 'discount' parameter
Update is through a sequence of proposed adjacent transpositions.
There are likely better (more efficient) ways to do this.
"""
K = size(PP,1)
PP_bar = cumsum(PP)
for j in 1:(K-1)
j==1 ? ppbar_jm1 = 0 : ppbar_jm1 = PP_bar[j-1]
if PP[j]==PP[j+1] # swap is a 50-50 flip
logp_swap = log(0.5)
logp_noswap = log(0.5)
elseif ppbar_jm1 + PP[j+1] >= T[j+1] - 1
logp_swap = lgamma(ppbar_jm1 + PP[j+1] - T[j] + 1) - lgamma(PP_bar[j+1] - PP[j] - T[j+1] + 2)
logp_noswap = lgamma(ppbar_jm1 + PP[j] - T[j] + 1) - lgamma(PP_bar[j] - T[j+1] + 2)
else # impossible to swap blocks given arrival times
logp_swap = -Inf
logp_noswap = 0.
end
swap = wsample([true;false],log_sum_exp_weights([logp_swap;logp_noswap]))
if swap
swap_elements!(PP,j,j+1)
swap_elements!(perm,j,j+1)
(j == 1) ? PP_bar[j] = PP[j] : PP_bar[j] = PP_bar[j-1] + PP[j]
PP_bar[j+1] = PP_bar[j] + PP[j+1]
end
end
return perm
end