Skip to content

Instantly share code, notes, and snippets.

View torfjelde's full-sized avatar

Tor Erlend Fjelde torfjelde

View GitHub Profile
# Partial implementation of linear-trend model described in [1] using Turing.jl.
#
# # References
# [1] Taylor, S. J., & Letham, B., Forecasting at scale, PeerJ Preprints, 5(), 3190–2 (2017). http://dx.doi.org/10.7287/peerj.preprints.3190v2
using DataFrames, CSV
using Turing
using MCMCChain, Plots, StatsPlots
pyplot()
@torfjelde
torfjelde / advi.png
Last active May 17, 2019 11:17
An example of how one can implement Automatic Derivative Variational Inference (VI) for Turing.jl.
advi.png
@torfjelde
torfjelde / turing_plot.jl
Created May 27, 2019 00:20
Simple parsing of Turing.Model into MetaGraph, allowing visualization of the probabilistic model.
using MacroTools
using Turing
using LightGraphs, MetaGraphs
# Expressions
ex1 = quote
m(x) = begin
# Assumptions
σ ~ InverseGamma(2,3)
@torfjelde
torfjelde / bijectors_interface.jl
Created June 12, 2019 01:27
Possible Bijectors.jl interface
using Distributions, Bijectors
using ForwardDiff
using Tracker
using Turing
import Random: AbstractRNG
import Distributions: logpdf, rand, rand!, _rand!, _logpdf
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ADVI on MNIST"
]
},
{
@torfjelde
torfjelde / FSSD_gof_pushforward.jl
Last active September 17, 2019 11:04
Example of applying the FSSD goodness-of-fit test to a ChiSq distribution using Log as as push-forward.
julia> using Bijectors
julia> using KernelGoodnessOfFit
julia> using Random; Random.seed!(123);
julia> using ForwardDiff
julia> function Distributions.gradlogpdf(d::ContinuousUnivariateDistribution, x::Real)
ForwardDiff.derivative(z -> logpdf(d, z), x)
@torfjelde
torfjelde / turing-condition.jl
Last active July 29, 2020 16:55
Implementation of a `condition` method for Turing.jl
julia> using Turing
julia> function condition(
model::Turing.Model,
latent::NamedTuple;
sampler = DynamicPPL.SampleFromPrior()
)
vi = Turing.VarInfo(model)
md = vi.metadata
"""
@bijector function f(b::Bijector, x) ... end
Takes the method `forward` and uses it to define both `transform`
and `logabsdetjac`, while ensuring that any shared computation is
taken advantage of in `forward`.
"""
macro bijector(expr)
def = MacroTools.splitdef(expr)
body = def[:body]
@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)
@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