-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.f90
1993 lines (1828 loc) · 73.4 KB
/
main.f90
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!----------------------------------------------------------------------------------85
! MCMC ~ ~ ~ MAIN.F90 !
!-----------------------------------------------------------------------------------!
!..Lightly edited version of MCMC code used in mcmc ice sticking work, now being used
!..for MCMC constraint of 1D rain shaft model with BOSS microphysics !
!.. start edits: Nov 2 2015 !
!.. !
!..UPDATE 12/01/2017 -- specific to 2-term BOSS !
!.. Owing to the possible need to sample both the positive and negative ranges of !
!.. a-parameter space while ALSO sampling in log- (rather than linear) space of !
!.. those parameters, we have to do lots of funny stuff that doesn't exist in !
!.. the "standard" version of this AM sampler. For example, "p_naught" serves as a !
!.. switch for this funny behavior, and is fed into perturbation and rangecheck !
!.. routines... !
!...................................................................................!
PROGRAM main
use rand_util, only: random_real, kissinit
USE fwd_model
USE netcdf
IMPLICIT NONE
!.. variables
INTEGER, PARAMETER :: &
unit_nl = 303, &
unit_rand = 314, &
unit_icov = 315, &
unit_mean = 316, &
unit_lhs= 152
REAL, PARAMETER :: &
target_rate = 0.23, & !...Desired (target) acceptance rate
acc_thresh = 0.05 !...Convergence threshold (target /pm thresh)
!.......................................
CHARACTER (LEN=200) :: &
out_file, & !..
sa_file, &
prior_icovfile, &
prior_meanfile, &
cov_file
INTEGER, ALLOCATABLE, DIMENSION(:) :: &
pflag, & !..
b_test, &
monotonic, &
obs_types
REAL, ALLOCATABLE, DIMENSION(:) :: &
params, & !..
ptrue, & !..
mptrue, & !..
obs, & !..
fwd_obs, & !..
fwd_obs_alt, & !..
obs_alt, & !..
fwd_obs_star, &
fwd_obs_save, &
fwd_obs_alt_star, &
fwd_obs_mle, &
p_fwd_obs, &
p_fwd_obs_alt, &
sigma_obs, &
p_params, & !..Proposal parameter values
params_mle, & !..MLE parameter vector
dp, & !..Amount to perturb parameters by
psigma, & !..Parameter proposal std. dev. (independent)
psigma_anneal, & !..Param proposal std. dev. for sim. annealing
psigma_running, & !..Running (adaptive) standard dev.
psigma_old, & !..Saved for posterity
pmean, & !..Mean parameter value
pmean_new, & !..New mean parameter value
pmeen, &
pmeen_new, &
pstar, & !..Intermediate parameter value (delay. rej.)
pmin, &
pmax, &
p_sa_mean, &
p_sa_accmean, &
pnaught, &
p_linspace_ratio, &
mparams, &
mparams_mle, &
p_mparams, &
mpstar, &
priormean
DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:) :: &
p_like_save, &
p_phi_save, &
sigma_mle, &
sigma_diag
REAL, ALLOCATABLE, DIMENSION(:,:) :: &
first_guess, &
dp_save, &
p_store, &
icovmat, &
!....CHECK THESE v v v
pcov_cut, & !...Cut down to only perturbed parameters
R, & !...Cholesky decomp of pcov_cut
pcov, & !...Proposal covariance matrix
pcov_old, & !...Same as above, saved for posterity
pcov_running, & !...Running (adaptive) inv
pcond !...Conditioning matrix (for smooth inversion)
!data_save, & !...Extra saved data array
!p_data_save !...proposal saved data array
!....CHECK THESE ^ ^ ^
!LOGICAL, ALLOCATABLE, DIMENSION(:) :: &
! gauss_prior
INTEGER :: &
nmc, & !..
nguess, & !..
nparams, & !..
mc_ncid, & !..
sa_ncid, & !..For simulated annealing
sb_ncid, & !..To get pcov if skip_burn
param_dimid, & !..
par_dimid, & !..For simulated annealing
obs_dimid, & !..
obs_alt_dimid, & !..
mc_dimid, & !..
sa_dimid, & !..For simulated annealing
params_varid, & !..
m_params_varid, & !..
par_varid, & !..For simulated annealing
pcov_varid, &
sb_pcov_varid, & !..For pcov if skip_burn
obs_varid, & !..
obso_varid, & !..
obs_alt_varid, & !..
obso_alt_varid, & !..
fpsigma_varid, & !..
mle_varid, &
mmle_varid, &
mptrue_varid, &
ll_varid, & !..log-likelihood netcdf varid
lls_varid, & !..log-likelihood netcdf varid for sim annealing
!pcov_varid, & !..
!.........................MCMC VARS
max_burn, & !...Max number of burn iterations = the lesser of
!...this or 0.5*nmc
nchain, & !...Chain number - used for random number stream
nchain_lhs, & !...Chain number - for LHS random number stream
delay_lev, &
delay_count, &
max_delay_lev, & !...Maximum delay level (delayed rejection > 1)
cov_mem, &
cov_check, &
iburn_spin, &
cov_count, &
n_adapt, &
adapt_count, &
bcount, &
option, &
icount, &
ios, &
naccept, &
nobs_alt, &
n_obs_types, &
diagnostic_mod, &
out_mod, & !..How often to write
!..
success, &
nprtb, & !..Number of perturbed parameters
nprtb_tmp, & !..Number of
i,j,k,p, &
sim_ann_type, & !..Controls if/how simulated annealing pre-samplr
n_sa !..number of simulated annealing samples
REAL(8) :: urand1
REAL :: &
urand, & !...Uniform random number
nrand1, & !...First 0-mean, unit std. dev rand number
nrand2, & !...Second 0-mean, unit std dev rand number
s_n, & !...Factor to inflate gaussian std dev
s_n_sq, & !...Factor to inflate gaussian covariance
acc_rate, & !...Current acceptance rate
inv_adapt_count, & !...1/adapt_count
init_par_var, & !..Initial proposal variance scaling
init_par_var_sa, & !..Initial proposal variance scaling sim anneal
scale_s_n, & !..Scale factor for MCMC perturbation scaling
obs_err_mult, & !..Scale factor to inflate obs error
invT_scale, & !..Temp scaling factor for sim annealing
invT, &
prior_phi !..log likelihood for the prior distribution
DOUBLE PRECISION :: &
!...cost function vars:
phi, & !...Cost function value (log-likelihood)
phi_save, &
p_phi, & !...Proposal cost function
phi_mle, & !...Maximum log-likelihood
phi_star, & !...Intermediate cost function
like, & !...Accepted (prior) likelihood
p_like, & !...Proposal likelihood
prop_fac, &
like_mle, & !...Maximum likelihood estimate
like_star, & !...Intermediate likelihood (adaptive)
alpha_star !...Intermediate factor... see Haario et al '06
INTEGER, DIMENSION(2) :: &
count_params, & !..
count_obs, & !..
count_obs_alt, & !..
start !..
LOGICAL :: &
set_guess, & !..Use prescribed or random first guess
sim_obs, & !..External 'obs' from truth params or real obs
morr_obs, & !..Use simulated obs from MORR bulk scheme sim
cov_prop, & !..Consider correlated proposal perturbations
uniform_dist, & !..Sample parameter space uniformly
skip_burn, & !..Skip burn-in procedure, move to main or adapt
adaptive, & !..Use adaptive Metropolis algorithm
dr_star, & !..Switch for choice of p_star
found_pert, & !
main_chain, & !..
accepted, & !..
delaying, & !..
burn_in, & !..
use_alt_obs, & !..
!monotonic, & !..Whether to demand monotonicity in parameters
err_from_file, & !..Err from file or from namelist?
logdum, &
save_fwd_obs, & !..Whether to save fwd_obs from each sample
save_obs_alt, & !..Whether to save obs_alt from each sample
print_diag, & !..Whether to print diagnostics at this iteration
scam_burn, & !..Whether to allow for covariance in burn-in
sim_ann_run, & !..Whether we are within simulated annealing loop
gauss_prior, & !..Whether to use a Gaussian prior for parameters.
sigma_unknown !..Whether to diagnose sigmas instead of reading them in.
! We could just declare this with "external :: dpotf2".
! But an explicit interface is better for compiler error checking.
interface
subroutine dpotf2(uplo, n, a, lda, info)
use shr_kind_mod, only: r8 => shr_kind_r8
character(len=1) :: uplo
integer :: n
integer :: lda
real(r8), dimension(lda,*) :: a
integer :: info
end subroutine dpotf2
end interface
!.......................... ~ ~ ~END DECLARATIONS ~ ~ ~ ............................!
!......................................................................!
! Initial Setup !
!......................................................................!
!..Primary namelist statement, open and read-in
NAMELIST /record2/ nchain, nchain_lhs, nmc, set_guess, nguess, nparams, out_file, &
sim_obs, morr_obs, cov_check, cov_mem, max_burn, scale_s_n, &
init_par_var, &
uniform_dist, cov_prop, n_adapt, adaptive, max_delay_lev, &
skip_burn, dr_star, obs_err_mult, use_alt_obs, &
err_from_file, save_fwd_obs, save_obs_alt, &
diagnostic_mod, out_mod, ic_filename, scam_burn, &
sim_ann_type, invT_scale, n_sa, sa_file, init_par_var_sa, &
cov_file, sigma_unknown
NAMELIST /record3/ ptrue, pmin, pmax, pflag, first_guess, monotonic, &
pnaught, p_linspace_ratio, gauss_prior, prior_icovfile, prior_meanfile
!..
PRINT*, 'STARTING MARCUS MAIN PROGRAM!!'
CALL initialize_nl
!..If there are other initialization routines that need to be run from fwd_model module, add below
!call initialize_wv_sat
!..
!OPEN ( unit_nl, FILE='namelist.input', FORM='formatted', ACTION='read', &
OPEN ( unit_nl, FILE=namelist_flnm, FORM='formatted', ACTION='read', &
ACCESS='sequential', STATUS='old')
!..Read in MCMC parameters
READ ( unit_nl, NML= record2 )
CLOSE ( unit_nl )
!IF (cov_prop) STOP 'Code does not allow this yet!!'
IF (max_delay_lev .GT. 2) STOP 'Maximum delay level is 2!!!'
!..Get value of nobs, however that is done. Note that fwd_model may require that
!..this is run before anything else
! Initialize random number generator
PRINT*,'LHS Random number seed = ',nchain_lhs,' x 100'
CALL kissinit(nchain_lhs*100)
DO i = 1,10
urand1 = random_real()
print*, 'random number ',i,' = ',urand1
ENDDO
!............................
CALL initialize_values(nobs_alt)
print*,'nobs = ',nobs
print*,'nobs_alt = ',nobs_alt
!............................
! Initialize random number generator
PRINT*,'MCMC Random number seed = ',nchain,' x 100'
CALL kissinit(nchain*100)
DO i = 1,10
urand1 = random_real()
print*, 'random number ',i,' = ',urand1
ENDDO
!! Initialize random number generator
!OPEN(unit_rand, FILE='random_seed', STATUS='old', FORM='formatted', ACTION='read', &
! ACCESS='sequential')
!READ ( unit_rand, * ) k
!ALLOCATE ( iseed(k) )
!READ ( unit_rand, * ) iseed
!CLOSE ( unit_rand )
!!..Intialize random number seed
!iseed = iseed + (nchain*100000)
!CALL RANDOM_SEED(put=iseed)
!............................
ALLOCATE( ptrue(nparams) )
ALLOCATE( mptrue(nparams) )
ALLOCATE( pmin(nparams) )
ALLOCATE( pmax(nparams) )
ALLOCATE( pflag(nparams) )
ALLOCATE( pmean(nparams) )
ALLOCATE( pmean_new(nparams) )
ALLOCATE( pmeen(nparams) )
ALLOCATE( pmeen_new(nparams) )
ALLOCATE( pstar(nparams) )
ALLOCATE( params(nparams) )
ALLOCATE( p_params(nparams) )
ALLOCATE( params_mle(nparams) )
ALLOCATE( dp(nparams))
ALLOCATE( psigma(nparams))
ALLOCATE( psigma_anneal(nparams) )
ALLOCATE( psigma_running(nparams) )
ALLOCATE( psigma_old(nparams) )
ALLOCATE( monotonic(nparams) )
ALLOCATE( p_sa_mean(nparams) )
ALLOCATE( p_sa_accmean(nparams) )
!..
ALLOCATE( pnaught(nparams) )
ALLOCATE( p_linspace_ratio(nparams) )
ALLOCATE( mparams(nparams) )
ALLOCATE( mparams_mle(nparams) )
ALLOCATE( p_mparams(nparams) )
ALLOCATE( mpstar(nparams) )
!..
ALLOCATE( obs(nobs) )
ALLOCATE( fwd_obs(nobs) )
ALLOCATE( fwd_obs_star(nobs) )
ALLOCATE( fwd_obs_save(nobs) )
ALLOCATE( obs_alt(nobs_alt) )
ALLOCATE( fwd_obs_alt(nobs_alt) )
ALLOCATE( fwd_obs_alt_star(nobs_alt) )
ALLOCATE( fwd_obs_mle(nobs) )
ALLOCATE( p_fwd_obs(nobs) )
ALLOCATE( p_fwd_obs_alt(nobs_alt) )
ALLOCATE( sigma_obs(nobs) )
ALLOCATE( obs_types(nobs) )
ALLOCATE( b_test(cov_mem) )
!..
ALLOCATE( pcov(nparams,nparams) )
ALLOCATE( pcov_old(nparams,nparams) )
ALLOCATE( pcov_running(nparams,nparams) )
ALLOCATE( pcond(nparams,nparams) )
!..
ALLOCATE( dp_save(nparams,max_delay_lev) )
ALLOCATE( p_like_save(max_delay_lev) )
ALLOCATE( p_phi_save(max_delay_lev) )
ALLOCATE( p_store(nparams, cov_mem) )
pmax=0.
print*,'pmax = ',pmax
!..Structure of output paramters (nparams,nmc) where nmc is an unlimited dimension
!..in the netcdf file
!..Structure of output fwd obs (nvd,nzd,nmc) where nmc is an unlimited dimension in
!..the netcdf file
CALL check(NF90_CREATE( path=out_file, cmode=NF90_CLOBBER, ncid=mc_ncid ))
!..
CALL check(NF90_DEF_DIM( mc_ncid, 'param_dim', nparams, param_dimid))
CALL check(NF90_DEF_DIM( mc_ncid, 'fwd_obs_dim', nobs, obs_dimid))
CALL check(NF90_DEF_DIM( mc_ncid, 'fwd_obs_alt_dim', nobs_alt, obs_alt_dimid))
CALL check(NF90_DEF_DIM( mc_ncid, 'mc_dim', NF90_UNLIMITED, mc_dimid))
!..
CALL check(NF90_DEF_VAR( mc_ncid, 'parameters', NF90_DOUBLE,(/param_dimid, mc_dimid/), &
params_varid))
CALL check(NF90_DEF_VAR( mc_ncid, 'm_parameters', NF90_DOUBLE,(/param_dimid, mc_dimid/), &
m_params_varid))
CALL check(NF90_DEF_VAR( mc_ncid, 'loglikelihood',NF90_DOUBLE, mc_dimid, ll_varid))
CALL check(NF90_DEF_VAR( mc_ncid, 'params_mle', NF90_DOUBLE,(/param_dimid/),mle_varid))
CALL check(NF90_DEF_VAR( mc_ncid, 'm_params_mle', NF90_DOUBLE,(/param_dimid/),mmle_varid))
CALL check(NF90_DEF_VAR( mc_ncid, 'm_params_true', NF90_DOUBLE,(/param_dimid/),mptrue_varid))
IF (save_fwd_obs) &
CALL check(NF90_DEF_VAR( mc_ncid, 'fwd_obs_unrav',NF90_DOUBLE,(/obs_dimid, mc_dimid/), &
obs_varid))
IF (save_obs_alt) &
CALL check(NF90_DEF_VAR( mc_ncid, 'fwd_obs_alt_unrav',NF90_DOUBLE,(/obs_alt_dimid,mc_dimid/), &
obs_alt_varid))
IF (cov_prop) &
CALL check(NF90_DEF_VAR( mc_ncid, 'pcov', NF90_DOUBLE, (/param_dimid,param_dimid/), &
pcov_varid))
CALL check(NF90_DEF_VAR( mc_ncid,'obs_unrav',NF90_DOUBLE,(/obs_dimid/),obso_varid))
CALL check(NF90_DEF_VAR(mc_ncid,'obs_alt_unrav',NF90_DOUBLE,(/obs_alt_dimid/),obso_alt_varid))
CALL check(NF90_DEF_VAR( mc_ncid,'final_psigma',NF90_DOUBLE,(/param_dimid/),fpsigma_varid))
CALL check(NF90_ENDDEF( mc_ncid ))
CALL check(NF90_CLOSE(mc_ncid ))
!...................................................................!
! Load Observations and Obs Error from ... !
!...................................................................!
!..If first guess from file, read in, else generate randomly from prior dist'n
!..Read in namelist record with first guess
ALLOCATE (first_guess(nparams,nguess))
ALLOCATE (priormean(nparams) )
ALLOCATE (icovmat(nparams, nparams))
!ALLOCATE (gauss_prior(nparams))
!OPEN ( unit_nl, FILE='namelist.input', FORM='formatted', ACTION='read', &
OPEN ( unit_nl, FILE=namelist_flnm, FORM='formatted', ACTION='read', &
ACCESS='sequential', STATUS='old')
READ ( unit_nl, NML = record3 )
CLOSE( unit_nl )
print*,'pmax = ',pmax
print*,'first guess =',first_guess
nprtb = SUM(pflag)
ALLOCATE( pcov_cut (nprtb,nprtb) )
ALLOCATE( R (nprtb,nprtb) )
print*,'monotonic = ',monotonic
!..Load obs from file
option=0
!..Load/set obs error vector or matrix
!CALL load_obs(obs,sigma_obs,option)
!..we assume here that ptrue is actually a model-readable vector of parameter values
mptrue = ptrue
CALL inv_preprocess_parameters(mptrue, pmin, pmax, pnaught, p_linspace_ratio, ptrue)
IF (sim_obs) THEN
CALL load_sim_obs(obs,obs_alt,nparams,mptrue,nobs_alt)
CALL load_sigma_obs(sigma_obs, err_from_file)
ELSE
IF (morr_obs) THEN
CALL load_morr_obs(obs,obs_alt,nobs_alt)
CALL load_sigma_obs(sigma_obs, err_from_file)
ELSE
STOP "No function to load real obs. FIX plz!"
ENDIF
ENDIF
if (sigma_unknown) then
call load_obs_types(n_obs_types, obs_types)
allocate(sigma_diag(n_obs_types))
allocate(sigma_mle(n_obs_types))
else
call load_sigma_obs(sigma_obs, err_from_file)
end if
if (.not. sigma_unknown) then
sigma_obs = obs_err_mult*sigma_obs
end if
!..Write obs vector to file
print*,'got here 00'
CALL check(NF90_OPEN(path=out_file, mode = NF90_WRITE, ncid=mc_ncid))
print*,'got here 01'
CALL check(NF90_PUT_VAR(mc_ncid,obso_varid,obs))
print*,'got here 02'
CALL check(NF90_PUT_VAR(mc_ncid,obso_alt_varid,obs_alt))
print*,'mptrue = ',mptrue
CALL check(NF90_PUT_VAR(mc_ncid,mptrue_varid,mptrue))
print*,'got here 03'
CALL check(NF90_CLOSE(mc_ncid))
print*,'got here 05'
PRINT*, 'done with obs loading'
!...................................................................!
! Run First Guess !
!...................................................................!
params = ptrue
p_params = ptrue
mparams = mptrue
p_mparams = mptrue
!IF(ANY(pmax<pmin)) STOP 'pmax and pmin set wrong!'
print*,'pflag = ',pflag
print*,'pmin = ',pmin
print*,'pmax = ',pmax
print*,'ptrue = ',ptrue
print*,'first_guess = ',first_guess
print*,'nparams = ',nparams
!..Read in the prior mean and prior inverse covariance matrix.
!..These arrays are assumed to act in the space of sampled parameters, so
!..in log space if sampling accurs in log space.
icovmat = 0.
priormean = 0.
If (gauss_prior) THEN
OPEN(unit_icov, FILE=prior_icovfile, ACTION='read')
READ(unit_icov,*) icovmat
CLOSE(unit_icov)
print *, "icovmat = ", icovmat
!..
OPEN(unit_mean, FILE=prior_meanfile, ACTION='read')
READ(unit_mean,*) priormean
CLOSE(unit_mean)
print *, "priormean = ", priormean
ENDIF
!..Set scale factors, according to Gelman (?)
s_n = scale_s_n * 2.4 / SQRT(SUM(real(pflag)))
s_n_sq = s_n**2
!..Run forward model using first_guess (nguess times)
count_params = (/nparams,1/)
count_obs = (/nobs,1/)
count_obs_alt= (/nobs_alt,1/)
start = (/1,0/)
IF (set_guess) THEN
DO i=1,nguess
print*,'Guess no. ',i
start(2) = start(2) + 1
mparams = first_guess(:,i)
!..Run forward model
!..No need to run preprocessor on first_guess, but need to run inverse to
!..initialize value of params
CALL inv_preprocess_parameters(mparams, pmin, pmax, pnaught, p_linspace_ratio, params)
print*,'params first guess = ',params
print*,'mparams first guess = ',mparams
!..
CALL run_fwd_model(mparams, fwd_obs, fwd_obs_alt) !..has been pre-processed
if (sigma_unknown) then
call likelihood_ind_diag_sigma(obs, fwd_obs, n_obs_types, obs_types, phi, like, sigma_diag)
else
IF (use_alt_obs) THEN
CALL likelihood_ind(obs, fwd_obs_alt, sigma_obs, phi, like)
ELSE
CALL likelihood_ind(obs, fwd_obs, sigma_obs, phi, like)
ENDIF
end if
PRINT*,'TEST. first guess fwd_obs = ',fwd_obs
IF (save_obs_alt) PRINT*,'TEST. first guess fwd_obs_alt = ',fwd_obs_alt
phi_save = phi
PRINT*,'First guess phi = ',phi
PRINT*,'First guess like = ',like
if (sigma_unknown) then
print *, 'First guess sigma_diag = ', sigma_diag
end if
!..Write output
CALL check(NF90_OPEN(path=out_file, mode = NF90_WRITE, ncid=mc_ncid))
CALL check(NF90_PUT_VAR(mc_ncid,params_varid, params, start=start,count=count_params))
CALL check(NF90_PUT_VAR(mc_ncid,m_params_varid, mparams, start=start,count=count_params))
CALL check(NF90_PUT_VAR(mc_ncid,ll_varid, [phi], start=[start(2)],count=[1]))
IF (save_fwd_obs) CALL check(NF90_PUT_VAR(mc_ncid,obs_varid, fwd_obs, &
start=start,count=count_obs))
IF (save_obs_alt) CALL check(NF90_PUT_VAR(mc_ncid,obs_alt_varid,fwd_obs_alt, &
start=start,count=count_obs_alt))
CALL check(NF90_CLOSE(mc_ncid))
ENDDO
ELSE
print*,'doing random first guess'
CALL perturb_uni (pflag,pmin,pmax,pnaught,params)
CALL preprocess_parameters(params, pmin, pmax, pnaught, p_linspace_ratio, mparams)
print*,'params first guess = ',params
print*,'mparams first guess = ',mparams
start(2) = start(2) + 1
CALL run_fwd_model(mparams, fwd_obs, fwd_obs_alt)
if (sigma_unknown) then
call likelihood_ind_diag_sigma(obs, fwd_obs, n_obs_types, obs_types, phi, like, sigma_diag)
else
IF (use_alt_obs) THEN
CALL likelihood_ind(obs, fwd_obs_alt, sigma_obs, phi, like)
ELSE
CALL likelihood_ind(obs, fwd_obs, sigma_obs, phi, like)
ENDIF
end if
PRINT*,'First guess phi = ',phi
PRINT*,'First guess like = ',like
if (sigma_unknown) then
print *, 'First guess sigma_diag = ', sigma_diag
end if
!..Write output
CALL check(NF90_OPEN(path=out_file, mode = NF90_WRITE, ncid=mc_ncid))
CALL check(NF90_PUT_VAR(mc_ncid,params_varid, params, start=start,count=count_params))
CALL check(NF90_PUT_VAR(mc_ncid,m_params_varid, mparams, start=start,count=count_params))
CALL check(NF90_PUT_VAR(mc_ncid,ll_varid, [phi], start=[start(2)],count=[1]))
IF (save_fwd_obs) CALL check(NF90_PUT_VAR(mc_ncid,obs_varid, fwd_obs, start=start,count=count_obs))
IF (save_obs_alt) CALL check(NF90_PUT_VAR(mc_ncid,obs_alt_varid,fwd_obs_alt,start=start,count=count_obs_alt))
CALL check(NF90_CLOSE(mc_ncid))
ENDIF
prior_phi = -50.
!..Figure out the prior (relative) probability of the first guess
if (gauss_prior) prior_phi = return_prior_phi(params, priormean, icovmat)
!..Note that while this will apply the gaussian prior, it should have a 100% accept probability
IF ( .NOT. (rangecheck(pmin,pmax,pnaught,params,pflag,monotonic, &
gauss_prior,priormean,icovmat,prior_phi))) THEN
PRINT*,'First guess is not in range you dummy!'
PRINT*,'PMIN PMAX PARAMS MPARAMS'
DO i=1,nparams
IF (pnaught(i) < 900.) THEN
IF(params(i) .GT. 1.0) THEN
PRINT*,'w',i,0.,1.,params(i),mparams(i)
ELSEIF(params(i) .LT. 0.0) THEN
PRINT*,'w',i,0.,1.,params(i),mparams(i)
ENDIF
ELSEIF (params(i) .GT. pmax(i)) THEN
PRINT*,i,pmin(i),pmax(i),params(i),mparams(i)
ELSEIF (params(i) .LT. pmin(i)) THEN
PRINT*,i,pmin(i),pmax(i),params(i),mparams(i)
ENDIF
ENDDO
STOP 'you done messed up'
ENDIF
!..Initialize maximum likelihood values
phi_mle = phi
like_mle = like
params_mle = params
mparams_mle = mparams
fwd_obs_mle = fwd_obs
if (sigma_unknown) sigma_mle = sigma_diag
!..Set spinup in burn-in to 10x var checks (make sure burn-in continues for a while)
iburn_spin = cov_check * 10
! Set scale parameter (depends on number of perturbed parameters)
! s_n = scale_s_n * 2.4 / sqrt(float(nparams))
! Bugfix here--make sure we are scaling by actual number of parameters perturbed,
! not the total number of parameters...
s_n = scale_s_n * 2.4 / SQRT(SUM(real(pflag)))
s_n_sq = s_n**2
PRINT '(a,f12.3)','Scale factor on Gaussian perturbation: ',s_n
pcond = 0.
pcov_cut = 0.
pcov = 0.
!..Prepare initial proposal variance
IF (cov_prop) THEN
nprtb = 0
DO i = 1,nparams
!...If skip_burn is true, load covariance from file. Otherwise, set.
IF ( skip_burn ) THEN
CALL check(NF90_OPEN(path=cov_file, mode=NF90_NOWRITE, ncid = sb_ncid) )
CALL check(NF90_INQ_VARID(sb_ncid, "pcov", sb_pcov_varid) )
CALL check(NF90_GET_VAR(sb_ncid, sb_pcov_varid, pcov) )
CALL check(NF90_CLOSE(sb_ncid) )
!OPEN ( unit_pcov, FILE = TRIM('parameter_cov_04.dat'), STATUS = 'old', &
! FORM = 'unformatted', ACTION = 'read', ACCESS = 'stream')
!READ ( unit_pcov ) pcov
!CLOSE( unit_pcov )
!pcov = s_n_sq*pcov
ELSE
IF (pnaught(i) > 900.) THEN
pcov(i,i) = (init_par_var*( pmax(i) - pmin(i)))**2
!pcov(i,i) = init_par_var*( pmax(i) - pmin(i))
ELSE
pcov(i,i) = init_par_var**2 !..bc the range of these is 0-1
ENDIF
ENDIF
pcond(i,i) = 0.01*pcov(i,i)
!pcond(i,i) = 1e-10
!pcond(i,i) = 1.d-15
IF ( pflag(i) .EQ. 1) THEN
nprtb = nprtb + 1
nprtb_tmp = 0
DO j = 1,nparams
IF ( pflag(j) .EQ. 1) THEN
nprtb_tmp = nprtb_tmp + 1
pcov_cut(nprtb,nprtb_tmp) = pcov(i,j)
ENDIF
ENDDO
ENDIF
ENDDO
!...Perform Cholesky decomposition, test for success/failure
R = pcov_cut
!CALL spotf2('L', nprtb, R, nprtb, success)
CALL dpotf2('L', nprtb, R, nprtb, success)
!CALL dpotrf_f95(R, 'U', info=success)
IF ( success .NE. 0 ) THEN
STOP 'Cholesky Decomposition Failed'
ENDIF
!..Zero non-diagonal elements
DO i = 1,nprtb
DO j = 1, i-1
R(j,i) = 0.d0 !..If dpotf2('L',...)
ENDDO
ENDDO
ELSE
DO i=1,nparams
IF (pnaught(i) > 900.) THEN
psigma(i) = init_par_var*( pmax(i) - pmin(i) )
ELSE
psigma(i) = init_par_var
ENDIF
ENDDO
psigma = s_n*psigma
PRINT*,'psigma = ', psigma
ENDIF
!.................................................................................!
! SIMULATED ANNEALING PRE-SAMPLER (IF REQUESTED) !
!.................................................................................!
IF (sim_ann_type /= 0) THEN
!..Set up output just for Simulated Annealing pre-sampler
CALL check(NF90_CREATE( path = sa_file, cmode=NF90_CLOBBER, ncid = sa_ncid ) )
CALL check(NF90_DEF_DIM( sa_ncid, 'param_dim', nparams, par_dimid) )
CALL check(NF90_DEF_DIM( sa_ncid, 'mc_dim', NF90_UNLIMITED, sa_dimid) )
!..
CALL check(NF90_DEF_VAR( sa_ncid, 'parameters', NF90_DOUBLE, (/par_dimid, sa_dimid/), &
par_varid))
CALL check(NF90_DEF_VAR( sa_ncid, 'loglikelihood', NF90_DOUBLE, sa_dimid, lls_varid))
!..
CALL check(NF90_ENDDEF( sa_ncid ) )
CALL check(NF90_CLOSE( sa_ncid) )
!..NB: Simulated Annealing here will ONLY use uncorrelated proposal
!..Take care here with weird parameters!
DO i=1,nparams
IF (pnaught(i) > 900.) THEN
psigma_anneal(i) = init_par_var_sa*( pmax(i) - pmin(i) )
ELSEIF (pnaught(i) > -900.) THEN
PRINT*,'THIS IS A FUNNY VAR, var no. = ',i
psigma_anneal(i) = init_par_var_sa
ENDIF
ENDDO
!..Initialize sigmulated annealing statistics
p_sa_mean = params !..The full mean
p_sa_accmean = params !..The mean of only accepted points (no duplicates)
!..Se anealing parameters and initialize variables
k = 1 !
naccept = 0 !
count_params = (/nparams,1/)
start = (/1,0/)
CALL check(NF90_OPEN(path=sa_file, mode=NF90_WRITE, ncid = sa_ncid) )
!DO WHILE (k < n_sa )
sim_ann_run = .TRUE.
DO WHILE (sim_ann_run)
start(2) = start(2) + 1
!
IF (sim_ann_type == 1) THEN
invT = 1.
ELSE IF (sim_ann_type == 2) THEN
invT = REAL(k+1)*invT_scale
ELSE IF (sim_ann_type == 3) THEN
invT = LOG(REAL(k+1))*invT_scale
ELSE IF (sim_ann_type == 4) THEN
invT = SQRT(REAL(k+1))*invT_scale
ELSE
STOP 'THIS mcmc_type OPTION NOT YET CODED'
ENDIF
IF (k>1) THEN
!..Generate perturbation parameter
DO i = 1,nparams
IF (pflag(i) == 1) THEN
CALL box_muller_polar(nrand1, nrand2)
dp(i) = nrand1*psigma_anneal(i)
ELSE
dp(i) = 0.
ENDIF
ENDDO
p_params = params + dp
ENDIF
!..Note that this rangecheck will enforce the prior, but that prior will NOT be
!..scaled by the simulated annealing temperature
IF ( .NOT. (rangecheck(pmin,pmax,pnaught,p_params,pflag,monotonic, &
gauss_prior,priormean,icovmat,prior_phi))) THEN
PRINT*,'OUT OF RANGE, SIMULATED ANNEALING, OUT OF RANGE ! ! ! ! ! ! '
!print*,'p_params = ',p_params
!p_like = 0.
p_like = 1.e-40
p_phi = -9.e10
ELSE
CALL preprocess_parameters(p_params, pmin, pmax, pnaught, p_linspace_ratio, p_mparams)
!print*,'params SA = ',p_params
!print*,'mparams SA = ',p_mparams
CALL run_fwd_model(p_mparams, p_fwd_obs, p_fwd_obs_alt)
if (sigma_unknown) then
call likelihood_ind_diag_sigma(obs, p_fwd_obs, n_obs_types, obs_types, p_phi, p_like, sigma_diag)
else
IF (use_alt_obs) THEN
CALL likelihood_ind(obs, p_fwd_obs_alt, sigma_obs, p_phi, p_like)
ELSE
CALL likelihood_ind(obs, p_fwd_obs, sigma_obs, p_phi, p_like)
ENDIF
end if
ENDIF
!..Check to see if accepted proposal is a new MLE
IF (p_phi > phi_mle) THEN
like_mle = p_like
params_mle = p_params
mparams_mle = p_mparams
phi_mle = p_phi
fwd_obs_mle = p_fwd_obs
if (sigma_unknown) sigma_mle = sigma_diag
ENDIF
!..Apply accept/reject criterion
urand1 = random_real()
urand = REAL(urand1)
IF ( urand <= EXP((p_phi - phi)*invT) ) THEN
IF (MOD(k,100) == 0) THEN
PRINT*,'DIAGNOSTIC: Sim. Annealing Proposal Accepted!!'
PRINT*,'Iter no. ',k,' of ',n_sa
PRINT*,'Accpted total = ',naccept
PRINT*,'urand = ',urand,' ratio = ',EXP(p_phi-phi),' sa_ratio = ',EXP((p_phi - phi)*invT)
PRINT*,'phi = ',phi,' p_phi = ',p_phi
if (sigma_unknown) then
print *, 'sigma_diag = ', sigma_diag
end if
ENDIF
naccept = naccept + 1
params = p_params
mparams = p_mparams
like = p_like
phi = p_phi
fwd_obs = p_fwd_obs
fwd_obs_alt = p_fwd_obs_alt
fwd_obs_save= fwd_obs
!..ADD IO step here?
IF (gauss_prior) prior_phi = return_prior_phi(params, priormean, icovmat)
ELSE
IF (MOD(k,100) == 0) THEN
PRINT*,'DIAGNOSTIC: Sim Annealing proposal Rejected'
PRINT*,'Iter no. ',k,' of ',n_sa
PRINT*,'Accpted total = ',naccept
PRINT*,'urand = ',urand,' ratio = ',EXP(p_phi-phi),' sa_ratio = ',EXP((p_phi - phi)*invT)
PRINT*,'phi = ',phi,' p_phi = ',p_phi
if (sigma_unknown) then
print *, 'sigma_diag = ', sigma_diag
end if
ENDIF
ENDIF
p_sa_mean = p_sa_mean*(k-1)/k +params/k
!..IO step
CALL check(NF90_PUT_VAR(sa_ncid, par_varid, mparams, start = start, count= count_params))
CALL check(NF90_PUT_VAR(sa_ncid, lls_varid, [phi], start = [start(2)], count = [1]))
k = k + 1
IF (k > n_sa+1) THEN
sim_ann_run = .FALSE.
!..If the log-likelihood is too low, then stop because sampling will likely fail
!..Minimum phi = 2xobs standard deviation = -2*nobs
IF ( phi_mle < MIN(phi_save,-2.*(REAL(nobs)) )) THEN
PRINT*, '---------------------------------------------------------------'
PRINT*, '---------------------------------------------------------------'
PRINT*, ' SIMULATED ANNEALING SAMPLER PHI_MLE TOO LOW! HAS NOT FOUND'
PRINT*, ' A HIGH-PROBABILITY REGION. TRY A DIFFERENT RANDOM SEED!'
PRINT*, ' phi_mle = ',phi_mle,', phi_min = ',-2.*(REAL(nobs))
PRINT*, ' p_params = ',p_params
PRINT*, ' p_mparams= ',p_mparams
PRINT*, 'p_fwd_obs = ',p_fwd_obs
if (sigma_unknown) print *, 'sigma_mle = ',sigma_mle
PRINT*, '---------------------------------------------------------------'
PRINT*, '---------------------------------------------------------------'
STOP 'MCMC chain stopped owing to poor simulated annealing performance'
ENDIF
ENDIF
ENDDO !..while k < n_sa
CALL check(NF90_CLOSE(sa_ncid) )
!..Start annealing loop, to conclude when k == n_sa
!..Print diagnostic outputs
PRINT*,'--------------------------------------------'
PRINT*,'SIMULATED ANNEALING PRE-SAMPLER COMPLETE!!!'
PRINT*,'Number of accepted samples = ', naccept
PRINT*,'Accept percentage = ', 100.*naccept/n_sa
PRINT*,'MLE log-likelihood = ',phi_mle
PRINT*,'MLE likelihood = ',like_mle
PRINT*,'MLE params = ',params_mle
PRINT*,'mean params = ',p_sa_mean
if (sigma_unknown) print *, 'sigma_mle = ',sigma_mle
PRINT*,'--------------------------------------------'
PRINT*,'Last proposed parameter values, etc:'
PRINT*, ' p_params = ',p_params
PRINT*, ' p_mparams= ',p_mparams
PRINT*, 'p_fwd_obs = ',p_fwd_obs
PRINT*,'--------------------------------------------'
!..Set params to params_mle
params = params_mle
mparams = mparams_mle
phi = phi_mle
like = like_mle
fwd_obs = fwd_obs_mle
if (gauss_prior) prior_phi = return_prior_phi(params, priormean, icovmat)
ENDIF !..sim_ann_type /= 0
!.................................................................................!
! ~ ~ ~ MAIN MCMC LOOP ~ ~ ~ !
!.................................................................................!
!..Initialize counters
icount = 1
bcount = 1
cov_count = 1
naccept = 0
main_chain = .TRUE.
delay_lev = 1
delay_count = 0
b_test = 0
!..Set burn-in flag to true -- NB: to skip burn-in, set to false
burn_in = .TRUE.
adapt_count = cov_mem
!n_adapt = 100 !..Proposal variance adapts every n_adapt samples
acc_rate = 0.
!..Initialize random number generator (if this hasn't been done in set_guess)
!IF (set_guess) CALL RANDOM_NUMBER(urand)
IF (set_guess) urand1 = random_real()
urand = REAL(urand1)
print_diag = .TRUE.
PRINT*, 'STARTING MAIN MCMC LOOP!'
CALL check(NF90_OPEN(path=out_file, mode = NF90_WRITE, ncid=mc_ncid) )
!...Allow for namelist specified skipping of burn-in
IF ( skip_burn ) THEN
burn_in = .false.
naccept = 0
IF ( adaptive ) THEN
psigma_running = psigma
pcov_running = pcov
adapt_count = cov_mem
ENDIF
ENDIF
DO WHILE ( main_chain )
IF (MOD(icount,diagnostic_mod)==0) print_diag=.TRUE.
!..Draw proposal perturbation
found_pert = .FALSE.
accepted = .FALSE.
!..Generate parameter perturbation vector & parameter proposal vector
IF (uniform_dist) THEN
CALL perturb_uni(pflag,pmin,pmax,pnaught,p_params)
ELSE
IF ( cov_prop ) THEN
CALL perturb(nparams, nprtb, 1, pflag, R, dp)
ELSE
dp = 0.
DO i = 1,nparams
IF (pflag(i) .EQ. 1) THEN
CALL Box_Muller_polar(nrand1,nrand2)
dp(i) = nrand1 * psigma(i)
ENDIF
ENDDO
ENDIF
!..Scale parameter perturbation by delayed rejection level
p_params = params + dp/SQRT(REAL(delay_lev))
ENDIF
logdum = rangecheck(pmin,pmax,pnaught,p_params,pflag,monotonic, &
gauss_prior,priormean,icovmat,prior_phi)
!..Determine how to handle this proposal..........................!
!..Out of range, reject
IF ( .NOT. (logdum) .AND. &
(delay_lev .GE. max_delay_lev)) THEN
IF (print_diag) THEN
PRINT*,'DIAGNOSTIC: option 1 - out of range, reject'
PRINT*,'Params = ',params
PRINT*,'P_params = ',p_params
ENDIF
accepted = .false.
delaying = .false.
!..Out of range, delay rejection
ELSEIF ( .NOT. (logdum) .AND. &
(delay_lev .LT. max_delay_lev)) THEN
IF (print_diag) PRINT*,'DIAGNOSTIC: option 2 - out of range, delay'
accepted = .false.
delaying = .true.
p_like_save(delay_lev) = 0.
p_phi_save(delay_lev) = -9999.
dp_save(:,delay_lev) = dp
!..In range, run fwd model, calculate likelihood, accept probabilistically
ELSE
IF (print_diag) THEN
PRINT*,'DIAGNOSTIC: option 3 - in range'
PRINT*,'Params = ',params
PRINT*,'P_params = ',p_params
ENDIF
CALL preprocess_parameters(p_params, pmin, pmax, pnaught, p_linspace_ratio, p_mparams)
CALL run_fwd_model(p_mparams, p_fwd_obs, p_fwd_obs_alt)
!CALL likelihood_cov(nobs, obs, p_fwd_obs, cov_obs, p_phi, p_like)
if (sigma_unknown) then
call likelihood_ind_diag_sigma(obs, p_fwd_obs, n_obs_types, obs_types, p_phi, p_like, sigma_diag)
else
IF (use_alt_obs) THEN
CALL likelihood_ind(obs, p_fwd_obs_alt, sigma_obs, p_phi, p_like)