Skip to content

Commit

Permalink
Add comparison of HMC samples to node positions
Browse files Browse the repository at this point in the history
  • Loading branch information
athowes committed May 5, 2023
1 parent 183ff58 commit c3679eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/check_hyper-marginals/orderly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ artefacts:
filenames:
- hist.pdf
- pairs.pdf
- nodes-samples-comparison.pdf

packages:
- dplyr
Expand All @@ -26,3 +27,7 @@ depends:
id: latest(parameter:tmbstan == TRUE && parameter:niter > 50000)
use:
depends/tmbstan.rds: out.rds
- naomi-simple_fit:
id: latest(parameter:aghq == TRUE && parameter:k == 3 && parameter:s == 8)
use:
depends/aghq.rds: out.rds
34 changes: 34 additions & 0 deletions src/check_hyper-marginals/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# setwd("src/check_hyper-marginals")

tmbstan <- readRDS("depends/tmbstan.rds")
aghq <- readRDS("depends/aghq.rds")

hyper <- names(tmbstan$mcmc$obj$par)
stopifnot(length(hyper) == 24)
Expand All @@ -22,3 +23,36 @@ bayesplot::mcmc_pairs(tmbstan$mcmc$stanfit, pars = hyper[13:18], off_diag_fun =
bayesplot::mcmc_pairs(tmbstan$mcmc$stanfit, pars = hyper[19:24], off_diag_fun = "hex")

dev.off()

#' So it looks like things are not totally Gaussian -- that's good
#' Now look at the positions of the aghq nodes on here

nodes_samples_comparison <- function(par) {
tmbstan_samples <- as.numeric(unlist(tmbstan$mcmc$sample[par]))
aghq_nodes <- aghq$quad$modesandhessians[[par]]

l <- floor(min(tmbstan_samples))
u <- ceiling(max(tmbstan_samples))

plot_nodes <- data.frame(index = 1:length(aghq_nodes), node = aghq_nodes) %>%
ggplot(aes(y = index, x = node)) +
geom_point(alpha = 0.2) +
xlim(l, u) +
labs(y = "Index of node", x = paste0(par), subtitle = "AGHQ nodes") +
theme_minimal()

plot_samples <- data.frame(x = tmbstan_samples) %>%
ggplot(aes(x = x)) +
geom_histogram(fill = "lightgrey", col = "darkgrey") +
xlim(l, u) +
labs(x = paste0(par), y = "", subtitle = "HMC samples") +
theme_minimal()

plot_nodes / plot_samples
}

pdf("nodes-samples-comparison.pdf", h = 7, w = 6.25)

lapply(1:length(hyper), function(i) nodes_samples_comparison(hyper[i]))

dev.off()

0 comments on commit c3679eb

Please sign in to comment.