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

Create likelihood help #262

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions BayesianTools/R/classLikelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param sampler sampler
#' @seealso \code{\link{likelihoodIidNormal}} \cr
#' \code{\link{likelihoodAR1}} \cr
#' @example /inst/examples/createLikelihoodHelp.R
#' @export
createLikelihood <- function(likelihood, names = NULL, parallel = F, catchDuplicates=T,
sampler = NULL, parallelOptions = NULL){
Expand Down
13 changes: 10 additions & 3 deletions BayesianTools/R/plotSensitivityOAT.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
#' @author Florian Hartig
#' @param bayesianSetup An object of class BayesianSetup
#' @param selection indices of selected parameters
#' @param equalScale if T, y axis of all plots will have the same scale
#' @param equalScale logical, if TRUE - y axis of all plots will have the same scale
#' @note This function can also be used for sensitivity analysis of an arbitrary output - just create a BayesianSetup with this output.
#' @details
#' When the scale of the parameter do not match then the plot looks like this :
#' ![](plotSensitivity-ScaleDontMatch.png "Sensitivity Plot")
#'
#' In that case, we should put 'equalScale = F'. Then the plot looks like this:
#' ![](plotSensitivity-ScaleFalse.png "Sensitivity Plot")
#'
#' @example /inst/examples/plotSensitivityHelp.R
#' @export
plotSensitivity <- function(bayesianSetup, selection = NULL, equalScale = T){
plotSensitivity <- function(bayesianSetup, selection = NULL, equalScale = TRUE){

if (is.null(selection)) selection = 1:bayesianSetup$numPars
n = length(selection)
Expand Down Expand Up @@ -39,7 +46,7 @@ plotSensitivity <- function(bayesianSetup, selection = NULL, equalScale = T){


for (i in 1:n){
if(equalScale == T) plot(resp~par, xlab = names[i], type = "l", col = "red", data = post[[i]], ylim = c(minR, maxR), ylab = "Response")
if(equalScale == TRUE) plot(resp~par, xlab = names[i], type = "l", col = "red", data = post[[i]], ylim = c(minR, maxR), ylab = "Response")
else plot(resp~par, xlab = names[i], type = "l", col = "red", data = post[[i]], ylab = "Response")

abline(v = refPar[i])
Expand Down
63 changes: 63 additions & 0 deletions BayesianTools/inst/examples/createLikelihoodHelp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@


data = rnorm(20)

# create standardized likelihood density for Gaussian likelihood function
# fitting parameters mean and standard deviation

likelihood <- function(x) {
predicted <- rep(x[1], length(data))
LL = likelihoodIidNormal(predicted, data, x[2])
return(LL)
}

likelihood(x = c(1,1))

# The next step is optional, you can provide the likelihood density directly
# to BayesianSetup and createLikelihood will be called automatically.
# The purpose of the createLikelihood function is to parallelize the
# density function and to add some convenience functions,
# for example to catch exceptions that are raised by the call to likelihood
likelihoodClass <- createLikelihood(likelihood)

bayesianSetup <- createBayesianSetup(likelihood = likelihoodClass,
lower = c(-10,0.001),
upper = c(10, 5))

settings = list(iterations = 1000)
out <- runMCMC(bayesianSetup, sampler = "DEzs", settings)
summary(out)



# simulate temporally autocorrelated data
AR1sim<-function(n, a){
x = rep(NA, n)
x[1] = 0
for(i in 2:n){
x[i] = a * x[i-1] + (1-a) * rnorm(1)
}
return(x)
}

set.seed(123)
data = AR1sim(1000, 0.5)
plot(data, type = "l")


# create likelihood function for AR1 likelihood function
likelihood <- function(x) {
predicted <- rep(x[1], length(data))
LL = likelihoodAR1(predicted, data, x[2], x[3])
return(LL)
}

bayesianSetup <- createBayesianSetup(likelihood = likelihood,
lower = c(-10,0.001, 0),
upper = c(10, 5, 0.99))

settings = list(iterations = 1000)
out <- runMCMC(bayesianSetup, sampler = "DEzs", settings)
summary(out, start = 200)
plot(out, start = 200)

23 changes: 20 additions & 3 deletions BayesianTools/inst/examples/plotSensitivityHelp.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
data = rnorm(20)

# create standardized likelihood density for Gaussian likelihood function
# fitting parameters mean and standard deviation
likelihood <- function(x) {
predicted <- rep(x[1], length(data))
LL = likelihoodIidNormal(predicted, data, x[2])
return(LL)
}

setup = createBayesianSetup(likelihood, lower = c(-10, 0.01), upper = c(10,5))

# showing posterior response surface for parameter 1
# note that this is plotted at
setup$prior$best
plotSensitivity(setup, selection = 2)

plotSensitivity(setup)
# when parameters are not on the same scale, we can use
plotSensitivity(setup, equalScale = FALSE)

ll <- testDensityBanana
bayesianSetup <- createBayesianSetup(likelihood = ll, lower = rep(-10, 2), upper = rep(10, 2))

plotSensitivity(bayesianSetup)
65 changes: 65 additions & 0 deletions BayesianTools/man/createLikelihood.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 29 additions & 5 deletions BayesianTools/man/plotSensitivity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Development/Figures/figures.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
##### plotSensitivity #####

data = rnorm(20)

# create standardized likelihood density for Gaussian likelihood function
# fitting parameters mean and standard deviation
likelihood <- function(x) {
predicted <- rep(x[1], length(data))
LL = likelihoodIidNormal(predicted, data, x[2])
return(LL)
}

setup = createBayesianSetup(likelihood, lower = c(-10, 0.01), upper = c(10,5))

png(filename = "BayesianTools/man/figures/plotSensitivity-ScaleDontMatch.png", width = 400, height = 300)
plotSensitivity(setup)
dev.off()

png(filename = "BayesianTools/man/figures/plotSensitivity-ScaleFalse.png", width = 400, height = 300)
plotSensitivity(setup, equalScale = F)
dev.off()
Loading