Skip to content

Acc ratio structure #50

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 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MolecularEvolution"
uuid = "9f975960-e239-4209-8aa0-3d3ad5a82892"
authors = ["Ben Murrell <[email protected]> and contributors"]
version = "0.2.4"
version = "0.2.5"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
4 changes: 2 additions & 2 deletions examples/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mutable struct MyModelSampler{
T1<:ContinuousUnivariateDistribution,
T2<:ContinuousUnivariateDistribution,
} <: ModelsUpdate
acc_ratio::Vector{Int}
acc_ratio::Tuple{Float64, Int64, Int64}
log_var_drift_proposal::T1
log_var_drift_prior::T2
mean_drift::Float64
Expand All @@ -52,7 +52,7 @@ mutable struct MyModelSampler{
log_var_drift_prior::T2,
mean_drift::Float64,
) where {T1<:ContinuousUnivariateDistribution, T2<:ContinuousUnivariateDistribution}
new{T1, T2}([0, 0], log_var_drift_proposal, log_var_drift_prior, mean_drift)
new{T1, T2}((0.0, 0, 0), log_var_drift_proposal, log_var_drift_prior, mean_drift)
end
end
# Then we let this struct implement our [`metropolis_step`](@ref) interface
Expand Down
4 changes: 2 additions & 2 deletions src/core/algorithms/root_optim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ mutable struct StandardRootOpt <: RootOpt
end

struct StandardRootSample <: UniformRootPositionSample
acc_ratio::Array{Int64,1}
acc_ratio::Tuple{Float64, Int64, Int64}
consecutive::Int64

function StandardRootSample(consecutive::Int64)
new(zeros(Int64, 2), consecutive)
new((0.0, 0, 0), consecutive)
end
end

Expand Down
16 changes: 9 additions & 7 deletions src/utils/simple_sample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ end
"""
BranchlengthSampler

A type that allows you to specify a additive proposal function in the log domain and a prior distrubution over the log of the branchlengths. It also holds the acceptance ratio acc_ratio (acc_ratio[1] stores the number of accepts, and acc_ratio[2] stores the number of rejects).
A type that allows you to specify a additive proposal function in the log domain and a prior distrubution over the log of the branchlengths. It also stores `acc_ratio` which is a tuple of `(ratio, total, #acceptances)`, where `ratio::Float64` is the acceptance ratio, `total::Int64` is the total number of proposals, and `#acceptances::Int64` is the number of acceptances.
"""
struct BranchlengthSampler <: UnivariateSampler
acc_ratio::Vector{Int}
mutable struct BranchlengthSampler <: UnivariateSampler
acc_ratio::Tuple{Float64, Int64, Int64} #(ratio, total, #acceptances)
log_bl_proposal::Distribution
log_bl_prior::Distribution
BranchlengthSampler(log_bl_proposal,log_bl_prior) = new([0,0],log_bl_proposal,log_bl_prior)
BranchlengthSampler(log_bl_proposal,log_bl_prior) = new((0.0, 0, 0),log_bl_proposal,log_bl_prior)
end

"""
Expand Down Expand Up @@ -61,11 +61,13 @@ proposal(modifier::BranchlengthSampler, curr_value) = exp(log(curr_value) + rand
log_prior(modifier::BranchlengthSampler, x) = logpdf(modifier.log_bl_prior, log(x + 1e-12))
#Default definition. Overload it for your own modifier type
function apply_decision(modifier, accept::Bool)
ratio, total, acc = modifier.acc_ratio
total += 1
if accept
modifier.acc_ratio[1] += 1
else
modifier.acc_ratio[2] += 1
acc += 1
end
ratio = acc / total
modifier.acc_ratio = (ratio, total, acc)
end

tr(modifier, x) = x
Expand Down
Loading