xxxxxxxxxxPackage initialization
xxxxxxxxxxxxxxxxxxxxbegin using AdvancedMH using LogExpFunctions using OrdinaryDiffEq using PlutoUI using StatsPlots using Turing using LinearAlgebra using RandomendProbabilistic Modelling with Turing.jl
David Widmann (@devmotion )
Uppsala University, Sweden
Julia User Group Munich, 5 July 2021
xxxxxxxxxxAbout me
👨🎓 PhD student at the Department of Information Technology and the Centre for Interdisciplinary Mathematics (CIM) in Uppsala
BSc and MSc in Mathematics (TU Munich)
Human medicine (LMU and TU Munich)
👨🔬 Research topic: "Uncertainty-aware deep learning"
I.e., statistics, probability theory, machine learning, and computer science
💻 Julia programming, e.g.,
SciML, in particular DelayDiffEq.jl
xxxxxxxxxxSIR model
Classical mathematical model of infectious diseases (e.g., influenza or COVID-19):
with infection rate
xxxxxxxxxxsir! (generic function with 1 method)xxxxxxxxxxfunction sir!(du, u, p, t) S, I, R = u β, γ = p N = S + I + R a = β * S * I / N b = γ * I du[1] = -a du[2] = a - b du[3] = b return nothingendxxxxxxxxxxxxxxxxxxxxReproduction number:
xxxxxxxxxxInference problem
Model can be used to make predictions or reason about possible interventions
But...
We do not know parameters such as
transmission rate
recovery rate
initial number of infected individuals
We might just know
population size
initial number of removed individuals
noisy (i.e., slightly incorrect) number of newly infected individuals for some days
xxxxxxxxxxProbabilistic modelling
Idea: model uncertainty of unknown parameters with probability distributions
Bayesian approach
Model uncertainty with conditional probability
of unknown parameters
E.g.,
: unknown parameters , , and that we want to infer : number of newly infected individuals
Bayes' theorem
Conditional probability
Workflow
Choose model
e.g., describes how daily cases of newly infected individuals depend on parameters
Choose prior
should incorporate initial beliefs and knowledge about
e.g.,
and are positive and is a natural number
Compute
xxxxxxxxxxExample: Coin flip
Coin with unknown probability of heads
-times head in coinflips (observation)unknown probability
of heads (parameter)
xxxxxxxxxxLikelihood
xxxxxxxxxxxxxxxxxxxxPrior
We choose a Beta distribution as prior:
Note
Without any further information,
xxxxxxxxxxxxxxxxxxxxPosterior
For these choices of likelihood and prior, Bayes' theorem yields
Note
Families of prior distributions for which the posterior is in the same family are called conjugate priors.
xxxxxxxxxxtrue
xxxxxxxxxx25xxxxxxxxxxcoinflip_k = let Random.seed!(100) heads = rand(coinflip_n) .< coinflip_p sum(heads)endxxxxxxxxxxxxxxxxxxxx⚠️ Issue
Often it is not possible to compute
xxxxxxxxxxDiscrete approximation
Idea: Approximate
where
This implies that
and more generally
xxxxxxxxxxnumber of samples:
xxxxxxxxxxxxxxxxxxxxThe approximation can be constructed with e.g.
importance sampling
sequential Monte Carlo (SMC)
Markov Chain Monte Carlo (MCMC)
xxxxxxxxxxStandalone samplers
One main design of the Turing ecosystem is to have many smaller packages that
can be used independently of Turing
are supported by Turing
For MCMC, currently we have e.g.
Additionally, Turing supports also DynamicHMC.jl.
xxxxxxxxxxAdvancedMH.jl
AdvancedMH.jl contains an implementation of the Metropolis-Hastings algorithm.
xxxxxxxxxxWe have to define the unnormalized log density, i.e., the sum of the log-likelihood and the log prior, as a function of the unknown parameter.
xxxxxxxxxx#1 (generic function with 1 method)xxxxxxxxxxlogdensity_coinflip = let α = coinflip_α, β = coinflip_β, k = coinflip_k, n = coinflip_n p -> begin # compute log prior logprior = logpdf(Beta(α, β), p) return if isfinite(logprior) # only evalute log-likelihood if it is finite # (otherwise `p` might be < 0 or > 1 logprior + logpdf(Binomial(n, p), k) else logprior end endendThe model is fully specified by the log density function.
xxxxxxxxxx#1 (generic function with 1 method)
xxxxxxxxxxmodel_coinflip_mh = DensityModel(logdensity_coinflip)We use a Metropolis-Hastings algorithm with a random walk proposal.
xxxxxxxxxxDistributions.Normal{Float64}(μ=0.0, σ=1.0)xxxxxxxxxxsampler_coinflip_mh = RWMH(Normal())AbstractMCMC.jl
AbstractMCMC.jl defines an interface for MCMC algorithms.
The interface is supposed to make it easy to implement MCMC algorithms. The default definitions provide users with
progress bars,
support for user-provided callbacks,
support for thinning and discarding initial samples,
support for sampling with a custom stopping criterion,
support for sampling multiple chains, serially or in parallel with multiple threads or multiple processes,
an iterator and a transducer for sampling Markov chains.
xxxxxxxxxxThe main user-facing API is sample.
xxxxxxxxxx-0.658136
-Inf
-0.658136
-Inf
-0.658136
-Inf
-0.658136
-Inf
-0.658136
-Inf
0.979575
-65.3201
0.765583
-10.4737
0.765583
-10.4737
0.67811
-5.57922
0.67811
-5.57922
xxxxxxxxxxlet # set seed Random.seed!(100) sample(model_coinflip_mh, sampler_coinflip_mh, 10)endSome common keyword arguments:
discard_initial(default:0): number of initial samples that are discardedthinning(default: 1): factor by which samples are thinnedchain_type(default:Any): type of the returned chaincallback(default:nothing): callback that is called after every sampling stepprogress(default:AbstractMCMC.PROGRESS[]which istrueinitially): toggles progress logging
xxxxxxxxxx| iteration | chain | param_1 | lp | |
|---|---|---|---|---|
| Int64 | Int64 | Float64 | Float64 | |
| 1 | 1 | 1 | 0.628946 | -3.90739 |
| 2 | 2 | 1 | 0.628946 | -3.90739 |
| 3 | 3 | 1 | 0.468516 | -2.28612 |
| 4 | 4 | 1 | 0.468516 | -2.28612 |
| 5 | 5 | 1 | 0.468516 | -2.28612 |
| 6 | 6 | 1 | 0.468516 | -2.28612 |
| 7 | 7 | 1 | 0.509164 | -2.1952 |
| 8 | 8 | 1 | 0.509164 | -2.1952 |
| 9 | 9 | 1 | 0.509164 | -2.1952 |
| 10 | 10 | 1 | 0.509164 | -2.1952 |
xxxxxxxxxxlet # set seed Random.seed!(100) sample( model_coinflip_mh, sampler_coinflip_mh, 10; discard_initial=25, thinning=4, chain_type=Chains, )endAbstractMCMC.jl allows you to sample multiple chains with the following three algorithms:
MCMCSerial(): sample in serial (no parallelization)MCMCThreads(): parallel sampling with multiple threadsMCMCDistributed(): parallel sampling with multiple processes
xxxxxxxxxx| iteration | chain | p | lp | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Int64 | Int64 | Float64 | Float64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1 | 1 | 1 | 0.535545 | -2.31347 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | 1 | 0.535545 | -2.31347 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | 1 | 0.55383 | -2.47826 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | 1 | 0.430189 | -2.67898 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | 1 | 0.47345 | -2.25739 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | 1 | 0.610073 | -3.42876 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | 1 | 0.416558 | -2.89294 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | 1 | 0.406132 | -3.08382 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | 1 | 0.412865 | -2.95782 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | 1 | 0.49555 | -2.18878 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
xxxxxxxxxxcoinflip_mh = let # set seed Random.seed!(100) sample( model_coinflip_mh, sampler_coinflip_mh, MCMCThreads(), 1_000, 3; discard_initial=100, thinning=10, param_names=["p"], chain_type=Chains, )endThe iterator interface allows us to e.g. plot the samples in every step:
xxxxxxxxxxxxxxxxxxxxlet Random.seed!(100) # Container for samples n = 1_000 samples = Float64[] sizehint!(samples, n) # Create animation for transition in Iterators.take(AbstractMCMC.steps(model_coinflip_mh, sampler_coinflip_mh), n) push!(samples, transition.params) plot(samples; xlabel="iteration", ylabel="value", title="p", legend=false) end every 10endMCMCChains.jl
MCMCChains.jl contains tools for analyzing and visualizing MCMC chains.
xxxxxxxxxxxxxxxxxxxxPlots.plot(coinflip_mh)xxxxxxxxxxautocorplot(coinflip_mh)| parameters | mean | std | naive_se | mcse | ess | rhat | |
|---|---|---|---|---|---|---|---|
| Symbol | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
| 1 | :p | 0.501525 | 0.0708991 | 0.00129444 | 0.00135053 | 1473.82 | 1.00004 |
xxxxxxxxxxsummarystats(coinflip_mh)Posterior predictive check
It is often useful to generate simulated data using samples from the posterior distribution and compare them to the original data.
(see, e.g., "Statistical Rethinking" (section 3.3.2 - model checking) and the documentation)
xxxxxxxxxx24
25
28
24
27
29
21
17
22
20
20
23
15
21
28
20
25
23
26
22
23
25
34
26
25
22
36
33
26
24
xxxxxxxxxxcoinflip_check_mh = let Random.seed!(100) map(Array(coinflip_mh)) do p return rand(Binomial(coinflip_n, p)) endendxxxxxxxxxxProbabilistic programming
Source: Lecture slides
Developing probabilistic models and inference algorithms is a time-consuming and error-prone process.
Probabilistic model written as a computer program
Automatic inference (integral part of the programming language)
Advantages:
Fast development of models
Expressive models
Widely applicable inference algorithms
xxxxxxxxxxTuring
Turing is a probabilistic programming language (PPL).
General design
Probabilistic models are implemented as a Julia function
One may use any Julia code inside of the model
Random variables and observations are declared with the
~operator:function mymodel(x, y) ... # random variable `a` with prior distribution `dist_a` a ~ dist_a ... # observation `y` with data distribution `dist_y` y ~ dist_y ... endPPL is implemented in DynamicPPL.jl, including
@modelmacroTuring.jl integrates and reexports different parts of the ecosystem such as the PPL, inference algorithms, and tools for automatic differentiation
xxxxxxxxxxCoinflip model
A possible Turing model:
xxxxxxxxxxMain.workspace2.coinflipxxxxxxxxxx""" coinflip(n, k; α=1, β=1)Create a probabilistic model of `n` coinflips where head shows up `k` times andthe prior distribution of probability `p` of heads is `Beta(α, β)`.""" function coinflip(n::Int, k; α::Real=1, β::Real=1) # the prior distribution of parameter `p` is `Beta(α, β)` p ~ Beta(α, β) # observation `k` is binomially distributed with parameters `n` and `k` k ~ Binomial(n, p) return (; p, k)endcoinflip is a Julia function: it creates a model of type DynamicPPL.Model that stores
the name of the model,
the generative function of the model (i.e., the function above that declares how to run the model),
and the arguments of the model and their default values.
xxxxxxxxxx:coinflip
#2 (generic function with 1 method)
10
1
1
1
1
1
xxxxxxxxxxcoinflip(10, 1)We can look up the documentation of coinflip:
xxxxxxxxxxcoinflip(n, k; α=1, β=1)
Create a probabilistic model of n coinflips where head shows up k times and the prior distribution of probability p of heads is Beta(α, β).
xxxxxxxxxx coinflipAnd we can inspect how the @model macro rewrites the function definition:
xxxxxxxxxxquote
$(Expr(:meta, :doc))
function conflip(n, k; α::Real, β::Real)
#= /home/david/Documents/Projects/github/Talks/2021/07/Turing/notebook.jl#==#6c9181b4-a157-4afc-862f-6f603a8023e4:1 =#
var"##evaluator#2137" = ((__model__::DynamicPPL.Model, __varinfo__::DynamicPPL.AbstractVarInfo, __context__::DynamicPPL.AbstractContext, n, k, α::Real, β::Real)->begin
begin
#= /home/david/Documents/Projects/github/Talks/2021/07/Turing/notebook.jl#==#6c9181b4-a157-4afc-862f-6f603a8023e4:1 =#
#= /home/david/Documents/Projects/github/Talks/2021/07/Turing/notebook.jl#==#6c9181b4-a157-4afc-862f-6f603a8023e4:2 =#
begin
var"##vn#2129" = (AbstractPPL.VarName){:p}()
var"##inds#2130" = ()
var"##isassumption#2131" = begin
let var"##vn#2132" = (AbstractPPL.VarName){:p}()
if !((DynamicPPL.inargnames)(var"##vn#2132", __model__)) || (DynamicPPL.inmissings)(var"##vn#2132", __model__)
true
else
p === missing
end
end
end
if var"##isassumption#2131"
p = (DynamicPPL.tilde_assume!)(__context__, (DynamicPPL.unwrap_right_vn)((DynamicPPL.check_tilde_rhs)(Beta(α, β)), var"##vn#2129")..., var"##inds#2130", __varinfo__)
else
(DynamicPPL.tilde_observe!)(__context__, (DynamicPPL.check_tilde_rhs)(Beta(α, β)), p, var"##vn#2129", var"##inds#2130", __varinfo__)
end
end
#= /home/david/Documents/Projects/github/Talks/2021/07/Turing/notebook.jl#==#6c9181b4-a157-4afc-862f-6f603a8023e4:3 =#
begin
var"##vn#2133" = (AbstractPPL.VarName){:k}()
var"##inds#2134" = ()
var"##isassumption#2135" = begin
let var"##vn#2136" = (AbstractPPL.VarName){:k}()
if !((DynamicPPL.inargnames)(var"##vn#2136", __model__)) || (DynamicPPL.inmissings)(var"##vn#2136", __model__)
true
else
k === missing
end
end
end
if var"##isassumption#2135"
k = (DynamicPPL.tilde_assume!)(__context__, (DynamicPPL.unwrap_right_vn)((DynamicPPL.check_tilde_rhs)(Binomial(n, p)), var"##vn#2133")..., var"##inds#2134", __varinfo__)
else
(DynamicPPL.tilde_observe!)(__context__, (DynamicPPL.check_tilde_rhs)(Binomial(n, p)), k, var"##vn#2133", var"##inds#2134", __varinfo__)
end
end
#= /home/david/Documents/Projects/github/Talks/2021/07/Turing/notebook.jl#==#6c9181b4-a157-4afc-862f-6f603a8023e4:4 =#
return (; p, k)
end
end)
return (DynamicPPL.Model)(:conflip, var"##evaluator#2137", (DynamicPPL.namedtuple)(NamedTuple{(:n, :k, :α, :β), Tuple{Core.Typeof(n), Core.Typeof(k), Core.Typeof(α), Core.Typeof(β)}}, (n, k, α, β)), NamedTuple())
end
endxxxxxxxxxx function conflip(n, k; α::Real, β::Real) p ~ Beta(α, β) k ~ Binomial(n, p) return (; p, k)endSince coinflip is a regular Julia function, we can also extend it. E.g., we can allow users to specify the results of the coin flips as a vector of true (heads) and false (tail):
xxxxxxxxxxcoinflip (generic function with 2 methods)xxxxxxxxxxfunction coinflip(flips::AbstractVector{Bool}; α::Real=1, β::Real=1) return coinflip(length(flips), sum(flips); α, β)end:coinflip
#2 (generic function with 1 method)
6
4
1
1
1
1
xxxxxxxxxxcoinflip([true, false, false, true, true, true])Inference
We choose the same parameters as above:
xxxxxxxxxx:coinflip
#2 (generic function with 1 method)
50
25
1
1
1
1
xxxxxxxxxxcoinflip_model = coinflip(coinflip_n, coinflip_k; α=coinflip_α, β=coinflip_β)If you call the model without arguments, it is executed and all variables are sampled from the prior.
xxxxxxxxxx0.0159315
25
xxxxxxxxxxcoinflip_model()Algorithm:
xxxxxxxxxxxxxxxxxxxxcoinflip_alg| iteration | chain | p | lp | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Int64 | Int64 | Float64 | Float64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1 | 101 | 1 | 0.528507 | -2.2682 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 111 | 1 | 0.513181 | -2.20418 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 121 | 1 | 0.431461 | -2.66103 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 131 | 1 | 0.467184 | -2.29473 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 141 | 1 | 0.486704 | -2.20449 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 151 | 1 | 0.398265 | -3.24384 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 161 | 1 | 0.519854 | -2.22625 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 171 | 1 | 0.471834 | -2.26626 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 181 | 1 | 0.41144 | -2.98365 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 191 | 1 | 0.468481 | -2.28635 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
xxxxxxxxxxcoinflip_turing = let # set seed Random.seed!(100) sample( coinflip_model, coinflip_alg, MCMCThreads(), 1_000, 3; discard_initial=100, thinning=10, param_names=["p"], chain_type=Chains, )endAnalysis
xxxxxxxxxxxxxxxxxxxxPlots.plot(coinflip_turing)xxxxxxxxxxautocorplot(coinflip_turing)| parameters | mean | std | naive_se | mcse | ess | rhat | ess_per_sec | |
|---|---|---|---|---|---|---|---|---|
| Symbol | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
| 1 | :p | 0.498296 | 0.0679105 | 0.00123987 | 0.00106426 | 2842.3 | 0.9998 | 182.902 |
xxxxxxxxxxsummarystats(coinflip_turing)Posterior predictive check
Similar to above, we can check if the posterior predictions fit the observed data.
xxxxxxxxxx23
24
22
26
27
22
27
19
21
21
28
33
26
22
30
25
22
26
20
21
31
30
29
31
28
27
22
14
21
26
xxxxxxxxxxcoinflip_check_turing = let Random.seed!(100) map(Array(coinflip_turing)) do p return rand(Binomial(coinflip_n, p)) endendxxxxxxxxxxIn more complicated models it can be tedious to rewrite how to sample the observations explicitly. Fortunately, we can use the following feature of Turing:
If the left hand side of a
~expression ismissing, it is sampled even if it is an argument to the model.
xxxxxxxxxx0.939222
49
xxxxxxxxxxcoinflip(coinflip_n, missing)()generated_quantities can be used to compute/sample an array of the return values of a model for each set of samples in the chain:
xxxxxxxxxx1000×3 Matrix{NamedTuple{(:p, :k), Tuple{Float64, Int64}}}:
(p = 0.528507, k = 26) (p = 0.42211, k = 18) (p = 0.466682, k = 24)
(p = 0.513181, k = 23) (p = 0.487452, k = 27) (p = 0.474066, k = 22)
(p = 0.431461, k = 25) (p = 0.576841, k = 34) (p = 0.411804, k = 18)
(p = 0.467184, k = 25) (p = 0.610048, k = 28) (p = 0.445647, k = 21)
(p = 0.486704, k = 23) (p = 0.531082, k = 27) (p = 0.462252, k = 22)
(p = 0.398265, k = 17) (p = 0.548944, k = 28) (p = 0.462252, k = 19)
(p = 0.519854, k = 26) (p = 0.3966, k = 13) (p = 0.592647, k = 35)
⋮
(p = 0.529214, k = 21) (p = 0.541318, k = 33) (p = 0.491337, k = 28)
(p = 0.513032, k = 20) (p = 0.462376, k = 24) (p = 0.376187, k = 27)
(p = 0.437821, k = 25) (p = 0.612827, k = 30) (p = 0.48598, k = 22)
(p = 0.499246, k = 29) (p = 0.575813, k = 27) (p = 0.360154, k = 14)
(p = 0.470747, k = 17) (p = 0.408261, k = 18) (p = 0.384433, k = 21)
(p = 0.599063, k = 30) (p = 0.420366, k = 22) (p = 0.494838, k = 26)xxxxxxxxxxcoinflip_return_turing = let # Model with `k` set to `missing` coinflip_missing_model = coinflip(coinflip_n, missing; α=coinflip_α, β=coinflip_β) # Return values for each sample of `p` in the chain Random.seed!(100) generated_quantities(coinflip_missing_model, coinflip_turing)end26
23
25
25
23
17
26
19
19
23
35
27
22
26
25
19
26
30
30
18
31
30
29
31
28
27
22
14
21
26
xxxxxxxxxxcoinflip_check_turing2 = map(x -> x.k, vec(coinflip_return_turing))xxxxxxxxxxxxxxxxxxxxAutomatic differentiation
Algorithms such as Hamiltonian Monte Carlo (HMC) or no-U-turn sampler (NUTS) require the gradient of the log density function as well.
Turing computes the gradient automatically with automatic differentiation (AD). Different backends and algorithms are supported:
ForwardDiff.jl: forward-mode AD, the default backend
Tracker.jl: reverse-mode AD
ReverseDiff.jl: reverse-mode AD, has to be loaded explicitly (optional cache for some models)
Zygote.jl: reverse-mode AD, has to be loaded explicitly
xxxxxxxxxxIf not set explicitly in the sampler, Turing uses the currently active global AD backend. It can be set with
setadbackend(:forwarddiff),setadbackend(:tracker),setadbackend(:reversediff), orsetadbackend(:zygote).
Alternatively, the backend can be set explicitly in the sampler:
xxxxxxxxxxxxxxxxxxxxlet Random.seed!(100) chain = sample( coinflip_model, NUTS{Turing.ForwardDiffAD{3}}(0.65), # NUTS{Turing.TrackerAD}(0.65), 1_000; discard_initial=100, thinning=10, param_names=["p"], chain_type=Chains, ) plot(chain)endRule of thumb
Use forward-mode AD for models with few parameters and reverse-mode AD for models with many parameters or linear algebra operations.
If you use reverse-mode AD, in particular with Tracker or Zygote, you should avoid loops and use vectorized operations.
xxxxxxxxxxBayesian inference of SIR model
Based on Simon Frost's example
Setting
SIR model:
We assume that
, , and at day 0, , and .
xxxxxxxxxxxxxxxxxxxxWe observe the number of new cases for days 1 to 40 with some Poisson measurement error:
xxxxxxxxxxxxxxxxxxxxbegin # new cases without noise sir_newcases = abs.(diff(sir_solution(0:40; idxs=1).u)) # with Poisson noise sir_data = let Random.seed!(100) map(sir_newcases) do x return rand(Poisson(x)) end endend;xxxxxxxxxxTuring model
We define a probabilistic model of the observations, the initial proportion of infected individuals
xxxxxxxxxxxxxxxxxxxx function sir_model(t, y, ::Type{T}=Float64) where {T} # sample the parameters: uniform priors for `i₀`, `β` and `γ` i₀ ~ Uniform(0.0, 1.0) β ~ Uniform(0.0, 1.0) γ ~ Uniform(0.0, 1.0) # simulate the SIR model and save the number of infected # individuals at times `t` I₀ = 1_000 * i₀ prob = ODEProblem{true}(sir!, T[1_000 - I₀, I₀, 0], (0.0, last(t)), (; β, γ)) sol = solve(prob, Tsit5(); saveat=t, save_idxs=1) # ensure that the simulation was successful if sol.retcode !== :Success Turing.! -Inf else # compute new cases cases = map(abs, diff(Array(sol))) # noisy observations for i in 1:length(y) y[i] ~ Poisson(cases[i]) end end return (; i₀, β, γ, y)end;Info
The initial state of the SIR model has to be compatible with automatic differentiation if a gradient-based sampler such as HMC or NUTS is used. This is ensured with the type parameter T which Turing sets to a suitable type for the sampler automatically.
xxxxxxxxxxInference
xxxxxxxxxxxxxxxxxxxxplot(sir_nuts)xxxxxxxxxxautocorplot(sir_nuts)| parameters | mean | std | naive_se | mcse | ess | rhat | ess_per_sec | |
|---|---|---|---|---|---|---|---|---|
| Symbol | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
| 1 | :i₀ | 0.0114602 | 0.00220059 | 4.01772e-5 | 4.09611e-5 | 2772.04 | 1.00003 | 21.0922 |
| 2 | :β | 0.478066 | 0.0343876 | 0.000627829 | 0.000631054 | 2918.91 | 0.99957 | 22.2097 |
| 3 | :γ | 0.227447 | 0.029688 | 0.000542027 | 0.000558474 | 2952.89 | 0.999492 | 22.4683 |
xxxxxxxxxxsummarystats(sir_nuts)xxxxxxxxxxsir_nuts = let Random.seed!(100) sample( sir_model(0:40, sir_data), NUTS(0.65), MCMCThreads(), 1_000, 3; thinning=10, discard_initial=100, )end;Posterior predictive check
xxxxxxxxxx1000×3 Matrix{NamedTuple{(:i₀, :β, :γ, :y), Tuple{Float64, Float64, Float64, Vector{Union{Missing, Float64}}}}}:
(i₀ = 0.0117984, β = 0.470022, γ = 0.229061, y = [8.0, 9.0, 9.0, 11.0, 18.0, 18.0, 23.0, 21.0, 25.0, 40.0 … 8.0, 9.0, 3.0, 4.0, 6.0, 5.0, 3.0, 2.0, 3.0, 1.0]) … (i₀ = 0.0154555, β = 0.403928, γ = 0.180931, y = [2.0, 11.0, 5.0, 15.0, 19.0, 16.0, 19.0, 24.0, 33.0, 27.0 … 10.0, 5.0, 1.0, 7.0, 12.0, 7.0, 5.0, 6.0, 8.0, 2.0])
(i₀ = 0.0111037, β = 0.482378, γ = 0.225529, y = [5.0, 6.0, 15.0, 13.0, 18.0, 23.0, 20.0, 24.0, 36.0, 41.0 … 9.0, 11.0, 5.0, 3.0, 2.0, 0.0, 0.0, 1.0, 2.0, 4.0]) (i₀ = 0.0111962, β = 0.471755, γ = 0.226175, y = [7.0, 5.0, 6.0, 11.0, 5.0, 23.0, 17.0, 24.0, 32.0, 26.0 … 5.0, 6.0, 3.0, 4.0, 4.0, 2.0, 1.0, 2.0, 2.0, 4.0])
(i₀ = 0.0108824, β = 0.486026, γ = 0.230159, y = [2.0, 7.0, 10.0, 15.0, 16.0, 18.0, 24.0, 30.0, 22.0, 29.0 … 5.0, 9.0, 4.0, 5.0, 3.0, 5.0, 1.0, 0.0, 3.0, 3.0]) (i₀ = 0.0111547, β = 0.486378, γ = 0.234458, y = [11.0, 12.0, 9.0, 11.0, 19.0, 23.0, 23.0, 25.0, 38.0, 31.0 … 10.0, 7.0, 3.0, 4.0, 3.0, 1.0, 0.0, 2.0, 0.0, 1.0])
(i₀ = 0.01451, β = 0.447743, γ = 0.209684, y = [9.0, 11.0, 17.0, 14.0, 22.0, 18.0, 20.0, 37.0, 35.0, 38.0 … 6.0, 6.0, 9.0, 1.0, 4.0, 2.0, 1.0, 3.0, 0.0, 1.0]) (i₀ = 0.00966814, β = 0.483003, γ = 0.219866, y = [6.0, 8.0, 14.0, 8.0, 15.0, 22.0, 26.0, 37.0, 25.0, 38.0 … 5.0, 3.0, 1.0, 4.0, 5.0, 4.0, 4.0, 1.0, 1.0, 3.0])
(i₀ = 0.00815737, β = 0.523331, γ = 0.255746, y = [5.0, 13.0, 7.0, 8.0, 9.0, 23.0, 24.0, 18.0, 32.0, 42.0 … 5.0, 3.0, 7.0, 4.0, 2.0, 4.0, 4.0, 2.0, 3.0, 1.0]) (i₀ = 0.0134974, β = 0.453871, γ = 0.217568, y = [5.0, 8.0, 5.0, 7.0, 19.0, 18.0, 25.0, 35.0, 28.0, 44.0 … 13.0, 3.0, 10.0, 6.0, 5.0, 4.0, 1.0, 0.0, 1.0, 3.0])
(i₀ = 0.0100787, β = 0.491488, γ = 0.234026, y = [3.0, 15.0, 6.0, 10.0, 21.0, 21.0, 26.0, 25.0, 24.0, 39.0 … 10.0, 8.0, 5.0, 4.0, 2.0, 3.0, 4.0, 2.0, 2.0, 2.0]) … (i₀ = 0.010865, β = 0.493458, γ = 0.252121, y = [12.0, 6.0, 8.0, 13.0, 9.0, 11.0, 29.0, 28.0, 29.0, 38.0 … 3.0, 4.0, 5.0, 6.0, 0.0, 4.0, 2.0, 4.0, 1.0, 1.0])
(i₀ = 0.0097824, β = 0.507822, γ = 0.253211, y = [6.0, 7.0, 8.0, 14.0, 17.0, 25.0, 24.0, 24.0, 28.0, 21.0 … 11.0, 6.0, 4.0, 4.0, 6.0, 1.0, 2.0, 1.0, 3.0, 1.0]) (i₀ = 0.0108123, β = 0.474993, γ = 0.226182, y = [8.0, 5.0, 9.0, 9.0, 9.0, 11.0, 19.0, 24.0, 24.0, 43.0 … 12.0, 10.0, 1.0, 5.0, 2.0, 3.0, 8.0, 3.0, 3.0, 1.0])
⋮ ⋱
(i₀ = 0.0106781, β = 0.477503, γ = 0.218882, y = [3.0, 10.0, 11.0, 12.0, 14.0, 12.0, 24.0, 29.0, 34.0, 31.0 … 4.0, 6.0, 3.0, 2.0, 3.0, 3.0, 3.0, 5.0, 3.0, 1.0]) (i₀ = 0.0102792, β = 0.497104, γ = 0.242215, y = [6.0, 12.0, 16.0, 7.0, 7.0, 18.0, 17.0, 25.0, 29.0, 30.0 … 4.0, 5.0, 6.0, 3.0, 4.0, 3.0, 3.0, 2.0, 3.0, 1.0])
(i₀ = 0.009452, β = 0.506246, γ = 0.25407, y = [4.0, 4.0, 12.0, 9.0, 13.0, 18.0, 21.0, 18.0, 38.0, 29.0 … 9.0, 9.0, 3.0, 3.0, 3.0, 2.0, 2.0, 2.0, 0.0, 2.0]) … (i₀ = 0.0111689, β = 0.478732, γ = 0.230093, y = [6.0, 5.0, 9.0, 16.0, 10.0, 18.0, 33.0, 23.0, 29.0, 36.0 … 5.0, 4.0, 5.0, 0.0, 9.0, 2.0, 0.0, 2.0, 2.0, 1.0])
(i₀ = 0.0123274, β = 0.435584, γ = 0.198006, y = [15.0, 4.0, 5.0, 11.0, 8.0, 16.0, 26.0, 22.0, 28.0, 35.0 … 17.0, 5.0, 3.0, 3.0, 7.0, 3.0, 5.0, 4.0, 2.0, 5.0]) (i₀ = 0.00997878, β = 0.489763, γ = 0.241924, y = [3.0, 7.0, 15.0, 13.0, 13.0, 10.0, 22.0, 25.0, 31.0, 36.0 … 9.0, 3.0, 8.0, 3.0, 3.0, 7.0, 2.0, 1.0, 3.0, 1.0])
(i₀ = 0.0191635, β = 0.407819, γ = 0.178192, y = [8.0, 11.0, 7.0, 14.0, 19.0, 21.0, 28.0, 29.0, 48.0, 42.0 … 10.0, 8.0, 7.0, 3.0, 6.0, 5.0, 5.0, 1.0, 3.0, 0.0]) (i₀ = 0.0117381, β = 0.447, γ = 0.200144, y = [6.0, 7.0, 6.0, 12.0, 9.0, 15.0, 23.0, 23.0, 24.0, 22.0 … 7.0, 4.0, 6.0, 3.0, 5.0, 5.0, 3.0, 1.0, 1.0, 1.0])
(i₀ = 0.0126501, β = 0.498931, γ = 0.240358, y = [8.0, 7.0, 9.0, 11.0, 23.0, 19.0, 31.0, 27.0, 39.0, 41.0 … 6.0, 7.0, 3.0, 9.0, 1.0, 2.0, 4.0, 1.0, 2.0, 0.0]) (i₀ = 0.0119046, β = 0.457044, γ = 0.211689, y = [3.0, 7.0, 7.0, 13.0, 15.0, 21.0, 23.0, 25.0, 35.0, 34.0 … 7.0, 8.0, 4.0, 5.0, 3.0, 5.0, 5.0, 4.0, 2.0, 0.0])
(i₀ = 0.0131974, β = 0.447402, γ = 0.208132, y = [4.0, 5.0, 7.0, 12.0, 19.0, 15.0, 23.0, 25.0, 26.0, 43.0 … 12.0, 7.0, 10.0, 2.0, 1.0, 6.0, 1.0, 3.0, 5.0, 0.0]) (i₀ = 0.012124, β = 0.462968, γ = 0.222125, y = [3.0, 12.0, 11.0, 12.0, 12.0, 25.0, 21.0, 18.0, 38.0, 35.0 … 8.0, 8.0, 9.0, 4.0, 4.0, 4.0, 3.0, 0.0, 2.0, 2.0])xxxxxxxxxxsir_return = let sir_missing_model = sir_model(0:40, Vector{Missing}(undef, 40)) Random.seed!(100) generated_quantities(sir_missing_model, sir_nuts)end8.0
9.0
9.0
11.0
18.0
18.0
23.0
21.0
25.0
1.0
5.0
6.0
15.0
13.0
18.0
23.0
20.0
24.0
36.0
4.0
2.0
7.0
10.0
15.0
16.0
18.0
24.0
30.0
22.0
3.0
9.0
11.0
17.0
14.0
22.0
18.0
20.0
37.0
35.0
1.0
5.0
13.0
7.0
8.0
9.0
23.0
24.0
18.0
32.0
1.0
3.0
15.0
6.0
10.0
21.0
21.0
26.0
25.0
24.0
2.0
6.0
7.0
8.0
14.0
17.0
25.0
24.0
24.0
28.0
1.0
5.0
7.0
3.0
10.0
15.0
22.0
19.0
32.0
26.0
0.0
9.0
4.0
13.0
9.0
16.0
34.0
27.0
37.0
35.0
1.0
7.0
6.0
5.0
6.0
12.0
16.0
28.0
29.0
22.0
0.0
8.0
5.0
9.0
14.0
16.0
23.0
33.0
35.0
24.0
3.0
6.0
8.0
5.0
10.0
14.0
14.0
19.0
22.0
32.0
1.0
8.0
6.0
9.0
13.0
15.0
25.0
19.0
27.0
40.0
1.0
10.0
2.0
8.0
11.0
15.0
18.0
14.0
23.0
24.0
0.0
4.0
7.0
13.0
12.0
18.0
14.0
34.0
29.0
24.0
3.0
3.0
7.0
6.0
12.0
14.0
11.0
28.0
26.0
32.0
2.0
8.0
8.0
11.0
9.0
17.0
21.0
27.0
30.0
25.0
2.0
3.0
3.0
5.0
11.0
24.0
13.0
19.0
26.0
25.0
0.0
5.0
6.0
7.0
15.0
16.0
15.0
31.0
27.0
32.0
1.0
7.0
8.0
11.0
12.0
13.0
22.0
21.0
27.0
28.0
0.0
4.0
8.0
8.0
9.0
9.0
15.0
16.0
20.0
30.0
2.0
5.0
5.0
10.0
10.0
13.0
17.0
21.0
33.0
30.0
2.0
8.0
8.0
8.0
9.0
7.0
23.0
23.0
32.0
31.0
1.0
14.0
7.0
8.0
16.0
14.0
21.0
26.0
25.0
30.0
2.0
6.0
12.0
16.0
7.0
7.0
18.0
17.0
25.0
29.0
1.0
6.0
5.0
9.0
16.0
10.0
18.0
33.0
23.0
29.0
1.0
3.0
7.0
15.0
13.0
13.0
10.0
22.0
25.0
31.0
1.0
6.0
7.0
6.0
12.0
9.0
15.0
23.0
23.0
24.0
1.0
3.0
7.0
7.0
13.0
15.0
21.0
23.0
25.0
35.0
0.0
3.0
12.0
11.0
12.0
12.0
25.0
21.0
18.0
38.0
2.0
xxxxxxxxxxsir_check = map(x -> convert(Vector{Float64}, x.y), vec(sir_return))xxxxxxxxxxxxxxxxxxxxAdditional resources
Official documentation and tutorials: Please open an issue or a pull request if you discover anything that is broken or outdated 🙏❤️
Turing versions of the Bayesian models in the book Statistical Rethinking by Richard McElreath
xxxxxxxxxx