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

Function collection p to w #265

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Updated helper function for the functions whose name starts with mcmc.
  • Loading branch information
Tahmina Mojumder committed Aug 31, 2023
commit 9b0190fa3993e594e6deda226ae9351baa2abd62
472 changes: 235 additions & 237 deletions BayesianTools/R/mcmcDE.R

Large diffs are not rendered by default.

795 changes: 398 additions & 397 deletions BayesianTools/R/mcmcDEzs.R

Large diffs are not rendered by default.

706 changes: 353 additions & 353 deletions BayesianTools/R/mcmcDREAM.R

Large diffs are not rendered by default.

148 changes: 73 additions & 75 deletions BayesianTools/R/mcmcDREAM_helperFunctions.R
Original file line number Diff line number Diff line change
@@ -1,75 +1,73 @@


##' Generates matrix of CR values based on pCR
##' @param pCR Vector of crossover probabilities. Needs to be of length nCR.
##' @param settings settings list
##' @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 settings list
#' @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)

# 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
Loading