@@ -51,7 +51,7 @@ SEXP MCMCDyn_wrapper(SEXP stateR, // ergm_state
51
51
SEXP sample = PROTECT (allocVector (REALSXP , (asInteger (nsteps ) + 1 )* m -> n_stats ));
52
52
memset (REAL (sample ), 0 , (asInteger (nsteps ) + 1 )* m -> n_stats * sizeof (double ));
53
53
memcpy (REAL (sample ), s -> stats , m -> n_stats * sizeof (double ));
54
-
54
+
55
55
kvint difftime , difftail , diffhead , diffto ;
56
56
kv_init (difftime );
57
57
kv_init (difftail );
@@ -64,7 +64,6 @@ SEXP MCMCDyn_wrapper(SEXP stateR, // ergm_state
64
64
kv_push (int , diffhead , 0 );
65
65
kv_push (int , diffto , 0 );
66
66
67
-
68
67
SEXP status ;
69
68
if (MHp ) status = PROTECT (ScalarInteger (MCMCSampleDyn (s ,
70
69
dur_inf ,
@@ -73,18 +72,18 @@ SEXP MCMCDyn_wrapper(SEXP stateR, // ergm_state
73
72
asInteger (nsteps ), asInteger (min_MH_interval ), asInteger (max_MH_interval ), asReal (MH_pval ), asReal (MH_interval_add ), asInteger (burnin ), asInteger (interval ),
74
73
asInteger (verbose ))));
75
74
else status = PROTECT (ScalarInteger (MCMCDyn_MH_FAILED ));
76
-
75
+
77
76
const char * outn [] = {"status" , "s" , "state" , "diffnwtime" , "diffnwtails" , "diffnwheads" , "diffnwdirs" , "" };
78
77
SEXP outl = PROTECT (mkNamed (VECSXP , outn ));
79
78
SET_VECTOR_ELT (outl , 0 , status );
80
79
SET_VECTOR_ELT (outl , 1 , sample );
81
-
80
+
82
81
/* record new generated network to pass back to R */
83
82
if (asInteger (status ) == MCMCDyn_OK ){
84
83
s -> stats = REAL (sample ) + asInteger (nsteps )* m -> n_stats ;
85
84
SET_VECTOR_ELT (outl , 2 , ErgmStateRSave (s ));
86
85
}
87
-
86
+
88
87
SET_VECTOR_ELT (outl , 3 , PROTECT (kvint_to_SEXP (difftime )));
89
88
SET_VECTOR_ELT (outl , 4 , PROTECT (kvint_to_SEXP (difftail )));
90
89
SET_VECTOR_ELT (outl , 5 , PROTECT (kvint_to_SEXP (diffhead )));
@@ -95,7 +94,7 @@ SEXP MCMCDyn_wrapper(SEXP stateR, // ergm_state
95
94
kv_destroy (diffhead );
96
95
kv_destroy (diffto );
97
96
98
- ErgmStateDestroy (s );
97
+ ErgmStateDestroy (s );
99
98
PutRNGstate (); /* Disable RNG before returning */
100
99
UNPROTECT (7 );
101
100
return outl ;
@@ -107,9 +106,9 @@ SEXP MCMCDyn_wrapper(SEXP stateR, // ergm_state
107
106
Using the parameters contained in the array eta, obtain the
108
107
network statistics for a sample of size nsteps. burnin is the
109
108
initial number of Markov chain steps before sampling anything
110
- and interval is the number of MC steps between successive
109
+ and interval is the number of MC steps between successive
111
110
networks in the sample. Put all the sampled statistics into
112
- the statistics array.
111
+ the statistics array.
113
112
*********************/
114
113
MCMCDynStatus MCMCSampleDyn (ErgmState * s ,
115
114
StoreTimeAndLasttoggle * dur_inf ,
@@ -122,7 +121,7 @@ MCMCDynStatus MCMCSampleDyn(ErgmState *s,
122
121
kvint * difftime , kvint * difftail , kvint * diffhead , kvint * diffto ,
123
122
// MCMC settings.
124
123
unsigned int nsteps , unsigned int min_MH_interval , unsigned int max_MH_interval , double MH_pval , double MH_interval_add ,
125
- unsigned int burnin , unsigned int interval ,
124
+ unsigned int burnin , unsigned int interval ,
126
125
// Verbosity.
127
126
int verbose ){
128
127
Network * nwp = s -> nwp ;
@@ -135,8 +134,8 @@ MCMCDynStatus MCMCSampleDyn(ErgmState *s,
135
134
/*if (verbose)
136
135
Rprintf("Total m->n_stats is %i; total nsteps is %d\n",
137
136
m->n_stats,nsteps);*/
138
-
139
-
137
+
138
+
140
139
/* Burn in step. */
141
140
142
141
for (i = 0 ;i < burnin ;i ++ ){
@@ -150,18 +149,18 @@ MCMCDynStatus MCMCSampleDyn(ErgmState *s,
150
149
// Check that we didn't run out of log space.
151
150
if (status == MCMCDyn_TOO_MANY_CHANGES )
152
151
return MCMCDyn_TOO_MANY_CHANGES ;
153
-
152
+
154
153
// If we need to return a network, then stop right there, since the network is too big to return, so stop early.
155
154
if (maxedges != 0 && EDGECOUNT (nwp ) >= maxedges - 1 )
156
155
return MCMCDyn_TOO_MANY_EDGES ;
157
156
}
158
-
157
+
159
158
//Rprintf("MCMCSampleDyn post burnin numdissolve %d\n", *numdissolve);
160
-
159
+
161
160
if (verbose ){
162
161
Rprintf ("Returned from STERGM burnin\n" );
163
162
}
164
-
163
+
165
164
/* Now sample networks */
166
165
for (i = 0 ; i < nsteps ; i ++ ){
167
166
/* Set current vector of stats equal to previous vector */
@@ -181,16 +180,16 @@ MCMCDynStatus MCMCSampleDyn(ErgmState *s,
181
180
stats ,
182
181
maxchanges , log_changes ? & nextdiffedge : NULL , difftime , difftail , diffhead , diffto ,
183
182
min_MH_interval , max_MH_interval , MH_pval , MH_interval_add , verbose );
184
-
183
+
185
184
// Check that we didn't run out of log space.
186
185
if (status == MCMCDyn_TOO_MANY_CHANGES )
187
186
return MCMCDyn_TOO_MANY_CHANGES ;
188
-
187
+
189
188
// If we need to return a network, then stop right there, since the network is too big to return, so stop early.
190
189
if (maxedges != 0 && EDGECOUNT (nwp ) >= maxedges - 1 )
191
190
return MCMCDyn_TOO_MANY_EDGES ;
192
191
}
193
-
192
+
194
193
//Rprintf("MCMCSampleDyn loop numdissolve %d\n", *numdissolve);
195
194
if (verbose ){
196
195
if ( ((3 * i ) % nsteps )< 3 && nsteps > 500 ){
@@ -241,7 +240,7 @@ MCMCDynStatus MCMCDyn1Step(ErgmState *s,
241
240
// MCMC settings.
242
241
unsigned int min_MH_interval , unsigned int max_MH_interval , double MH_pval , double MH_interval_add ,
243
242
// Verbosity.
244
- int verbose ){
243
+ int verbose ){
245
244
Network * nwp = s -> nwp ;
246
245
Model * m = s -> m ;
247
246
MHProposal * MHp = s -> MHp ;
@@ -254,20 +253,20 @@ MCMCDynStatus MCMCDyn1Step(ErgmState *s,
254
253
if (stats ) addonto (stats , m -> workspace , m -> n_stats );
255
254
256
255
/* Run the process. */
257
-
256
+
258
257
double cutoff ;
259
- double
258
+ double
260
259
si = 0 , // sum of increments
261
260
si2 = 0 , // sum of squared increments
262
- sw = 0 , // sum of weights
261
+ sw = 0 , // sum of weights
263
262
sw2 = 0 // sum of squared weights
264
263
;
265
264
double sdecay = 1 - 1.0 /min_MH_interval ;
266
-
265
+
267
266
unsigned int step = 0 ; // So that we could print out the number of steps later.
268
267
for (unsigned int finished = 0 , extrasteps = 0 ; step < max_MH_interval && finished < extrasteps + 1 ; step ++ ) {
269
268
unsigned int prev_discord = kh_size (discord );
270
-
269
+
271
270
MHp -> logratio = 0 ;
272
271
(* (MHp -> p_func ))(MHp , nwp ); /* Call MHp function to propose toggles */
273
272
// Rprintf("Back from proposal; step=%d\n",step);
@@ -277,32 +276,32 @@ MCMCDynStatus MCMCDyn1Step(ErgmState *s,
277
276
switch (MHp -> togglehead [0 ]){
278
277
case MH_UNRECOVERABLE :
279
278
error ("Something very bad happened during proposal. Memory has not been deallocated, so restart R soon." );
280
-
279
+
281
280
case MH_IMPOSSIBLE :
282
281
Rprintf ("MH MHProposal function encountered a configuration from which no toggle(s) can be proposed.\n" );
283
282
return MCMCDyn_MH_FAILED ;
284
-
283
+
285
284
case MH_UNSUCCESSFUL :
286
285
case MH_CONSTRAINT :
287
286
continue ;
288
287
}
289
288
}
290
289
291
290
ChangeStats (MHp -> ntoggles , MHp -> toggletail , MHp -> togglehead , nwp , m );
292
-
293
- // Rprintf("change stats:");
291
+
292
+ // Rprintf("change stats:");
294
293
/* Calculate inner product */
295
294
double ip = dotprod (eta , m -> workspace , m -> n_stats );
296
- // Rprintf("%f ", m->workspace[i]);
295
+ // Rprintf("%f ", m->workspace[i]);
297
296
//}
298
- // Rprintf("\n ip %f dedges %f\n", ip, m->workspace[0]);
297
+ // Rprintf("\n ip %f dedges %f\n", ip, m->workspace[0]);
299
298
/* The logic is to set exp(cutoff) = exp(ip) * qratio ,
300
299
then let the MHp probability equal min{exp(cutoff), 1.0}.
301
300
But we'll do it in log space instead. */
302
301
cutoff = ip + MHp -> logratio ;
303
-
302
+
304
303
/* if we accept the proposed network */
305
- if (cutoff >= 0.0 || log (unif_rand ()) < cutoff ) {
304
+ if (cutoff >= 0.0 || log (unif_rand ()) < cutoff ) {
306
305
/* Hold off updating timesteamps until the changes are committed,
307
306
which doesn't happen until later. */
308
307
for (unsigned int i = 0 ; i < MHp -> ntoggles ; i ++ ){
@@ -319,17 +318,17 @@ MCMCDynStatus MCMCDyn1Step(ErgmState *s,
319
318
sw ++ ; si += i ;
320
319
sw2 *= sdecay * sdecay ; si2 *= sdecay ;
321
320
sw2 ++ ; si2 += i * i ;
322
-
323
- if (step >= min_MH_interval && !finished ) {
321
+
322
+ if (step >= min_MH_interval && !finished ) {
324
323
// Now, perform the test:
325
324
double mi = (double )si / sw , mi2 = (double )si2 / sw ;
326
-
325
+
327
326
double vi = mi2 - mi * mi ;
328
327
double zi = mi / sqrt (vi * sw2 /(sw * sw )); // denom = sqrt(sum(w^2 * v)/sum(w)^2)
329
328
double pi = pnorm (zi , 0 , 1 , FALSE, FALSE); // Pr(Z > zi)
330
329
331
330
if (verbose >=5 ) Rprintf ("%u: sw=%2.2f sw2=%2.2f d=%d i=%d si=%2.2f si2=%2.2f mi=%2.2f vi=%2.2f ni=%2.2f zi=%2.2f pi=%2.2f\n" , step , sw , sw2 , kh_size (discord ), i , si , si2 , mi , vi , (sw * sw )/sw2 , zi , pi );
332
-
331
+
333
332
if (pi > MH_pval ){
334
333
extrasteps = step * MH_interval_add + round (runif (0 ,1 ));
335
334
finished ++ ;
@@ -340,7 +339,7 @@ MCMCDynStatus MCMCDyn1Step(ErgmState *s,
340
339
}
341
340
342
341
/* Step finished: record changes. */
343
-
342
+
344
343
if (verbose >=4 ){
345
344
if (step >=max_MH_interval ) Rprintf ("Convergence not achieved after %u M-H steps.\n" ,step );
346
345
else Rprintf ("Convergence achieved after %u M-H steps.\n" ,step );
@@ -362,14 +361,14 @@ MCMCDynStatus MCMCDyn1Step_advance(ErgmState *s,
362
361
int verbose ){
363
362
StoreDyadMapInt * discord = dur_inf -> discord ;
364
363
int t = dur_inf -> time ;
365
-
364
+
366
365
Network * nwp = s -> nwp ;
367
366
Model * m = s -> m ;
368
367
MHProposal * MHp = s -> MHp ;
369
368
370
369
if (nextdiffedge ) {
371
370
TailHead dyad ;
372
- kh_foreach_key (discord , dyad ,{
371
+ kh_foreach_key (discord , dyad ,{
373
372
if (* nextdiffedge < maxchanges ){
374
373
// and record the toggle.
375
374
if (difftime ) kv_push (int , * difftime , t );
0 commit comments