Skip to content

Commit a3d2752

Browse files
Merge pull request #173 from florianhartig/0.1.5.1
0.1.5.1
2 parents d0bd51b + e682f9b commit a3d2752

10 files changed

Lines changed: 198 additions & 39 deletions

File tree

BayesianTools/DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: BayesianTools
22
Title: General-Purpose MCMC and SMC Samplers and Tools for Bayesian Statistics
3-
Version: 0.1.5
4-
Date: 2018-07-16
3+
Version: 0.1.5.1
4+
Date: 2019-01-20
55
Authors@R: c(person("Florian", "Hartig", email = "florian.hartig@biologie.uni-regensburg.de", role = c("aut", "cre")), person("Francesco", "Minunno", role = c("aut")), person("Stefan", " Paul", role = c("aut") ), person("David", "Cameron", role = "ctb"), person("Tankred", "Ott", role = "ctb"), person("Maximilian", "Pichler", role = "ctb"))
66
Author: Florian Hartig [aut, cre],
77
Francesco Minunno [aut],

BayesianTools/NEWS

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
NOTE: for more news about the package, see https://github.com/florianhartig/BayesianTools/releases
22

3+
# BayesianTools 0.1.6
4+
5+
New features
6+
7+
- calibrationTest + helper functions to test for https://github.com/florianhartig/BayesianTools/pull/172
8+
9+
Major changes
10+
11+
- None
12+
13+
Minor changes
14+
15+
- Help improvements and clarifications
16+
17+
Bugfixes
18+
19+
- Bugfix AR1 likelihood https://github.com/florianhartig/BayesianTools/issues/169
20+
- Bugfix restart MCMC https://github.com/florianhartig/BayesianTools/issues/158
21+
22+
323
# BayesianTools 0.1.5
424

525
Changes / improvements

