Coder Social home page Coder Social logo

mhadaptive's People

Contributors

cjbayesian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mhadaptive's Issues

Automated adaptive cessation

Elevating issues out of TODO file. I am not sure how easy this is, but further development on adaptive functionality would be nice.

Output from Metro_hastings() as `coda` object

May I please elevate the excellent todo items you have listed in the file. This would be really important and helpful for comparing performance across packages and alleviate the need for custom plot functions.

Sampling from a mutlivariate normal; toy example

Hi,

For pedagogical reasons, I wanted to use different MCMC samplers to sample from a multivariate normal [the goal was to show that MCMC is great and all, but nothing beats sampling directly]. Here is the code for the analysis I wanted to present:

set.seed(666^3)
M <- 1E4
K <- 31
Mu <- rnorm(K, mean = 0, sd = 10)
P <- qr.Q(qr(matrix(rnorm(K^2), K)))
SS <- crossprod(P, P*(K:1))

target <- function(x){
  mvtnorm::dmvnorm(x, mean = Mu, sigma = SS, log = TRUE)
}
########
indep.samples <- MASS::mvrnorm(n = M, mu = Mu, Sigma = SS)


MH1 <- mcmc::metrop(target, initial = rnorm(K), nbatch = M)
MH1.samples <- MH1$batch

MH2 <- MHadaptive::Metro_Hastings(li_func = target, 
                                  pars = rnorm(K),
                                  burn_in = 1000,
                                  iterations = M + round(.1 * M))
MH2
MH2.samples <- MH2$trace

library(rstan)
mvmodel <- '
data{
  int <lower=0> k;
  matrix[k , k] Sigma;
  vector[k] mu;
}
parameters{
  vector[k] x;
}
model{
 x ~ multi_normal(mu, Sigma);
}
'
stan.data <- list(
  k = K,
  mu = Mu,
  Sigma = SS
)
StanRun <- stan(model_code = mvmodel, data = stan.data, iter = M, chains = 2 )
Stan.samples <- extract(StanRun, 'x')$x

Means <- list(
  indep = data.frame(est = colMeans(indep.samples), truth = Mu, sampler = "indep"),
  MH = data.frame(est = colMeans(MH1.samples), truth = Mu, sampler = "MH"),
  adapMH = data.frame(est = colMeans(MH2.samples), truth = Mu, sampler = "adaptMH"),
  Stan = data.frame(est = colMeans(Stan.samples), truth = Mu, sampler = "Stan")
)

####
## mean squared error and mean relative error
lapply(Means, function(dt) mean((dt$truth-dt$est)^2))
lapply(Means, function(dt) mean((dt$truth-dt$est)/dt$truth))

###
mean.results <- do.call(rbind, Means)
mean.results$sampler <- factor(mean.results$sampler, 
                               levels = sort(levels(as.factor(mean.results$sampler)), FALSE)  )

library(ggplot2)
p0 <- ggplot(mean.results,
             aes(x  =  sampler, y = (truth-est)/truth,
                 fill = sampler, colour = sampler) ) + 
  stat_summary(fun.data = mean_se, size = 1.5) +
  scale_x_discrete("Sampler") +
  scale_y_continuous(expression((mu-hat(mu))/mu),
                     expand = c(0, 0)) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme_bw() + 
  NULL
p0

p1 <- ggplot(mean.results,
             aes(x  =  sampler, y = (truth-est)/truth,
                 fill = sampler) ) + 
  geom_boxplot(position = "dodge") +
  scale_x_discrete("Sampler") +
  scale_y_continuous(expression((mu-hat(mu))/mu),
                expand = c(0, 0)) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme_bw() + 
  NULL
p1

p2 <- ggplot(mean.results,
             aes(x  =  truth, y = est,
                 fill = sampler, colour = sampler) ) + 
  geom_point() +
  scale_x_continuous(expression(mu)) +
  scale_y_continuous(expression(hat(mu))) +
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  theme_bw() + 
  NULL
p2

It's a bit long, I know, and includes stuff unrelated to MHadaptive, but I wanted to show the whole thing for transparency. It seems to me that Metro_Hastings() does exceptionally badly in this example, and I can't think of a reason why this would be. Am I employing the function incorrectly? Am I perhaps computing the wrong quantities?

Thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.