Skip to content

Commit ffd58f6

Browse files
committed
updates to logic for changing thin when reset=FALSE
1 parent 48bd8df commit ffd58f6

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

packages/nimble/R/MCMC_build.R

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ buildMCMC <- nimbleFunction(
246246
}
247247
iterationTotal <- 0
248248
iterUntilSaveVec <- c(0, 0)
249+
warnUnfinishedThinningInterval <- getNimbleOption('MCMCwarnUnfinishedThinningInterval')
249250
firstRun <- TRUE
250251
setupOutputs(derivedTypes)
251252
},
@@ -276,10 +277,6 @@ buildMCMC <- nimbleFunction(
276277
thinToUseVec <<- thinFromConfVec
277278
if(thin != -1) thinToUseVec[1] <<- thin
278279
if(thin2 != -1) thinToUseVec[2] <<- thin2
279-
for(iThin in 1:2) {
280-
if(thinToUseVec[iThin] < 1) stop('cannot use thin < 1')
281-
if(thinToUseVec[iThin] != floor(thinToUseVec[iThin])) stop('cannot use non-integer thin')
282-
}
283280
for(i in seq_along(derivedFunctions)) {
284281
if(derivedIntervals[i] == 0) {
285282
derivedIntervals[i] <<- thinToUseVec[1]
@@ -294,22 +291,34 @@ buildMCMC <- nimbleFunction(
294291
iterationTotal <<- 0
295292
iterUntilSaveVec <<- thinToUseVec
296293
} else {
297-
if(nburnin != 0) stop('cannot specify nburnin when using reset = FALSE.')
298-
## when reset = FALSE, only permit changing the value of
299-
## thin when iterUntilSaveVec[1] == thinToUseVec[1], and thin2 when iterUntilSaveVec[2] == thinToUseVec[2]
300-
## (which means saving on the previous thinning interval just took place)
301-
if((thin != -1) & (thin != thinToUseVec[1]) & (iterUntilSaveVec[1] != thinToUseVec[1]))
302-
##stop('can only change value of thin after a complete thinning interval, when using reset = FALSE.')
303-
cat('Warning: value of thin is being changed, in the midst of a thinning interval.\n')
304-
if((thin2 != -1) & (thin2 != thinToUseVec[2]) & (iterUntilSaveVec[2] != thinToUseVec[2]))
305-
##stop('can only change value of thin2 after a complete thinning interval, when using reset = FALSE.')
306-
cat('Warning: value of thin2 is being changed, in the midst of a thinning interval.\n')
294+
if(nburnin != 0) stop('cannot specify nburnin when using reset = FALSE.')
295+
## now allowing changing of thin and thin2, when reset = FALSE; Jan 2026.
296+
## when reset = FALSE and a new value of thin (or thin2) is provided,
297+
## issue a warning if the previous MCMC run didn't exactly finish a thinning/save cycle.
298+
## this is checked using iterUntilSaveVec[1] == thinToUseVec[1] or iterUntilSaveVec[2] == thinToUseVec[2],
299+
## which means saving on the previous thinning interval just took place.
300+
if((thin != -1) & (thin != thinToUseVec[1])) {
301+
if((iterUntilSaveVec[1] != thinToUseVec[1]) & warnUnfinishedThinningInterval)
302+
cat('Warning: value of thin is being changed, in the midst of a thinning interval.\n')
303+
thinToUseVec[1] <<- thin
304+
iterUntilSaveVec[1] <<- thin
305+
}
306+
if((thin2 != -1) & (thin2 != thinToUseVec[2])) {
307+
if((iterUntilSaveVec[2] != thinToUseVec[2]) & warnUnfinishedThinningInterval)
308+
cat('Warning: value of thin2 is being changed, in the midst of a thinning interval.\n')
309+
thinToUseVec[2] <<- thin2
310+
iterUntilSaveVec[2] <<- thin2
311+
}
307312
if(dim(samplerTimes)[1] != length(samplerFunctions) + 1) samplerTimes <<- numeric(length(samplerFunctions) + 1) ## first run: default inititialization to zero
308313
if(!resetMV) {
309314
mvSamples_copyRow <- getsize(mvSamples)
310315
mvSamples2_copyRow <- getsize(mvSamples2)
311316
}
312317
}
318+
for(iThin in 1:2) {
319+
if(thinToUseVec[iThin] < 1) stop('cannot use thin < 1')
320+
if(thinToUseVec[iThin] != floor(thinToUseVec[iThin])) stop('cannot use non-integer thin')
321+
}
313322
nimCopy(from = model, to = mvSaved, row = 1, logProb = TRUE)
314323
if(onlineWAIC & resetWAIC) waicFun[[1]]$reset()
315324
resize(mvSamples, mvSamples_copyRow + 1 + floor((niter-nburnin-iterUntilSaveVec[1]) / thinToUseVec[1]))

packages/nimble/R/options.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ nimOptimMethod("bobyqa",
213213
MCMCassignSamplersToPosteriorPredictiveNodes = TRUE, ## whether any samplers are assigned (by default) to PP nodes
214214
MCMCusePosteriorPredictiveSampler = TRUE, ## for PP nodes being sampled, use post_pred (or otherwise RW, etc)
215215
MCMCwarnUnsampledStochasticNodes = TRUE,
216+
MCMCwarnUnfinishedThinningInterval = TRUE,
216217
MCMCRJcheckHyperparam = TRUE,
217218
MCMCenableWAIC = FALSE,
218219
MCMCuseBarkerAsDefaultMV = FALSE,

0 commit comments

Comments
 (0)