BayesianTools/R/marginalLikelihood.R

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,35 @@
1414
#' @param numSamples number of samples to use. How this works, and if it requires recalculating the likelihood, depends on the method
1515
#' @param method method to choose. Currently available are "Chib" (default), the harmonic mean "HM", sampling from the prior "prior", and bridge sampling "Bridge". See details
1616
#' @param ... further arguments passed to \code{\link{getSample}}
17-
#' @details The function currently implements four ways to calculate the marginal likelihood. The recommended (and default) method is the method "Chib" (Chib and Jeliazkov, 2001), which is based on MCMC samples, but performs additional calculations. Despite being the current recommendation, note there are some numeric issues with this algorithm that may limit reliability for larger dimensions.
17+
#' @details The marginal likelihood is the average likelihood across the prior space. It is used, for example, for Bayesian model selection and model averaging.
18+
#'
19+
#' It is defined as \deqn{ML = \int L(\Theta) p(\Theta) d\Theta}
20+
#'
21+
#' Given that MLs are calculated for each model, you can get posterior weights (for model selection and/or model averaging) on the model by
22+
#'
23+
#' \deqn{P(M_i|D) = ML_i * p(M_i) / (\sum_i ML_i * p(M_i) )}
24+
#'
25+
#' In BT, we return the log ML, so you will have to exp all values for this formula.
26+
#'
27+
#' It is well-known that the ML is VERY dependent on the prior, and in particular the choice of the width of uninformative priors may have major impacts on the relative weights of the models. It has therefore been suggested to not use the ML for model averaging / selection on uninformative priors. If you have no informative priors, and option is to split the data into two parts, use one part to generate informative priors for the model, and the second part for the model selection. See Dormann et al., 2018, in particular the Appendix, for an example.
28+
#'
29+
#' The marginalLikelihood function currently implements four ways to calculate the marginal likelihood. Be aware that marginal likelihood calculations are notoriously prone to numerical stability issues. Especially in high-dimensional parameter spaces, there is no guarantee that any of the implemented algorithms will converge reasonably fast. The recommended (and default) method is the method "Chib" (Chib and Jeliazkov, 2001), which is based on MCMC samples, with a limited number of additional calculations. Despite being the current recommendation, note there are some numeric issues with this algorithm that may limit reliability for larger dimensions.
1830
#'
19-
#' The harmonic mean approximation, is implemented only for comparison. Note that the method is numerically unrealiable and usually should not be used. \cr
31+
#' The harmonic mean approximation, is implemented only for comparison. Note that the method is numerically unrealiable and usually should not be used.
2032
#'
21-
#' The third method is simply sampling from the prior. While in principle unbiased, it will only converge for a large number of samples, and is therefore numerically inefficient. \cr
33+
#' The third method is simply sampling from the prior. While in principle unbiased, it will only converge for a large number of samples, and is therefore numerically inefficient.
2234
#'
23-
#' The Bridge method uses bridge sampling as implemented in the R package "bridgesampling".
35+
#' The Bridge method uses bridge sampling as implemented in the R package "bridgesampling". It is potentially more exact than the Chib method, but might require more computation time. However, this may be very dependent on the sampler.
2436
#'
25-
#' @note Be aware that marginal likelihood calculations are notoriously prone to numerical stability issues. Especially in high-dimensional parameter spaces, there is no guarantee that any of the implemented algorithms will converge reasonably fast.
37+
#' @return A list with log of the marginal likelihood, as well as other diagnostics depending on the chose method
2638
#'
2739
#' @example /inst/examples/marginalLikelihoodHelp.R
28-
#' @references Chib, Siddhartha, and Ivan Jeliazkov. "Marginal likelihood from the Metropolis-Hastings output." Journal of the American Statistical Association 96.453 (2001): 270-281.
40+
#' @references
41+
#'
42+
#' Chib, Siddhartha, and Ivan Jeliazkov. "Marginal likelihood from the Metropolis-Hastings output." Journal of the American Statistical Association 96.453 (2001): 270-281.
43+
#'
44+
#' Dormann et al. 2018. Model averaging in ecology: a review of Bayesian, information-theoretic, and tactical approaches for predictive inference. Ecological Monographs
45+
#'
2946
#' @seealso \code{\link{WAIC}}, \code{\link{DIC}}, \code{\link{MAP}}
3047
marginalLikelihood <- function(sampler, numSamples = 1000, method = "Chib", ...){
3148

BayesianTools/R/mcmcDREAM.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
##' @param iterations Number of model evaluations
88
##' @param nCR parameter determining the number of cross-over proposals. If nCR = 1 all parameters are updated jointly.
99
##' @param updateInterval determining the intervall for the pCR update
10-
##' @param gamma Kurtosis parameter Bayesian Inference Scheme. Default is 0.
11-
##' @param eps Ergodicity term. Default to 0.
12-
##' @param e Ergodicity term. Default to 5e-2.
13-
##' @param pCRupdate Update of crossover probabilities, default is TRUE
10+
##' @param gamma Kurtosis parameter Bayesian Inference Scheme
11+
##' @param eps Ergodicity term
12+
##' @param e Ergodicity term
13+
##' @param pCRupdate Update of crossover probabilities
1414
##' @param burnin number of iterations treated as burn-in. These iterations are not recorded in the chain.
1515
##' @param thin thin thinning parameter. Determines the interval in which values are recorded.
1616
##' @param adaptation Number or percentage of samples that are used for the adaptation in DREAM (see Details).

BayesianTools/R/mcmcDREAMzs.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
##' @param iterations Number of model evaluations
88
##' @param nCR parameter determining the number of cross-over proposals. If nCR = 1 all parameters are updated jointly.
99
##' @param updateInterval determining the intervall for the pCR (crossover probabilities) update
10-
##' @param gamma Kurtosis parameter Bayesian Inference Scheme. Default is 0.
11-
##' @param eps Ergodicity term. Default to 0.
12-
##' @param e Ergodicity term. Default to 5e-2.
13-
##' @param pCRupdate Update of crossover probabilities, default is TRUE
10+
##' @param gamma Kurtosis parameter Bayesian Inference Scheme.
11+
##' @param eps Ergodicity term
12+
##' @param e Ergodicity term
13+
##' @param pCRupdate Update of crossover probabilities
1414
##' @param burnin number of iterations treated as burn-in. These iterations are not recorded in the chain.
1515
##' @param thin thin thinning parameter. Determines the interval in which values are recorded.
1616
##' @param adaptation Number or percentage of samples that are used for the adaptation in DREAM (see Details)

BayesianTools/inst/examples/marginalLikelihoodHelp.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
1+
2+
# Comparison of ML for two regression models
3+
4+
sampleSize = 30
5+
x <- (-(sampleSize-1)/2):((sampleSize-1)/2)
6+
y <- 1 * x + 1*x^2 + rnorm(n=sampleSize,mean=0,sd=10)
7+
#plot(x,y, main="Test Data")
8+
9+
10+
# linear and quadratic effect
11+
likelihood1 <- function(param){
12+
pred = param[1] + param[2]*x + param[3] * x^2
13+
singlelikelihoods = dnorm(y, mean = pred, sd = 1/(param[4]^2), log = TRUE)
14+
return(sum(singlelikelihoods))
15+
}
16+
17+
# linear effect
18+
likelihood2 <- function(param){
19+
pred = param[1] + param[2]*x
20+
singlelikelihoods = dnorm(y, mean = pred, sd = 1/(param[3]^2), log = TRUE)
21+
return(sum(singlelikelihoods))
22+
}
23+
24+
setUp1 <- createBayesianSetup(likelihood1, lower = c(-5,-5,-5,0.01), upper = c(5,5,5,30))
25+
26+
setUp2 <- createBayesianSetup(likelihood2, lower = c(-5,-5,0.01), upper = c(5,5,30))
27+
28+
out1 <- runMCMC(bayesianSetup = setUp1)
29+
M1 = marginalLikelihood(out1, start = 1000)
30+
31+
out2 <- runMCMC(bayesianSetup = setUp2)
32+
M2 = marginalLikelihood(out2, start = 1000)
33+
34+
35+
### Bayes factor
36+
37+
exp(M1$ln.ML - M2$ln.ML)
38+
39+
# BF > 1 means the evidence is in favor of M1. See Kass, R. E. & Raftery, A. E.
40+
# (1995) Bayes Factors. J. Am. Stat. Assoc., Amer Statist Assn, 90, 773-795.
41+
42+
### Posterior weight
43+
44+
exp(M1$ln.ML) / ( exp(M1$ln.ML) + exp(M2$ln.ML))
45+
46+
# If models have different model priors, multiply with the prior probabilities of each model.
47+
48+
############################################################
49+
50+
### Performance comparison ###
51+
52+
153
# Low dimensional case with narrow priors - all methods have low error
254

355
# we use a truncated normal for the likelihood to make sure that the density

BayesianTools/man/DREAM.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BayesianTools/man/DREAMzs.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BayesianTools/man/marginalLikelihood.Rd

Lines changed: 73 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BayesianTools/vignettes/BayesianTools.Rmd

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
---
2-
title: "Bayesian Tools"
2+
title: "Bayesian Tools - General-Purpose MCMC and SMC Samplers and Tools for Bayesian Statistics"
33
output:
44
rmarkdown::html_vignette:
55
toc: true
66
vignette: >
77
%\VignetteIndexEntry{Manual for the BayesianTools R package}
88
%\VignetteEngine{knitr::rmarkdown}
9-
%\VignetteEncoding{UTF-8}
10-
abstract: "The BayesianTools (BT) package supports model analysis (including sensitivity analysis and uncertainty analysis), Bayesian model calibration, as well as model selection and multi-model inference techniques for system models.
11-
\n \n \n"
12-
9+
\usepackage[utf8]{inputenc}
10+
abstract: "The BayesianTools (BT) package supports model analysis (including sensitivity analysis and uncertainty analysis), Bayesian model calibration, as well as model selection and multi-model inference techniques for system models."
1311
---
1412

1513
```{r global_options, include=FALSE}
@@ -364,7 +362,7 @@ out <- runMCMC(bayesianSetup = bayesianSetup, settings = settings)
364362

365363
# MCMC sampler reference
366364

367-
## runMCMC
365+
## The runMCMC() function
368366

369367
The runMCMC function is the central function for starting MCMC algorithms in the BayesianTools package. It requires a bayesianSetup, a choice of sampler (standard is DEzs), and optionally changes to the standard settings of the chosen sampler.
370368

@@ -723,12 +721,18 @@ M2
723721
Bayes factor (need to reverse the log)
724722

725723
```{r}
726-
exp(M1$marginalLikelihod - M2$marginalLikelihod)
724+
exp(M1$ln.ML - M2$ln.ML)
727725
```
728726

729727
BF > 1 means the evidence is in favor of M1. See Kass, R. E. & Raftery, A. E. (1995) Bayes Factors. J. Am. Stat. Assoc., Amer Statist Assn, 90, 773-795.
730728

731-
Note that we would have to multiply still with the model priors to arrive at Bayesian model weights.
729+
Assuming equal prior weights on all models, we can calculate the posterior weight of M1 as
730+
731+
```{r}
732+
exp(M1$ln.ML) / ( exp(M1$ln.ML) + exp(M2$ln.ML))
733+
```
734+
735+
If models have different model priors, multiply with the prior probabilities of each model.
732736

733737

734738
### Model comparison via DIC

0 commit comments

Comments
 (0)