Skip to content

Commit 02fcf71

Browse files
committed
Merge remote-tracking branch 'origin/SUBSET_TEST_SEP26' into Eren_HF_DF_28Jun23
2 parents ff1d75c + 43c5d0e commit 02fcf71

62 files changed

Lines changed: 4619 additions & 164 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.swp.*
88
*.out
99
*.out.*
10+
*.log*
1011
DUMPFILES
1112
MATLAB/startup.m
1213
MATLAB/CARDAMOM_DISK/*
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
#pragma once
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <math.h>
5+
#include "../../../math_fun/std.c"
6+
#include "NORMPARS.c"
7+
#include "STEP_AFDEMCMC.c"
8+
#include "STEP_DEMCMCZS.c"
9+
#include "WRITE_DEMCMC_RESULTS.c"
10+
11+
/*here including additional functions needed to initialise and clear memory*/
12+
#include "INITIALIZE_MCMC_OUTPUT.c"
13+
14+
/*Hybrid affine-invariant / DE-MCZS sampler.
15+
*
16+
* For the first fADAPT fraction of the run, this follows AFDEMCMC and uses
17+
* STEP_AFDEMCMC. After that switch point, it uses DEMCMCZS proposals from an
18+
* archive. The archive is initialized like DEMCMCZS and is grown throughout
19+
* the whole run, including the affine phase, so DEMCMCZS can use the affine
20+
* exploration history after handoff.
21+
*/
22+
double *AFDEMCMCZS(
23+
double (MODEL_LIKELIHOOD)(DATA, double *),
24+
DATA DATA, PARAMETER_INFO PI, MCMC_OPTIONS MCO, MCMC_OUTPUT *MCOUT){
25+
26+
printf("CARDAMOM: Successfully entered AFDEMCMCZS!\n"); fflush(stdout);
27+
28+
/*ERASING PREVIOUS FILE IF APPEND == 0 */
29+
if(MCO.APPEND==0 && MCO.nWRITE>0){FILE *fileout=fopen(MCO.outfile,"wb");fclose(fileout);}
30+
31+
int NC=MCO.nchains;
32+
int initialNC=NC;
33+
int demNC=10;
34+
if (demNC>initialNC){demNC=initialNC;}
35+
36+
/*DEMCMCZS archive constants, matching DEMCMCZS.c*/
37+
int M0=10*PI.npars;
38+
if (M0<NC){M0=NC;}
39+
int K=10;
40+
double psnooker=0.10;
41+
42+
int switch_iter=(int)((double)MCO.nOUT*MCO.fADAPT);
43+
if (switch_iter<0){switch_iter=0;}
44+
if (switch_iter>MCO.nOUT){switch_iter=MCO.nOUT;}
45+
46+
/*The archive keeps all affine-phase 400-chain history, then only appends the
47+
*10 live DEMCMCZS chains after the switch.*/
48+
int max_affine_appends=switch_iter/K+2;
49+
int max_demcmczs_appends=(MCO.nOUT-switch_iter)/K+2;
50+
int maxM=M0+initialNC*max_affine_appends+demNC*max_demcmczs_appends;
51+
52+
double *Z=calloc((size_t)maxM*PI.npars,sizeof(double));
53+
double *PARS=calloc(PI.npars*NC,sizeof(double));
54+
double *pars_new=calloc(PI.npars,sizeof(double));
55+
double *P=calloc(NC,sizeof(double));
56+
double *BESTPARS=calloc(PI.npars*NC,sizeof(double));
57+
double *BESTP=calloc(NC,sizeof(double));
58+
59+
int n,nn,M=M0;
60+
double par;
61+
62+
/*Initialize live chains from PI.parini, same convention as AFDEMCMC/DEMCMCZS.*/
63+
for (nn=0;nn<NC;nn++){
64+
for (n=0;n<PI.npars;n++){
65+
if (MCO.randparini==1 && PI.parfix[n]!=1){
66+
par=nor2par((double)random()/(double)RAND_MAX,PI.parmin[n],PI.parmax[n]);
67+
}else{
68+
par=PI.parini[n+nn*PI.npars];
69+
if (par>PI.parmax[n] || par<PI.parmin[n]){printf("Warning, prescribed initial parameters are out of range\n");}
70+
}
71+
PARS[nn*PI.npars+n]=par;
72+
Z[nn*PI.npars+n]=par;
73+
}}
74+
75+
/*Fill the rest of the initial archive with random prior draws, matching DEMCMCZS.c.*/
76+
for (nn=NC;nn<M0;nn++){
77+
for (n=0;n<PI.npars;n++){
78+
if (PI.parfix[n]==1){par=PI.parini[n];}
79+
else{par=nor2par((double)random()/(double)RAND_MAX,PI.parmin[n],PI.parmax[n]);}
80+
Z[nn*PI.npars+n]=par;
81+
}}
82+
83+
oksofar("Established PI.parini - beginning AFDEMCMCZS now");
84+
85+
memcpy(BESTPARS,PARS,PI.npars*NC*sizeof(double));
86+
87+
/*STEP 1 - RUN MODEL WITH INITIAL PARAMETERS*/
88+
for (nn=0;nn<NC;nn++){
89+
P[nn]=MODEL_LIKELIHOOD(DATA,&PARS[nn*PI.npars]);
90+
if (isnan(P[nn])){printf("Warning: MLF generated NaN... treating as -Inf");P[nn]=log(0);}
91+
if (isinf(P[nn])==-1){printf("WARNING! P(0)=-inf - AFDEMCMCZS may get stuck - if so, please check your initial conditions\n");}
92+
}
93+
memcpy(BESTP,P,NC*sizeof(double));
94+
95+
double P_new,lr,gratio;
96+
int withinrange,wrlocal=0;
97+
98+
COUNTERS N;
99+
N.ACC=0;
100+
N.ITER=MCO.nSTART;
101+
N.ACCLOC=0;
102+
N.ACCRATE=0;
103+
104+
printf("AFDEMCMCZS: affine phase ends at iteration %d out of %d\n",switch_iter,MCO.nOUT);
105+
printf("AFDEMCMCZS: switching from %d affine chains to %d DEMCMCZS chains\n",initialNC,demNC);
106+
107+
int activeNC=initialNC;
108+
int switched_to_demcmczs=0;
109+
110+
/*STEP 2 - BEGIN MCMC*/
111+
for ( ;N.ITER<MCO.nOUT;N.ITER++){
112+
113+
if (!switched_to_demcmczs && N.ITER>=switch_iter){
114+
double *PARS_SMALL=calloc(PI.npars*demNC,sizeof(double));
115+
double *BESTPARS_SMALL=calloc(PI.npars*demNC,sizeof(double));
116+
double *P_SMALL=calloc(demNC,sizeof(double));
117+
double *BESTP_SMALL=calloc(demNC,sizeof(double));
118+
int *selected=calloc(demNC,sizeof(int));
119+
double *rankP=calloc(initialNC,sizeof(double));
120+
for (nn=0;nn<initialNC;nn++){rankP[nn]=P[nn];}
121+
122+
int k,ii,best_idx;
123+
for (k=0;k<demNC;k++){
124+
best_idx=0;
125+
double best_val=rankP[0];
126+
for (ii=1;ii<initialNC;ii++){
127+
if (rankP[ii]>best_val){best_val=rankP[ii];best_idx=ii;}}
128+
selected[k]=best_idx;
129+
P_SMALL[k]=P[best_idx];
130+
BESTP_SMALL[k]=BESTP[best_idx];
131+
for (n=0;n<PI.npars;n++){
132+
PARS_SMALL[k*PI.npars+n]=PARS[best_idx*PI.npars+n];
133+
BESTPARS_SMALL[k*PI.npars+n]=BESTPARS[best_idx*PI.npars+n];}
134+
rankP[best_idx]=log(0);
135+
}
136+
137+
for (k=0;k<demNC;k++){
138+
P[k]=P_SMALL[k];
139+
BESTP[k]=BESTP_SMALL[k];
140+
for (n=0;n<PI.npars;n++){
141+
PARS[k*PI.npars+n]=PARS_SMALL[k*PI.npars+n];
142+
BESTPARS[k*PI.npars+n]=BESTPARS_SMALL[k*PI.npars+n];}
143+
}
144+
145+
printf("AFDEMCMCZS: selected DEMCMCZS live-chain source indices:");
146+
for (k=0;k<demNC;k++){printf(" %d(%2.1f)",selected[k],P_SMALL[k]);}
147+
printf("\n");
148+
149+
free(PARS_SMALL);
150+
free(BESTPARS_SMALL);
151+
free(P_SMALL);
152+
free(BESTP_SMALL);
153+
free(selected);
154+
free(rankP);
155+
156+
activeNC=demNC;
157+
switched_to_demcmczs=1;
158+
}
159+
160+
for (nn=0;nn<activeNC;nn++){
161+
162+
gratio=0;
163+
if (N.ITER<switch_iter){
164+
withinrange=STEP_AFDEMCMC(PARS,pars_new,PI,nn,activeNC,&gratio);
165+
}else{
166+
if ((double)random()/(double)RAND_MAX<psnooker){
167+
withinrange=STEP_DEMCMCZ_SNOOKER(&PARS[nn*PI.npars],Z,M,pars_new,PI,&gratio);
168+
}else{
169+
withinrange=STEP_DEMCMCZ_PARALLEL(&PARS[nn*PI.npars],Z,M,pars_new,PI);
170+
}
171+
}
172+
173+
lr=log((double)random()/(double)RAND_MAX);
174+
175+
if (withinrange==1){
176+
wrlocal=wrlocal+1;
177+
P_new=MODEL_LIKELIHOOD(DATA,pars_new);
178+
}else{
179+
P_new=log(0);
180+
}
181+
if (isnan(P_new)){P_new=log(0);}
182+
183+
if (P_new-P[nn]+gratio>lr || (isinf(P[nn]) && withinrange==1)){
184+
N.ACC=N.ACC+1;
185+
for (n=0;n<PI.npars;n++){PARS[n+nn*PI.npars]=pars_new[n];}
186+
if (P_new>=BESTP[nn]){
187+
for (n=0;n<PI.npars;n++){BESTPARS[n+nn*PI.npars]=pars_new[n];}
188+
BESTP[nn]=P_new;}
189+
P[nn]=P_new;
190+
}
191+
}
192+
193+
/*Regularly write results.*/
194+
if (MCO.nWRITE>0 && (N.ITER % MCO.nWRITE)==0){
195+
MCMC_OPTIONS MCO_WRITE=MCO;
196+
MCO_WRITE.nchains=activeNC;
197+
WRITE_DEMCMC_RESULTS(PARS,PI,MCO_WRITE,N.ITER);}
198+
199+
/*Regularly write restart file.*/
200+
if (MCO.nWRITE>0 && (N.ITER % 1000)==0){
201+
MCMC_OPTIONS MCO_WRITE=MCO;
202+
MCO_WRITE.nchains=activeNC;
203+
WRITE_DEMCMC_RESTART(PARS,PI,MCO_WRITE,N.ITER);}
204+
205+
/*Append current chain states to the DEMCMCZS archive during both phases.*/
206+
if ((N.ITER+1) % K==0 && M+activeNC<=maxM){
207+
for (nn=0;nn<activeNC;nn++){
208+
for (n=0;n<PI.npars;n++){Z[(M+nn)*PI.npars+n]=PARS[nn*PI.npars+n];}}
209+
M=M+activeNC;
210+
}
211+
212+
/*Printing Info*/
213+
if (MCO.nPRINT>0 && N.ITER % MCO.nPRINT==0){
214+
printf("%d out of %d iterations (archive size = %d out of %d)\n",N.ITER,MCO.nOUT,M,maxM);
215+
printf("AFDEMCMCZS phase = %s\n",(N.ITER<switch_iter) ? "affine" : "DEMCMCZS");
216+
printf("active chains = %d\n",activeNC);
217+
printf("within range = %2.2f%%\n",wrlocal/((double)(N.ITER+1)*activeNC)*100);
218+
printf("Local Acceptance rate %5.1f%%\n",100*(double)N.ACC/((double)(N.ITER+1)*activeNC));
219+
printf("Log Likelihoods: ");
220+
for (nn=0;nn<activeNC;nn++){printf("%2.1f ",P[nn]);}
221+
printf("\n");
222+
}
223+
224+
}
225+
226+
/*filling in MCOUT details*/
227+
for (n=0;n<PI.npars*activeNC;n++){MCOUT->best_pars[n]=BESTPARS[n];}
228+
MCOUT->complete=1;
229+
230+
free(BESTPARS);
231+
free(BESTP);
232+
free(Z);
233+
free(PARS);
234+
free(pars_new);
235+
free(P);
236+
printf("AFDEMCMCZS DONE\n");
237+
238+
return 0;
239+
}

0 commit comments

Comments
 (0)