Skip to content

Metropolis extension #42

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

Merged
merged 6 commits into from
Jan 16, 2025
Merged

Conversation

nossleinad
Copy link
Member

@nossleinad nossleinad commented Jan 6, 2025

Generalize metropolis_sample by introducing metropolis_sample(update!::Function, -||-)

Now, we can customize how a "metropolis step" looks, typically with the do-syntax (thanks @AntonOresten for the tip). Omitting update! is equivalent with the previous behaviour. This enables us to, for instance, fix branch lengths and only sample over topologies with nni_update!. We can also add parameters to the chain as exemplified below.

Example usage

From Phylotrajectories.jl:

trees, LLs = metropolis_sample(
        newt,
        [bm_model],
        model.n_samples,
        burn_in = model.burn_in,
        sample_interval = model.sample_interval,
        collect_LLs = true,
    ) do tree, models
        sample_leafs!(temp_messages, tree, x -> models, model.frequency_sampler)
        for i = 1:model.consecutive_root_samples
            sample_root_distribution!(temp_messages[1], tree, model.root_distribution_sampler)
        end
        nni_update!(softmax_sampler, tree, x -> models)
        branchlength_update!(model.branchlength_sampler, tree, x -> models)
    end

this extension called for introducing the

metropolis_step interface

metropolis_step(LL::Function, modifier, curr_value)

Does a standard metropolis step in an MCMC, i.e. proposes a candidate symmetrically and returns the next
state in the chain, decided by the candidate being rejected or not.

The interface for metropolis_step is as follows
You need a MySampler <: Any to implement

  • proposal(modifier::MySampler, curr_value)
  • log_prior(modifier::MySampler, x)
  • apply_decision(modifier::MySampler, accept::Bool)

An example is given for BranchlengthSampler

Add branchlength_update! & nni_update!

Update can be either an optim or a sampling, determined by the first argument passed in. We still export branchlength_optim! and nni_optim! and they behave identically as before. This separation is made to avoid confusion when we want *_optim! to sample.

@nossleinad nossleinad merged commit 040d2a6 into MurrellGroup:main Jan 16, 2025
4 checks passed
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.

1 participant