Skip to content

Instantly share code, notes, and snippets.

View torfjelde's full-sized avatar

Tor Erlend Fjelde torfjelde

View GitHub Profile
@torfjelde
torfjelde / benchmarking-turing.jl
Last active February 27, 2024 21:19
Determining chunksize to use in ForwardDiff.jl for a Turing.jl model
julia> using Turing, TuringBenchmarking, ADTypes
julia> @model function f_model(n)
μ ~ truncated(Normal(), lower=0)
σ² ~ InverseGamma(2, 3)
x ~ filldist(truncated(Normal(μ, sqrt(σ²)); lower=0), n)
end
f_model (generic function with 2 methods)
@torfjelde
torfjelde / Manifest.toml
Last active February 10, 2024 12:56
Quick and dirty example of a mixture sampler using some internals of MCMCTempering.jl.
# This file is machine-generated - editing it directly is not advised
julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "a0590676db40552635a5877095b47d6a64ec5413"
[[deps.ADTypes]]
git-tree-sha1 = "41c37aa88889c171f1300ceac1313c06e891d245"
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
version = "0.2.6"
@torfjelde
torfjelde / hackmd-local.md
Last active December 23, 2023 11:53
Useful set up for working an HackMD file without having to edit through the browser.

Setup

Before you do anything, you'll need to install the following pieces of software:

  • pandoc for converting Markdown to HTML.
  • entr for automagic conversion of Markdown to HTML upon changes to underlying Markdown.
  • browser-sync for automagic refresh upon changes to underlying HTML.

And you need a web browser. Hopefully one is available to you.

The next step is to get yourself a local copy of MathJax v3, which can be achieved by executing

GC error (probable corruption) :
Allocations: 871149 (Pool: 870842; Big: 307); GC: 0
!!! ERROR in jl_ -- ABORTING !!!
0x7f59051d5010: Root object: 0x7f59435fc010 :: 0x7f593ed1a300 (bits: 1)
of type Task
0x7f59051d5028: Root object: 0x7f593e6e9cb0 :: 0x7f593ed1c740 (bits: 3)
of type Module
0x7f59051d5040: Root object: 0x7f593ebffff0 :: 0x7f593ed1a110 (bits: 3)
of type Array{Any, 1}
@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.
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 / 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
@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 / 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 / turing-ad-benchmark.jl
Last active November 18, 2023 22:26
Convenient code for benchmarking different AD-backends on a particular Turing.jl model.
# Use packages to ensure that we trigger Requires.jl.
using Zygote: Zygote
using ReverseDiff: ReverseDiff
using ForwardDiff: ForwardDiff
using Tracker: Tracker
using Memoization: Memoization # used for ReverseDiff.jl cache.
using Turing.Core: ForwardDiffAD, ReverseDiffAD, TrackerAD, ZygoteAD, CHUNKSIZE
const DEFAULT_ADBACKENDS = [