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
julia> using Bijectors | |
julia> using ForwardDiff | |
julia> using ComponentArrays, UnPack | |
julia> using Bijectors: LeakyReLU, Shift, Scale | |
julia> # Affine transformation is simply a `Scale` composed with `Shift` | |
using Bijectors: Shift, Scale |
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
julia> using Distributions, ForwardDiff | |
julia> import Distributions: StatsFuns # StatsFuns.jl is used within Distributions.jl, so we just grab from there | |
julia> ForwardDiff.derivative(λ -> StatsFuns.poiscdf(λ, 1), 1.0) | |
ERROR: MethodError: no method matching poiscdf(::ForwardDiff.Dual{ForwardDiff.Tag{var"#1#2", Float64}, Float64, 1}, ::Int64) | |
Closest candidates are: | |
poiscdf(::Union{Float64, Int64}, ::Union{Float64, Int64}) at /home/tor/.julia/packages/StatsFuns/zJ1EI/src/rmath.jl:77 | |
Stacktrace: | |
[1] (::var"#1#2")(λ::ForwardDiff.Dual{ForwardDiff.Tag{var"#1#2", Float64}, Float64, 1}) |
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
julia> using DynamicPPL, Distributions, Bijectors | |
julia> using Symbolics | |
julia> using Symbolics: SymbolicUtils | |
julia> import StatsFuns | |
julia> # Make stuff from Distributions.jl primitives so that we don't recurse into them. | |
@register Normal() |
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
julia> using DynamicPPL | |
julia> @model function demo1(x) | |
x ~ Normal() | |
end | |
demo1 (generic function with 1 method) | |
julia> @model function demo2(x) | |
x ~ Normal() | |
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
julia> include("nested_samplers.jl") | |
julia> @model function demo() | |
m ~ Normal() | |
x ~ Normal(m, 1) | |
y ~ Normal(x, 1) | |
return (; m, x, y) | |
end | |
demo (generic function with 2 methods) |
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
using DynamicPPL | |
varnames_to_ranges(model::DynamicPPL.Model) = varnames_to_ranges(DynamicPPL.VarInfo(model)) | |
varnames_to_ranges(varinfo::DynamicPPL.UntypedVarInfo) = varnames_to_ranges(varinfo.metadata) | |
function varnames_to_ranges(varinfo::DynamicPPL.TypedVarInfo) | |
offset = 0 | |
dicts = map(varinfo.metadata) do md | |
vns2ranges = varnames_to_ranges(md) | |
vals = collect(values(vns2ranges)) | |
vals_offset = map(r -> offset .+ r, vals) |
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
#!/usr/bin/env julia | |
using ArgParse | |
# Scenarios: | |
# 1. Project is specified, but no files => watch project + run `runtests.jl`. | |
# 2. Project is specified, and files => watch project + run files. | |
# Command line arguments. | |
s = ArgParseSettings() | |
@add_arg_table s begin |
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
using DynamicPPL: OrderedDict, SamplingContext, AbstractContext, IsParent, VarName, Distribution, evaluate!!, VarInfo | |
import DynamicPPL: tilde_assume, dot_tilde_assume, childcontext, setchildcontext, NodeTrait | |
Base.@kwdef struct PriorExtractorContext{D,Ctx} <: AbstractContext | |
priors::D=OrderedDict{VarName,Any}() | |
context::Ctx=SamplingContext() | |
end | |
NodeTrait(::PriorExtractorContext) = IsParent() | |
childcontext(context::PriorExtractorContext) = context.context |
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
julia> using PosteriorDB, StanDistributions, Turing, BridgeStan, LinearAlgebra | |
julia> # Necessary overloads to make it work with Turing. | |
function DynamicPPL.init(rng, dist::StanDistribution, ::DynamicPPL.SampleFromPrior) | |
# `init` uses `rand` by default but this is not supported for `StanDistribution`. | |
return BridgeStan.param_constrain(dist.model, randn(rng, length(dist))) | |
end | |
julia> function DynamicPPL.with_logabsdet_jacobian_and_reconstruct(f, dist::StanDistribution, x) | |
# HACK: This is cheating. |