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

Proof-of-concept parallel tempering #106

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Proof-of-concept parallel tempering #106

wants to merge 13 commits into from

Conversation

richfitz
Copy link
Member

@richfitz richfitz commented Nov 8, 2024

Merge after #104, contains those commits

Closes #24

A basic parallel tempering sampler. There's much more to add (particularly embedding other samplers and sorting out sampling from the hot chain), but this is a start at least

This PR does not include tests of statistical correctness; I'm not really sure how we'll do these generally. Marc and I have picked over this pretty closely with some long runs and it's performing well at the moment.

There's quite a lot of refactoring work to do around the samplers once this is in; see comments for details

old comments from a fairly protracted period of validation follow:


Marc, a simple example to get you going. This actually looks pretty good now I think:

set.seed(1) # so we see the same thing
pkgload::load_all()
likelihood <- ex_mixture(5)
prior <- monty_dsl({
  x ~ Normal(0, 10)
})
posterior <- likelihood + prior
s <- monty_sampler_parallel_tempering(n_rungs = 10, vcv = matrix(0.1))
res <- monty_sample(posterior, s, 10000, n_chains = 4)

hist(c(res$pars), freq = FALSE)
x <- seq(min(res$pars), max(res$pars), length.out = 1001)
y <- exp(posterior$density(rbind(x)))
lines(x, y / sum(y) / diff(x)[[1]], col = "red")

(takes 17s on my machine for 40,000 steps, so 2k steps/s which is ok, especially as this is really 11 times that many samples).

I've run out a much longer chain and this is what I see:

set.seed(42) # so we see the same thing
pkgload::load_all()
likelihood <- ex_mixture(5)
prior <- monty_dsl({
  x ~ Normal(0, 10)
})
posterior <- likelihood + prior
s <- monty_sampler_parallel_tempering(n_rungs = 10, vcv = matrix(0.1))
r <- monty_runner_callr(dust2::dust_openmp_threads(10, "fix"))
res <- monty_sample(posterior, s, 50000, n_chains = 10, runner = r)

hist(c(res$pars), freq = FALSE)
x <- seq(min(res$pars), max(res$pars), length.out = 1001)
y <- exp(posterior$density(rbind(x)))
lines(x, y / sum(y) / diff(x)[[1]], col = "red")

(27s for 500,000 points, I can live with that)

image

I suspect we're out by a factor related to the beta swaps at the moment?

Copy link

codecov bot commented Nov 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.79%. Comparing base (7dc0478) to head (a5bc87c).

Additional details and impacted files
@@           Coverage Diff            @@
##             main     #106    +/-   ##
========================================
  Coverage   99.79%   99.79%            
========================================
  Files          66       67     +1     
  Lines        5240     5356   +116     
========================================
+ Hits         5229     5345   +116     
  Misses         11       11            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@richfitz richfitz marked this pull request as ready for review January 5, 2025 23:38
@richfitz richfitz requested a review from weshinsley January 5, 2025 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants