Skip to content

Instantly share code, notes, and snippets.

View torfjelde's full-sized avatar

Tor Erlend Fjelde torfjelde

View GitHub Profile
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 / 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()
@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 / turing-new-interface.jl
Last active November 18, 2023 22:26
Simple example of using NUTS with the new iterator interface in AbstractMCMC.jl available using Turing.jl > 0.15.
julia> using Turing, Random
julia> @model function gdemo(xs)
# Assumptions
σ² ~ InverseGamma(2, 3)
μ ~ Normal(0, √σ²)
# Observations
for i = 1:length(xs)
xs[i] ~ Normal(μ, √σ²)
end
@torfjelde
torfjelde / tennis_data.mat
Last active November 22, 2020 16:31
Implementation of Gibbs sampling for TrueSkill in Turing.jl.
@torfjelde
torfjelde / generated_quantities_chains.jl
Last active March 28, 2024 20:03
Converting output from `generated_quantities(model, chain)` into a `MCMCChains.Chains` object
julia> using Turing
julia> include("utils.jl")
julia> @model function demo(xs)
s ~ InverseGamma(2, 3)
m ~ Normal(0, √s)
for i in eachindex(xs)
xs[i] ~ Normal(m, √s)
end
@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 / loo.jl
Last active November 18, 2023 22:26
Using`loo` in Turing.jl
julia> using ArviZ, Turing
julia> J = 8;
julia> y = [28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0];
julia> σ = [15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0];
julia> schools = [
"Choate",
@torfjelde
torfjelde / logging-while-sampling.jl
Last active September 19, 2020 00:09
An example of how to use TensorboardLogging.jl to log certain statistics during sampling in Turing.jl
using Turing
using TensorBoardLogger, Logging
using OnlineStats # used to compute different statistics on-the-fly
using StatsBase # Provides us with the `Histogram` which is supported by `TensorBoardLogger.jl`
using LinearAlgebra
using DataStructures # will use a `CircularBuffer` to only keep track of some `n` last samples
struct TBCallback
logger::TBLogger
end
@torfjelde
torfjelde / manual-sample.jl
Last active September 18, 2020 11:20
Example of writing out `Turing.sample` by hand.
julia> using Random
julia> using Turing
julia> rng = MersenneTwister(42);
julia> @model function demo(x)
s ~ InverseGamma(2, 3)
m ~ Normal(0, √s)
for i in eachindex(x)