-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from sebapersson/AddPigeonExt
Add PigeonExt
- Loading branch information
Showing
13 changed files
with
185 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,7 @@ | ||
LogDensityProblems.dimension(lp::PEtab.PEtabPigeonReference) = lp.dim | ||
|
||
LogDensityProblems.logdensity(lp::PEtab.PEtabPigeonReference, x) = lp(x) | ||
|
||
function PEtab.get_correction(logreference::PEtab.PEtabPigeonReference, x) | ||
return Bijectors.logabsdetjac(logreference.inference_info.inv_bijectors, x) | ||
end |
This file contains 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 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,58 @@ | ||
module PEtabPigeonsExtension | ||
|
||
using ModelingToolkit | ||
using Distributions | ||
using PEtab | ||
using Pigeons | ||
using Bijectors | ||
using LogDensityProblems | ||
using LogDensityProblemsAD | ||
using MCMCChains | ||
|
||
function Pigeons.initialization(log_potential::PEtab.PEtabLogDensity, rng, ::Int) | ||
return deepcopy(log_potential.initial_value) | ||
end | ||
|
||
function LogDensityProblemsAD.ADgradient(::Symbol, log_potential::PEtab.PEtabLogDensity, | ||
buffers::Pigeons.Augmentation) | ||
Pigeons.BufferedAD(log_potential, buffers) | ||
end | ||
|
||
function LogDensityProblems.logdensity_and_gradient(logpotential::Pigeons.BufferedAD{<:PEtabLogDensity}, | ||
x) | ||
logdens, grad = logpotential.enclosed.logtarget_gradient(x) | ||
logpotential.buffer .= grad | ||
return logdens, logpotential.buffer | ||
end | ||
|
||
function Pigeons.sample_iid!(log_prior::PEtab.PEtabPigeonReference, replica, shared) | ||
@unpack state, rng = replica | ||
sample_iid!(state, rng, log_prior.inference_info) | ||
end | ||
|
||
function sample_iid!(state::AbstractVector, rng, | ||
inference_info::PEtab.InferenceInfo)::Nothing | ||
for i in eachindex(state) | ||
state[i] = rand(rng, inference_info.priors[i]) | ||
end | ||
state .= inference_info.bijectors(state) | ||
return nothing | ||
end | ||
|
||
function PEtab.to_chains(res::Pigeons.PT, target::PEtab.PEtabLogDensity; | ||
start_time = nothing, end_time = nothing) | ||
# Dependent on method | ||
inference_info = target.inference_info | ||
out = sample_array(res)[:, 1:(end - 1), :] | ||
for i in 1:size(out)[1] | ||
out[i, :, :] .= inference_info.inv_bijectors(out[i, :, 1]) | ||
end | ||
if isnothing(start_time) || isnothing(end_time) | ||
return MCMCChains.Chains(out, inference_info.parameters_id) | ||
else | ||
_chain = MCMCChains.Chains(out, inference_info.parameters_id) | ||
return MCMCChains.setinfo(_chain, (start_time = start_time, stop_time = end_time)) | ||
end | ||
end | ||
|
||
end |
This file contains 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 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 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