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

Class function g to m #266

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated help files for functions with names starting from g to m.
  • Loading branch information
Tahmina Mojumder committed Aug 31, 2023
commit 0df6ff5f0438061bd2e2538532ddde415d32a778
38 changes: 19 additions & 19 deletions BayesianTools/R/MAP.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#' calculates the Maxiumum APosteriori value (MAP)
#' @author Florian Hartig
#' @param bayesianOutput an object of class BayesianOutput (mcmcSampler, smcSampler, or mcmcList)
#' @param ... optional values to be passed on the the getSample function
#' @details Currently, this function simply returns the parameter combination with the highest posterior in the chain. A more refined option would be to take the MCMC sample and do additional calculations, e.g. use an optimizer, a kerne delnsity estimator, or some other tool to search / interpolate around the best value in the chain
#' @seealso \code{\link{WAIC}}, \code{\link{DIC}}, \code{\link{marginalLikelihood}}
#' @export
MAP <- function(bayesianOutput, ...){

samples = getSample(bayesianOutput, parametersOnly = F, ...)

if("mcmcSamplerList" %in% class(bayesianOutput)) nPars <- bayesianOutput[[1]]$setup$numPars
else nPars = bayesianOutput$setup$numPars

best = which.max(samples[,nPars + 1])

return(list(parametersMAP = samples[best, 1:nPars], valuesMAP = samples[best, (nPars + 1):(nPars + 3)] ))

}
#' calculates the Maxiumum APosteriori value (MAP)
#' @author Florian Hartig
#' @param bayesianOutput an object of class BayesianOutput (mcmcSampler, smcSampler, or mcmcList)
#' @param ... optional values to be passed on the the getSample function
#' @details Currently, this function simply returns the parameter combination with the highest posterior in the chain. A more refined option would be to take the MCMC sample and do additional calculations, e.g. use an optimizer, a kernel density estimator, or some other tool to search / interpolate around the best value in the chain.
#' @seealso \code{\link{WAIC}}, \code{\link{DIC}}, \code{\link{marginalLikelihood}}
#' @export
MAP <- function(bayesianOutput, ...){
samples = getSample(bayesianOutput, parametersOnly = F, ...)
if("mcmcSamplerList" %in% class(bayesianOutput)) nPars <- bayesianOutput[[1]]$setup$numPars
else nPars = bayesianOutput$setup$numPars
best = which.max(samples[,nPars + 1])
return(list(parametersMAP = samples[best, 1:nPars], valuesMAP = samples[best, (nPars + 1):(nPars + 3)] ))
}

70 changes: 35 additions & 35 deletions BayesianTools/R/getVolume.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#' Calculate posterior volume
#' @author Florian Hartig
#' @param sampler an object of superclass bayesianOutput or any other class that has the getSample function implemented (e.g. Matrix)
#' @param prior schould also prior volume be calculated
#' @param method method for volume estimation. Currently, the only option is "MVN"
#' @param ... additional parameters to pass on to the \code{\link{getSample}}
#' @details The idea of this function is to provide an estimate of the "posterior volume", i.e. how "broad" the posterior is. One potential application is to the overall reduction of parametric uncertainty between different data types, or between prior and posterior.
#'
#' Implemented methods for volume estimation:
#'
#' Option "MVN" - in this option, the volume is calculated as the determinant of the covariance matrix of the prior / posterior sample.
#'
#' @example /inst/examples/getVolume.R
#' @export
getVolume <- function(sampler, prior = F, method = "MVN", ...){

x = getSample(sampler, ...)

if(method == "MVN"){
nPars = ncol(x)
postVol = det(cov(x))
}else stop("BayesianTools: unknown method argument in getVolume")

if(prior == T){
x = sampler$setup$prior$sampler(5000)

if(method == "MVN"){
nPars = ncol(x)
priorVol = det(cov(x))
}else stop("BayesianTools: unknown method argument in getVolume")
return(list(priorVol = priorVol, postVol = postVol))
}else return(postVol)
}


#' Calculate posterior volume
#' @author Florian Hartig
#' @param sampler an object of superclass bayesianOutput or any other class that has implemented the getSample function (e.g. Matrix)
#' @param prior logical, should prior volume be calculated?
#' @param method method for volume estimation. Currently, the only option is "MVN"
#' @param ... additional parameters to pass on to the \code{\link{getSample}}
#' @details The idea of this function is to provide an estimate of the "posterior volume", i.e. how "broad" the posterior is. One potential application is the overall reduction of parametric uncertainty between different data types, or between prior and posterior.
#'
#' Implemented methods for volume estimation:
#'
#' Option "MVN" - in this option, the volume is calculated as the determinant of the covariance matrix of the prior / posterior sample.
#'
#' @example /inst/examples/getVolume.R
#' @export
getVolume <- function(sampler, prior = F, method = "MVN", ...){
x = getSample(sampler, ...)
if(method == "MVN"){
nPars = ncol(x)
postVol = det(cov(x))
}else stop("BayesianTools: unknown method argument in getVolume")
if(prior == T){
x = sampler$setup$prior$sampler(5000)
if(method == "MVN"){
nPars = ncol(x)
priorVol = det(cov(x))
}else stop("BayesianTools: unknown method argument in getVolume")
return(list(priorVol = priorVol, postVol = postVol))
}else return(postVol)
}
Loading