-
Notifications
You must be signed in to change notification settings - Fork 4
Extend optimization methods #37
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
nossleinad
merged 13 commits into
MurrellGroup:main
from
Glowster:extend-optimization-methods
Sep 1, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d2e84c1
WIP
c28534c
WIP
c129675
WIP
64cc880
WIP
a31bf41
WIP
a94ef1c
WIP
4618009
WIP
394133b
Merged with Maxs PR
e544aac
WIP
fc02a8b
WIP
185fcae
WIP
fbf74a2
WIP
409c5ba
Updated the test and made some minor adjustments based on the feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include("sampling.jl") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
""" | ||
function metropolis_sample( | ||
initial_tree::FelNode, | ||
models::Vector{<:BranchModel}, | ||
num_of_samples; | ||
bl_modifier::UnivariateSampler = BranchlengthSampler(Normal(0,2), Normal(-1,1)) | ||
burn_in=1000, | ||
sample_interval=10, | ||
collect_LLs = false, | ||
midpoint_rooting=false, | ||
) | ||
|
||
Samples tree topologies from a posterior distribution. | ||
|
||
# Arguments | ||
- `initial_tree`: An initial tree topology with the leaves populated with data, for the likelihood calculation. | ||
- `models`: A list of branch models. | ||
- `num_of_samples`: The number of tree samples drawn from the posterior. | ||
- `bl_sampler`: Sampler used to drawn branchlengths from the posterior. | ||
- `burn_in`: The number of samples discarded at the start of the Markov Chain. | ||
- `sample_interval`: The distance between samples in the underlying Markov Chain (to reduce sample correlation). | ||
- `collect_LLs`: Specifies if the function should return the log-likelihoods of the trees. | ||
- `midpoint_rooting`: Specifies whether the drawn samples should be midpoint rerooted (Important! Should only be used for time-reversible branch models starting in equilibrium). | ||
|
||
!!! note | ||
The leaves of the initial tree should be populated with data and felsenstein! should be called on the initial tree before calling this function. | ||
|
||
# Returns | ||
- `samples`: The trees drawn from the posterior. Returns shallow tree copies, which needs to be repopulated before running felsenstein! etc. | ||
- `sample_LLs`: The associated log-likelihoods of the tree (optional). | ||
""" | ||
function metropolis_sample( | ||
initial_tree::FelNode, | ||
models::Vector{<:BranchModel}, | ||
num_of_samples; | ||
bl_sampler::UnivariateSampler = BranchlengthSampler(Normal(0,2), Normal(-1,1)), | ||
burn_in=1000, | ||
sample_interval=10, | ||
collect_LLs = false, | ||
midpoint_rooting=false, | ||
ladderize = false, | ||
) | ||
|
||
# The prior over the (log) of the branchlengths should be specified in bl_sampler. | ||
# Furthermore, a non-informative/uniform prior is assumed over the tree topolgies (excluding the branchlengths). | ||
|
||
sample_LLs = [] | ||
samples = FelNode[] | ||
tree = deepcopy(initial_tree) | ||
iterations = burn_in + num_of_samples * sample_interval | ||
|
||
softmax_sampler = x -> rand(Categorical(softmax(x))) | ||
for i=1:iterations | ||
|
||
# Updates the tree topolgy and branchlengths. | ||
nni_optim!(tree, x -> models, selection_rule = softmax_sampler) | ||
branchlength_optim!(tree, x -> models, bl_modifier = bl_sampler) | ||
|
||
if (i-burn_in) % sample_interval == 0 && i > burn_in | ||
|
||
push!(samples, copy_tree(tree, true)) | ||
|
||
if collect_LLs | ||
push!(sample_LLs, log_likelihood!(tree, models)) | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
if midpoint_rooting | ||
for (i,sample) in enumerate(samples) | ||
node, len = midpoint(sample) | ||
samples[i] = reroot!(node, dist_above_child=len) | ||
end | ||
end | ||
|
||
if ladderize | ||
for sample in samples | ||
ladderize!(sample) | ||
end | ||
end | ||
|
||
if collect_LLs | ||
return samples, sample_LLs | ||
end | ||
|
||
return samples | ||
end | ||
|
||
# Below are some functions that help to assess the mixing by looking at the distance between leaf nodes. | ||
|
||
""" | ||
collect_leaf_dists(trees::Vector{<:AbstractTreeNode}) | ||
|
||
Returns a list of distance matrices containing the distance between the leaf nodes, which can be used to assess mixing. | ||
""" | ||
function collect_leaf_dists(trees::Vector{<:AbstractTreeNode}) | ||
distmats = [] | ||
for tree in trees | ||
push!(distmats, leaf_distmat(tree)) | ||
end | ||
return distmats | ||
end | ||
|
||
""" | ||
leaf_distmat(tree) | ||
|
||
Returns a matrix of the distances between the leaf nodes where the index on the columns and rows are sorted by the leaf names. | ||
""" | ||
function leaf_distmat(tree) | ||
|
||
distmat, node_dic = MolecularEvolution.tree2distances(tree) | ||
|
||
leaflist = getleaflist(tree) | ||
|
||
sort!(leaflist, by = x-> x.name) | ||
|
||
order = [node_dic[leaf] for leaf in leaflist] | ||
|
||
return distmat[order, order] | ||
end | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.