forked from i-am-sorri/Sparse_Bayesian_Imaging_RHESSI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsi_vis_bayes_moves.pro
834 lines (670 loc) · 27.6 KB
/
hsi_vis_bayes_moves.pro
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
;+
; NAME:
; COMBIGEN
;
; PURPOSE:
; Generates all possible combinations n-choose-k.
;
; CATEGORY:
; Math
;
; CALLING SEQUENCE:
; Result = COMBIGEN(N, K)
;
; INPUTS:
; N: Maximum number.
;
; K: Number of elements in each combination.
;
; OUTPUTS:
; Returns a M x K array of all K-length combinations of numbers from 0 to N-1.
;
; EXAMPLE:
; Generate all combinations 5-choose-3:
;
; IDL> print, combigen(5,3)
; 0 0 0 0 0 0 1 1 1 2
; 1 1 1 2 2 3 2 2 3 3
; 2 3 4 3 4 4 3 4 4 4
;
; MODIFICATION HISTORY:
; Written by Jeremy Bailin
; 1 April 2011 Initial writing.
;-
function combigen, n, k
if n lt 2 then message, 'N must be greater than 1.'
if k gt n then message, 'K must be less than or equal to N.'
possible_prev = indgen(n)
for vi=1l, k-1 do begin
possible_next = indgen(n)
; only really possible when possible_next > possible_prev
prevsize = size(possible_prev, /dimen)
possible_prev = rebin(reform(possible_prev,1,prevsize), [n,prevsize], /sample)
possible_next = rebin(possible_next, [n,prevsize], /sample)
good = where(possible_next gt possible_prev, ngood)
if vi eq 1 then begin
; set up array that answers get stored in
result = [[possible_prev[good]],[possible_next[good]]]
endif else begin
; add to result
good_ai = array_indices(possible_prev, good)
result = [[result[good_ai[1,*],*]], [possible_next[good]]]
endelse
possible_prev = possible_next[good]
endfor
return, result
end
;+
; NAME:
; hsi_vis_bayes_accept_source
;
; PURPOSE:
; Accept or reject the new particle
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_accept_source, likeratio, Nsources, new_types, proposta,p, new_sample=new_sample, sample=sample, types=types, dato_new, old_vis=old_vis
if (randomu(seed) le likeratio) then begin
types[0:Nsources[p]-1,p] = new_types
sample[*,*,p] = proposta
new_sample[*,*,p] = proposta
old_vis = dato_new
endif
return, Nsources
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_birth_source
;
; PURPOSE:
; Perform the birth move
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_birth_source, param, visxyobs, expon, p, Nsources=Nsources, sample, n_circles, n_ellipses, n_loops, types=types, new_sample=new_sample, old_vis=old_vis
COMMON uvdata, u,v, pa, mapcenter,alb_apply_index
COMMON srcshape, shape
; Determine the shape and the parameters of the proposed new source.
newx = mapcenter[0]+(randomu(seed)-0.5)*param.fov
newy = mapcenter[1]+(randomu(seed)-0.5)*param.fov
newfw = randomu(seed)*param.maxfwhm
newfl = randomu(seed)*param.maxflux
rand = randomu(seed)
if (rand le param.cdf_prior_types[0]) then begin
type = 0
newsrc = transpose([newx,newy,0,0,newfw,newfl,0])
endif $
else if (rand gt param.cdf_prior_types[0]) and (rand le param.cdf_prior_types[1]) then begin
type = 1
newsrc = transpose([newx,newy,randomu(seed)*param.pa_max,randomu(seed)*0.9+0.1,newfw,newfl,0])
endif else begin
type = 2
newecc = randomu(seed)*0.9+0.1
tmp_max_ecc=hsi_vis_bayes_compute_la(newecc)
newloop = (randomu(seed)*tmp_max_ecc*2-tmp_max_ecc)
newsrc = transpose([newx,newy,randomu(seed)*param.pa_max, newecc,newfw,newfl,newloop])
endelse
if (Nsources[p] ge 1) then begin
proposed_sample = [sample[0:Nsources[p]-1,*,p], newsrc]
types_proposed_sample = [types[0:Nsources[p]-1,p],type]
endif $
else if (Nsources[p] eq 0) then begin
proposed_sample = newsrc
types_proposed_sample = type
endif
; compute the visibilities of the proposed source
data2 = hsi_vis_bayes_compute_vis(types_proposed_sample, proposed_sample, Nsources[p]+1)
; compute the loglikelihood ratio
logratio=hsi_vis_bayes_compute_logratio(old_vis, data2, param.sigma_noise, visxyobs, expon)
jumpratio = (param.lam/(Nsources[p]+1))*param.ratio_birth*exp(logratio)
case type of
0: jumpratio = jumpratio/(n_circles + 1)
1: jumpratio = jumpratio/(n_ellipses + 1)
2: jumpratio = jumpratio/(n_loops + 1)
endcase
; Decide either to accept or not the proposed source.
if (randomu(seed) le jumpratio) then begin
sample[Nsources[p],*,p] = newsrc
new_sample[Nsources[p],*,p] = newsrc
types[Nsources[p],p] = type
Nsources[p] = Nsources[p] + 1
old_vis = data2
endif
return, sample
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_death_source
;
; PURPOSE:
; Perform the death move
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_death_source, param, visxyobs, expon, p, Nsources=Nsources, sample, n_circles, n_ellipses, n_loops, types=types, new_sample=new_sample, old_vis=old_vis
COMMON uvdata, u,v, pa, mapcenter,alb_apply_index
COMMON srcshape, shape
if (Nsources[p] gt 1) then begin ; if there are more than one source
; Propose a source to be eliminated
JJ = [(RANDOMU(SEED,/LONG) MOD NSOURCES[P])]
ARRAY=SAMPLE[0:NSOURCES[P]-1,*,P]
DIMS = SIZE(ARRAY, /DIMENSIONS)
proposed_sample = array[Where(~Histogram(jj, MIN=0, MAX=dims[0]-1)),*] ;removecols(sample[0:Nsources[p]-1,*,p],jj)
array = types[0:Nsources[p]-1,p]
dims = Size(array, /DIMENSIONS)
types_proposed_sample = array[Where(~Histogram(jj, MIN=0, MAX=dims[0]-1))] ; removeind(types[0:Nsources[p]-1,p],jj)
; Compute the corresponding visibilities and the loglikelihood ratio.
data2 = hsi_vis_bayes_compute_vis(types_proposed_sample,proposed_sample, Nsources[p]-1)
logratio=hsi_vis_bayes_compute_logratio(old_vis, data2, param.sigma_noise, visxyobs, expon)
jumpratio = (Nsources[p]/param.lam)*param.ratio_death*exp(logratio)
case types[jj,p] of
0: jumpratio = jumpratio*n_circles
1: jumpratio = jumpratio*n_ellipses
2: jumpratio = jumpratio*n_loops
endcase
; Accept or reject
if (randomu(seed) le jumpratio) then begin
sample[*,*,p] = [proposed_sample,make_array(param.N_max_sources-Nsources[p]+1,param.N_param)]
new_sample[*,*,p] = sample[*,*,p]
types[*,p] = [types_proposed_sample,make_array(param.N_max_sources-Nsources[p]+1,/integer)]
Nsources[p] = Nsources[p] - 1
old_vis = data2
endif
endif $
else if (Nsources[p] eq 1) then begin ; if there is only one source:
proposed_sample = make_array(param.N_param)
types_proposed_sample = 0
; Compute the corresponding visibilities and the loglikelihood ratio.
data2 = hsi_vis_bayes_compute_vis(types_proposed_sample, proposed_sample, Nsources[p]-1)
logratio=hsi_vis_bayes_compute_logratio(old_vis, data2, param.sigma_noise, visxyobs, expon)
jumpratio = (Nsources[p]/param.lam)*param.ratio_death*exp(logratio)
; Accept or reject
if (randomu(seed) le jumpratio) then begin
sample[0,*,p] = proposed_sample
new_sample[0,*,p] = proposed_sample
types[0,p] = types_proposed_sample
Nsources[p] = Nsources[p] - 1
old_vis = data2
endif
endif
return, sample
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_change_sources
;
; PURPOSE:
; Perform the source changing move: ellipse <-> cirlce and ellipse <-> loop
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_change_sources, param, Nsources, p, sample=sample, types=types, visxyobs, expon, new_sample=new_sample, old_vis=old_vis
; CIRCLE <---> ELLIPSE
circles = where(types[0:Nsources[p]-1,p] eq 0, n_circles)
ellipses = where(types[0:Nsources[p]-1,p] eq 1, n_ellipses)
; Circle --> Ellipse
if (n_circles ge 1) then begin
; Set the new three parameters: eccentricity, pa, and loop angle.
ind_src = (randomu(seed,/long) mod n_circles)
src = circles[ind_src]
new_types = types[0:Nsources[p]-1,p]
new_types[src] = 1 ; circle is transformed into an ellipse
proposed_sample = sample[*,*,p] ; use the same first four parameters of the circle
proposed_sample[src,2] = randomu(seed)*param.pa_max ; ellipse angle computed with the uniform.
rand = randomu(seed)
proposed_sample[src,3] = 1-sqrt(1-rand) ; ecc is computed by using the inverse transform of g(u) = 2(1-u)
; Compute the visibilities and the loglikelihood ratio
new_vis = hsi_vis_bayes_compute_vis(new_types,proposed_sample,Nsources[p])
logratio=hsi_vis_bayes_compute_logratio(old_vis, new_vis, param.sigma_noise, visxyobs, expon)
likeratio = exp(logratio)*(n_circles/((n_ellipses+1)*(2*(1-rand))))*(param.prior_types[1]/param.prior_types[0])
; Accept or reject the proposal
Nsources= hsi_vis_bayes_accept_source(likeratio, Nsources, new_types, proposed_sample, p, new_sample=new_sample, sample=sample, types=types, new_vis, old_vis=old_vis)
endif
circles = where(types[0:Nsources[p]-1,p] eq 0, n_circles)
ellipses = where(types[0:Nsources[p]-1,p] eq 1, n_ellipses)
; Ellipse --> Circle
if (n_ellipses ge 1) then begin
; choose the ellipse to be converted into a circle
ind_src = (randomu(seed,/long) mod n_ellipses)
src = ellipses[ind_src]
new_types = types[0:Nsources[p]-1,p]
new_types[src] = 0 ; ellipse -> circle
proposed_sample = sample[*,*,p]
proposed_sample[src,2] = 0
proposed_sample[src,3] = 0
; Compute the visibilities and the loglikelihood ratio
new_vis = hsi_vis_bayes_compute_vis(new_types,proposed_sample,Nsources[p])
logratio=hsi_vis_bayes_compute_logratio(old_vis, new_vis, param.sigma_noise, visxyobs, expon)
likeratio = exp(logratio)*(n_ellipses*(2*(1-sample[src,3,p]))/(n_circles+1))*(param.prior_types[0]/param.prior_types[1])
; Accept or reject the proposal
Nsources= hsi_vis_bayes_accept_source(likeratio, Nsources, new_types, proposed_sample, p, new_sample=new_sample, sample=sample, types=types, new_vis, old_vis=old_vis )
endif
; ELLIPSE <---> LOOP
; ------------------
loop = where(types[0:Nsources[p]-1,p] eq 2, n_loops)
ellipses = where(types[0:Nsources[p]-1,p] eq 1, n_ellipses)
; Ellipse -> loop
if (n_ellipses ge 1) then begin
; Select the ellipse to be converted into a loop and compute the loop angle
ind_src = (randomu(seed,/long) mod n_ellipses)
src = ellipses[ind_src]
new_types = types[0:Nsources[p]-1,p]
new_types[src] = 2 ; ellipse -> loop
proposed_sample = sample[*,*,p] ; use the first 6 parameters of the ellipse
temp=randomu(seed)
la_tmp=hsi_vis_bayes_compute_la(proposed_sample[src,3])
proposed_sample[src,6] = (la_tmp-sqrt(la_tmp^2-(abs(temp*la_tmp*2-la_tmp))^2))*(abs(temp-0.5)/(temp-0.5))
; Compute the visibilities and the loglikelihood ratio
new_vis = hsi_vis_bayes_compute_vis(new_types,proposed_sample,Nsources[p])
logratio=hsi_vis_bayes_compute_logratio(old_vis, new_vis, param.sigma_noise, visxyobs, expon)
likeratio = exp(logratio)*(n_ellipses/(n_loops+1))*(param.prior_types[2]/param.prior_types[1])
; Accept or reject the proposal
Nsources= hsi_vis_bayes_accept_source(likeratio, Nsources, new_types, proposed_sample, p, new_sample=new_sample, sample=sample, types=types, new_vis, old_vis=old_vis)
endif
loop = where(types[0:Nsources[p]-1,p] eq 2, n_loops)
ellipses = where(types[0:Nsources[p]-1,p] eq 1, n_ellipses)
if (n_loops ge 1) then begin ; LOOP --> ELLIPSE
; Select the loop to be converted into an ellipse
ind_src = (randomu(seed,/long) mod n_loops)
src = loop[ind_src]
new_types = types[0:Nsources[p]-1,p]
new_types[src] = 1 ; loop -> ellipse
proposed_sample = sample[*,*,p] ; use the first six parameters of the loop
proposed_sample[src,6] = 0
; Compute the visibilities and the loglikelihood ratio
new_vis = hsi_vis_bayes_compute_vis(new_types,proposed_sample,Nsources[p])
logratio=hsi_vis_bayes_compute_logratio(old_vis, new_vis, param.sigma_noise, visxyobs, expon)
likeratio = exp(logratio)*(n_loops/(n_ellipses+1))*(param.prior_types[1]/param.prior_types[2])
; Accept or reject the proposal
Nsources= hsi_vis_bayes_accept_source(likeratio, Nsources, new_types, proposed_sample, p, new_sample=new_sample, sample=sample, types=types, new_vis, old_vis=old_vis)
endif
return, Nsources
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_parameters_split_ellipse
;
; PURPOSE:
; Compute the parameters of the two proposed ellipses
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_parameters_split_ellipse, n_ellipses, ellipses, sample, ind_src,p, Nsources, pr=pr
; Pick an ellipse (ell) to be split into two ellipses
ind_src = (randomu(seed,/long) mod n_ellipses)
ell = ellipses[ind_src]
; Determine the parameters of the two new proposed ellipses
sinpa = sin(sample[ell,2,p]*!DTOR)
cospa = cos(sample[ell,2,p]*!DTOR)
semiaxis_max = sample[ell,4,p]/(3*(1-sample[ell,3,p]^2)^0.25)
dx = semiaxis_max*sinpa
dy = semiaxis_max*cospa
z0 = randomu(seed)*dx/2 + (3./4)*dx
z1 = randomu(seed)*dy/2 + (3./4)*dy
z2 = randomu(seed)*sample[ell,2,p]/2 + (1./4)*sample[ell,2,p]
z3 = randomu(seed)*sample[ell,3,p]/4 + (1./4)*sample[ell,3,p]/2
z4 = randomu(seed)*sample[ell,4,p]/4 + (1./4)*sample[ell,4,p]/2
z5 = randomu(seed)*sample[ell,5,p]/4 + (1./4)*sample[ell,5,p]/2
pr = (abs(dx)/2)*(abs(dy)/2)*(sample[ell,2,p]/2)*(sample[ell,3,p]/4)*(sample[ell,4,p]/4)*(sample[ell,5,p]/4)
x1 = sample[ell,0,p] + z0
y1 = sample[ell,1,p] - z1
pa1 = sample[ell,2,p] - z2
e1 = sample[ell,3,p]/2 - z3
fw1 = sample[ell,4,p]/2 - z4
fl1 = sample[ell,5,p]/2 - z5
s1 = transpose([x1,y1,pa1,e1,fw1,fl1,0])
x2 = sample[ell,0,p] - z0
y2 = sample[ell,1,p] + z1
pa2 = sample[ell,2,p] + z2
e2 = sample[ell,3,p]/2 + z3
fw2 = sample[ell,4,p]/2 + z4
fl2 = sample[ell,5,p]/2 + z5
s2 = transpose([x2,y2,pa2,e2,fw2,fl2,0])
proposed_sample = sample[*,*,p]
proposed_sample[ell,*] = s1
proposed_sample[Nsources[p],*] = s2
return, proposed_sample
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_split_ellipse
;
; PURPOSE:
; Perform the split of an ellipse
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_split_ellipse, param, Nsources, p, visxyobs, expon, new_sample=new_sample, sample=sample, types=types, old_vis=old_vis
if (Nsources[p] lt param.N_max_sources) then begin ; if there are less than the num. max of sources
ellipses = where(types[0:Nsources[p]-1,p] eq 1, n_ellipses)
if (n_ellipses ge 1 ) then begin ; if there is at least an ellipse
; Compute the parameters of the two new proposed ellipses
proposed_sample= hsi_vis_bayes_parameters_split_ellipse(n_ellipses, ellipses, sample,ind_src,p, Nsources, pr=pr)
new_types = [types[0:Nsources[p]-1,p],1]
; Compute the visibilities and the loglikelihood ratio
new_vis = hsi_vis_bayes_compute_vis(new_types,proposed_sample,Nsources[p]+1)
logratio=hsi_vis_bayes_compute_logratio(old_vis, new_vis, param.sigma_noise, visxyobs, expon)
likeratio = (param.lam*n_ellipses/(Nsources[p]+1))*param.c_split*pr*exp(logratio)
; Accept or reject
if (randomu(seed) le likeratio) then begin
types[Nsources[p],p] = 1
sample[*,*,p] = proposed_sample
new_sample[*,*,p] = proposed_sample
Nsources[p] = Nsources[p] + 1
old_vis = new_vis
endif
endif
endif
return, Nsources
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_merge_ellipse
;
; PURPOSE:
; Perform the merge between two ellipses
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_merge_ellipse, param, p, visxyobs, expon, sample=sample, Nsources, types=types, new_sample=new_sample, old_vis=old_vis
ellipses = where(types[0:Nsources[p]-1,p] eq 1, n_ellipses)
if (n_ellipses ge 2) then begin ; if there are at least two ellipses
numcombi = long(factorial(n_ellipses)/(factorial(2)*factorial(n_ellipses-2)))
combi = combigen(n_ellipses,2)
merge_before = 0
for couple = 0, numcombi-1 do begin
if (keyword_set(merge_before) eq 0) then begin ; if the merge move has not been done before
; Check if the two ellipses are close enough to be merged
ind1 = combi[couple,0]
ind2 = combi[couple,1]
ell1 = ellipses[ind1]
ell2 = ellipses[ind2]
x1 = sample[ell1,0,p]
y1 = sample[ell1,1,p]
x2 = sample[ell2,0,p]
y2 = sample[ell2,1,p]
dist = distance_measure([[x1,y1],[x2,y2]])
ecc_merge = sample[ell1,3,p]+sample[ell2,3,p]
fw_merge = sample[ell1,4,p]+sample[ell2,4,p]
merge_toll_max = 5*fw_merge/(6*(1-ecc_merge^2)^0.25)
merge_toll_min = fw_merge/(2*(1-ecc_merge^2)^0.25)
if ((dist ge merge_toll_min) and (dist le merge_toll_max)) then begin
; Compute the parameters of the proposed ellipse
merge_before = 1
x0 = (x1+x2)/2
y0 = (y1+y2)/2
pa0 = (sample[ell1,2,p]+sample[ell2,2,p])/2
e0 = ecc_merge
fw0 = fw_merge
fl0 = sample[ell1,5,p]+sample[ell2,5,p]
s0 = transpose([x0,y0,pa0,e0,fw0,fl0,0])
sinpa = sin(pa0*!DTOR)
cospa = cos(pa0*!DTOR)
max_semiaxis = fw0/(3*(1-e0^2)^0.25)
dx = max_semiaxis*sinpa
dy = max_semiaxis*cospa
pr = (abs(dx)/2)*(abs(dy)/2)*(pa0/2)*(e0/4)*(fw0/4)*(fl0/4)
proposed_sample = sample[0:Nsources[p]-1,*,p]
proposed_sample[ell1,*] = s0
dims = Size(proposed_sample, /DIMENSIONS)
proposed_sample = proposed_sample[Where(~Histogram([ell2], MIN=0, MAX=dims[0]-1)),*]; removecols(proposed_sample,ell2)
new_types = types[0:Nsources[p]-1,p]
dims = Size(new_types, /DIMENSIONS)
new_types = new_types[Where(~Histogram([ell2], MIN=0, MAX=dims[0]-1))] ;removeind(new_types,ell2)
; Compute the visibilities and the loglikelihood ratio
new_vis = hsi_vis_bayes_compute_vis(new_types,proposed_sample,Nsources[p]-1)
logratio=hsi_vis_bayes_compute_logratio(old_vis, new_vis, param.sigma_noise, visxyobs, expon)
likeratio = (Nsources[p]/(param.lam*(n_ellipses-1)))*(1./(param.c_split*pr))*exp(logratio)
; Accept or reject
if (randomu(seed) le likeratio) then begin
types[0:Nsources[p]-2,p] = new_types
types[Nsources[p]-1,p] = 0
sample[0:Nsources[p]-2,*,p] = proposed_sample
sample[Nsources[p]-1,*,p] = make_array(param.N_param)
new_sample[*,*,p] = sample[*,*,p]
Nsources[p] = Nsources[p] - 1
old_vis = new_vis
endif
endif
endif
endfor
endif
return, Nsources
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_update_parameters
;
; PURPOSE:
; Update (slightly perturb) the parameters of each source
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_update_parameters, param, Nsources, types, p, sample, visxyobs, expon, new_sample=new_sample, old_vis=old_vis
COMMON uvdata, u,v, pa, mapcenter,alb_apply_index
; parameter update move of the Metropolis - Hastings algorithm
sig=param.sigmas
for k = 0, Nsources[p]-1 do begin
; Adaptively determine the sigmas to perturb the parameters
if (types[k,p] ne 0) then begin
sig[2] = param.sigmas_max[2]
new_sigma_ecc = param.sigmas_max[3]*(1-sample[k,3,p]) ; linear
;new_sigma_ecc = sigma_ecc*exp(-sample[k,3,p])*(1-sample[k,3,p]) ; exponential
;new_sigma_ecc = sigma_ecc*(1-sample[k,3,p])/(1+sample[k,3,p]) ; hyperbolic
sig[3] = min([new_sigma_ecc,sample[k,3,p]/4])
endif
sig[4] = min([param.sigmas_max[4],sample[k,4,p]/4])
sig[5] = min([param.sigmas_max[5],(param.maxflux-sample[k,5,p])/4,sample[k,5,p]/4])
if (types[k,p] eq 2) then begin
sig[6] = min([param.sigmas_max[6],abs((180-sample[k,6,p])/4),abs(sample[k,6,p]/4)])
endif
for i = 0, param.N_param-1 do begin
if (types[k,p] eq 2) $
or $
( (types[k,p] eq 1) and (i ne 6) ) $
or $
( (types[k,p] eq 0) and (i ne 2) and (i ne 3) and (i ne 6) ) $
then begin
new_sample[k,i,p] = randomn(seed)*sig[i] + sample[k,i,p] ; update the parameters with the sigmas computed before
if (i eq 6) and (new_sample[k,i,p] ge param.la_max) then new_sample[k,i,p]=param.la_max ; project the loop angle into the correct interval
if (i eq 6) and (new_sample[k,i,p] le -param.la_max) then new_sample[k,i,p]=-param.la_max
if (i eq 4) and (new_sample[k,i,p] le 2) then new_sample[k,i,p]=2. ; lower bound for the fwhm
; Compute the visibilities and the loglikelihood ratio
new_vis = hsi_vis_bayes_compute_vis(types[0:Nsources[p]-1,p],new_sample[0:Nsources[p]-1,*,p], Nsources[p])
logratio=hsi_vis_bayes_compute_logratio(old_vis, new_vis, param.sigma_noise, visxyobs, expon)
likeratio = exp(logratio)
; Accept or reject
if (randomu(seed) le likeratio) then begin
sample[k,i,p] = new_sample[k,i,p]
old_vis = new_vis
endif $
else begin
new_sample[k,i,p] = sample[k,i,p]
endelse
endif
endfor
; Re-assign the shape flag (types)
if (new_sample[k,3,p] lt param.ecc_toll_min) then begin
types[k,p] = 0
endif $
else if ((new_sample[k,3,p] ge param.ecc_toll_min) and (new_sample[k,3,p] lt param.ecc_toll_max)) then begin
if (abs(new_sample[k,6,p]) lt param.loop_toll_max) then begin
types[k,p] = 1
endif else begin
types[k,p] = 2
endelse
endif $
else if (new_sample[k,3,p] ge param.ecc_toll_max) then begin
if (abs(new_sample[k,6,p]) ge param.loop_toll_min) then begin
types[k,p] = 2
endif else begin
types[k,p] = 1
endelse
endif
endfor
return, sample
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_compute_incremental_weights
;
; PURPOSE:
; Compute incremental weights
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_compute_incremental_weights, param, visxyobs, old_nsource, old_types, old_sample, p, incr, log_weights, like_unit=like_unit, new_log_weights
d=param.dmax
vis_tmp = dblarr(N_elements(visxyobs))
if (old_nsource ge 1) then begin ; The (j+1)-th weight depends only on the old_sample (j-th sample).
vis_tmp = hsi_vis_bayes_compute_vis(old_types,old_sample[0:old_nsource-1,*,p], old_nsource)
endif
like_unit[p] = -(norm((visxyobs - vis_tmp)/param.sigma_noise))^2
incr[p] = (d/2.)*like_unit[p]
new_log_weights[p] = log_weights[p] + incr[p]
return, new_log_weights
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_birth_death_sources_post
;
; PURPOSE:
; Call the birth and death functions
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_birth_death_sources, param, visxyobs, expon, p, Nsources, sample=sample, types=types, new_sample=new_sample, old_vis=old_vis
; find the number of circles, ellipses, and loops
if (Nsources[p] ge 1) then begin
cerchi = where(types[0:Nsources[p]-1,p] eq 0, n_circles)
ellissi = where(types[0:Nsources[p]-1,p] eq 1, n_ellipses)
loop = where(types[0:Nsources[p]-1,p] eq 1, n_loops)
endif else begin ; Do we need it?
n_circles = 0
n_ellipses = 0
n_loops = 0
endelse
q = randomu(seed)
; birth move
if (q ge param.Q_birth) and (Nsources[p] lt param.N_max_sources) then begin
sample=hsi_vis_bayes_birth_source(param, visxyobs, expon, p, Nsources=Nsources, sample, n_circles, n_ellipses, n_loops, types=types, new_sample=new_sample, old_vis=old_vis)
endif $
; death move
else if (q le param.Q_death) and (Nsources[p] ge 1) then begin
sample=hsi_vis_bayes_death_source(param, visxyobs, expon, p, Nsources=Nsources, sample, n_circles, n_ellipses, n_loops, types=types, new_sample=new_sample, old_vis=old_vis)
endif
return, Nsources
end
;----------------------------------------------------------------------------
;+
; NAME:
; hsi_vis_bayes_moves
;
; PURPOSE:
; Perform the Monte Carlo moves (birth, death, merge, split and update) and compute the weights
;
; HISTORY:
; July 2018 Written by S. Lugaro, F. Sciacchitano and A. Sorrentino
;
; CONTACT:
; sciacchitano [at] dima.unige.it
; sorrentino [at] dima.unige.it
;
;-
function hsi_vis_bayes_moves, param, visxyobs, expon, p, Nsources=Nsources, sample=sample, types=types, new_sample=new_sample, old_vis=old_vis, old_types, old_sample, incr, log_weights, like_unit=like_unit, new_log_weights
COMMON uvdata, u,v, pa, mapcenter,alb_apply_index
for p = 0, param.N_particles-1 do begin
; Save for each particle p, the number of sources and the visibilities at the j-th iteration.
old_nsource = Nsources[p]
if (Nsources[p] ge 1) then begin
old_vis = hsi_vis_bayes_compute_vis(types[0:Nsources[p]-1,p],sample[0:Nsources[p]-1,*,p], Nsources[p])
endif $
else if (Nsources[p] eq 0) then begin
old_vis = make_array(2*size(pa,/dimension), /double)
endif
; Perform the birth-death move:
Nsources = hsi_vis_bayes_birth_death_sources(param, visxyobs, expon, p, Nsources, sample=sample, types=types, new_sample=new_sample, old_vis=old_vis)
if (Nsources[p] ge 1) then begin ; if the particle has at least one source, perform the other moves.
; Change move:
Nsources = hsi_vis_bayes_change_sources(param, Nsources, p, sample=sample, types=types, visxyobs, expon, new_sample=new_sample, old_vis=old_vis)
; Split move:
Nsources = hsi_vis_bayes_split_ellipse(param, Nsources, p, visxyobs, expon, new_sample=new_sample, sample=sample, types=types, old_vis=old_vis)
; Merge move:
Nsources = hsi_vis_bayes_merge_ellipse(param, p, visxyobs, expon, sample=sample, Nsources, types=types, new_sample=new_sample, old_vis=old_vis)
; Update move:
sample = hsi_vis_bayes_update_parameters(param, Nsources, types, p, sample, visxyobs, expon, new_sample=new_sample, old_vis=old_vis)
; Compute incremental weights
new_log_weights = hsi_vis_bayes_compute_incremental_weights(param,visxyobs, old_nsource, old_types, old_sample, p, incr, log_weights, like_unit=like_unit, new_log_weights)
endif
endfor
return, new_log_weights
end