Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated help files name starting with mcmc #257

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
updateMCMC
  • Loading branch information
TahminaMojumder committed Sep 22, 2023
commit 405d7cbb7363c382424148619bb9ccd23c803fa1
705 changes: 352 additions & 353 deletions BayesianTools/R/mcmcDREAM.R

Large diffs are not rendered by default.

144 changes: 71 additions & 73 deletions BayesianTools/R/mcmcDREAM_helperFunctions.R
Original file line number Diff line number Diff line change
@@ -1,73 +1,71 @@
##' Generates matrix of CR values based on pCR
##' @param pCR vector of crossover probabilities. Needs to be of length nCR.
##' @param settings list of settings
##' @param Npop number of chains
##' @return Matrix with CR values
#' @keywords internal
generateCRvalues <- function(pCR,settings, Npop){

# Random vector, add zero to get first position
RandomVec <- c(0,cumsum(as.numeric(rmultinom(1, size = Npop*settings$updateInterval, prob = pCR))))

# get candidate points
cand <- sample(Npop*settings$updateInterval)
CR <- rep(NA, Npop*settings$updateInterval)

## Now loop over chains to generate CR values
for(i in 1:settings$nCR){
#Start and End
Start <- RandomVec[i]+1
End <- RandomVec[i+1]

# get candidates
candx <- cand[Start:End]

# Assign these indices settings$CR
CR[candx] <- i/settings$nCR
}
## Reshape CR
CR <- matrix(CR,Npop,settings$updateInterval)

return(CR)
}




#' Adapts pCR values
#' @param CR vector of crossover probabilities. Needs to be of length nCR.
#' @param settings list of settings
#' @param delta vector with differences
#' @param lCR values to weight delta
#' @param Npop number of chains.
#' @return Matrix with CR values
#' @keywords internal
AdaptpCR <- function(CR, delta ,lCR, settings, Npop){
if(any(delta >0)){ ## Adaptions can only be made if there are changes in X

# Change CR to vector
CR <- c(CR)

# Store old lCR values
lCROld <- lCR
## Determine lCR
lCR <- rep(NA,settings$nCR)

for (k in 1:settings$nCR){

## how many times a CR value is used. This is used to weight delta
CR_counter <- length(which(CR==k/settings$nCR))
lCR[k] <- lCROld[k]+ CR_counter
}

## Adapt pCR
pCR <- Npop * (delta / lCR) / sum(delta)

pCR[which(is.nan(pCR))] <- 1/settings$nCR # catch possible error if delta and lCR = 0

## Normalize values
pCR <- pCR/sum(pCR)

}
return(list(pCR=pCR,lCR=lCR))
} ##AdaptpCR
##' Generates matrix of CR values based on pCR
##' @param pCR vector of crossover probabilities. Needs to be of length nCR.
##' @param settings list of settings
##' @param Npop number of chains
##' @return Matrix with CR values
#' @keywords internal
generateCRvalues <- function(pCR,settings, Npop){

# Random vector, add zero to get first position
RandomVec <- c(0,cumsum(as.numeric(rmultinom(1, size = Npop*settings$updateInterval, prob = pCR))))

# get candidate points
cand <- sample(Npop*settings$updateInterval)
CR <- rep(NA, Npop*settings$updateInterval)

## Now loop over chains to generate CR values
for(i in 1:settings$nCR){
#Start and End
Start <- RandomVec[i]+1
End <- RandomVec[i+1]

# get candidates
candx <- cand[Start:End]

# Assign these indices settings$CR
CR[candx] <- i/settings$nCR
}
## Reshape CR
CR <- matrix(CR,Npop,settings$updateInterval)

return(CR)
}


#' Adapts pCR values
#' @param CR vector of crossover probabilities. Needs to be of length nCR.
#' @param settings list of settings
#' @param delta vector with differences
#' @param lCR values to weight delta
#' @param Npop number of chains.
#' @return Matrix with CR values
#' @keywords internal
AdaptpCR <- function(CR, delta ,lCR, settings, Npop){
if(any(delta >0)){ ## Adaptions can only be made if there are changes in X

# Change CR to vector
CR <- c(CR)