Skip to content

Instantly share code, notes, and snippets.

View torfjelde's full-sized avatar

Tor Erlend Fjelde torfjelde

View GitHub Profile
@torfjelde
torfjelde / advi-simple-example.jl
Created October 6, 2020 14:50
Simple example of using AdvancedVI.jl + Bijectors.jl to construct a approximate/variational posterior.
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
@torfjelde
torfjelde / tennis_data.mat
Last active November 22, 2020 16:31
Implementation of Gibbs sampling for TrueSkill in Turing.jl.
@torfjelde
torfjelde / statsfuns-overload.jl
Last active April 6, 2021 18:50
Example of fixing `poiscdf` from Statsfuns.jl
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})
@torfjelde
torfjelde / dppl-trace.jl
Last active April 26, 2021 12:45
A simple attempt to work generate logjoint for a simple DPPL model. Requires the following branch to work: https://github.com/TuringLang/DynamicPPL.jl/tree/tor/symbolics.
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()
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
@torfjelde
torfjelde / example.jl
Last active December 20, 2021 12:22
This requires `NestedSamplers#tor/improvements` and `Turing@0.19.2` or higher.
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)
@torfjelde
torfjelde / linearization.jl
Last active February 10, 2022 14:11
Example of (un)linearization of `VarInfo`.
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)
@torfjelde
torfjelde / revise-test.jl
Last active June 21, 2023 06:33
Script for watching and running tests or certain files (using Revise.jl to avoid full re-compilation). Useful if you're taking a test-driven development. Requries the following packages to installed in the global environment: - `ArgParse` - `Revise` - `TestEnv`
#!/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
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
@torfjelde
torfjelde / stan-within-turing.jl
Last active September 2, 2023 11:31
Rough example of using StanDistributions.jl within Turing.jl.
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.