├── .JuliaFormatter.toml ├── .gitignore ├── 2023-Geilo-Winter-School ├── Part-1-Intro_Turing │ ├── 01-Julia-Multiple-Dispatch.jl │ ├── 01-Why_Julia.md │ ├── 02-Turing.jl │ ├── 03-hierarchical_models.jl │ ├── 04-diagnostics.jl │ ├── 05-predictive_checks.jl │ ├── Project.toml │ ├── data │ │ └── cheese.csv │ └── images │ │ ├── 01-what-you-see.dot.png │ │ ├── 02-what-turinglang-is.dot.png │ │ ├── 03-the-main-packages-you-use-with-turing.dot.png │ │ └── 04-samplers.dot.png ├── Part-2-Turing-and-other-things │ ├── Manifest.toml │ ├── Project.toml │ ├── README.md │ ├── assets │ │ ├── attachments │ │ │ ├── 2023-01-18_14-46-02_Screenshot_20230118_144454.png │ │ │ ├── 2023-01-18_14-49-24_471337_3317365246956_1262712540_o.jpg │ │ │ ├── 2023-01-18_14-57-08_Preikestolen-plateau-Go-Fjords-Bob-Engelsen-P1026771_kljg5o.jpeg │ │ │ ├── 2023-01-18_15-31-28_Screenshot_20230118_153122.png │ │ │ ├── 2023-01-19_19-48-23_Screenshot_20230119_194737.png │ │ │ ├── 2023-01-19_19-48-41_Screenshot_20230119_194838.png │ │ │ ├── 2023-01-19_19-56-00_sir_illu.png │ │ │ ├── 2023-01-22_12-09-24_Screenshot_20230122_120848.png │ │ │ ├── 2023-01-22_12-30-07_Screenshot_20230122_122936.png │ │ │ ├── 2023-01-25_01-01-31_Screenshot_20230125_010111.png │ │ │ ├── 2023-01-25_01-16-13_Screenshot_20230125_011603.png │ │ │ ├── 2023-01-25_01-29-05_Screenshot_20230125_012853.png │ │ │ ├── 2023-01-25_02-23-23_Screenshot_20230125_022312.png │ │ │ ├── 2023-01-25_20-50-11_tensorboard_demo_histograms_screen.png │ │ │ ├── 2023-01-26_15-47-56_Screenshot_20230126_154747.png │ │ │ ├── 2023-01-26_15-49-34_EMS144307-f009.jpg │ │ │ ├── 2023-01-27_21-50-51_tensorboard_demo_histograms_screen.png │ │ │ └── 2023-01-27_21-51-28_tensorboard_demo_histograms_screen.png │ │ └── outputs │ │ │ ├── 0d84524cc352ba3471bf66156dda0a8cfd1e66b9.png │ │ │ ├── 1265078e6dd1a955c2c437e28a8d0d0904467ad2.png │ │ │ ├── 145ba1245ad3196f9bb3001254df5b3cf451e778.png │ │ │ ├── 18165db131e144d97419fbf55851e2b6a13633b4.png │ │ │ ├── 2a32e4a8ec0b064306909a4bc887c7f0baab4650.png │ │ │ ├── 2e1579a71c4072534cabe41a233cd283543874e6.png │ │ │ ├── 34a52974b7bc0320bef1eb49f39ea8875566a0b3.png │ │ │ ├── 3f7598c6f0df5f376d9f89161504465787bacf25.png │ │ │ ├── 53d5bcefafe0e9c29e4ee0f560bc2648d8b43118.png │ │ │ ├── 62ee261fb15aead33e6f49e82e01a7482b56017f.png │ │ │ ├── 6326dbbbf15b88051f47afcbeb63f6b3c64accd5.png │ │ │ ├── 76b1f4f528c5027a5c432ed6f8a42d20057833ca.png │ │ │ ├── 7763ba1e7b6bb223fde4d453cab14cbcf723b75a.png │ │ │ ├── 780e8cc1c00812abcfba1fe711f0a8b7b168b7e7.png │ │ │ ├── 80c76ad5d3151ec0f3e044a075b2a5b88956d14b.png │ │ │ ├── 856d2889f03822cd9dffef93e27f71c0c4c4c645.png │ │ │ ├── 870b8fafd890396444043fd96c7d303fb7722627.png │ │ │ ├── a1b75cd5d378febc7029c1ed63db2831e09ab9ee.png │ │ │ ├── ba6a57881c744560992e6aeb0ff47cfb9aaf9238.png │ │ │ ├── bc9d6892a46d578ee1b80fb8d98ab74a44e0e9ce.png │ │ │ ├── be1c12fbb76e6f039cc149b75842d8ee461b8f89.png │ │ │ ├── cffea7a80541e6f61fd182de85cb18bc8520b0b4.png │ │ │ ├── ed3058dc552f72dbe5151feb938d103b75251f63.png │ │ │ ├── f110b08ba2b09629fd9d3c498a45a8ab21e0915a.png │ │ │ ├── f64dea8a5533f00397817ee6703ff49dcb105c25.png │ │ │ └── f8f05d979000abd498ef90389bec61e543f004f3.png │ ├── data │ │ └── influenza_england_1978_school.csv │ ├── negative_binomial.gif │ ├── notes.ipynb │ ├── notes.jl │ ├── notes.md │ ├── notes.org │ └── src │ │ └── GeiloWinterSchool2023Part2.jl └── README.md ├── 2023-Intro-Turing ├── .gitignore ├── Project.toml ├── README.md ├── data │ └── wells.csv ├── images │ └── logo.svg ├── intro-turing.html ├── intro-turing.qmd ├── references.bib └── setting_up_julia.md ├── 2023-MRC-BSU-and-UKHSA ├── Part-1-Intro_Julia │ └── julia_intro.jl ├── Part-2-More-Julia-and-some-Bayesian-inference │ ├── .more-julia │ │ └── attachments │ │ │ ├── 2023-01-22_12-09-24_Screenshot_20230122_120848.png │ │ │ ├── 2023-09-15_11-49-35_Screenshot_20230915_114925.png │ │ │ ├── 2023-09-15_13-06-48_Screenshot_20230915_130639.png │ │ │ └── 2023-09-19_21-36-15_Screenshot_20230919_213557.png │ ├── Manifest.toml │ ├── Project.toml │ ├── README.md │ ├── assets │ │ ├── attachments │ │ │ ├── 2023-01-18_14-46-02_Screenshot_20230118_144454.png │ │ │ ├── 2023-01-18_14-49-24_471337_3317365246956_1262712540_o.jpg │ │ │ ├── 2023-01-18_14-57-08_Preikestolen-plateau-Go-Fjords-Bob-Engelsen-P1026771_kljg5o.jpeg │ │ │ ├── 2023-01-18_15-31-28_Screenshot_20230118_153122.png │ │ │ ├── 2023-01-19_19-48-23_Screenshot_20230119_194737.png │ │ │ ├── 2023-01-19_19-48-41_Screenshot_20230119_194838.png │ │ │ ├── 2023-01-19_19-56-00_sir_illu.png │ │ │ ├── 2023-01-22_12-09-24_Screenshot_20230122_120848.png │ │ │ ├── 2023-01-22_12-30-07_Screenshot_20230122_122936.png │ │ │ ├── 2023-01-25_01-01-31_Screenshot_20230125_010111.png │ │ │ ├── 2023-01-25_01-16-13_Screenshot_20230125_011603.png │ │ │ ├── 2023-01-25_01-29-05_Screenshot_20230125_012853.png │ │ │ ├── 2023-01-25_20-50-11_tensorboard_demo_histograms_screen.png │ │ │ └── synthetic-timeseries-data.png │ │ ├── css │ │ │ └── custom.css │ │ ├── images │ │ │ └── turing-logo-wide.svg │ │ └── outputs │ │ │ └── more-julia │ │ │ ├── 0d84524cc352ba3471bf66156dda0a8cfd1e66b9.svg │ │ │ ├── 1425b863a8661542a4b00f6bce46205730eb2531.svg │ │ │ ├── 145ba1245ad3196f9bb3001254df5b3cf451e778.svg │ │ │ ├── 16d623348c3f18f48e1304d2e2300b5e289e1b86.svg │ │ │ ├── 1e1f607dc21364143f2f8b67328de490ce367807.svg │ │ │ ├── 2e1579a71c4072534cabe41a233cd283543874e6.svg │ │ │ ├── 348bd16b3b818fcfbedfaf14f50cb941ca1b7ca3.svg │ │ │ ├── 3f7598c6f0df5f376d9f89161504465787bacf25.svg │ │ │ ├── 42df237b1d7d830e89b857e29552483eb45ebc0c.svg │ │ │ ├── 53d5bcefafe0e9c29e4ee0f560bc2648d8b43118.svg │ │ │ ├── 7763ba1e7b6bb223fde4d453cab14cbcf723b75a.svg │ │ │ ├── 780e8cc1c00812abcfba1fe711f0a8b7b168b7e7.svg │ │ │ ├── 7cc0dffb8818815a0ac1bd1a2ed82b37365dceba.svg │ │ │ ├── 80c76ad5d3151ec0f3e044a075b2a5b88956d14b.svg │ │ │ ├── 870b8fafd890396444043fd96c7d303fb7722627.svg │ │ │ ├── 98e1cf87a13e33c358badda9dd87043e02cc227a.svg │ │ │ ├── a1b75cd5d378febc7029c1ed63db2831e09ab9ee.svg │ │ │ ├── ba6a57881c744560992e6aeb0ff47cfb9aaf9238.svg │ │ │ ├── be1c12fbb76e6f039cc149b75842d8ee461b8f89.svg │ │ │ ├── c4a9d9cc8632820164ac0b1c77cded80e46b9a39.svg │ │ │ ├── ce69f397ca10a9e2661b065d19ad1468ed77de9a.svg │ │ │ ├── cf61c8af8f10097a63ca84e48ff0b4831f7d8a59.svg │ │ │ ├── cffea7a80541e6f61fd182de85cb18bc8520b0b4.svg │ │ │ ├── d49b052ee893e7410f4c67e9ca34a5e5faee8850.svg │ │ │ ├── ed3058dc552f72dbe5151feb938d103b75251f63.svg │ │ │ ├── f110b08ba2b09629fd9d3c498a45a8ab21e0915a.svg │ │ │ ├── lorenz.gif │ │ │ └── pi.gif │ ├── case-studies.org │ ├── data │ │ ├── influenza_england_1978_school.csv │ │ ├── pest_data.csv │ │ └── time-series.csv │ ├── more-julia.html │ ├── more-julia.ipynb │ ├── more-julia.jl │ ├── more-julia.md │ ├── more-julia.org │ └── src │ │ └── Part2.jl ├── Part-3-Case-Study-JuliaBUGS │ ├── Manifest.toml │ ├── Project.toml │ ├── bugs_sir.jl │ ├── presentation.md │ ├── presentation.pdf │ └── rats_plot.png ├── Part-4-Case-Study-GPs-in-Julia │ ├── Manifest.toml │ ├── Project.toml │ ├── README.md │ ├── explainer_script.jl │ ├── extra_script.jl │ ├── presentation.pdf │ └── script.jl ├── Part-5-free-form-session │ ├── MyResearch │ │ ├── Manifest.toml │ │ ├── Project.toml │ │ ├── not_a_module.jl │ │ ├── scripts │ │ │ └── inference.jl │ │ └── src │ │ │ └── MyResearch.jl │ └── README.md └── README.md ├── LICENSE └── README.md /.JuliaFormatter.toml: -------------------------------------------------------------------------------- 1 | ident=4 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/julia 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=julia 3 | 4 | ### Julia ### 5 | # Files generated by invoking Julia with --code-coverage 6 | *.jl.cov 7 | *.jl.*.cov 8 | 9 | # Files generated by invoking Julia with --track-allocation 10 | *.jl.mem 11 | 12 | # System-specific files and directories generated by the BinaryProvider and BinDeps packages 13 | # They contain absolute paths specific to the host computer, and so should not be committed 14 | deps/deps.jl 15 | deps/build.log 16 | deps/downloads/ 17 | deps/usr/ 18 | deps/src/ 19 | 20 | # Build artifacts for creating documentation generated by the Documenter package 21 | docs/build/ 22 | docs/site/ 23 | 24 | # File generated by Pkg, the package manager, based on a corresponding Project.toml 25 | # It records a fixed state of all packages used by the project. As such, it should not be 26 | # committed for packages, but should be committed for applications that require a static 27 | # environment. 28 | # Manifest.toml 29 | 30 | # Jupyter 31 | *.ob-jupyter 32 | 33 | # Emacs 34 | *~undo-tree~ -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/01-Julia-Multiple-Dispatch.jl: -------------------------------------------------------------------------------- 1 | # Example with One-hot Vector 2 | # Based on: https://storopoli.github.io/Bayesian-Julia/pages/01_why_Julia/ 3 | 4 | # How to represent one-hot vectors? 5 | # We actually just need two things: 6 | # 1. Length 7 | # 2. Which index is "hot" 8 | 9 | # This is a new type that I am creating 10 | struct OneHotVector <: AbstractVector{Int} 11 | len::Int 12 | ind::Int 13 | end 14 | 15 | # Similar to Python's: 16 | # class OneHotVector(AbstractVector[Int]): 17 | # def __init__(len, ind): 18 | # self.len: Int = len 19 | # self.ind: Int = ind 20 | 21 | # Let's add some auxiliary functions 22 | # (similar to methods) 23 | Base.size(v::OneHotVector) = (v.len,) 24 | Base.getindex(v::OneHotVector, i::Integer) = Int(i == v.ind) 25 | 26 | # Since OneHotVector is a struct derived from AbstractVector we can use all of the methods 27 | # previously defined for AbstractVector and it simply works right off the bat. 28 | # Here we are constructing an Array with a list comprehension: 29 | onehot = [OneHotVector(3, rand(1:3)) for _ = 1:4] 30 | 31 | # Define a new function inner_sum that is basically a recursive dot product with a summation: 32 | using LinearAlgebra 33 | 34 | function inner_sum(A, vs) 35 | t = zero(eltype(A)) 36 | for v in vs 37 | t += inner(v, A, v) # multiple dispatch! 38 | end 39 | return t 40 | end 41 | 42 | inner(v, A, w) = v ⋅ (A * w) # very general definition 43 | 44 | # It works! 45 | A = rand(3, 3) 46 | vs = [rand(3) for _ = 1:4] 47 | inner_sum(A, vs) 48 | 49 | # Why? 50 | supertype(OneHotVector) # subtype of AbstractVector 51 | 52 | # But this is still slow: 53 | using BenchmarkTools 54 | 55 | @btime inner_sum($A, $onehot); 56 | 57 | # We can optimize this. 58 | # Multiplying a matrix by a one-hot is pretty much column indexing: 59 | Base.:*(A::AbstractMatrix, v::OneHotVector) = A[:, v.ind] 60 | 61 | # And doing inner on a matrix sandwiched between one-hot vectors is row/column indexing: 62 | inner(v::OneHotVector, A, w::OneHotVector) = A[v.ind, w.ind] 63 | 64 | # Now benchmark again: 65 | @btime inner_sum($A, $onehot); 66 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/01-Why_Julia.md: -------------------------------------------------------------------------------- 1 | # Why Julia? 2 | 3 | > "runs like C but reads like Python", meaning that is blazing fast, easy to prototype and read/write code 4 | 5 | 1. Speed 6 | 1. Multiple Dispatch 7 | 1. Ease of use 8 | 1. Very good built-in Pkg manager 9 | 10 | ## Speed 11 | 12 | ![benchmarks](https://storopoli.github.io/Bayesian-Julia/pages/images/benchmarks.svg) 13 | 14 | ## Ease of Use 15 | 16 | 1. The "two-language" problem 17 | 18 | ## Multiple Dispatch 19 | 20 | A generalization of single-dispatch polymorphism, 21 | "multiple dispatch is a feature that allows a function or method to be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments." 22 | 23 | ## Oh another thing... 24 | 25 | If you need to code something new in R and Python, in order to be fast you'll need to do it in C++ also. 26 | In Julia you can do 100% Julia and it will be faster from the get-go. 27 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/02-Turing.jl: -------------------------------------------------------------------------------- 1 | # Let's generate some fake data from a biased six-sided dice 2 | using Distributions 3 | using Random 4 | 5 | Random.seed!(123) 6 | 7 | dice = Categorical([0.1, 0.1, 0.1, 0.1, 0.1, 0.5]) 8 | 9 | mean(dice) 10 | mode(dice) 11 | 12 | my_data = rand(dice, 1_000) 13 | 14 | # Now let's see how we define a model using Turing 15 | using Turing 16 | 17 | # we use the @model macro to annotate a function 18 | @model function dice_throw(y) 19 | # Inside we have probabilistic statements using ~ 20 | # or deterministic statements using = 21 | 22 | # let's define a random variable p that will act 23 | # as a parameter and a distribution as a prior 24 | p ~ Dirichlet(6, 1) # a uniform prior 25 | 26 | # Since we are supplying y, if we do a ~ 27 | # assignment it will be threated as a likelihood 28 | for i = 1:length(y) 29 | y[i] ~ Categorical(p) 30 | end 31 | end 32 | 33 | # With the model defined, we can instantiate a model with our fake data as y: 34 | my_model = dice_throw(my_data) 35 | 36 | # Now that the model is instantiated, we can use sample() on it. 37 | # sample() takes: 38 | # 1. model 39 | # 2. sampler 40 | # 3. number of samples 41 | # optional. parallel algorithm (multi-threaded or multi-process) 42 | # optional. number of chains 43 | my_chain_mh = sample(my_model, MH(), 1_000) 44 | my_chain_nuts = sample(my_model, NUTS(), 1_000) 45 | 46 | # Example of Sampler: MH, SMC, Gibbs, HMC, NUTS, IS, NestedSampling, Emcee, and the list goes on 47 | 48 | # Multiple dispatch sample with the optional parallel algorithm 49 | # and number of chains 50 | my_chains_mh = sample(my_model, MH(), MCMCThreads(), 1_000, 4) 51 | my_chains_nuts = sample(my_model, NUTS(), MCMCThreads(), 1_000, 4) 52 | 53 | # We can inspect chains with describe(). 54 | # It returns two DataFrames: 55 | # 1. summarystats - summary statistics and diagnostics 56 | # 2. quantile - quantiles 57 | summaries, quantiles = describe(my_chains_nuts) 58 | 59 | # We can also get those separately by calling either 60 | # summarystats() or quantile() 61 | summarystats(my_chains_nuts) 62 | quantile(my_chains_nuts) 63 | 64 | # You can also get the whole Markov chain with DataFrames.jl 65 | using DataFrames 66 | 67 | DataFrame(my_chains_nuts) 68 | 69 | # Visualizations 70 | # We can use MCMCChains.jl to plot chains 71 | using MCMCChains 72 | using StatsPlots 73 | 74 | # there are several visualizations 75 | # check https://turinglang.github.io/MCMCChains.jl/stable/statsplots/ 76 | plot(my_chains_nuts) 77 | 78 | # There are also alternative to plotting 79 | # ArViZ, Makie, etc. -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/03-hierarchical_models.jl: -------------------------------------------------------------------------------- 1 | # Now let's do a more complex model 2 | # Hierarchical Linear Regression 3 | using Turing 4 | using LinearAlgebra: I 5 | using Statistics: mean, std 6 | 7 | @model function varying_intercept( 8 | X, 9 | idx, 10 | y; 11 | n_gr = length(unique(idx)), 12 | predictors = size(X, 2), 13 | ) 14 | # priors 15 | α ~ Normal(0, 2.5) # population-level intercept 16 | β ~ filldist(Normal(0, 2), predictors) # population-level coefficients 17 | σ ~ Exponential(1) # residual SD 18 | 19 | # prior for variance of random intercepts 20 | # usually requires thoughtful specification 21 | τ ~ truncated(Cauchy(0, 2); lower = 0) # group-level SDs intercepts 22 | αⱼ ~ filldist(Normal(0, τ), n_gr) # group-level intercepts 23 | 24 | # likelihood 25 | ŷ = α .+ X * β .+ αⱼ[idx] 26 | y ~ MvNormal(ŷ, σ^2 * I) 27 | end; 28 | 29 | # There's a famous dataset called cheese which is data from cheese ratings. 30 | # A group of 10 rural and 10 urban raters rated 31 | # 4 types of different cheeses (A, B, C and D) in two samples. 32 | using DataFrames 33 | using CSV 34 | 35 | file = joinpath(@__DIR__, "data", "cheese.csv") # compatible with different filesystems 36 | cheese = CSV.read(file, DataFrame) 37 | describe(cheese) 38 | 39 | # A nice shameless plug for data science in Julia is the open book: 40 | # https://juliadatascience.io/ 41 | # In order to prepare the data for Turing, I will convert the Strings in variables cheese 42 | # and background to Ints. Regarding cheese, I will create 4 dummy variables one for each cheese type; 43 | # and background will be converted to integer data taking two values: 44 | # one for each background type. 45 | # 46 | # My intent is to model background as a group for intercepts. 47 | for c in unique(cheese[:, :cheese]) 48 | cheese[:, "cheese_$c"] = ifelse.(cheese[:, :cheese] .== c, 1, 0) 49 | end 50 | 51 | cheese[:, :background_int] = map(cheese[:, :background]) do b 52 | if b == "rural" 53 | 1 54 | elseif b == "urban" 55 | 2 56 | else 57 | missing 58 | end 59 | end 60 | 61 | first(cheese, 5) 62 | 63 | # Now let's us instantiate our model with the data: 64 | X = Matrix(select(cheese, Between(:cheese_A, :cheese_D))) 65 | y = cheese[:, :y] 66 | y_standard = (y .- mean(y)) ./ std(y) 67 | idx = cheese[:, :background_int] 68 | 69 | model_cheese = varying_intercept(X, idx, y_standard) 70 | 71 | # and let's sample from it 72 | chain_cheese = sample(model_cheese, NUTS(), MCMCThreads(), 1_000, 4) 73 | summarystats(chain_cheese) 74 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/04-diagnostics.jl: -------------------------------------------------------------------------------- 1 | # Let's recall our model from the intro to Turing session 2 | using Distributions 3 | using Random 4 | using Turing 5 | 6 | Random.seed!(123) 7 | dice = Categorical([0.1, 0.1, 0.1, 0.1, 0.1, 0.5]) 8 | my_data = rand(dice, 1_000) 9 | # Different more efficient syntax 10 | @model function dice_throw(N) 11 | p ~ Dirichlet(6, 1) # a uniform prior 12 | y ~ filldist(Categorical(p), N) 13 | end 14 | # instantating a a model 15 | my_model = dice_throw(1_000) 16 | # "condition" the model with data 17 | my_model_cond = my_model | (y = my_data,) 18 | my_chains_nuts = sample(my_model_cond, NUTS(), MCMCThreads(), 1_000, 2) 19 | 20 | # You can get a summary of the internals with summary statistics mean and quantile 21 | # using kwarg sections=:internals 22 | mean(my_chains_nuts; sections = :internals) 23 | quantile(my_chains_nuts; sections = :internals) 24 | 25 | # Now for diagnostics. 26 | # All of the diagnostics are available in MCMCDiagnostics.jl 27 | # Check them in: https://turinglang.github.io/MCMCDiagnosticTools.jl/stable/ 28 | # MCMCDiagnostics is automatically loaded with Turing 29 | ess_rhat(my_chains_nuts) 30 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/05-predictive_checks.jl: -------------------------------------------------------------------------------- 1 | # Let's recall our model from the intro to Turing session 2 | using Distributions 3 | using Random 4 | using Turing 5 | 6 | Random.seed!(123) 7 | dice = Categorical([0.1, 0.1, 0.1, 0.1, 0.1, 0.5]) 8 | my_data = rand(dice, 1_000) 9 | 10 | @model function dice_throw(N) 11 | p ~ Dirichlet(6, 1) # a uniform prior 12 | y ~ filldist(Categorical(p), N) 13 | end 14 | # instantating a a model 15 | my_model = dice_throw(1_000) 16 | # "condition" the model with data 17 | my_model_cond = my_model | (y = my_data,) 18 | my_chains_nuts = sample(my_model_cond, NUTS(), MCMCThreads(), 1_000, 2) 19 | 20 | # Prior Check 21 | # just use the Prior() sampler: 22 | # (computational advantages) 23 | prior_chain = sample(my_model_cond, Prior(), 2_000) 24 | 25 | # This is a regular MCMCChains object, 26 | # you can plot it: 27 | using MCMCChains 28 | using StatsPlots 29 | plot(prior_chain) 30 | 31 | # Prior Predictive Check 32 | # we just use the "unconditioned model": 33 | # Now we call predict() on the predictive model and the prior samples: 34 | prior_check = predict(my_model, prior_chain) 35 | summarystats(prior_check[:, 1:5, :]) # just the first 5 prior samples 36 | 37 | # Posterior Predictive Checks 38 | # the same ideia but not we use the normal chain and not the prior chain: 39 | posterior_check = predict(my_model, my_chains_nuts) 40 | summarystats(posterior_check[:, 1:5, :]) # just the first 5 posterior samples 41 | # You can also plot 42 | plot(posterior_check[:, 1:3, 1]) # just the first 3 posterior samples of a single chain -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 3 | CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 4 | DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 5 | DataFramesMeta = "1313f7d8-7da2-5740-9ea0-a2ca25f37964" 6 | Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" 7 | LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 8 | MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" 9 | Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 10 | Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 11 | StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" 12 | Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/data/cheese.csv: -------------------------------------------------------------------------------- 1 | "cheese","rater","background","y" 2 | "A",1,"rural",67 3 | "A",1,"rural",66 4 | "B",1,"rural",51 5 | "B",1,"rural",53 6 | "C",1,"rural",75 7 | "C",1,"rural",70 8 | "D",1,"rural",68 9 | "D",1,"rural",66 10 | "A",2,"rural",76 11 | "A",2,"rural",76 12 | "B",2,"rural",56 13 | "B",2,"rural",65 14 | "C",2,"rural",82 15 | "C",2,"rural",82 16 | "D",2,"rural",81 17 | "D",2,"rural",77 18 | "A",3,"rural",80 19 | "A",3,"rural",84 20 | "B",3,"rural",71 21 | "B",3,"rural",67 22 | "C",3,"rural",82 23 | "C",3,"rural",82 24 | "D",3,"rural",71 25 | "D",3,"rural",70 26 | "A",4,"rural",79 27 | "A",4,"rural",83 28 | "B",4,"rural",63 29 | "B",4,"rural",60 30 | "C",4,"rural",80 31 | "C",4,"rural",83 32 | "D",4,"rural",66 33 | "D",4,"rural",71 34 | "A",5,"rural",74 35 | "A",5,"rural",72 36 | "B",5,"rural",55 37 | "B",5,"rural",58 38 | "C",5,"rural",76 39 | "C",5,"rural",79 40 | "D",5,"rural",75 41 | "D",5,"rural",74 42 | "A",6,"rural",66 43 | "A",6,"rural",71 44 | "B",6,"rural",49 45 | "B",6,"rural",49 46 | "C",6,"rural",72 47 | "C",6,"rural",70 48 | "D",6,"rural",65 49 | "D",6,"rural",65 50 | "A",7,"rural",63 51 | "A",7,"rural",68 52 | "B",7,"rural",38 53 | "B",7,"rural",33 54 | "C",7,"rural",72 55 | "C",7,"rural",71 56 | "D",7,"rural",70 57 | "D",7,"rural",72 58 | "A",8,"rural",67 59 | "A",8,"rural",60 60 | "B",8,"rural",56 61 | "B",8,"rural",48 62 | "C",8,"rural",72 63 | "C",8,"rural",70 64 | "D",8,"rural",67 65 | "D",8,"rural",69 66 | "A",9,"rural",59 67 | "A",9,"rural",65 68 | "B",9,"rural",41 69 | "B",9,"rural",48 70 | "C",9,"rural",61 71 | "C",9,"rural",70 72 | "D",9,"rural",57 73 | "D",9,"rural",59 74 | "A",10,"rural",72 75 | "A",10,"rural",64 76 | "B",10,"rural",55 77 | "B",10,"rural",57 78 | "C",10,"rural",78 79 | "C",10,"rural",78 80 | "D",10,"rural",72 81 | "D",10,"rural",67 82 | "A",1,"urban",67 83 | "A",1,"urban",64 84 | "B",1,"urban",44 85 | "B",1,"urban",54 86 | "C",1,"urban",75 87 | "C",1,"urban",83 88 | "D",1,"urban",64 89 | "D",1,"urban",60 90 | "A",2,"urban",83 91 | "A",2,"urban",85 92 | "B",2,"urban",60 93 | "B",2,"urban",63 94 | "C",2,"urban",91 95 | "C",2,"urban",90 96 | "D",2,"urban",67 97 | "D",2,"urban",69 98 | "A",3,"urban",74 99 | "A",3,"urban",75 100 | "B",3,"urban",58 101 | "B",3,"urban",54 102 | "C",3,"urban",77 103 | "C",3,"urban",80 104 | "D",3,"urban",64 105 | "D",3,"urban",66 106 | "A",4,"urban",81 107 | "A",4,"urban",84 108 | "B",4,"urban",58 109 | "B",4,"urban",62 110 | "C",4,"urban",86 111 | "C",4,"urban",90 112 | "D",4,"urban",71 113 | "D",4,"urban",67 114 | "A",5,"urban",83 115 | "A",5,"urban",79 116 | "B",5,"urban",54 117 | "B",5,"urban",57 118 | "C",5,"urban",91 119 | "C",5,"urban",82 120 | "D",5,"urban",78 121 | "D",5,"urban",84 122 | "A",6,"urban",84 123 | "A",6,"urban",82 124 | "B",6,"urban",65 125 | "B",6,"urban",61 126 | "C",6,"urban",80 127 | "C",6,"urban",87 128 | "D",6,"urban",78 129 | "D",6,"urban",84 130 | "A",7,"urban",89 131 | "A",7,"urban",84 132 | "B",7,"urban",70 133 | "B",7,"urban",76 134 | "C",7,"urban",79 135 | "C",7,"urban",89 136 | "D",7,"urban",89 137 | "D",7,"urban",91 138 | "A",8,"urban",80 139 | "A",8,"urban",80 140 | "B",8,"urban",62 141 | "B",8,"urban",56 142 | "C",8,"urban",85 143 | "C",8,"urban",86 144 | "D",8,"urban",77 145 | "D",8,"urban",79 146 | "A",9,"urban",74 147 | "A",9,"urban",73 148 | "B",9,"urban",56 149 | "B",9,"urban",61 150 | "C",9,"urban",78 151 | "C",9,"urban",79 152 | "D",9,"urban",77 153 | "D",9,"urban",82 154 | "A",10,"urban",77 155 | "A",10,"urban",82 156 | "B",10,"urban",64 157 | "B",10,"urban",57 158 | "C",10,"urban",86 159 | "C",10,"urban",86 160 | "D",10,"urban",81 161 | "D",10,"urban",83 162 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/images/01-what-you-see.dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-1-Intro_Turing/images/01-what-you-see.dot.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/images/02-what-turinglang-is.dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-1-Intro_Turing/images/02-what-turinglang-is.dot.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/images/03-the-main-packages-you-use-with-turing.dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-1-Intro_Turing/images/03-the-main-packages-you-use-with-turing.dot.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-1-Intro_Turing/images/04-samplers.dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-1-Intro_Turing/images/04-samplers.dot.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/Project.toml: -------------------------------------------------------------------------------- 1 | name = "GeiloWinterSchool2023Part2" 2 | uuid = "66107438-c220-4dd3-9f45-fde6aa9dc0eb" 3 | authors = ["Tor Erlend Fjelde "] 4 | version = "0.1.0" 5 | 6 | [deps] 7 | BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 8 | CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 9 | DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 10 | DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" 11 | DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" 12 | Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" 13 | ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" 14 | LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c" 15 | LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1" 16 | Optim = "429524aa-4258-5aef-a3af-852621145aeb" 17 | ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7" 18 | ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" 19 | SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" 20 | SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" 21 | StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" 22 | Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" 23 | TuringBenchmarking = "0db1332d-5c25-4deb-809f-459bc696f94f" 24 | Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" 25 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/README.md: -------------------------------------------------------------------------------- 1 | # Geilo Winter School 2023 (Part 2): Bayesian inference and other things with the `TuringLang` ecosystem 2 | 3 | Here you find the presentation in several different formats: 4 | - `notes.jl`: Julia file which you can just step through and execute. Nice if you're in VS Code. 5 | - `notes.md`: A markdown file which comes with more context than the Julia file, easily readable in Github itself. 6 | - `notes.ipynb`: A Jupyter notebook version of the markdown file. 7 | - `notes.org`: A Org-mode file, which is the source of all the files above and probably the easiest to read on Github, _but_ not supported by most editors (unless you're using Emacs). 8 | 9 | Pick your poison, and follow along! 10 | 11 | The presentation itself can be viewed online here: https://torfjelde.github.io/presentations/2023-01-25-Geilo-Winter-School/ 12 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_14-46-02_Screenshot_20230118_144454.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_14-46-02_Screenshot_20230118_144454.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_14-49-24_471337_3317365246956_1262712540_o.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_14-49-24_471337_3317365246956_1262712540_o.jpg -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_14-57-08_Preikestolen-plateau-Go-Fjords-Bob-Engelsen-P1026771_kljg5o.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_14-57-08_Preikestolen-plateau-Go-Fjords-Bob-Engelsen-P1026771_kljg5o.jpeg -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_15-31-28_Screenshot_20230118_153122.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-18_15-31-28_Screenshot_20230118_153122.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-19_19-48-23_Screenshot_20230119_194737.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-19_19-48-23_Screenshot_20230119_194737.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-19_19-48-41_Screenshot_20230119_194838.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-19_19-48-41_Screenshot_20230119_194838.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-19_19-56-00_sir_illu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-19_19-56-00_sir_illu.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-22_12-09-24_Screenshot_20230122_120848.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-22_12-09-24_Screenshot_20230122_120848.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-22_12-30-07_Screenshot_20230122_122936.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-22_12-30-07_Screenshot_20230122_122936.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_01-01-31_Screenshot_20230125_010111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_01-01-31_Screenshot_20230125_010111.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_01-16-13_Screenshot_20230125_011603.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_01-16-13_Screenshot_20230125_011603.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_01-29-05_Screenshot_20230125_012853.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_01-29-05_Screenshot_20230125_012853.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_02-23-23_Screenshot_20230125_022312.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_02-23-23_Screenshot_20230125_022312.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_20-50-11_tensorboard_demo_histograms_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-25_20-50-11_tensorboard_demo_histograms_screen.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-26_15-47-56_Screenshot_20230126_154747.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-26_15-47-56_Screenshot_20230126_154747.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-26_15-49-34_EMS144307-f009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-26_15-49-34_EMS144307-f009.jpg -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-27_21-50-51_tensorboard_demo_histograms_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-27_21-50-51_tensorboard_demo_histograms_screen.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-27_21-51-28_tensorboard_demo_histograms_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/attachments/2023-01-27_21-51-28_tensorboard_demo_histograms_screen.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/0d84524cc352ba3471bf66156dda0a8cfd1e66b9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/0d84524cc352ba3471bf66156dda0a8cfd1e66b9.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/1265078e6dd1a955c2c437e28a8d0d0904467ad2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/1265078e6dd1a955c2c437e28a8d0d0904467ad2.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/145ba1245ad3196f9bb3001254df5b3cf451e778.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/145ba1245ad3196f9bb3001254df5b3cf451e778.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/18165db131e144d97419fbf55851e2b6a13633b4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/18165db131e144d97419fbf55851e2b6a13633b4.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/2a32e4a8ec0b064306909a4bc887c7f0baab4650.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/2a32e4a8ec0b064306909a4bc887c7f0baab4650.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/2e1579a71c4072534cabe41a233cd283543874e6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/2e1579a71c4072534cabe41a233cd283543874e6.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/34a52974b7bc0320bef1eb49f39ea8875566a0b3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/34a52974b7bc0320bef1eb49f39ea8875566a0b3.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/3f7598c6f0df5f376d9f89161504465787bacf25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/3f7598c6f0df5f376d9f89161504465787bacf25.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/53d5bcefafe0e9c29e4ee0f560bc2648d8b43118.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/53d5bcefafe0e9c29e4ee0f560bc2648d8b43118.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/62ee261fb15aead33e6f49e82e01a7482b56017f.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/62ee261fb15aead33e6f49e82e01a7482b56017f.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/6326dbbbf15b88051f47afcbeb63f6b3c64accd5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/6326dbbbf15b88051f47afcbeb63f6b3c64accd5.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/76b1f4f528c5027a5c432ed6f8a42d20057833ca.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/76b1f4f528c5027a5c432ed6f8a42d20057833ca.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/7763ba1e7b6bb223fde4d453cab14cbcf723b75a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/7763ba1e7b6bb223fde4d453cab14cbcf723b75a.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/780e8cc1c00812abcfba1fe711f0a8b7b168b7e7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/780e8cc1c00812abcfba1fe711f0a8b7b168b7e7.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/80c76ad5d3151ec0f3e044a075b2a5b88956d14b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/80c76ad5d3151ec0f3e044a075b2a5b88956d14b.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/856d2889f03822cd9dffef93e27f71c0c4c4c645.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/856d2889f03822cd9dffef93e27f71c0c4c4c645.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/870b8fafd890396444043fd96c7d303fb7722627.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/870b8fafd890396444043fd96c7d303fb7722627.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/a1b75cd5d378febc7029c1ed63db2831e09ab9ee.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/a1b75cd5d378febc7029c1ed63db2831e09ab9ee.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/ba6a57881c744560992e6aeb0ff47cfb9aaf9238.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/ba6a57881c744560992e6aeb0ff47cfb9aaf9238.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/bc9d6892a46d578ee1b80fb8d98ab74a44e0e9ce.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/bc9d6892a46d578ee1b80fb8d98ab74a44e0e9ce.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/be1c12fbb76e6f039cc149b75842d8ee461b8f89.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/be1c12fbb76e6f039cc149b75842d8ee461b8f89.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/cffea7a80541e6f61fd182de85cb18bc8520b0b4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/cffea7a80541e6f61fd182de85cb18bc8520b0b4.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/ed3058dc552f72dbe5151feb938d103b75251f63.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/ed3058dc552f72dbe5151feb938d103b75251f63.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/f110b08ba2b09629fd9d3c498a45a8ab21e0915a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/f110b08ba2b09629fd9d3c498a45a8ab21e0915a.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/f64dea8a5533f00397817ee6703ff49dcb105c25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/f64dea8a5533f00397817ee6703ff49dcb105c25.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/f8f05d979000abd498ef90389bec61e543f004f3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/assets/outputs/f8f05d979000abd498ef90389bec61e543f004f3.png -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/data/influenza_england_1978_school.csv: -------------------------------------------------------------------------------- 1 | "","date","in_bed","convalescent" 2 | "1",1978-01-22,3,0 3 | "2",1978-01-23,8,0 4 | "3",1978-01-24,26,0 5 | "4",1978-01-25,76,0 6 | "5",1978-01-26,225,9 7 | "6",1978-01-27,298,17 8 | "7",1978-01-28,258,105 9 | "8",1978-01-29,233,162 10 | "9",1978-01-30,189,176 11 | "10",1978-01-31,128,166 12 | "11",1978-02-01,68,150 13 | "12",1978-02-02,29,85 14 | "13",1978-02-03,14,47 15 | "14",1978-02-04,4,20 16 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/negative_binomial.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-Geilo-Winter-School/Part-2-Turing-and-other-things/negative_binomial.gif -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/Part-2-Turing-and-other-things/src/GeiloWinterSchool2023Part2.jl: -------------------------------------------------------------------------------- 1 | module GeiloWinterSchool2023Part2 2 | 3 | using DiffEqBase 4 | using Turing 5 | using StatsPlots 6 | using DataFrames 7 | using SciMLSensitivity 8 | 9 | using ReverseDiff: ReverseDiff 10 | 11 | export to_chains, plot_trajectories!, plot_trajectories, plot_trajectory_quantiles!, plot_trajectory_quantiles, issuccess 12 | 13 | 14 | # HACK: https://github.com/JuliaDiff/ReverseDiff.jl/pull/216 15 | if isdefined(ReverseDiff, :supports_linear_indexing) 16 | ReverseDiff.supports_linear_indexing(::DiffEqBase.ODESolution) = true 17 | 18 | import DiffEqBase: solve_up 19 | ReverseDiff.@grad function solve_up(prob, sensealg, u0, p, args...; kwargs...) 20 | out = DiffEqBase._solve_adjoint(prob, sensealg, ReverseDiff.value(u0), 21 | ReverseDiff.value(p), 22 | SciMLBase.ReverseDiffOriginator(), args...; kwargs...) 23 | function actual_adjoint(_args...) 24 | original_adjoint = out[2](_args...) 25 | if isempty(args) # alg is missing 26 | tuple(original_adjoint[1:4]..., original_adjoint[6:end]...) 27 | else 28 | original_adjoint 29 | end 30 | end 31 | 32 | out[1], actual_adjoint 33 | end 34 | end 35 | 36 | # Improve `show` a bit for the sake of presentation and demonstration. 37 | function Base.show(io::IO, model::DynamicPPL.Model) 38 | println(io, "Model(") 39 | print(io, " args = ") 40 | println(io, keys(model.args)) 41 | print(io, " defaults = ") 42 | println(io, keys(model.defaults)) 43 | print(io, " context = ") 44 | println(io, model.context) 45 | print(io, ")") 46 | end 47 | 48 | # Utility method for converting a `Vector{<:NamedTuple}` to a `Chains` object. 49 | function unravel(x::NamedTuple, vns=Iterators.map(DynamicPPL.VarName, keys(x))) 50 | vn_iter = Iterators.flatten(DynamicPPL.TestUtils.varname_leaves(vn, get(x, vn)) for vn in vns) 51 | return vn_iter, Iterators.map(Base.Fix1(get, x), vn_iter) 52 | end 53 | 54 | """ 55 | to_chains(results; exclude, include) 56 | 57 | Return a `MCMCChains.Chains` constructed from `results`. 58 | 59 | If `exclude` is specified, then those keys will be excluded. 60 | If `include` is specified, then those keys will be included. 61 | """ 62 | function to_chains(results::AbstractVector{<:NamedTuple}; exclude=nothing, include=nothing) 63 | @assert !isempty(results) "input is empty" 64 | 65 | # TODO: Handle ragged arrays. 66 | # Probably best done my first just mapping everything to flatten dictionaries, e.g. 67 | # 68 | # x_vns, x_vals = unravel(x) 69 | # OrderedDict(zip(map(Symbol, x_vns), x_vals)) 70 | # 71 | # (or using Dictionaries.jl for better perf), and then basically `hcat`ing these 72 | # and insertin `missing` where necessary. 73 | 74 | example = first(results) 75 | 76 | # Construct names once. 77 | syms = collect(keys(example)) 78 | # Filter out if desired. 79 | if !isnothing(include) 80 | syms = filter(∈(include), syms) 81 | end 82 | if !isnothing(exclude) 83 | syms = filter(∉(exclude), syms) 84 | end 85 | 86 | @assert !isempty(syms) "symbols to include is empty" 87 | 88 | # Convert to `VarName`. 89 | names = collect(first(unravel(example, map(DynamicPPL.VarName, syms)))) 90 | 91 | # Extract the values. 92 | vals = mapreduce(hcat, results) do x 93 | # NOTE: Providing the `names` here assumes every sample has the same variables. 94 | collect(last(unravel(x, names))) 95 | end 96 | 97 | @assert !isempty(vals) "resulting array is empty" 98 | 99 | vals_transposed = transpose(vals) 100 | # Shape: iterations × num_vars × num_chains 101 | chain_array = reshape(vals_transposed, size(vals_transposed)..., 1) 102 | return MCMCChains.Chains(chain_array, names) 103 | end 104 | 105 | function to_chains(results::AbstractMatrix{<:NamedTuple}; kwargs...) 106 | return reduce(MCMCChains.chainscat, map(col -> to_chains(col; kwargs...), eachcol(results));) 107 | end 108 | 109 | # HACK: https://github.com/TuringLang/DynamicPPL.jl/pull/452 110 | function DynamicPPL.TestUtils.varname_leaves(vn::DynamicPPL.VarName, val::NamedTuple) 111 | iter = Iterators.map(keys(val)) do sym 112 | lens = DynamicPPL.Setfield.PropertyLens{sym}() 113 | DynamicPPL.TestUtils.varname_leaves(vn ∘ lens, get(val, lens)) 114 | end 115 | return Iterators.flatten(iter) 116 | end 117 | 118 | # Some plotting utilities. 119 | """ 120 | plot_trajectories!(p::Plots.Plot, chain::MCMCChains.Chains; kwargs...) 121 | 122 | Plot trajectories in `chain`. 123 | 124 | # Keyword arguments 125 | n = 1000, trajectory_color="#BBBBBB", alpha=1/√n 126 | - `n`: number of trajectories to sample. Default: 1000. 127 | - `trajectory_color`: color of the trajectories to use. Default: "#BBBBBB". 128 | - `alpha`: alpha to use when plotting the trajectories. Default: `1/√n`. 129 | - `data`: include the data in the plot. Default: `nothing`. 130 | - `tspan`: 2-tuple containing first and last time represented by `chain`. Default: (1, 14) 131 | """ 132 | function plot_trajectories!( 133 | p::Plots.Plot, chain::MCMCChains.Chains; 134 | n = 1000, data=nothing, tspan=(1, 14), 135 | trajectory_color="#BBBBBB", alpha=1/√n 136 | ) 137 | # Convert the `chain` into a flattened `Array`. 138 | chain_arr = Array(chain; append_chains=true) # Shape: [num_chains × num_iterations, num_params] 139 | # Sample a subset of indices to use. 140 | total_num_samples = size(chain_arr, 1) 141 | indices = rand(1:total_num_samples, n) # Shape: [n, ] 142 | # Plot 'em! 143 | chain_arr = chain_arr[indices, :] # Shape: [n, num_params] 144 | for in_bed in eachrow(chain_arr) 145 | plot!(p, tspan[1]:tspan[2], in_bed, alpha=alpha, color=trajectory_color) 146 | end 147 | 148 | if !isnothing(data) 149 | scatter!(p, 1:14, data.in_bed, label="Data", color="black") 150 | end 151 | 152 | return p 153 | end 154 | 155 | """ 156 | plot_trajectories(chain::MCMCChains.Chains; kwargs...) 157 | 158 | Plot trajectories in `chain`. 159 | 160 | See [`plot_trajectories!`](@ref) for more info. 161 | """ 162 | plot_trajectories(chain::MCMCChains.Chains; kwargs...) = plot_trajectories!(plot(), chain; kwargs...) 163 | 164 | """ 165 | plot_trajectory_quantiles!(p::Plots.Plot, chain::MCMCChains.Chains; kwargs...) 166 | 167 | Plot quantiles of trajectories in `chain`. 168 | 169 | By default, the 95% quantiles are used. 170 | 171 | # Keyword arguments 172 | - q: quantiles to compute (only three ordered values supported). Default: `[0.025, 0.5, 0.975]`. 173 | - `data`: if specified, include the data in the plot. Default: `nothing`. 174 | - `tspan`: 2-tuple containing first and last time represented by `chain`. Default: (1, 14) 175 | """ 176 | function plot_trajectory_quantiles!( 177 | p::Plots.Plot, chain::MCMCChains.Chains; 178 | q=[0.025, 0.5, 0.975], data=nothing, tspan=(1, 14) 179 | ) 180 | # Compute quantiles. 181 | qs = Array(DataFrame(quantile(chain; q = q))[:, Not(:parameters)]); 182 | 183 | # Plot! 184 | num_timesteps = size(qs, 1) 185 | lb, mid, ub = qs[:, 1], qs[:, 2], qs[:, 3] 186 | plot!(p, tspan[1]:tspan[2], mid, ribbon=(mid - lb, ub - mid)) 187 | 188 | if !isnothing(data) 189 | scatter!(p, 1:14, data.in_bed, label="Data", color="black") 190 | end 191 | 192 | return p 193 | end 194 | 195 | 196 | """ 197 | plot_trajectory_quantiles(chain::MCMCChains.Chains; kwargs...) 198 | 199 | Plot quantiles of trajectories in `chain`. 200 | 201 | See [`plot_trajectory_quantiles!`](@ref) for more info. 202 | """ 203 | plot_trajectory_quantiles(chain::MCMCChains.Chains; kwargs...) = plot_trajectory_quantiles!(plot(), chain; kwargs...) 204 | 205 | # Utility method to check if we're happy with ODE solution. 206 | """ 207 | issuccess(sol) 208 | 209 | Return `true` if the ODE solution `sol` is successful, and `false` otherwise. 210 | """ 211 | function issuccess(sol) 212 | # Some AD frameworks use their custom types to wrap arrays. 213 | # Hence we just need to unwrap before we access the `retcode`. 214 | # Thanks to multiple dispatch, this is a no-op in the case where 215 | # there's no need for unwrapping. 216 | sol_unwrapped = DiffEqBase.value(sol) 217 | # `Terminated` can occur if we give the solver a callback-function which 218 | # dynamically decides whether or not to terminate. `Success` is, well, success. 219 | return (sol_unwrapped.retcode === ReturnCode.Success) || (sol_unwrapped.retcode === ReturnCode.Terminated) 220 | end 221 | 222 | # HACK: Don't need to diff through `issuccess`, so we just drop the tracking. 223 | issuccess(sol::ReverseDiff.TrackedArray) = issuccess(ReverseDiff.value(sol)) 224 | issuccess(sol::AbstractArray{<:ReverseDiff.TrackedReal}) = issuccess(ReverseDiff.value.(sol)) 225 | 226 | end # module GeiloWinterSchool2023Part2 227 | -------------------------------------------------------------------------------- /2023-Geilo-Winter-School/README.md: -------------------------------------------------------------------------------- 1 | # 2023 Geilo Winter School - Turing Workshop 2 | 3 | Geilo Winter School, Geilo Norway, 2023-01-22 to 2023-01-27. 4 | 5 | ## Instructors 6 | 7 | - [Jose Storopoli](https://github.com/storopoli) 8 | - [Tor Erlend Fjelde](https://github.com/torfjelde) 9 | 10 | ## Contents 11 | 12 | 1. Julia/Turing Basics (90 mins) 13 | 14 | - Quick intro to Julia 15 | - Multiple Dispatch: Motivating example 16 | - Basics of Turing 17 | - Overview of ecosystem and packages 18 | 19 | 1. Using Turing in practice (90 mins) 20 | 21 | - How to define Bayesian/generative models 22 | - Different MCMC samplers 23 | - What to do after sampling (post-processing) 24 | - Diagnostics 25 | - Predictive Checks 26 | 27 | 1. Advanced Turing usage (90 mins) 28 | 29 | - Performance of Turing models 30 | - Debugging Turing of models 31 | - ODE in Turing models 32 | 33 | 1. Cutting-edge MCMC algorithms research with Turing (90 mins) 34 | 35 | - How to use the exposed interfaces in the Turing ecosystem 36 | - Case example of Parallel MCMC Tempering 37 | 38 | ## Getting set up with Julia 39 | 40 | See [setting_up_julia.md](../setting_up_julia.md) for how to get set up with Julia. 41 | 42 | ## Datasets 43 | 44 | - `cheese` (hierarchical models): data from cheese ratings. 45 | A group of 10 rural and 10 urban raters rated 4 types of different cheeses (A, B, C and D) in two samples. 46 | Source: Boatwright, P., McCulloch, R., & Rossi, P. (1999). Account-level modeling for trade promotion: An application of a constrained parameter hierarchical model. _Journal of the American Statistical Association_, 94(448), 1063–1073. 47 | -------------------------------------------------------------------------------- /2023-Intro-Turing/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/julia 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=julia 3 | 4 | ### Julia ### 5 | # Files generated by invoking Julia with --code-coverage 6 | *.jl.cov 7 | *.jl.*.cov 8 | 9 | # Files generated by invoking Julia with --track-allocation 10 | *.jl.mem 11 | 12 | # System-specific files and directories generated by the BinaryProvider and BinDeps packages 13 | # They contain absolute paths specific to the host computer, and so should not be committed 14 | deps/deps.jl 15 | deps/build.log 16 | deps/downloads/ 17 | deps/usr/ 18 | deps/src/ 19 | 20 | # Build artifacts for creating documentation generated by the Documenter package 21 | docs/build/ 22 | docs/site/ 23 | 24 | # File generated by Pkg, the package manager, based on a corresponding Project.toml 25 | # It records a fixed state of all packages used by the project. As such, it should not be 26 | # committed for packages, but should be committed for applications that require a static 27 | # environment. 28 | Manifest.toml 29 | 30 | # End of https://www.toptal.com/developers/gitignore/api/julia 31 | 32 | # Quarto 33 | /.quarto/ 34 | intro-turing_files/ 35 | .jupyter_cache/ 36 | *.ipynb 37 | 38 | # Python venv 39 | /env/ 40 | pyproject.toml 41 | 42 | # LaTeX stuff 43 | *.aux 44 | *.fdb_latexmk 45 | *.fls 46 | *.log 47 | 48 | -------------------------------------------------------------------------------- /2023-Intro-Turing/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 3 | CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 4 | DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 5 | DataFramesMeta = "1313f7d8-7da2-5740-9ea0-a2ca25f37964" 6 | Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" 7 | LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02" 8 | LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 9 | Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 10 | Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" 11 | StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" 12 | Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" 13 | -------------------------------------------------------------------------------- /2023-Intro-Turing/README.md: -------------------------------------------------------------------------------- 1 | # 2023 Bayesian Data Analysis Meetup Group - Introduction to Turing 2 | 3 | Bayesian Data Analysis Meetup Group, Online, 2023-02-23. 4 | 5 | Link: 6 | 7 | ## Instructors 8 | 9 | - [Jose Storopoli](https://github.com/storopoli) 10 | 11 | ## Contents 12 | 13 | 1. Define a Model --- Basic PPL (Probabilistic Programming Language) syntax 14 | 1. Deterministics and probabilistic assignments 15 | 1. avoiding `for`-loops and efficient coding 16 | 1. Fit a model 17 | 1. MCMC sampler choice 18 | 1. Multi-threaded and Multiprocess 19 | 1. Inspecting chains --- built-in functionality 20 | 1. Visualizing chains --- built-in functionality 21 | 22 | ## Getting set up with Julia 23 | 24 | See [setting_up_julia.md](../setting_up_julia.md) for how to get set up with Julia. 25 | 26 | ## Datasets 27 | 28 | - `wells` (logistic regression): Data from a survey of 3,200 residents in a small 29 | area of Bangladesh suffering from arsenic contamination of groundwater. 30 | Respondents with elevated arsenic levels in their wells had been encouraged 31 | to switch their water source to a safe public or private well in the nearby 32 | area and the survey was conducted several years later to learn which of the 33 | affected residents had switched wells. 34 | Source: Gelman, A., & Hill, J. (2007). Data analysis using regression and 35 | multilevel/hierarchical models. Cambridge university press. 36 | -------------------------------------------------------------------------------- /2023-Intro-Turing/intro-turing.qmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Intro to Turing.jl" 3 | author: "Jose Storopoli, PhD" 4 | format: 5 | revealjs: 6 | slide-number: true 7 | transition: slide 8 | chalkboard: 9 | buttons: false 10 | preview-links: auto 11 | footer: 12 | logo: images/logo.svg 13 | callout-appearance: minimal 14 | execute: 15 | echo: true 16 | cache: true 17 | bibliography: references.bib 18 | jupyter: julia-1.8 19 | --- 20 | 21 | ```{julia} 22 | #| echo: false 23 | #| output: false 24 | using Turing 25 | using Distributions 26 | using StatsPlots 27 | using CSV 28 | using DataFramesMeta 29 | using LazyArrays 30 | using BenchmarkTools 31 | using Random: seed! 32 | setprogress!(false) # hide 33 | seed!(123) 34 | ``` 35 | 36 | ## Agenda {.scrollable} 37 | 38 | ::: incremental 39 | 1. Define a Model --- Basic PPL (Probabilistic Programming Language) syntax 40 | 1. Deterministics and probabilistic assignments 41 | 1. avoiding `for`-loops and efficient coding 42 | 1. Fit a model 43 | 1. MCMC sampler choice 44 | 1. Multi-threaded and Multiprocess 45 | 1. Inspecting chains --- built-in functionality 46 | 1. Visualizing chains --- built-in functionality 47 | ::: 48 | 49 | ## What we are not covering 50 | 51 | But I highly suggest you learn more about: 52 | 53 |
54 | 55 | ::: incremental 56 | - [Posterior/Prior predictive checks](https://storopoli.github.io/Bayesian-Julia/pages/04_Turing/) 57 | - [Increment log of the joint probability](https://github.com/TuringLang/TuringExamples/blob/master/benchmarks/hmm_semisup/turing.jl), _e.g._ Hidden Markov Models 58 | - [Incorporating a ODE system into your model](https://storopoli.github.io/Bayesian-Julia/pages/13_epi_models/) 59 | - [Data optimizations such as centering and/or QR decomposition](https://storopoli.github.io/Bayesian-Julia/pages/12_Turing_tricks/) 60 | ::: 61 | 62 | 63 | ## PPL (Probabilistic Programming Language) 64 | 65 | 66 | It is a programming paradigm in which probabilistic models are specified and inference for these models is performed automatically [@Hardesty_Technology] 67 | 68 | . . . 69 | 70 | In more clear terms, PP and PP Languages (PPLs) allows us to specify variables as random variables (like `Normal`, `Binominal` etc.) with known or unknown parameters. 71 | 72 | . . . 73 | 74 | Then, we construct a model using these variables by specifying how the variables related to each other, and finally automatic inference of the variables' unknown parameters is then performed. 75 | 76 | ## PPL (Probabilistic Programming Language) 77 | 78 | In a Bayesian approach this means specifying priors, likelihoods and letting the PPL compute the posterior. 79 | 80 | . . . 81 | 82 | Since the denominator in the posterior is often intractable, we use Markov Chain Monte Carlo and some fancy sampler algorithm to approximate the posterior. 83 | 84 | . . . 85 | 86 | This involves, besides a suitable PPL, automatic differentiation, MCMC chains interface, and also an efficient HMC algorithm implementation. 87 | 88 | ## What is `Turing.jl` 89 | 90 | In order to provide all of these features, Turing has a whole ecosystem to address each and every one of these components. 91 | 92 | ::: incremental 93 | - [`Turing.jl`](https://github.com/TuringLang/Turing.jl) [@Ge_Xu_Ghahramani_2018] 94 | - [`MCMCChains.jl`](https://github.com/TuringLang/MCMCChains.jl) 95 | - [`DynamicPPL.jl`](https://github.com/TuringLang/DynamicPPL.jl) [@Tarek_Xu_Trapp_Ge_Ghahramani_2020] 96 | - [`AdvancedHMC.jl`](https://github.com/TuringLang/AdvancedHMC.jl) [@Xu_Ge_Tebbutt_Tarek_Trapp_Ghahramani_2020] 97 | - [`DistributionsAD.jl`](https://github.com/TuringLang/DistributionsAD.jl) 98 | - [`Bijectors.jl`](https://github.com/TuringLang/Bijectors.jl) 99 | ::: 100 | 101 | ## What is `Turing.jl` {.scrollable} 102 | 103 | `Turing.jl` [@Ge_Xu_Ghahramani_2018] is the main package in the Turing ecosystem and the backbone that glues all the other packages together. 104 | We specify the model inside a macro `@model` where we can assign variables in two ways: 105 | 106 | ::: incremental 107 | - using `~`: which means that a variable follows some probability distribution (Normal, Binomial etc.) and its value is random under that distribution 108 | - using `=`: which means that a variable does not follow a probability distribution and its value is deterministic (like the normal `=` assignment in programming languages) 109 | ::: 110 | 111 | 112 | ## Example --- Dice Throw 113 | 114 | ```{julia} 115 | #| output: false 116 | using Turing 117 | 118 | @model function dice_throw(y) 119 | p ~ Dirichlet(6, 1) 120 | 121 | for i in eachindex(y) 122 | y[i] ~ Categorical(p) 123 | end 124 | end 125 | ``` 126 | 127 | . . . 128 | 129 |
130 | 131 | ```{julia} 132 | #| output: false 133 | using Distributions 134 | 135 | my_data = rand(DiscreteUniform(1, 6), 1_000) 136 | model = dice_throw(my_data) 137 | 138 | # NUTS with 1k iter, 139 | # 1k warmup, and 140 | # acceptance rate of 80% 141 | chain = sample(model, NUTS(1_000, 0.8), 1_000) 142 | ``` 143 | 144 | ## Inspecting Chains {.scrollable} 145 | 146 | ```{julia} 147 | chain 148 | ``` 149 | 150 | ## Visualizing Chains 151 | 152 | ```{julia} 153 | using StatsPlots 154 | 155 | plot(chain) 156 | ``` 157 | 158 | ## Different MCMC Samplers 159 | 160 | We have [several samplers](https://turinglang.org/dev/docs/using-turing/sampler-viz) available: 161 | 162 | ::: incremental 163 | - `MH()`: **M**etropolis-**H**astings 164 | - `PG()`: **P**article **G**ibbs 165 | - `SMC()`: **S**equential **M**onte **C**arlo 166 | - `HMC()`: **H**amiltonian **M**onte **C**arlo 167 | - `HMCDA()`: **H**amiltonian **M**onte **C**arlo with Nesterov's **D**ual **A**veraging 168 | - `NUTS()`: **N**o-**U**-**T**urn **S**ampling 169 | ::: 170 | 171 | ::: aside 172 | There's also a way to combine MCMC samplers for different variables with `Gibbs`. 173 | ::: 174 | 175 | ## Example --- Dice Throw `MH()` {.scrollable} 176 | 177 | ```{julia} 178 | # Metropolis-Hastings with 2k iters 179 | chain_mh = sample(model, MH(), 2_000) 180 | ``` 181 | 182 | 183 | ## Fitting --- Multi-threading and Multiprocess 184 | 185 | There is some methods of `Turing`'s `sample()` that accepts either: 186 | 187 | ::: incremental 188 | - `MCMCThreads()`: uses multithread stuff with [`Threads.jl`](https://docs.julialang.org/en/v1/manual/multi-threading/#man-multithreading) 189 | - `MCMCDistributed()`: uses multiprocesses stuff with [`Distributed.jl`](https://docs.julialang.org/en/v1/manual/distributed-computing/) and uses the [MPI -- Message Passing Interface](https://en.wikipedia.org/wiki/Message_Passing_Interface) 190 | ::: 191 | 192 | . . . 193 | 194 | Just use `sample(model, sampler, MCMCThreads(), N, chains)`. 195 | 196 | ## Example --- Dice Throw Multi-threaded {.scrollable} 197 | 198 | ```{julia} 199 | # 4 chains 200 | chain_multi = sample(model, NUTS(1_000, 0.8), MCMCThreads(), 1_000, 4) 201 | ``` 202 | 203 | ## Performance Tips 204 | 205 | ::: incremental 206 | - Avoid `for`-loops 207 | - Use `LazyArrays` 208 | ::: 209 | 210 | ## Performance Tips -- Example {.scrollable} 211 | 212 | Let's use the `wells` dataset [@Gelman_Hill_2006]. 213 | 214 | . . . 215 | 216 | 217 | Data from a survey of 3,200 residents in a small area of Bangladesh suffering from arsenic contamination of groundwater. 218 | Respondents with elevated arsenic levels in their wells had been encouraged to switch their water source to a safe public or private well in the nearby area and the survey was conducted several years later to learn which of the affected residents had switched wells. 219 | 220 | . . . 221 | 222 | - `switch` -- binary/dummy (0 or 1) for well-switching. 223 | - `arsenic` -- arsenic level in respondent's well. 224 | - `dist` -- distance (meters) from the respondent's house to the nearest well with safe drinking water. 225 | - `association` -- binary/dummy (0 or 1) if member(s) of household participate in community organizations. 226 | - `educ` -- years of education (head of household). 227 | 228 | ```{julia} 229 | using CSV 230 | using DataFrames 231 | 232 | wells = CSV.read("data/wells.csv", DataFrame) 233 | describe(wells) 234 | ``` 235 | 236 | ## Logistic Regression `wells` --- Data Prep 237 | 238 | 239 | ```{julia} 240 | #| output: false 241 | X = Matrix(select(wells, Not(:switch))) 242 | y = wells[:, :switch] 243 | ``` 244 | 245 | ## Logistic Regression `wells` --- Naïve Model {.scrollable} 246 | 247 | ```{julia} 248 | using BenchmarkTools 249 | using LinearAlgebra: ⋅ 250 | 251 | @model function logreg_naive(X, y; predictors=size(X, 2)) 252 | # priors 253 | α ~ Normal(0, 2.5) 254 | β ~ filldist(TDist(3), predictors) 255 | 256 | # likelihood 257 | for i in eachindex(y) 258 | y[i] ~ BernoulliLogit(α + X[i, :] ⋅ β) 259 | end 260 | end 261 | 262 | model_naive = logreg_naive(X, y) 263 | 264 | @btime sample(model_naive, NUTS(1_000, 0.8), 1_000) 265 | ``` 266 | 267 | ::: aside 268 | `Turing`'s `BernoulliLogit()` is a logit-parameterised Bernoulli distribution that convert logodds to probability. 269 | ::: 270 | 271 | ## Logistic Regression `wells` --- Performance Model {.scrollable} 272 | 273 | ```{julia} 274 | using LazyArrays 275 | 276 | @model function logreg_perf(X, y; predictors=size(X, 2)) 277 | # priors 278 | α ~ Normal(0, 2.5) 279 | β ~ filldist(TDist(3), predictors) 280 | 281 | # likelihood 282 | y ~ arraydist(LazyArray(@~ BernoulliLogit.(α .+ X * β))) 283 | end 284 | 285 | model_perf = logreg_perf(X, y) 286 | 287 | @btime sample(model_perf, NUTS(1_000, 0.8), 1_000) 288 | ``` 289 | 290 | ## References 291 | 292 | ::: {#refs} 293 | ::: 294 | -------------------------------------------------------------------------------- /2023-Intro-Turing/references.bib: -------------------------------------------------------------------------------- 1 | @misc{Hardesty_Technology, 2 | title = {Probabilistic programming does in 50 lines of code what used to 3 | take thousands}, 4 | url = { 5 | https://phys.org/news/2015-04-probabilistic-lines-code-thousands.html 6 | }, 7 | author = {Hardesty, Larry}, 8 | language = {en}, 9 | year = {2015}, 10 | } 11 | 12 | @inproceedings{Ge_Xu_Ghahramani_2018, 13 | title = {Turing: A Language for Flexible Probabilistic Inference}, 14 | url = {https://proceedings.mlr.press/v84/ge18b.html}, 15 | booktitle = {Proceedings of the Twenty-First International Conference on 16 | Artificial Intelligence and Statistics}, 17 | publisher = {PMLR}, 18 | author = {Ge, Hong and Xu, Kai and Ghahramani, Zoubin}, 19 | year = {2018}, 20 | month = {Mar}, 21 | pages = {1682–1690}, 22 | language = {en}, 23 | } 24 | 25 | @article{Tarek_Xu_Trapp_Ge_Ghahramani_2020, 26 | title = {DynamicPPL: Stan-like Speed for Dynamic Probabilistic Models}, 27 | url = {http://arxiv.org/abs/2002.02702}, 28 | DOI = {10.48550/arXiv.2002.02702}, 29 | note = {arXiv:2002.02702 [cs, stat]}, 30 | publisher = {arXiv}, 31 | author = {Tarek, Mohamed and Xu, Kai and Trapp, Martin and Ge, Hong and 32 | Ghahramani, Zoubin}, 33 | year = {2020}, 34 | month = {Feb}, 35 | } 36 | 37 | @inproceedings{Xu_Ge_Tebbutt_Tarek_Trapp_Ghahramani_2020, 38 | title = {AdvancedHMC.jl: A robust, modular and efficient implementation 39 | of advanced HMC algorithms}, 40 | url = {https://proceedings.mlr.press/v118/xu20a.html}, 41 | booktitle = {Proceedings of The 2nd Symposium on Advances in Approximate 42 | Bayesian Inference}, 43 | publisher = {PMLR}, 44 | author = {Xu, Kai and Ge, Hong and Tebbutt, Will and Tarek, Mohamed and 45 | Trapp, Martin and Ghahramani, Zoubin}, 46 | year = {2020}, 47 | month = {Feb}, 48 | pages = {1–10}, 49 | language = {en}, 50 | } 51 | @book{Gelman_Hill_2006, 52 | address = {Cambridge ; New York}, 53 | edition = {1st edition}, 54 | title = {Data Analysis Using Regression and Multilevel/Hierarchical 55 | Models}, 56 | ISBN = {9780521686891}, 57 | publisher = {Cambridge University Press}, 58 | author = {Gelman, Andrew and Hill, Jennifer}, 59 | year = {2006}, 60 | month = {Dec}, 61 | language = {English}, 62 | } 63 | 64 | -------------------------------------------------------------------------------- /2023-Intro-Turing/setting_up_julia.md: -------------------------------------------------------------------------------- 1 | # Getting set up with Julia 2 | 3 | These workshops are intended to be somewhat interactive, 4 | and so you might be at risk of having to actually write and 5 | execute some code on your own device! 6 | 7 | If you're already familiar with Julia and have Julia 1.8 or greater already installed, 8 | then you can probably stop reading here. 9 | 10 | If you're new to Julia, 11 | you will unfortunately have to install yet another language on your device. 12 | Luckily, it's a fairly straight forward mission. 13 | 14 | ## "Woah there! I have to _execute_ Julia code? HOW?!" - Installing Julia 15 | 16 | We'll be using Julia **1.8.x** throughout, 17 | so we recommend you use the same version to avoid any issues. 18 | 19 | **Julia can be downloaded at the official website () 20 | and a more detailed instructions can also be found there ().** 21 | 22 | At the end of this process you should have access to some way of getting you 23 | into the Julia REPL which should present something similar to this: 24 | 25 | ```julia 26 | _ 27 | _ _ _(_)_ | Documentation: https://docs.julialang.org 28 | (_) | (_) (_) | 29 | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. 30 | | | | | | | |/ _` | | 31 | | | |_| | | | (_| | | Version 1.8.2 (2022-09-29) 32 | _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release 33 | |__/ | 34 | 35 | julia> 36 | ``` 37 | 38 | For example, on a Linux device this is generally achieved by running the command 39 | `julia` or the executable `/wherever/you/installed/julia/bin/julia`. 40 | 41 | (If the version is slightly different, e.g. 1.8.4, that is no problem; 42 | the important bit is that you're on a version ≥1.8) 43 | 44 | ## "But how do I _write_ Julia code?!" - Setting up an editor 45 | 46 | As mentioned earlier, 47 | at certain points during these workshops you're likely to 48 | find yourself in a position where you even need to _write_ some Julia code. 49 | I know, this is all very demanding. 50 | 51 | How and where and with what you want to write these chunks of text is of course 52 | completely up to yourself. 53 | 54 | - If you live and die by Notepad++, then you're welcome to use Notepad++. 55 | - If you're one of those wizards who doesn't have to spend 30 minutes trying to 56 | exit a file you opened in `vim`, then maybe you should use `vim`. 57 | - Or if you're part of the opposition of the group of people described above, 58 | i.e. an `emacs` user, then you might want to use `emacs`. 59 | 60 | **But most (sane) people will probably prefer something like:** 61 | 62 | 1. a proper IDE, in which case [Visual Studio (VS) Code](https://code.visualstudio.com/) 63 | with [the Julia extensions](https://www.julia-vscode.org/) is a great option, 64 | or 65 | 2. [Jupyter notebooks](https://jupyter.org/), which is easily supported 66 | by simply installing the Julia package `IJulia`; 67 | once this is installed you should find a `julia` kernel available in your notebook.[^1] 68 | 69 | The latter option might be more familiar if you're coming from a 70 | scientific Python background, 71 | but VS Code is generally the recommended option. 72 | 73 | Note that we expect everyone to act civil during the workshops, 74 | regardless of the differences in choices of editor people make. 75 | 76 | ## Additional resources 77 | 78 | - Julia Language website: 79 | - Turing website: 80 | - Julia Language YouTube channel: 81 | 82 | [^1]: Alternatively, one can start the notebook from the Julia REPL by running `using IJulia; IJulia.notebook()`. 83 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-1-Intro_Julia/julia_intro.jl: -------------------------------------------------------------------------------- 1 | # Julia Cheat Sheet: https://cheatsheet.juliadocs.org/ 2 | 3 | ### 4 | ### Basic Data Types in Julia 5 | ### 6 | 7 | # Integer 8 | x = 1 9 | 10 | # Float 11 | y = 2.0 12 | 13 | # unicode variable names: \alpha 14 | α = 1 15 | β = 2 16 | γ = 3 17 | x₁ = 1 # x\_1 18 | x⁵ = 5 # don't do this 19 | 🐘 = "elephant" # \:elephant: 20 | 🦓 = "zebra" # \:zebra_face: 21 | 🦁 = "lion" # \:lion_face: # definitely don't do this 22 | 🦁 * ": ROAR!!" 23 | 24 | # Boolean 25 | b = true 26 | b = false # all lower case 27 | 28 | # Strings 29 | s = "string" 30 | 31 | ### 32 | ### Arrays in Julia 33 | ### 34 | 35 | # Vector 36 | a = [1, 2, 3] 37 | a[1] 38 | a[begin] 39 | a[end] 40 | a[0] # error!: 1-based indexing 41 | a[-1] # error!: no negative indexing 42 | a[1:2] == [1, 2] # including whole range, unlike Python 43 | 44 | # Matrices 45 | m = [1 2 3; 4 5 6; 7 8 9] # Declare a matrix 46 | m[1, 1] # Accessing elements in a matrix 47 | m[1, :] # Accessing rows in a matrix 48 | m[1] # linear indexing 49 | m[:, 2] 50 | m[4:6] # column-major order 51 | 52 | sub_matrix = m[1:2, 1:2] # indexing copies 53 | sub_matrix[1, 1] = 10 54 | m[1, 1] == 10 55 | 56 | sub_matrix_view = view(m, 1:2, 1:2) # view does not 57 | sub_matrix_view[1, 1] = 10 58 | m[1, 1] == 10 59 | setindex!(m, 1, 1, 1) 60 | 61 | # Array Functions 62 | length(a) # Get the number of elements in an array 63 | size(m) # Get the size of an array 64 | ndims(m) # Get the number of dimensions of an array 65 | vcat(m...) # Concatenate arrays vertically 66 | hcat(m...) # Concatenate arrays horizontally 67 | push!(a, 4) # Add an element to the end of an array 68 | pop!(a) # Remove the last element from an array 69 | a = reverse(a) # Reverse the order of elements in vector a 70 | sort(a) # Sort an array in ascending order 71 | sort!(a) # Sort an array in place 72 | maximum(a) # Get the maximum value in an array 73 | minimum(a) # Get the minimum value in an array 74 | sum(a) # Get the sum of all elements in an array 75 | prod(a) # Get the product of all elements in an array 76 | 77 | # Vector, Matrix is alias for Array type 78 | typeof(a) 79 | typeof(m) 80 | 81 | ### 82 | ### Functions and Control Structures in Julia 83 | ### 84 | 85 | # Basic Function Form 86 | function increment(x) 87 | return x + 1 88 | end 89 | 90 | # Control Structures 91 | 92 | # Example of if statement 93 | if x > y 94 | println("x is greater than y") 95 | elseif x < y 96 | println("x is less than y") 97 | else 98 | println("x is equal to y") 99 | end 100 | 101 | # Example of for loop 102 | for i in a 103 | println(i) 104 | end 105 | 106 | # Example of while loop 107 | i = 1 108 | while i <= 3 109 | println(i) 110 | i += 1 111 | end 112 | 113 | # Example of break/continue 114 | for i in a 115 | if i == 2 116 | continue 117 | elseif i == 3 118 | break 119 | end 120 | println(i) 121 | end 122 | 123 | # Short-circuit evaluation 124 | true && println("true") 125 | 126 | # Ternary operator 127 | true ? println("true") : println("false") 128 | 129 | x = 1; y = 2.0 # multiple expressions on one line 130 | 131 | # Function Continued 132 | 133 | # Alternative function definition syntax: just like math equations 134 | increment(x) = x + 1 135 | increment(y) 136 | 137 | # Basic Arithmetic Operators (build-in functions) 138 | x + y 139 | x - y 140 | x * y 141 | x / y 142 | x ^ y 143 | x % y 144 | inv(y) 145 | sqrt(y) 146 | sin(y) 147 | atan(y) 148 | log(y) 149 | 2x + 3y # can omit * for multiplication 150 | 151 | # Dictionary in Julia 152 | d = Dict("name" => "Julia", :version => 1.10) 153 | d["name"] # Accessing elements in a dictionary 154 | d[:version] 155 | :version isa Symbol 156 | 157 | # Tuples in Julia 158 | t = (1, "two", 3.0) 159 | t[1] 160 | getindex(t, 2) 161 | 162 | # NamedTuples in Julia 163 | () # empty tuple 164 | (;) # empty NamedTuple 165 | nt = (name = "Julia", version = 1.10) 166 | nt.name # Accessing elements in a NamedTuple 167 | nt[:version] 168 | 169 | # x, y different type: multiple dispatch and primitive types 170 | @which x + x 171 | @which y + y 172 | @which x + y 173 | x + y * im # complex number 174 | x isa Real 175 | x isa Number 176 | x + y * im isa Number 177 | 178 | # Define function that specifies argument types -- taste of multiple dispatch 179 | function increment(a::Int) 180 | return a + 1 181 | end 182 | 183 | function increment(a::Float64)::Int 184 | # return a + 2 185 | a + 2 # last line is returned if no explicit return 186 | end 187 | 188 | function increment2(a::Float64)::Int 189 | return a + 2 190 | end 191 | 192 | increment(1) == 2 193 | increment(1.0) == 3.0 194 | increment2(1.5) # will error 195 | 196 | # Mutation on the Function theme 197 | 198 | # Keyword arguments 199 | function add(x; a, b = 2) 200 | return x + a + b 201 | end 202 | 203 | add(1, 2) 204 | add(1; a = 2) 205 | add(1; a = 2, b = 3) 206 | 207 | # Optional arguments 208 | function add(x, a = 1, b = 2) 209 | return x + a + b 210 | end 211 | 212 | add(1) 213 | add(1, 2, 3) 214 | 215 | # Varargs 216 | function sum_values(x, a...) 217 | println(a) 218 | return x + sum(a) 219 | end 220 | 221 | sum_values(1, 2, 3, 4, 5) 222 | 223 | # Keyword varargs 224 | function sum_values(x; a...) 225 | println(a) 226 | return x + sum(values(a)) 227 | end 228 | 229 | sum_values(1; a = 2, b = 3, c = 4) 230 | 231 | # Example of broadcasting 232 | a = [1, 2, 3] 233 | b = a .+ 2 234 | c = ones(Int, 4) 235 | c .= 2 # assigns every element of c to be 2 236 | c == b 237 | 238 | # Example of comprehensions 239 | [x^2 for x in a] 240 | [x^2 for x in a if x > 1] 241 | 242 | # Anonymous functions (lambda functions) 243 | (x -> x + 1)(1) == 2 244 | f(x) = x + 1 # these two are equivalent 245 | # Example of map function, also fold/reduce, mapreduce ... 246 | map(x -> x * 2, a) 247 | 248 | # Various do block 249 | map([1, 2, 3]) do x # same as map(x -> x, [1, 2, 3]) 250 | x * 2 251 | end 252 | # Various do block 253 | map([1, 2, 3]) do x # same as map(x -> x, [1, 2, 3]) 254 | a = x * 2 255 | return a 256 | end 257 | 258 | # Use tempname() to create a temporary file 259 | filename = tempname() 260 | open(filename, "w") do io 261 | write(io, "Hello world") 262 | end 263 | read(filename) # returns a byte array 264 | read(filename, String) # same as above 265 | 266 | # Try/Catch/Finally 267 | try 268 | println("Trying to read a file...") 269 | open("non_existent_file.txt", "r") 270 | catch e 271 | if e isa SystemError 272 | println("Caught an error: ", e) 273 | rethrow(e) # without `rethrow`, the program will not stop but carry on 274 | else 275 | println("NA") 276 | end 277 | finally 278 | println("This finally block gets executed no matter what!") 279 | end 280 | 281 | # struct and multiple dispatch 282 | abstract type Animal end 283 | abstract type Herbivore <: Animal end 284 | abstract type Carnivore <: Animal end 285 | 286 | struct Elephant <: Herbivore 287 | name::String 288 | daily_food_cost::Float64 289 | end 290 | 291 | struct Zebra <: Herbivore 292 | name::String 293 | daily_food_cost::Float64 294 | end 295 | 296 | mutable struct Lion <: Carnivore 297 | name::String 298 | lions_share::Float64 299 | end 300 | 301 | # compare these to Python's @property 302 | function get_daily_food_cost(animal::Animal) 303 | return animal.daily_food_cost 304 | end 305 | 306 | function get_daily_food_cost(animal::Lion) 307 | return animal.lions_share 308 | end 309 | 310 | # compare these to Python's @abstractmethod 311 | function daily_diet(animal::Animal) 312 | return "The daily diet details are not available." 313 | end 314 | 315 | function daily_diet(animal::Herbivore) 316 | return "$(animal.name) is a $(typeof(animal)), it primarily eats plants." 317 | end 318 | 319 | function daily_diet(animal::Carnivore) 320 | return "The animal is a carnivore, it primarily eats meat." 321 | end 322 | 323 | function daily_diet(animal::Elephant) 324 | return "The elephant primarily eats grass, fruits, and roots. Daily food cost: $(get_daily_food_cost(animal)) USD." 325 | end 326 | 327 | function daily_diet(animal::Lion) 328 | return "The lion primarily eats large ungulates such as zebras and wildebeests. Daily food cost: $(get_daily_food_cost(animal)) USD." 329 | end 330 | 331 | function yearly_food_cost(animal::Herbivore) 332 | summer_cost = get_daily_food_cost(animal) * 120 333 | winter_cost = get_daily_food_cost(animal) * 1.5 * 240 334 | return "Yearly food cost: $(summer_cost + winter_cost) USD." 335 | end 336 | 337 | function yearly_food_cost(animal::Carnivore) 338 | return "Yearly food cost: $(get_daily_food_cost(animal) * 365) USD." 339 | end 340 | 341 | elephant = Elephant("Dumbo", 50.0) 342 | zebra = Zebra("Marty", 25.0) 343 | lion = Lion("simba", 100.0) 344 | lion.name = "SIMBA" # mutable struct 345 | 346 | println(daily_diet(elephant)) 347 | println(daily_diet(lion)) 348 | println(daily_diet(zebra)) 349 | 350 | println(yearly_food_cost(elephant)) 351 | println(yearly_food_cost(lion)) 352 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-01-22_12-09-24_Screenshot_20230122_120848.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-01-22_12-09-24_Screenshot_20230122_120848.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-09-15_11-49-35_Screenshot_20230915_114925.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-09-15_11-49-35_Screenshot_20230915_114925.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-09-15_13-06-48_Screenshot_20230915_130639.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-09-15_13-06-48_Screenshot_20230915_130639.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-09-19_21-36-15_Screenshot_20230919_213557.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/.more-julia/attachments/2023-09-19_21-36-15_Screenshot_20230919_213557.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/Project.toml: -------------------------------------------------------------------------------- 1 | name = "Part2" 2 | uuid = "a327e1c7-bb70-4dcd-b231-d4939861f6f4" 3 | authors = ["Tor Erlend Fjelde "] 4 | version = "0.1.0" 5 | 6 | [deps] 7 | CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 8 | CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" 9 | DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe" 10 | DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 11 | Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" 12 | DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" 13 | DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" 14 | Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" 15 | IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" 16 | Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" 17 | OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" 18 | Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" 19 | ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" 20 | SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" 21 | StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" 22 | StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" 23 | Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" 24 | TuringBenchmarking = "0db1332d-5c25-4deb-809f-459bc696f94f" 25 | TuringCallbacks = "ea0860ee-d0ef-45ef-82e6-cc37d6be2f9c" 26 | TuringGLM = "0004c1f4-53c5-4d43-a221-a1dac6cf6b74" 27 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/README.md: -------------------------------------------------------------------------------- 1 | # MRC BSU & UKHSA 2023 (Part 2): Bayesian inference and other things with the `TuringLang` ecosystem 2 | 3 | Here you find the presentation in several different formats: 4 | - `more-julia.html`: The presentation itself, as you see on the big screen. 5 | - `more-julia.jl`: Julia file which you can just step through and execute. Nice if you're in VS Code. 6 | - `more-julia.md`: A markdown file which comes with more context than the Julia file, easily readable in Github itself. 7 | - `more-julia.ipynb`: A Jupyter notebook version of the markdown file. 8 | - `more-julia.org`: A Org-mode file, which is the source of all the files above and probably the easiest to read on Github, _but_ not supported by most editors (unless you're using Emacs). 9 | 10 | Pick your poison, and follow along! 11 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_14-46-02_Screenshot_20230118_144454.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_14-46-02_Screenshot_20230118_144454.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_14-49-24_471337_3317365246956_1262712540_o.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_14-49-24_471337_3317365246956_1262712540_o.jpg -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_14-57-08_Preikestolen-plateau-Go-Fjords-Bob-Engelsen-P1026771_kljg5o.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_14-57-08_Preikestolen-plateau-Go-Fjords-Bob-Engelsen-P1026771_kljg5o.jpeg -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_15-31-28_Screenshot_20230118_153122.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-18_15-31-28_Screenshot_20230118_153122.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-19_19-48-23_Screenshot_20230119_194737.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-19_19-48-23_Screenshot_20230119_194737.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-19_19-48-41_Screenshot_20230119_194838.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-19_19-48-41_Screenshot_20230119_194838.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-19_19-56-00_sir_illu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-19_19-56-00_sir_illu.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-22_12-09-24_Screenshot_20230122_120848.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-22_12-09-24_Screenshot_20230122_120848.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-22_12-30-07_Screenshot_20230122_122936.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-22_12-30-07_Screenshot_20230122_122936.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_01-01-31_Screenshot_20230125_010111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_01-01-31_Screenshot_20230125_010111.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_01-16-13_Screenshot_20230125_011603.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_01-16-13_Screenshot_20230125_011603.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_01-29-05_Screenshot_20230125_012853.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_01-29-05_Screenshot_20230125_012853.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_20-50-11_tensorboard_demo_histograms_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/2023-01-25_20-50-11_tensorboard_demo_histograms_screen.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/synthetic-timeseries-data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/attachments/synthetic-timeseries-data.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/css/custom.css: -------------------------------------------------------------------------------- 1 | /* body { */ 2 | /* background-color: grey; */ 3 | /* } */ 4 | 5 | .reveal { 6 | /* color: green; */ 7 | /* color: white; */ 8 | /* color: #eee8d5; */ 9 | font-size: 28px; 10 | } 11 | 12 | .reveal h1, 13 | .reveal h2, 14 | .reveal h3, 15 | .reveal h4, 16 | .reveal h5, 17 | .reveal h6 { 18 | /* color: green; */ 19 | text-transform: none; 20 | } 21 | 22 | .reveal table th, .reveal table td { 23 | font-size: 0.5em; 24 | text-align: center; 25 | } 26 | 27 | .reveal blockquote { 28 | border-radius: 5px; 29 | } 30 | 31 | .reveal section img { 32 | border: 0px; 33 | } 34 | 35 | .reveal div.definition { 36 | text-align: left; 37 | border-radius: 10px; 38 | line-height: 1.3; 39 | display: block; 40 | position: relative; 41 | width: 70%; 42 | margin: 10px auto; 43 | padding: 10px; 44 | font-style: italic; 45 | background: rgba(255, 255, 255, 0.05); 46 | box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.2); 47 | } 48 | 49 | div.definition::before { 50 | content: "Definition"; 51 | font-weight: bolder; 52 | text-decoration: underline; 53 | } 54 | 55 | .reveal pre { 56 | /* background-color: #002b36; */ 57 | padding: 0.5em; 58 | } 59 | 60 | pre.example { 61 | /* line-height: 1.3em; */ 62 | /* background-color: #002f3c; */ 63 | /* color: #9c9c9c; */ 64 | overflow: auto; 65 | max-height: 400px; 66 | border-radius: 5px; 67 | } 68 | 69 | pre.example::before { 70 | content: "[output]"; 71 | opacity: 0.3; 72 | font-size: 0.75em; 73 | /* Right-align it */ 74 | position: absolute; 75 | right: 0.5em; /* Same as padding of `pre.example` */ 76 | } 77 | 78 | /* Src blocks */ 79 | pre.src { 80 | border-radius: 5px; 81 | } 82 | 83 | .reveal pre code { 84 | display: block; 85 | overflow: auto; 86 | max-height: 600px; 87 | word-wrap: normal; 88 | } 89 | 90 | /* specific langs */ 91 | .reveal pre.src-julia code::before { 92 | content: "[julia]"; 93 | opacity: 0.3; 94 | font-size: 0.75em; 95 | /* Right-align it */ 96 | /* Right-align it */ 97 | position: absolute; 98 | right: 0.5em; /* Same as padding of `code` */ 99 | } 100 | 101 | .reveal pre.src-sh code::before { 102 | content: "[sh]"; 103 | opacity: 0.3; 104 | font-size: 0.75em; 105 | /* Right-align it */ 106 | /* Right-align it */ 107 | position: absolute; 108 | right: 0.5em; /* Same as padding of `code` */ 109 | } 110 | 111 | 112 | div#table-of-contents { 113 | font-size: 12px; 114 | } 115 | 116 | .figure > p > object, 117 | .figure > p > img { 118 | /* border: 2px solid rgba(0, 0, 0, 0.3); */ 119 | /* border-radius: 10px; */ 120 | /* box-shadow: 2px 5px rgba(0, 0, 0, 0.3); */ 121 | } 122 | 123 | /* Special cases */ 124 | #mvnormal-comparison div.figure { 125 | width: 35%; 126 | display: inline-block; 127 | } 128 | 129 | div.figure p:first-child { 130 | margin: 10px 0; 131 | } 132 | 133 | div.figure p:last-child { 134 | font-size: 0.5em; 135 | margin: 0 0 10px 0; 136 | } 137 | 138 | 139 | /* HEADERS AND FOOTERS */ 140 | /* 1. Style header/footer
so they are positioned as desired. */ 141 | #header-left { 142 | position: absolute; 143 | top: 0%; 144 | left: 0%; 145 | } 146 | 147 | #header-logo { 148 | height: 50px; 149 | padding: 20px; 150 | } 151 | 152 | #header-right { 153 | position: absolute; 154 | top: 0%; 155 | right: 0%; 156 | } 157 | #footer { 158 | position: absolute; 159 | bottom: 0%; 160 | left: 0%; 161 | width: 100%; 162 | /* margin: 0 auto; */ 163 | display: flex; 164 | } 165 | 166 | #footer > div { 167 | width: 100%; 168 | } 169 | 170 | #footer div img { 171 | height: 40px; 172 | width: 200px; 173 | margin: 0 auto; 174 | } 175 | 176 | #footer-logo { 177 | /* background: url(figures/juliacon2020-background.png); */ 178 | /* background-size: cover; */ 179 | 180 | height: 100px; 181 | 182 | font-size: 1.5em; 183 | display: flex; 184 | } 185 | 186 | #footer-logo strong { 187 | margin: 90px auto; 188 | } 189 | 190 | #footer-logo sup { 191 | background-color: #0d3640; 192 | border-radius: 10px; 193 | padding: 5px; 194 | text-align: center; 195 | } 196 | 197 | 198 | /* GENERAL CUSTOMIZATION */ 199 | 200 | .center { 201 | margin: auto; 202 | text-align: center; 203 | } 204 | 205 | .side-by-side { 206 | display: inline-flex; 207 | } 208 | 209 | .small-text { 210 | font-size: 0.75em; 211 | } 212 | 213 | .x-small-text { 214 | font-size: 0.5em; 215 | } 216 | 217 | .TASK { 218 | color: red; 219 | } 220 | 221 | .SOLUTION { 222 | color: lawngreen; 223 | } 224 | 225 | .WARNING { 226 | color: orange; 227 | } 228 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/outputs/more-julia/348bd16b3b818fcfbedfaf14f50cb941ca1b7ca3.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/outputs/more-julia/3f7598c6f0df5f376d9f89161504465787bacf25.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/outputs/more-julia/7cc0dffb8818815a0ac1bd1a2ed82b37365dceba.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/outputs/more-julia/lorenz.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/outputs/more-julia/lorenz.gif -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/outputs/more-julia/pi.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/assets/outputs/more-julia/pi.gif -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/data/influenza_england_1978_school.csv: -------------------------------------------------------------------------------- 1 | "","date","in_bed","convalescent" 2 | "1",1978-01-22,3,0 3 | "2",1978-01-23,8,0 4 | "3",1978-01-24,26,0 5 | "4",1978-01-25,76,0 6 | "5",1978-01-26,225,9 7 | "6",1978-01-27,298,17 8 | "7",1978-01-28,258,105 9 | "8",1978-01-29,233,162 10 | "9",1978-01-30,189,176 11 | "10",1978-01-31,128,166 12 | "11",1978-02-01,68,150 13 | "12",1978-02-02,29,85 14 | "13",1978-02-03,14,47 15 | "14",1978-02-04,4,20 16 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/data/pest_data.csv: -------------------------------------------------------------------------------- 1 | mus,building_id,wk_ind,date,traps,floors,sq_footage_p_floor,live_in_super,monthly_average_rent,average_tenant_age,age_of_building,total_sq_foot,month,complaints 2 | 0.3691335319623852,37,1,2017-01-15,8.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,1.0,1.0 3 | 0.35935496957806057,37,2,2017-02-14,8.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,2.0,3.0 4 | 0.2817826075341411,37,3,2017-03-16,9.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,3.0,0.0 5 | 0.12925388217756734,37,4,2017-04-15,10.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,4.0,1.0 6 | 0.4520411876082566,37,5,2017-05-15,11.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,5.0,0.0 7 | 0.44213045415802504,37,6,2017-06-14,11.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,6.0,0.0 8 | 0.9908648169807803,37,7,2017-07-14,10.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,7.0,4.0 9 | 0.7859767235696389,37,8,2017-08-13,10.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,8.0,3.0 10 | 0.6917967724489259,37,9,2017-09-12,9.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,9.0,2.0 11 | 0.4806956513437962,37,10,2017-10-12,9.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,10.0,2.0 12 | 0.5624306351765842,37,11,2017-11-11,8.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,11.0,0.0 13 | 0.2817826075341411,37,12,2017-12-11,9.0,8.0,5149.008111555364,0.0,3846.9490497533784,53.87742357476312,47.0,41192.06489244291,12.0,4.0 14 | 0.5200135519598523,13,1,2017-01-15,7.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,1.0,1.0 15 | 0.5102349895755277,13,2,2017-02-14,7.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,2.0,3.0 16 | 0.4317983477198414,13,3,2017-03-16,8.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,3.0,7.0 17 | 0.2784053425515008,13,4,2017-04-15,9.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,4.0,1.0 18 | 0.600328368170423,13,5,2017-05-15,10.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,5.0,1.0 19 | 0.8078702910314668,13,6,2017-06-14,9.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,6.0,4.0 20 | 1.1400162773547138,13,7,2017-07-14,9.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,7.0,0.0 21 | 0.717675527632297,13,8,2017-08-13,10.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,8.0,4.0 22 | 0.4069072000120756,13,9,2017-09-12,10.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,9.0,1.0 23 | 0.4132587352182212,13,10,2017-10-12,9.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,10.0,2.0 24 | 0.2784053425515008,13,11,2017-11-11,9.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,11.0,1.0 25 | 0.4317983477198414,13,12,2017-12-11,8.0,9.0,5165.969711355208,0.0,3563.5157321192496,49.566618025962946,43.0,46493.72740219688,12.0,1.0 26 | 1.3302407292893073,5,1,2017-01-15,8.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,1.0,7.0 27 | 1.3204621669049827,5,2,2017-02-14,8.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,2.0,2.0 28 | 1.6684111106987163,5,3,2017-03-16,7.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,3.0,2.0 29 | 1.9414036911797954,5,4,2017-04-15,6.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,4.0,5.0 30 | 2.4807793731099927,5,5,2017-05-15,6.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,5.0,12.0 31 | 2.261935710321617,5,6,2017-06-14,7.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,6.0,7.0 32 | 2.8030146259830087,5,7,2017-07-14,6.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,7.0,5.0 33 | 2.389193603233722,5,8,2017-08-13,7.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,8.0,18.0 34 | 2.078425275613501,5,9,2017-09-12,7.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,9.0,9.0 35 | 1.8673241545083712,5,10,2017-10-12,7.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,10.0,4.0 36 | 1.7324707618416508,5,11,2017-11-11,7.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,11.0,9.0 37 | 1.4594781813605717,5,12,2017-12-11,8.0,14.0,5045.836544469904,0.0,3028.6683753581287,47.85826135382058,49.0,70641.71162257866,12.0,0.0 38 | 1.0072374123393846,26,1,2017-01-15,6.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,1.0,1.0 39 | 0.99745884995506,26,2,2017-02-14,6.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,2.0,6.0 40 | 0.9352106765787537,26,3,2017-03-16,7.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,3.0,8.0 41 | 0.9992703277216883,26,4,2017-04-15,7.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,4.0,2.0 42 | 1.7399101974837812,26,5,2017-05-15,6.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,5.0,1.0 43 | 1.5287352762016544,26,6,2017-06-14,7.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,6.0,2.0 44 | 1.8608812625249014,26,7,2017-07-14,7.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,7.0,12.0 45 | 1.8572573569456552,26,8,2017-08-13,6.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,8.0,12.0 46 | 1.3452248414935384,26,9,2017-09-12,7.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,9.0,0.0 47 | 1.335387908220304,26,10,2017-10-12,6.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,10.0,4.0 48 | 0.9992703277216883,26,11,2017-11-11,7.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,11.0,2.0 49 | 0.7339464887468586,26,12,2017-12-11,8.0,10.0,4769.811965004639,0.0,4019.2368374403304,47.336702992913324,51.0,47698.11965004639,12.0,9.0 50 | 0.8797047464115018,93,1,2017-01-15,7.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,1.0,1.0 51 | 0.8699261840271771,93,2,2017-02-14,7.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,2.0,4.0 52 | 0.7749234671449955,93,3,2017-03-16,8.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,3.0,0.0 53 | 1.0730018496257008,93,4,2017-04-15,7.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,4.0,6.0 54 | 1.8463962628936692,93,5,2017-05-15,6.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,5.0,1.0 55 | 1.8364855294434377,93,6,2017-06-14,6.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,6.0,9.0 56 | 1.9346127844289138,93,7,2017-07-14,7.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,7.0,6.0 57 | 1.7297246910177724,93,8,2017-08-13,7.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,8.0,5.0 58 | 1.652975094735322,93,9,2017-09-12,6.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,9.0,1.0 59 | 1.441873973630192,93,10,2017-10-12,6.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,10.0,0.0 60 | 1.3070205809634716,93,11,2017-11-11,6.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,11.0,3.0 61 | 1.0089421984827662,93,12,2017-12-11,7.0,10.0,5740.006447795707,1.0,3676.6097451226005,45.13705114755641,49.0,57400.064477957065,12.0,6.0 62 | 1.1134623348466037,47,1,2017-01-15,7.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,1.0,3.0 63 | 1.1036837724622792,47,2,2017-02-14,7.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,2.0,1.0 64 | 1.2426997869178682,47,3,2017-03-16,7.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,3.0,9.0 65 | 1.1024910335051268,47,4,2017-04-15,8.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,4.0,3.0 66 | 1.8461351199910003,47,5,2017-05-15,7.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,5.0,2.0 67 | 1.8362243865407688,47,6,2017-06-14,7.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,6.0,9.0 68 | 2.1683703728640156,47,7,2017-07-14,7.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,7.0,3.0 69 | 2.16775068400855,47,8,2017-08-13,6.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,8.0,8.0 70 | 1.856982356388329,47,9,2017-09-12,6.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,9.0,3.0 71 | 1.8501496398388748,47,10,2017-10-12,5.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,10.0,1.0 72 | 1.5110278426164787,47,11,2017-11-11,6.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,11.0,9.0 73 | 1.2426997869178682,47,12,2017-12-11,7.0,15.0,5206.172620448094,0.0,3377.803349543014,48.54457905386281,49.0,78092.5893067214,12.0,1.0 74 | 1.3830887061906056,45,1,2017-01-15,5.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,1.0,6.0 75 | 1.3733101438062811,45,2,2017-02-14,5.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,2.0,5.0 76 | 1.3373820007092307,45,3,2017-03-16,6.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,3.0,0.0 77 | 1.4014416518521653,45,4,2017-04-15,6.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,4.0,4.0 78 | 1.7658731762297235,45,5,2017-05-15,7.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,5.0,6.0 79 | 1.5810182852268526,45,6,2017-06-14,8.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,6.0,9.0 80 | 1.7382201139974602,45,7,2017-07-14,9.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,7.0,1.0 81 | 1.5333320205863188,45,8,2017-08-13,9.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,8.0,2.0 82 | 1.2225636929660975,45,9,2017-09-12,9.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,9.0,8.0 83 | 1.0114625718609676,45,10,2017-10-12,9.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,10.0,0.0 84 | 0.8766091791942472,45,11,2017-11-11,9.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,11.0,2.0 85 | 0.6376053704986732,45,12,2017-12-11,10.0,11.0,4185.573519899658,0.0,3870.6944378649496,41.14222553902215,59.0,46041.30871889624,12.0,1.0 86 | -0.06467475908679043,70,1,2017-01-15,7.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,1.0,2.0 87 | -0.07445332147111505,70,2,2017-02-14,7.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,2.0,2.0 88 | 0.06456269298447395,70,3,2017-03-16,7.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,3.0,0.0 89 | 0.39571424065615496,70,4,2017-04-15,6.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,4.0,0.0 90 | 1.2021818191150988,70,5,2017-05-15,5.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,5.0,0.0 91 | 1.4593629821936136,70,6,2017-06-14,4.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,6.0,2.0 92 | 1.7915089685168606,70,7,2017-07-14,4.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,7.0,15.0 93 | 1.3195289785769728,70,8,2017-08-13,5.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,8.0,3.0 94 | 1.0087606509567513,70,9,2017-09-12,5.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,9.0,3.0 95 | 0.5305676333228754,70,10,2017-10-12,6.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,10.0,9.0 96 | 0.6628061371849012,70,11,2017-11-11,5.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,11.0,0.0 97 | 0.5987464860419666,70,12,2017-12-11,5.0,5.0,5290.4494228998965,1.0,3863.6575440719894,65.18269175736958,48.0,26452.24711449948,12.0,1.0 98 | -0.10083033653313556,62,1,2017-01-15,8.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,1.0,0.0 99 | 0.05574266123825844,62,2,2017-02-14,7.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,2.0,0.0 100 | 0.36111023584956614,62,3,2017-03-16,6.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,3.0,0.0 101 | 0.25881832683678213,62,4,2017-04-15,7.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,4.0,2.0 102 | 0.631842448611261,62,5,2017-05-15,8.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,5.0,4.0 103 | 0.6219317151610295,62,6,2017-06-14,8.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,6.0,0.0 104 | 0.9540777014842765,62,7,2017-07-14,8.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,7.0,1.0 105 | 0.5828380479174164,62,8,2017-08-13,9.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,8.0,3.0 106 | 0.4384212804529136,62,9,2017-09-12,8.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,9.0,1.0 107 | 0.39367171950350255,62,10,2017-10-12,7.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,10.0,0.0 108 | 0.09246676668106352,62,11,2017-11-11,8.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,11.0,1.0 109 | 0.02840711553812883,62,12,2017-12-11,8.0,4.0,4804.204981778229,0.0,3840.2377459076856,58.45573398813494,60.0,19216.819927112916,12.0,0.0 110 | 0.4128576497858034,98,1,2017-01-15,7.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,1.0,0.0 111 | 0.14289995289897062,98,2,2017-02-14,8.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,2.0,0.0 112 | 0.5420951018570678,98,3,2017-03-16,7.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,3.0,4.0 113 | 0.8663338875025108,98,4,2017-04-15,6.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,4.0,0.0 114 | 1.4057095694327084,98,5,2017-05-15,6.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,5.0,7.0 115 | 1.6559779704849853,98,6,2017-06-14,5.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,6.0,5.0 116 | 2.2483030913107402,98,7,2017-07-14,4.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,7.0,3.0 117 | 2.3035941324021074,98,8,2017-08-13,3.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,8.0,6.0 118 | 2.2530049392843945,98,9,2017-09-12,2.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,9.0,16.0 119 | 2.041903818179265,98,10,2017-10-12,2.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,10.0,5.0 120 | 1.9070504255125442,98,11,2017-11-11,2.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,11.0,5.0 121 | 2.103169908872118,98,12,2017-12-11,1.0,13.0,4557.78688327677,1.0,3785.1835479577094,42.138239917576556,39.0,59251.229482598006,12.0,16.0 122 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/data/time-series.csv: -------------------------------------------------------------------------------- 1 | t,y 2 | 0.0,-19.30092764671697 3 | 0.01515151515151515,-18.219462077966796 4 | 0.0303030303030303,-17.93100620179666 5 | 0.045454545454545456,-18.556187500202636 6 | 0.0606060606060606,-19.20058245534915 7 | 0.07575757575757576,-18.737634776266635 8 | 0.09090909090909091,-16.458587197985516 9 | 0.10606060606060606,-15.072272462419317 10 | 0.1212121212121212,-12.65825813065553 11 | 0.13636363636363638,-11.134694161748063 12 | 0.15151515151515152,-10.962643640595832 13 | 0.16666666666666666,-12.370018100549334 14 | 0.18181818181818182,-14.785272678127154 15 | 0.19696969696969696,-15.943995640006897 16 | 0.21212121212121213,-17.682588739573735 17 | 0.22727272727272727,-17.765858542174215 18 | 0.2424242424242424,-16.034618377325135 19 | 0.25757575757575757,-15.929170598269678 20 | 0.27272727272727276,-15.255422215383224 21 | 0.2878787878787879,-15.913257053170888 22 | 0.30303030303030304,-16.52381222316946 23 | 0.3181818181818182,-17.68779046352338 24 | 0.3333333333333333,-19.577761106544358 25 | 0.3484848484848485,-21.193256976033965 26 | 0.36363636363636365,-21.573839454278964 27 | 0.3787878787878788,-20.966158670045488 28 | 0.3939393939393939,-18.583468420528774 29 | 0.40909090909090906,-16.713988410535286 30 | 0.42424242424242425,-15.11991974903702 31 | 0.43939393939393934,-13.810660247123582 32 | 0.45454545454545453,-14.129908247430654 33 | 0.4696969696969697,-14.05016458086127 34 | 0.4848484848484848,-15.233278013873761 35 | 0.5,-13.966572381668943 36 | 0.5151515151515151,-12.747253286110032 37 | 0.5303030303030303,-10.285912563533067 38 | 0.5454545454545455,-7.7206513390833 39 | 0.5606060606060606,-5.143071276582004 40 | 0.5757575757575758,-3.15973119141146 41 | 0.5909090909090908,-3.410015467971153 42 | 0.6060606060606061,-4.1077490765020634 43 | 0.6212121212121212,-4.596081050414499 44 | 0.6363636363636364,-5.928227033897725 45 | 0.6515151515151515,-6.638048633808241 46 | 0.6666666666666666,-5.593346433891444 47 | 0.6818181818181818,-3.8894780339748536 48 | 0.696969696969697,-2.429566240481723 49 | 0.712121212121212,-2.0501075042141714 50 | 0.7272727272727273,-2.381946226994309 51 | 0.7424242424242423,-3.6471452737000227 52 | 0.7575757575757576,-6.114389175489983 53 | 0.7727272727272727,-8.06819898610401 54 | 0.7878787878787878,-9.912441890667758 55 | 0.803030303030303,-10.48842137133315 56 | 0.8181818181818181,-9.68239064261209 57 | 0.8333333333333333,-8.680276315413868 58 | 0.8484848484848485,-6.557818560035042 59 | 0.8636363636363636,-6.7073674164687525 60 | 0.8787878787878787,-6.59500598964304 61 | 0.8939393939393939,-7.910867544679576 62 | 0.9090909090909091,-8.780525250599903 63 | 0.9242424242424242,-9.81754681499304 64 | 0.9393939393939394,-9.062063950096407 65 | 0.9545454545454545,-7.485165464763737 66 | 0.9696969696969696,-4.721175430962729 67 | 0.9848484848484849,-1.8590790677106916 68 | 1.0,0.0 69 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/src/Part2.jl: -------------------------------------------------------------------------------- 1 | module Part2 2 | 3 | using Turing 4 | using StatsPlots 5 | using DataFrames 6 | using DiffEqBase 7 | 8 | export to_chains, plot_trajectories!, plot_trajectories, plot_trajectory_quantiles!, plot_trajectory_quantiles, issuccess 9 | 10 | # Improve `show` a bit for the sake of presentation and demonstration. 11 | function Base.show(io::IO, model::DynamicPPL.Model) 12 | println(io, "Model(") 13 | print(io, " args = ") 14 | println(io, keys(model.args)) 15 | print(io, " defaults = ") 16 | println(io, keys(model.defaults)) 17 | print(io, " context = ") 18 | println(io, model.context) 19 | print(io, ")") 20 | end 21 | 22 | # Utility method for converting a `Vector{<:NamedTuple}` to a `Chains` object. 23 | function unravel(x::NamedTuple, vns=Iterators.map(DynamicPPL.VarName, keys(x))) 24 | vn_iter = Iterators.flatten(DynamicPPL.TestUtils.varname_leaves(vn, get(x, vn)) for vn in vns) 25 | return vn_iter, Iterators.map(Base.Fix1(get, x), vn_iter) 26 | end 27 | 28 | function to_chains(results::AbstractMatrix{<:NamedTuple}; kwargs...) 29 | return reduce(MCMCChains.chainscat, map(col -> to_chains(col; kwargs...), eachcol(results));) 30 | end 31 | 32 | """ 33 | plot_trajectories!(p::Plots.Plot, chain::MCMCChains.Chains; kwargs...) 34 | 35 | Plot trajectories in `chain`. 36 | 37 | # Keyword arguments 38 | n = 1000, trajectory_color="#BBBBBB", alpha=1/√n 39 | - `n`: number of trajectories to sample. Default: 1000. 40 | - `trajectory_color`: color of the trajectories to use. Default: "#BBBBBB". 41 | - `alpha`: alpha to use when plotting the trajectories. Default: `1/√n`. 42 | - `data`: include the data in the plot. Default: `nothing`. 43 | - `tspan`: 2-tuple containing first and last time represented by `chain`. Default: (1, 14) 44 | """ 45 | function plot_trajectories!( 46 | p::Plots.Plot, chain::MCMCChains.Chains; 47 | n = 1000, data=nothing, tspan=(1, 14), 48 | trajectory_color="#BBBBBB", alpha=1/√n 49 | ) 50 | # Convert the `chain` into a flattened `Array`. 51 | chain_arr = Array(chain; append_chains=true) # Shape: [num_chains × num_iterations, num_params] 52 | # Sample a subset of indices to use. 53 | total_num_samples = size(chain_arr, 1) 54 | indices = rand(1:total_num_samples, n) # Shape: [n, ] 55 | # Plot 'em! 56 | chain_arr = chain_arr[indices, :] # Shape: [n, num_params] 57 | for in_bed in eachrow(chain_arr) 58 | plot!(p, tspan[1]:tspan[2], in_bed, alpha=alpha, color=trajectory_color) 59 | end 60 | 61 | if !isnothing(data) 62 | scatter!(p, 1:14, data.in_bed, label="Data", color="black") 63 | end 64 | 65 | return p 66 | end 67 | 68 | """ 69 | plot_trajectories(chain::MCMCChains.Chains; kwargs...) 70 | 71 | Plot trajectories in `chain`. 72 | 73 | See [`plot_trajectories!`](@ref) for more info. 74 | """ 75 | plot_trajectories(chain::MCMCChains.Chains; kwargs...) = plot_trajectories!(plot(), chain; kwargs...) 76 | 77 | """ 78 | plot_trajectory_quantiles!(p::Plots.Plot, chain::MCMCChains.Chains; kwargs...) 79 | 80 | Plot quantiles of trajectories in `chain`. 81 | 82 | By default, the 95% quantiles are used. 83 | 84 | # Keyword arguments 85 | - q: quantiles to compute (only three ordered values supported). Default: `[0.025, 0.5, 0.975]`. 86 | - `data`: if specified, include the data in the plot. Default: `nothing`. 87 | - `tspan`: 2-tuple containing first and last time represented by `chain`. Default: (1, 14) 88 | """ 89 | function plot_trajectory_quantiles!( 90 | p::Plots.Plot, chain::MCMCChains.Chains; 91 | q=[0.025, 0.5, 0.975], data=nothing, tspan=(1, 14) 92 | ) 93 | # Compute quantiles. 94 | qs = Array(DataFrame(quantile(chain; q = q))[:, Not(:parameters)]); 95 | 96 | # Plot! 97 | num_timesteps = size(qs, 1) 98 | lb, mid, ub = qs[:, 1], qs[:, 2], qs[:, 3] 99 | plot!(p, tspan[1]:tspan[2], mid, ribbon=(mid - lb, ub - mid)) 100 | 101 | if !isnothing(data) 102 | scatter!(p, 1:14, data.in_bed, label="Data", color="black") 103 | end 104 | 105 | return p 106 | end 107 | 108 | 109 | """ 110 | plot_trajectory_quantiles(chain::MCMCChains.Chains; kwargs...) 111 | 112 | Plot quantiles of trajectories in `chain`. 113 | 114 | See [`plot_trajectory_quantiles!`](@ref) for more info. 115 | """ 116 | plot_trajectory_quantiles(chain::MCMCChains.Chains; kwargs...) = plot_trajectory_quantiles!(plot(), chain; kwargs...) 117 | 118 | """ 119 | to_chains(results; exclude, include) 120 | 121 | Return a `MCMCChains.Chains` constructed from `results`. 122 | 123 | If `exclude` is specified, then those keys will be excluded. 124 | If `include` is specified, then those keys will be included. 125 | """ 126 | function to_chains(results::AbstractVector{<:NamedTuple}; exclude=nothing, include=nothing) 127 | @assert !isempty(results) "input is empty" 128 | 129 | # TODO: Handle ragged arrays. 130 | # Probably best done my first just mapping everything to flatten dictionaries, e.g. 131 | # 132 | # x_vns, x_vals = unravel(x) 133 | # OrderedDict(zip(map(Symbol, x_vns), x_vals)) 134 | # 135 | # (or using Dictionaries.jl for better perf), and then basically `hcat`ing these 136 | # and insertin `missing` where necessary. 137 | 138 | example = first(results) 139 | 140 | # Construct names once. 141 | syms = collect(keys(example)) 142 | # Filter out if desired. 143 | if !isnothing(include) 144 | syms = filter(∈(include), syms) 145 | end 146 | if !isnothing(exclude) 147 | syms = filter(∉(exclude), syms) 148 | end 149 | 150 | @assert !isempty(syms) "symbols to include is empty" 151 | 152 | # Convert to `VarName`. 153 | names = collect(first(unravel(example, map(DynamicPPL.VarName, syms)))) 154 | 155 | # Extract the values. 156 | vals = mapreduce(hcat, results) do x 157 | # NOTE: Providing the `names` here assumes every sample has the same variables. 158 | collect(last(unravel(x, names))) 159 | end 160 | 161 | @assert !isempty(vals) "resulting array is empty" 162 | 163 | vals_transposed = transpose(vals) 164 | # Shape: iterations × num_vars × num_chains 165 | chain_array = reshape(vals_transposed, size(vals_transposed)..., 1) 166 | return MCMCChains.Chains(chain_array, names) 167 | end 168 | 169 | # Utility method to check if we're happy with ODE solution. 170 | """ 171 | issuccess(sol) 172 | 173 | Return `true` if the ODE solution `sol` is successful, and `false` otherwise. 174 | """ 175 | function issuccess(sol) 176 | # Some AD frameworks use their custom types to wrap arrays. 177 | # Hence we just need to unwrap before we access the `retcode`. 178 | # Thanks to multiple dispatch, this is a no-op in the case where 179 | # there's no need for unwrapping. 180 | sol_unwrapped = DiffEqBase.value(sol) 181 | # `Terminated` can occur if we give the solver a callback-function which 182 | # dynamically decides whether or not to terminate. `Success` is, well, success. 183 | return (sol_unwrapped.retcode === ReturnCode.Success) || (sol_unwrapped.retcode === ReturnCode.Terminated) 184 | end 185 | 186 | end # module Part2 187 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-3-Case-Study-JuliaBUGS/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" 3 | AdvancedHMC = "0bf59076-c3b1-5ca4-86bd-e02cd72cde3d" 4 | AdvancedMH = "5b7e9947-ddc0-4b3f-9b55-0d8042f74170" 5 | DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" 6 | Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" 7 | ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" 8 | JuliaBUGS = "ba9fb4c0-828e-4473-b6a1-cd2560fee5bf" 9 | LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c" 10 | LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1" 11 | MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" 12 | ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" 13 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-3-Case-Study-JuliaBUGS/bugs_sir.jl: -------------------------------------------------------------------------------- 1 | using JuliaBUGS 2 | 3 | using Distributions 4 | using DifferentialEquations 5 | 6 | using LogDensityProblems, LogDensityProblemsAD 7 | using AbstractMCMC, AdvancedHMC 8 | using MCMCChains 9 | 10 | # Automatic differentiation 11 | using ReverseDiff 12 | using ForwardDiff 13 | 14 | # Demo 1: classic examples: rats -- details covered in the slides 15 | rats_model = JuliaBUGS.BUGSExamples.rats.model_def; 16 | data = JuliaBUGS.BUGSExamples.rats.data; 17 | inits = JuliaBUGS.BUGSExamples.rats.inits[1]; 18 | model = compile(rats_model, data, inits); 19 | 20 | # Inference 21 | ad_model = ADgradient(:ReverseDiff, model; compile = Val(true)) 22 | 23 | n_samples = 3000; 24 | n_adapts = 1000; 25 | 26 | initial_θ = rand(LogDensityProblems.dimension(model)) 27 | 28 | samples_and_stats = AbstractMCMC.sample( 29 | ad_model, 30 | AdvancedHMC.NUTS(0.65), 31 | n_samples; 32 | chain_type = Chains, 33 | n_adapts = n_adapts, 34 | init_params = initial_θ, 35 | discard_initial = n_adapts, 36 | ) 37 | 38 | # Demo 2: SIR model 39 | 40 | # english_boarding_schools from `outbreaks` 41 | I_data = [3, 8, 26, 76, 225, 298, 258, 233, 189, 128, 68, 29, 14, 4] 42 | 43 | # same as Tor's 44 | function SIR!( 45 | du, # buffer for the updated differential equation 46 | u, # current state 47 | p, # parameters 48 | t, # current time 49 | ) 50 | N = 763 # population 51 | S, I, R = u 52 | β, γ = p 53 | 54 | du[1] = dS = -β * I * S / N 55 | du[2] = dI = β * I * S / N - γ * I 56 | du[3] = dR = γ * I 57 | end 58 | 59 | # registering a distribution 60 | JuliaBUGS.@register_primitive function NegativeBinomial2(μ, ϕ) 61 | p = 1 / (1 + μ / ϕ) 62 | r = ϕ 63 | return NegativeBinomial(r, p) 64 | end 65 | 66 | # another way to register a primitive 67 | function solve_ode(u0, p) 68 | tspan = (0.0, 14.0) 69 | prob = DifferentialEquations.ODEProblem(SIR!, u0, tspan, p) 70 | sol = solve(prob; saveat = 1.0) 71 | return sol[2, 2:15] 72 | end 73 | 74 | JuliaBUGS.@register_primitive solve_ode 75 | 76 | sir_bugs_model = @bugs begin 77 | β ~ truncated(Normal(2, 1), 0, nothing) 78 | γ ~ truncated(Normal(0.4, 0.5), 0, nothing) 79 | ϕ⁻¹ ~ Exponential(1 / 5) 80 | ϕ = inv(ϕ⁻¹) 81 | 82 | p[1] = β 83 | p[2] = γ 84 | 85 | predicted[1:14] = solve_ode(u0[:], p[:]) 86 | for i = 1:14 87 | I_data[i] ~ NegativeBinomial2(predicted[i] + 1e-5, ϕ) 88 | end 89 | 90 | # generated quantities 91 | R0 = β / γ 92 | recovery_time = 1 / γ 93 | infected[1:14] = predicted[:] 94 | end 95 | data = Dict(:I_data => I_data, :u0 => [762.0, 1.0, 0.0]) 96 | inits = (; β = 2, γ = 0.5, ϕ⁻¹ = 0.2) 97 | model = compile(sir_bugs_model, data, inits); 98 | 99 | # Plot with Makie 100 | # Makie is a large package, so we didn't include it in the Project.toml 101 | # you can install it with `] add GLMakie, GraphMakie` 102 | using GLMakie, GraphMakie # only work if you installed Makie 103 | graphplot(model.g, model.parameters) 104 | 105 | # use `ForwardDiff` this time 106 | ad_model = ADgradient(:ForwardDiff, model) 107 | 108 | n_samples = 3000; 109 | n_adapts = 1000; 110 | initial_θ = [1.6, 7, 1.2] 111 | 112 | samples_and_stats = AbstractMCMC.sample( 113 | ad_model, 114 | AdvancedHMC.NUTS(0.65), 115 | n_samples; 116 | chain_type = Chains, 117 | n_adapts = n_adapts, 118 | init_params = initial_θ, 119 | discard_initial = n_adapts, 120 | ) 121 | 122 | samples_and_stats[[:β, :γ, :ϕ⁻¹]] 123 | 124 | # MH 125 | using AdvancedMH, LinearAlgebra 126 | 127 | # AdvancedMH is functional, but the bundle_samples method is not yet implemented 128 | samples_and_stats = AbstractMCMC.sample( 129 | model, 130 | AdvancedMH.RWMH(MvNormal(zeros(3), I)), # A simple random walk proposal 131 | n_samples; 132 | chain_type = Chains, 133 | n_adapts = n_adapts, 134 | init_params = initial_θ, 135 | discard_initial = n_adapts, 136 | ) 137 | 138 | # Parallel sampling with AdvancedHMC 139 | # Start Julia with multiple threads 140 | # julia -t 4 141 | Threads.nthreads() 142 | n_chains = 4 143 | samples_and_stats = AbstractMCMC.sample( 144 | ad_model, 145 | AdvancedHMC.NUTS(0.65), 146 | AbstractMCMC.MCMCThreads(), 147 | n_samples, 148 | n_chains; 149 | chain_type = Chains, 150 | n_adapts = n_adapts, 151 | init_params = [initial_θ for _ = 1:n_chains], 152 | discard_initial = n_adapts, 153 | ) 154 | 155 | # Distributed sampling with AdvancedHMC 156 | # Start Julia with multiple processes 157 | # julia -p 4 158 | using Distributed 159 | 160 | @everywhere begin 161 | using JuliaBUGS 162 | using Distributions 163 | using DifferentialEquations 164 | using LogDensityProblems, LogDensityProblemsAD 165 | using AbstractMCMC, AdvancedHMC 166 | using MCMCChains 167 | using ForwardDiff 168 | end 169 | 170 | # Here, we define the functions for each process. 171 | # A more efficient practice would be to encapsulate these functions within a module and then use `@everywhere` to import the module. 172 | # This ensures that the functions are accessible across all processes. 173 | @everywhere begin 174 | function SIR!( 175 | du, # buffer for the updated differential equation 176 | u, # current state 177 | p, # parameters 178 | t, # current time 179 | ) 180 | N = 763 # population 181 | S, I, R = u 182 | β, γ = p 183 | 184 | du[1] = dS = -β * I * S / N 185 | du[2] = dI = β * I * S / N - γ * I 186 | du[3] = dR = γ * I 187 | end 188 | 189 | JuliaBUGS.@register_primitive function NegativeBinomial2(μ, ϕ) 190 | p = 1 / (1 + μ / ϕ) 191 | r = ϕ 192 | return NegativeBinomial(r, p) 193 | end 194 | 195 | function solve_ode(u0, p) 196 | tspan = (0.0, 14.0) 197 | prob = DifferentialEquations.ODEProblem(SIR!, u0, tspan, p) 198 | sol = solve(prob; saveat = 1.0) 199 | return sol[2, 2:15] 200 | end 201 | JuliaBUGS.@register_primitive solve_ode 202 | end 203 | 204 | n_chains = nprocs() 205 | samples_and_stats = AbstractMCMC.sample( 206 | ad_model, 207 | AdvancedHMC.NUTS(0.65), 208 | AbstractMCMC.MCMCDistributed(), 209 | n_samples, 210 | n_chains; 211 | chain_type = Chains, 212 | n_adapts = n_adapts, 213 | init_params = [initial_θ for _ = 1:4], 214 | discard_initial = n_adapts, 215 | progress = false, # Base.TTY creating problems in distributed setting 216 | ) -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-3-Case-Study-JuliaBUGS/presentation.md: -------------------------------------------------------------------------------- 1 | --- 2 | marp: true 3 | theme: default 4 | class: 5 | - lead 6 | paginate: true 7 | --- 8 | 9 | # **JuliaBUGS** 10 | 11 | --- 12 | 13 | ## **What is `BUGS`?** 14 | 15 | - Influential project that began in 1989, right here 16 | - The first Probabilistic Programming Language 17 | - Software for Bayesian analysis of statistical model 18 | - Originally with Gibbs 19 | - Now HMC/NUTS with JuliaBUGS, and more algorithms coming 20 | 21 | --- 22 | 23 | ## **Why Choose `JuliaBUGS`?** 24 | 25 | - Compatible with existing `BUGS` syntax 26 | - User-friendly modelling language 27 | - Generates the Directed Acyclic Graph (DAG) 28 | - Works seamlessly with other Julia packages 29 | - Supports distributed, parallel, and GPU computations* 30 | 31 | --- 32 | 33 | ## **A Simple Example - Rats** 34 | 35 | The weights of 30 young rats were measured weekly for five weeks 36 | 37 | Rat | $x_j = 8$ | $x_j = 15$ | $x_j = 22$ | $x_j = 29$ | $x_j = 36$ | 38 | --- | --- | --- | --- | --- | --- | 39 | 1 | 151 | 199 | 246 | 283 | 320 | 40 | 2 | 145 | 199 | 249 | 293 | 354 | 41 | $\cdots$ | | | | | | 42 | 30 | 153 | 200 | 244 | 286 | 324 | 43 | 44 | --- 45 | 46 | ## **The Rats Model** 47 | 48 | $$ 49 | Y_{ij} \sim \text{Normal}\left( \alpha_i + \beta_i \left( x_j - \bar{x} \right), \tau_c \right) 50 | $$ 51 | 52 | $$ 53 | \alpha_i \sim \text{Normal}\left( \alpha_c, \tau_\alpha \right) 54 | $$ 55 | 56 | $$ 57 | \beta_i \sim \text{Normal}\left( \beta_c, \tau_\beta \right) 58 | $$ 59 | 60 | --- 61 | 62 | ## **`BUGS` Program for the Rats Model** 63 | 64 | ```julia 65 | model_def = @bugs("""model{ 66 | for( i in 1 : N ) { 67 | for( j in 1 : T ) { 68 | Y[i , j] ~ dnorm(mu[i , j],tau.c) 69 | mu[i , j] <- alpha[i] + beta[i] * (x[j] - xbar) 70 | } 71 | alpha[i] ~ dnorm(alpha.c,alpha.tau) 72 | beta[i] ~ dnorm(beta.c,beta.tau) 73 | } 74 | tau.c ~ dgamma(0.001,0.001) 75 | sigma <- 1 / sqrt(tau.c) 76 | alpha.c ~ dnorm(0.0,1.0E-6) 77 | alpha.tau ~ dgamma(0.001,0.001) 78 | beta.c ~ dnorm(0.0,1.0E-6) 79 | beta.tau ~ dgamma(0.001,0.001) 80 | alpha0 <- alpha.c - xbar * beta.c 81 | }""", false) # `false` means R-style variable names are kept 82 | ``` 83 | 84 | **Nothing** is different from the original BUGS 85 | 86 | --- 87 | 88 | ## **Workflow in `JuliaBUGS`** 89 | 90 | **Step 1** *Write the model in Julia-flavored or the original `BUGS` syntax, then use `@bugs` macro to transform it into Julia AST* 91 | 92 | ```julia 93 | quote 94 | for i = 1:N 95 | for j = 1:T 96 | $(Expr(:~, :(Y[i, j]), :(dnorm(mu[i, j], var"tau.c")))) 97 | mu[i, j] = alpha[i] + beta[i] * (x[j] - xbar) 98 | end 99 | $(Expr(:~, :(alpha[i]), :(dnorm(var"alpha.c", var"alpha.tau")))) 100 | $(Expr(:~, :(beta[i]), :(dnorm(var"beta.c", var"beta.tau")))) 101 | end 102 | $(Expr(:~, Symbol("tau.c"), :(dgamma(0.001, 0.001)))) 103 | sigma = 1 / sqrt(var"tau.c") 104 | $(Expr(:~, Symbol("alpha.c"), :(dnorm(0.0, 1.0e-6)))) 105 | $(Expr(:~, Symbol("alpha.tau"), :(dgamma(0.001, 0.001)))) 106 | $(Expr(:~, Symbol("beta.c"), :(dnorm(0.0, 1.0e-6)))) 107 | $(Expr(:~, Symbol("beta.tau"), :(dgamma(0.001, 0.001)))) 108 | alpha0 = var"alpha.c" - xbar * var"beta.c" 109 | end 110 | ``` 111 | 112 | --- 113 | 114 | ## **Workflow in `JuliaBUGS` (cont.)** 115 | 116 | **Step 2** *Prepare data and initial values* 117 | 118 | ```julia 119 | data=( 120 | x=[8.0, 15.0, 22.0, 29.0, 36.0], 121 | xbar=22, N=30, T=5, 122 | Y=[ 123 | 151 199 246 283 320 124 | 145 199 249 293 354 125 | ... 126 | 153 200 244 286 324 127 | ], 128 | ) 129 | inits=( 130 | alpha=ones(Int, 30) .* 250, 131 | beta=ones(Int, 30) .* 6, 132 | var"alpha.c"=150, var"beta.c"=10, var"tau.c"=1, 133 | var"alpha.tau"=1, var"beta.tau"=1, 134 | ) 135 | ``` 136 | 137 | --- 138 | 139 | ## **Workflow in `JuliaBUGS` (cont.)** 140 | 141 | **Step 3** *Compile the model* 142 | 143 | ```julia 144 | model = compile(model_def, data, inits) 145 | ``` 146 | 147 | A series of functions are implemented for the `model`: 148 | 149 | ```julia-repl 150 | julia> LogDensityProblems.dimension(model) # number of parameters 151 | 65 152 | ``` 153 | 154 | and 155 | 156 | ```julia-repl 157 | julia> LogDensityProblems.logdensity(model, rand(65)) # log-density of the model 158 | -1.2096997168777153e7 159 | ``` 160 | 161 | --- 162 | 163 | ## **Visualizing the `BUGSModel` as a DAG** 164 | 165 | To visualize the Directed Acyclic Graph (DAG) underlying the `BUGSModel`, we can utilize standard plotting libraries. In this example, we employ [`Makie.jl`](https://github.com/MakieOrg/GraphMakie.jl) and illustrate the DAG using only the first two rows of the rats' weights data. 166 | 167 | ![width:800](./rats_plot.png) 168 | 169 | --- 170 | 171 | ## **Workflow in `JuliaBUGS` (cont.)** 172 | 173 | **Step 4** *Transform the model to be AD-ready (only necessary for HMC/NUTS)* 174 | 175 | ```julia 176 | ad_model = ADgradient(:ReverseDiff, model; compile = Val(true)) # if using ReverseDiff 177 | ad_model = ADgradient(:ForwardDiff, model) # if using ForwardDiff 178 | ``` 179 | 180 | After the transformation, we can take the gradient of the log-density function 181 | 182 | ```julia-repl 183 | julia>logp, gradients = LogDensityProblems.logdensity_and_gradient(ad_model, rand(65)); 184 | 185 | julia> gradients 186 | 65-element Vector{Float64}: 187 | 11.027615568346802 188 | 14.156247939871195 189 | 3288.2000788518317 190 | ... 191 | 1800.9201449927032 192 | ``` 193 | 194 | --- 195 | 196 | ## **Workflow in `JuliaBUGS` (cont.)** 197 | 198 | **Step 5** *Ready to sample!* 199 | 200 | ```julia 201 | n_samples = 3000; n_adapts = 1000 # number of samples and adaptation steps 202 | initial_θ = rand(LogDensityProblems.dimension(model)) # initial values 203 | samples_and_stats = AbstractMCMC.sample( 204 | ad_model, 205 | AdvancedHMC.NUTS(0.65), 206 | n_samples; 207 | chain_type = Chains, 208 | n_adapts = n_adapts, 209 | init_params = initial_θ, 210 | discard_initial = n_adapts, 211 | ) 212 | ``` 213 | 214 | ### Here we 215 | 216 | - Use the `AbstractMCMC` interface to sample 217 | - An `MCMCChains.Chain` object is returned (see next slide) 218 | 219 | --- 220 | 221 | ## **Workflow in `JuliaBUGS` (cont.)** 222 | 223 | The sampling results (simplified for legibility) 224 | 225 | ```julia 226 | Chains MCMC chain (3000×80×1 Array{Real, 3}): 227 | ... 228 | Summary Statistics 229 | parameters mean std mcse ess_bulk rhat ess_per_sec 230 | Symbol Float64 Float64 Float64 Real Float64 Missing 231 | 232 | beta.c 6.1856 0.1083 0.0018 3594.9367 1.0005 missing 233 | sigma 6.0966 0.4802 0.0119 1640.5283 0.9998 missing 234 | alpha0 106.6111 3.5747 0.0574 3884.4420 1.0005 missing 235 | 236 | Quantiles 237 | parameters 2.5% 25.0% 50.0% 75.0% 97.5% 238 | Symbol Float64 Float64 Float64 Float64 Float64 239 | 240 | beta.c 5.9663 6.1156 6.1855 6.2578 6.3940 241 | sigma 5.2499 5.7528 6.0588 6.4025 7.1354 242 | alpha0 99.8404 104.1624 106.6278 108.9566 113.6170 243 | ``` 244 | 245 | which contains all the sample values and statistics 246 | 247 | --- 248 | 249 | ## **Registering Functions and Distributions** 250 | 251 | `JuliaBUGS` has implemented most of the distributions and functions in `BUGS` 252 | 253 | Users can also register their own functions and distributions by 254 | 255 | ```julia 256 | JuliaBUGS.@register_primitive function myfunc(x::Float64, y::Float64) 257 | return x + y 258 | end 259 | ``` 260 | 261 | alternatively, 262 | 263 | ```julia 264 | function myfunc(x::Float64, y::Float64) 265 | return x + y 266 | end 267 | JuliaBUGS.@register_primitive myfunc 268 | ``` 269 | 270 | then `myfunc` is ready to use in a `JuliaBUGS` model 271 | 272 | --- 273 | 274 | ## **Source Folder Tour** 275 | 276 | ```bash 277 | ├── ext # package extensions 278 | │   ├── JuliaBUGSAdvancedHMCExt.jl 279 | │   ├── JuliaBUGSGraphMakieExt.jl 280 | │   ├── JuliaBUGSGraphPlotExt.jl 281 | │   └── JuliaBUGSTikzGraphsExt.jl 282 | ├── src # source code 283 | │   ├── BUGSExamples # defines examples from OpenBUGS 284 | │   │   ├── BUGSExamples.jl 285 | │   │   ├── Backgrounds 286 | │   │   │   ├── README.md 287 | │   │   │   └── rats.md 288 | │   │   ├── README.md 289 | │   │   ├── Volume_I 290 | │   │   │   ├── Blocker.jl 291 | ... 292 | │   │   └── Volume_II 293 | │   │   ├── BiRats.jl 294 | │   │   └── Eyes.jl 295 | │   ├── BUGSPrimitives # defines BUGS distributions and functions 296 | │   │   ├── BUGSPrimitives.jl 297 | │   │   ├── distributions.jl 298 | │   │   └── functions.jl 299 | │   ├── JuliaBUGS.jl 300 | │   ├── compiler_pass.jl # compiler implementation 301 | │   ├── graphs.jl # the DAG MetaGraph interface 302 | │   ├── logdensityproblems.jl # the LogDensityProblem interface 303 | │   ├── model.jl # defines the model and log-density computation 304 | │   ├── parser.jl # the parser of BUGS 305 | │   ├── utils.jl 306 | │   └── variable_types.jl # defines some datatype used in the compiler 307 | ``` 308 | 309 | --- 310 | 311 | ## **Current Status and Caveats** 312 | 313 | - `JuliaBUGS` is in early development. Bugs and discrepancies with `BUGS` may exist. 314 | - The compiler is not optimized for large models. 315 | - Initial `compile` function calls may be slow, but subsequent calls will be faster. 316 | - Inference is supported via the `LogDensityProblem` interface with HMC/NUTS from `AdvancedHMC.jl` and MH algorithms from `AdvancedMH.jl`. 317 | - We are working on exploiting the graphical structure for implementing algorithms. 318 | 319 | --- 320 | 321 | ## **Future Plans** 322 | 323 | - Optimize the compiler to handle larger models efficiently 324 | - Extend inference support with more algorithms and more efficient implementations 325 | - Continue testing and debugging 326 | 327 | # **Collaborate with us!** 328 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-3-Case-Study-JuliaBUGS/presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-3-Case-Study-JuliaBUGS/presentation.pdf -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-3-Case-Study-JuliaBUGS/rats_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-3-Case-Study-JuliaBUGS/rats_plot.png -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-4-Case-Study-GPs-in-Julia/Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918" 3 | CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 4 | DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe" 5 | DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 6 | KernelFunctions = "ec8451be-7e33-11e9-00cf-bbf324bd1392" 7 | LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 8 | LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688" 9 | MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" 10 | Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" 11 | Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" 12 | ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" 13 | StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" 14 | Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" 15 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-4-Case-Study-GPs-in-Julia/README.md: -------------------------------------------------------------------------------- 1 | # GP Session 2 | 3 | Please activate this folder, instantiate the packages, and run 4 | ```julia 5 | include("script.jl") 6 | putting_example() 7 | ``` 8 | in order to check that everything is working as intended. 9 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-4-Case-Study-GPs-in-Julia/explainer_script.jl: -------------------------------------------------------------------------------- 1 | using AbstractGPs, KernelFunctions, Random 2 | import KernelFunctions: kernelmatrix, kernelmatrix_diag, TestUtils 3 | 4 | 5 | 6 | # 7 | # Using kernels from KernelFunctions.jl 8 | # 9 | 10 | # You can call a kernel on individual inputs to return a scalar: 11 | SEKernel()(5.0, 4.0) 12 | 13 | # In practice, you usually want to compute the kernel for a collection of inputs. 14 | # The function `kernelmatrix` does this. 15 | # There are several methods, but the first two listed are the important ones: 16 | # kernelmatrix(::Kernel, ::AbstractVector) 17 | # and 18 | # kernelmatrix(::Kernel, ::AbstractVector, ::AbstractVector) 19 | # Take a look at the docstring for details: 20 | @doc kernelmatrix 21 | 22 | # For example, in the one-dimensional case, you might do something like: 23 | x = randn(5) 24 | y = randn(4) 25 | kernelmatrix(SEKernel(), x, y) 26 | kernelmatrix(SEKernel(), x) 27 | 28 | # What variations? 29 | Matern32Kernel() 30 | with_lengthscale(SEKernel(), 2.0) 31 | 5.0 * with_lengthscale(SEKernel(), 2.0) 32 | 5.0 * with_lengthscale(Matern32Kernel(), 2.0) 33 | 5.0 * with_lengthscale(Matern32Kernel() + SEKernel(), 2.0) 34 | 5.0 * with_lengthscale(Matern32Kernel() * SEKernel(), 2.0) 35 | 36 | # If your data is multi-dimensional, store it data in a `Matrix`, and wrap this in 37 | # a `ColVecs` (if your matrix represents a collection of column vectors), and `RowVecs` (if 38 | # your matrix represents a collection of row vectors). For example, if we have 3-dimensional 39 | # data, we might do something like: 40 | x = ColVecs(randn(3, 5)) 41 | y = ColVecs(randn(3, 4)) 42 | kernelmatrix(SEKernel(), x, y) 43 | kernelmatrix(SEKernel(), x) 44 | 45 | # Note that length(x) == 5 and length(y) == 4 -- `ColVecs` is itself an `AbstractVector`. 46 | # It is a lightweight abstraction around a `Matrix` that says "interpret this data as a 47 | # vector-of-vectors". 48 | 49 | # This is rather than storing data as a vector-of-vectors, which has a number of negative 50 | # performance implications. 51 | 52 | # Note that the use of `ColVecs` and `RowVecs` resolves any ambiguity around how a matrix of 53 | # data ought to be interpreted (i.e. is each column an input, or each row an input?), and 54 | # doesn't introduce any performance problems. See the docs for more details around this 55 | # design choice: 56 | # https://juliagaussianprocesses.github.io/KernelFunctions.jl/dev/design/#why_abstract_vectors 57 | 58 | # Combining kernels 59 | # Normalisation of kernel (eveything is normalised by default). 60 | 61 | # 62 | # Defining your own kernel 63 | # 64 | 65 | # A kernel is defined via a struct. 66 | struct MyEQKernel{Tl<:Real, Tv<:Real} <: Kernel 67 | l::Tl 68 | v::Tv 69 | end 70 | 71 | # At a minimum, you must specify what evaluating your kernel on a pair of inputs returns. 72 | (k::MyEQKernel)(x::Real, y::Real) = k.v * exp(-(x - y)^2 / (2 * k.l^2)) 73 | 74 | # To check that you have satisfied the `Kernel` interface, you can call the 75 | # `TestUtils.test_interface` function. 76 | rng = Xoshiro(123456) 77 | TestUtils.test_interface(MyEQKernel(5.0, 4.0), randn(rng, 5), randn(rng, 5), randn(rng, 4)) 78 | 79 | # Specifying how to evaluate your kernel means that various high-level functions get default 80 | # definitions: `kernelmatrix`, `kernelmatrix_diag`, and their in-place versions, 81 | # `kernelmatrix`, and `kernelmatrix_diag!`. 82 | # Often you will find it beneficial to performance to implement these directly, in addition 83 | # to defining kernel evaluation: 84 | kernelmatrix(k::MyEQKernel, x::Vector{<:Real}) = k.v * exp.((x .- x').^2 ./ (-2 * k.l)) 85 | 86 | # `TestUtils.test_interface` should be run again: 87 | TestUtils.test_interface(MyEQKernel(5.0, 4.0), randn(rng, 5), randn(rng, 5), randn(rng, 4)) 88 | 89 | # `TestUtils.test_interface` has found a disagreement between the implementation of 90 | # `kernelmatrix`, and `(k::MyEQKernel)(x, y)`. If we fix this, tests should pass. 91 | kernelmatrix(k::MyEQKernel, x::Vector{<:Real}) = k.v * exp.((x .- x').^2 ./ (-2 * k.l^2)) 92 | TestUtils.test_interface(MyEQKernel(5.0, 4.0), randn(rng, 5), randn(rng, 5), randn(rng, 4)) 93 | 94 | 95 | # 96 | # Using AbstractGPs.jl 97 | # 98 | 99 | # AbstractGPs provides tools which allow you to specify a GP, 100 | # produce a multivariate Gaussian from the GP at a finite collection of inputs, and 101 | # do all the usual things you would hope to do with a multivariate Gaussian + conditioning 102 | # on data to produce an updated GP. 103 | 104 | # The GP type is a thin wrapper around a `MeanFunction` and `Kernel`. If you want a zero- 105 | # mean GP, do not provide a `MeanFunction`: 106 | f = GP(MyEQKernel(5.0, 4.0)) 107 | 108 | # This particular GP should be thought of as a distribution over functions. 109 | # In practice we typically work with the multivariate Gaussian obtained by considering the 110 | # GP at only a finite number of inputs: 111 | x = randn(5) 112 | fx = f(x, 0.1) 113 | 114 | # In practice, we always add some noise (for numerical reasons). So `fx` should be thought 115 | # of as the sum of `f` at `x` with a vector of `length(x)` IID Gaussian random variables, 116 | # with zero-mean and variance `0.1`. 117 | 118 | # `fx` is a multivariate Gaussian distribution, so you can sample from it: 119 | y = rand(rng, fx) 120 | 121 | # Compute the `logpdf` of a sample: 122 | logpdf(fx, y) 123 | 124 | # This function can be differentiated using Zygote, so is well-suited for use with Turing, 125 | # which requires this for HMC. 126 | 127 | # Moreover, you can condition on data: 128 | f_post = posterior(fx, y) 129 | 130 | # `f_post` is just another `AbstractGP`, and can do all of the things that a `GP` can. 131 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-4-Case-Study-GPs-in-Julia/extra_script.jl: -------------------------------------------------------------------------------- 1 | using AbstractGPs, CSV, DataDeps, DataFrames, KernelFunctions, LinearAlgebra, 2 | LogExpFunctions, MCMCChains, Plots, Random, RDatasets, Turing, Zygote 3 | 4 | @model function mcycle_model(t; jitter=1e-4) 5 | 6 | # Log variance process. 7 | v2 ~ Gamma(2, 1) 8 | l2 ~ Gamma(4, 1) 9 | log_σ = GP(v2 * with_lengthscale(SEKernel(), l2)) 10 | log_σ_t ~ log_σ(t, jitter) 11 | 12 | # Mean process. 13 | v1 ~ Gamma(2, 1) 14 | l1 ~ Gamma(4, 1) 15 | f = GP(v1 * with_lengthscale(SEKernel(), l1)) 16 | dist_y = transformed(f(t, 1e-2), Bijectors.Scale(exp.(log_σ_t))) 17 | y ~ dist_y 18 | return (y=y, dist_y=dist_y, f=f, log_σ_t=log_σ_t) 19 | end 20 | 21 | Turing.setadbackend(:zygote) 22 | 23 | function main() 24 | df = dataset("MASS", "mcycle") 25 | m = mcycle_model(df.Times) 26 | 27 | scatter(df.Times, m().y) 28 | 29 | # Standardise the data. 30 | a = (df.Accel .- mean(df.Accel)) ./ std(df.Accel) 31 | 32 | m_post = m | (y=a, ) 33 | sampler = Gibbs(ESS(:log_σ_t), HMC(0.03, 5, :v1, :l1, :v2, :l2)) 34 | chn_ess = sample(Xoshiro(123456), m_post, sampler, 1_000) 35 | 36 | # Debug hypers. 37 | hypers = map(collect ∘ vec, get(chn_ess, [:l1, :v1, :l2, :v2])) 38 | savefig(plot(map(x -> plot(x; label=""), hypers)...; layout=(2, 2)), "hypers.png") 39 | 40 | # Make predictions for y. 41 | preds = predict(Xoshiro(123456), m, chn_ess) 42 | ys = [x.y for x in generated_quantities(m, preds)] 43 | plt = plot() 44 | plot!(plt, df.Times, ys[500:10:end]; alpha=0.5, label="", color=:blue) 45 | scatter!(df.Times, a; label="y", color=:red) 46 | savefig(plt, "mcycle_partially_conditioned_samples.png") 47 | 48 | # TODO: generate posterior samples at these locations and plot them 49 | t_pred = range(0.0, 60.0; length=250) 50 | end 51 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-4-Case-Study-GPs-in-Julia/presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TuringLang/Turing-Workshop/5f34c470a1cbd990c74eebdde828270dcbe737cd/2023-MRC-BSU-and-UKHSA/Part-4-Case-Study-GPs-in-Julia/presentation.pdf -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-4-Case-Study-GPs-in-Julia/script.jl: -------------------------------------------------------------------------------- 1 | using AbstractGPs, CSV, DataDeps, DataFrames, KernelFunctions, LinearAlgebra, 2 | LogExpFunctions, MCMCChains, Plots, Random, ReverseDiff, Turing 3 | 4 | ENV["DATADEPS_ALWAYS_ACCEPT"] = true 5 | 6 | # We're going to use ReverseDiff to perform AD. 7 | Turing.setadbackend(:reversediff) 8 | Turing.setrdcache(true) 9 | 10 | # Very small dataset from Gelman et al. 11 | register(DataDep( 12 | "putting", 13 | "Putting data from BDA", 14 | "http://www.stat.columbia.edu/~gelman/book/data/golf.dat", 15 | "fc28d83896af7094d765789714524d5a389532279b64902866574079c1a977cc", 16 | )); 17 | 18 | @model function putting_model(d, n; jitter=1e-4) 19 | v ~ Gamma(2, 1) 20 | l ~ Gamma(4, 1) 21 | f = GP(v * with_lengthscale(SEKernel(), l)) 22 | f_latent ~ f(d, jitter) 23 | y ~ product_distribution(Binomial.(n, logistic.(f_latent))) 24 | return (fx=f(d, jitter), f_latent=f_latent, y=y) 25 | end 26 | 27 | function plot_data(d, n, y, xticks, yticks) 28 | ylims = (0, round(maximum(n), RoundUp; sigdigits=2)) 29 | margin = -0.5 * Plots.mm 30 | plt = plot(xticks=xticks, yticks=yticks, ylims=ylims, margin=margin, grid=false) 31 | bar!(plt, d, n; color=:red, label="", alpha=0.5) 32 | bar!(plt, d, y; label="", color=:blue, alpha=0.7) 33 | return plt 34 | end 35 | 36 | function putting_example() 37 | 38 | # Load up data. 39 | fname = joinpath(datadep"putting", "golf.dat") 40 | df = CSV.read(fname, DataFrame; delim=' ', ignorerepeated=true) 41 | 42 | # Construct model and run some prior predictive checks. 43 | m = putting_model(Float64.(df.distance), df.n) 44 | hists = map(1:20) do j 45 | xticks = j > 15 ? :auto : nothing 46 | yticks = rem(j, 5) == 1 ? :auto : nothing 47 | return plot_data(df.distance, df.n, m().y, xticks, yticks) 48 | end 49 | savefig(plot(hists...; layout=(4, 5), dpi=300), "prior_pred.png") 50 | 51 | # Construct a simple model with a latent GP. 52 | m_post = m | (y=df.y, ) 53 | chn = sample(Xoshiro(123456), m_post, NUTS(), 1_000) 54 | display(chn) 55 | println() 56 | 57 | # Compute sample probabilities of success. 58 | d_pred = 1:0.2:21 59 | samples = map(generated_quantities(m_post, chn)[1:10:end]) do x 60 | return logistic.(rand(posterior(x.fx, x.f_latent)(d_pred, 1e-4))) 61 | end 62 | p = plot() 63 | plot!(d_pred, reduce(hcat, samples); label="", color=:blue, alpha=0.2) 64 | scatter!(df.distance, df.y ./ df.n; label="", color=:red) 65 | savefig(p, "putting_success_probs.png") 66 | 67 | # Generate some replications of y to get a sense for the remaining marginal uncertainty. 68 | rng = Xoshiro(123456) 69 | ys = [x.y for x in generated_quantities(m, predict(rng, m, chn))[1:10:end]] 70 | post_hists = map(1:20) do j 71 | xticks = j > 15 ? :auto : nothing 72 | yticks = rem(j, 5) == 1 ? :auto : nothing 73 | return plot_data(df.distance, df.n, ys[j], xticks, yticks) 74 | end 75 | hists = vcat(plot_data(df.distance, df.n, df.y, nothing, :auto), post_hists[2:end]) 76 | savefig(plot(hists...; layout=(4, 5), dpi=300), "posterior_pred.png") 77 | 78 | # # Sample using ESS + HMC. 79 | # chn_ess = sample(Xoshiro(123456), m_post, Gibbs(ESS(:f_latent), HMC(0.1, 3, :v, :l)), 1_000) 80 | end 81 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-5-free-form-session/MyResearch/Project.toml: -------------------------------------------------------------------------------- 1 | name = "MyResearch" 2 | uuid = "337232d1-fc7d-4f60-b490-ad55f9e51d72" 3 | authors = ["Tor Erlend Fjelde "] 4 | version = "0.1.0" 5 | 6 | [deps] 7 | BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" 8 | CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" 9 | DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" 10 | Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" 11 | DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa" 12 | Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" 13 | LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" 14 | ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca" 15 | StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" 16 | Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" 17 | TuringBenchmarking = "0db1332d-5c25-4deb-809f-459bc696f94f" 18 | TuringCallbacks = "ea0860ee-d0ef-45ef-82e6-cc37d6be2f9c" 19 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-5-free-form-session/MyResearch/not_a_module.jl: -------------------------------------------------------------------------------- 1 | using DataFrames, CSV 2 | 3 | function load_data() 4 | return DataFrame(CSV.File("/home/tor/Projects/public/Turing-Workshop/2023-MRC-BSU-and-UKHSA/Part-2-More-Julia-and-some-Bayesian-inference/data/influenza_england_1978_school.csv")) 5 | end 6 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-5-free-form-session/MyResearch/scripts/inference.jl: -------------------------------------------------------------------------------- 1 | using MyResearch 2 | 3 | data = MyResearch.load_data() 4 | 5 | using Turing, DifferentialEquations, StatsPlots 6 | 7 | N = 763 8 | 9 | function SIR!( 10 | du, # buffer for the updated differential equation 11 | u, # current state 12 | p, # parameters 13 | t # current time 14 | ) 15 | S, I, R = u 16 | β, γ = p 17 | 18 | du[1] = dS = -β * I * S / N 19 | du[2] = dI = β * I * S / N - γ * I 20 | du[3] = dR = γ * I 21 | end 22 | 23 | # plot( 24 | # plot(truncated(Normal(2, 1); lower=0)), 25 | # plot(truncated(Normal(0.4, 0.5); lower=0)), 26 | # layout=(2, 1) 27 | # ) 28 | 29 | 30 | @model function sir_model( 31 | num_days; # Number of days to model 32 | tspan = (0.0, float(num_days)), # Timespan to model 33 | u0 = [N - 1, 1, 0.0], # Initial state 34 | p0 = [2.0, 0.6], # Placeholder parameters 35 | problem = ODEProblem(SIR!, u0, tspan, p0) # Create problem once so we can `remake`. 36 | ) 37 | β ~ truncated(Normal(2, 1); lower=0) 38 | γ ~ truncated(Normal(0.4, 0.5); lower=0) 39 | ϕ⁻¹ ~ Exponential(1/5) 40 | ϕ = inv(ϕ⁻¹) 41 | 42 | problem_new = remake(problem, p=[β, γ]) # Replace parameters `p`. 43 | sol = solve(problem_new, saveat=1) # Solve! 44 | 45 | # Check that `solve` was a success. 46 | # If it wasn't, we just reject this sample. 47 | if sol.retcode != ReturnCode.Success 48 | Turing.@addlogprob! -Inf 49 | return 50 | end 51 | 52 | sol_for_observed = sol[2, 2:num_days + 1] # Timesteps we have observations for. 53 | in_bed = Vector{Int}(undef, num_days) 54 | for i = 1:length(sol_for_observed) 55 | # Add a small constant to `sol_for_observed` to make things more stable. 56 | in_bed[i] ~ NegativeBinomial2(sol_for_observed[i] + 1e-5, ϕ) 57 | end 58 | end 59 | 60 | in_bed_subset = data.in_bed[1:8] 61 | model = sir_model(7) | (in_bed = in_bed_subset,) 62 | ret = model(); 63 | ret.sol.retcode 64 | 65 | 66 | # Sample. 67 | chain = sample(model, NUTS(), 1000) 68 | mean(chain) 69 | 70 | 71 | ### Create a model where we can start 72 | # sampling from previous last state. 73 | variables_samples = sample(chain, 10) 74 | gen_qts = generated_quantities(model, variables_samples); 75 | length(gen_qts) 76 | 77 | # A single generated quantity to check that it works. 78 | x = gen_qts; 79 | keys(x) 80 | sol = x.sol 81 | u0 = sol[end] 82 | u0 83 | 84 | in_bed_next = data.in_bed[9:end] 85 | model_next = sir_model(length(in_bed_next); u0=u0) | (in_bed=in_bed_next,) 86 | chain_next = sample(model_next, NUTS(), 1000) 87 | 88 | 89 | # All of them. 90 | using ProgressMeter 91 | 92 | chains = [] 93 | @showprogress for genval in gen_qts 94 | u0 = genval.sol[end] 95 | model_next = sir_model(length(in_bed_next); u0=u0) | (in_bed=in_bed_next,) 96 | chain_next = sample(model_next, NUTS(), 1000; progress=false) 97 | push!(chains, chain_next) 98 | end 99 | length(chains) 100 | chains_combined = chainscat(chains...) 101 | 102 | ### Conditioning some variables 103 | model_unconditioned = sir_model(14); 104 | rand(DynamicPPL.OrderedDict, model_unconditioned) 105 | 106 | model_conditioned = model_unconditioned | (@varname(in_bed[1]) => 3, @varname(in_bed[2]) => 5); 107 | rand(DynamicPPL.OrderedDict, model_conditioned) 108 | 109 | ret = model(); 110 | ret.sol.retcode 111 | 112 | 113 | @model function sir_model_fast( 114 | num_days; # Number of days to model 115 | tspan = (0.0, float(num_days)), # Timespan to model 116 | u0 = [N - 1, 1, 0.0], # Initial state 117 | p0 = [2.0, 0.6], # Placeholder parameters 118 | problem = ODEProblem(SIR!, u0, tspan, p0) # Create problem once so we can `remake`. 119 | ) 120 | β ~ truncated(Normal(2, 1); lower=0) 121 | γ ~ truncated(Normal(0.4, 0.5); lower=0) 122 | ϕ⁻¹ ~ Exponential(1/5) 123 | ϕ = inv(ϕ⁻¹) 124 | 125 | problem_new = remake(problem, p=[β, γ]) # Replace parameters `p`. 126 | sol = solve(problem_new, saveat=1) # Solve! 127 | 128 | # Check that `solve` was a success. 129 | # If it wasn't, we just reject this sample. 130 | if sol.retcode != ReturnCode.Success 131 | Turing.@addlogprob! -Inf 132 | return 133 | end 134 | 135 | sol_for_observed = sol[2, 2:num_days + 1] # Timesteps we have observations for. 136 | in_bed ~ product_distribution(NegativeBinomial2.(sol_for_observed .+ 1e-5, ϕ)) 137 | end 138 | 139 | length(in_bed_subset) 140 | model_fast = sir_model_fast(7) | (in_bed = in_bed_subset[1:end - 1],) 141 | chain_fast = sample(model_fast, NUTS(), 1000); 142 | 143 | model = sir_model(100; tspan=(0, 100)) 144 | chain_preds = predict(model, chain_fast) 145 | 146 | size(chain_preds) 147 | 148 | f(x) = x^2 149 | @time f(1) 150 | 151 | @time f.(1:100) 152 | 153 | @time model(); 154 | 155 | using BenchmarkTools 156 | @benchmark $model() 157 | 158 | model() 159 | 160 | using TuringBenchmarking 161 | results = benchmark_model(model_fast; adbackend=[:ForwardDiff,]) 162 | 163 | 164 | # TuringCallbacks.jl: viewing trace in TensorBoard 165 | using TuringCallbacks 166 | callback = TensorBoardCallback("logs") 167 | chain_fast = sample(model_fast, NUTS(), 1000; callback=callback); 168 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-5-free-form-session/MyResearch/src/MyResearch.jl: -------------------------------------------------------------------------------- 1 | module MyResearch 2 | 3 | using DataFrames, CSV, Distributions 4 | 5 | export NegativeBinomial2 6 | 7 | function NegativeBinomial2(μ, ϕ) 8 | p = 1/(1 + μ/ϕ) 9 | r = ϕ 10 | return NegativeBinomial(r, p) 11 | end 12 | 13 | function load_data() 14 | return DataFrame(CSV.File( 15 | joinpath( 16 | @__DIR__, 17 | "..", 18 | "..", 19 | "..", 20 | "Part-2-More-Julia-and-some-Bayesian-inference", 21 | "data", 22 | "influenza_england_1978_school.csv" 23 | ) 24 | )) 25 | end 26 | 27 | end # module MyResearch 28 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/Part-5-free-form-session/README.md: -------------------------------------------------------------------------------- 1 | # Free-form session 2 | 3 | In this session, we set up a "research" project from scratch and started doing a bit of inference. 4 | 5 | ## A research project from scratch 6 | 7 | Here's the guide we followed to set up the project `MyResearch`. 8 | 9 | ### Setting up the project environment 10 | 11 | (1) Navigate to directory where you want the project to be, for example `/tmp`. 12 | 13 | ```sh 14 | # Or whatever directory you want to be in 15 | cd /tmp 16 | ``` 17 | 18 | (2) `]` to get into Pkg 19 | 20 | You should see something like 21 | ```julia 22 | (@v1.9) pkg> 23 | ``` 24 | 25 | (3) From here, you type `generate MyResearch` 26 | 27 | That is, 28 | 29 | ```julia 30 | (@v1.9) pkg> generate MyResearch 31 | ``` 32 | 33 | The following should be your output 34 | 35 | ``` 36 | Generating project MyResearch: 37 | MyResearch/Project.toml 38 | MyResearch/src/MyResearch.jl 39 | ``` 40 | 41 | (4) Navigate into `MyResearch/` 42 | 43 | You can do this by hitting `;` to get into the shell mode, which should look like this 44 | 45 | ```julia 46 | shell> 47 | ``` 48 | 49 | and typing `path/to/MyResearch`, e.g. 50 | 51 | ```julia 52 | shell> cd MyResearch/ 53 | ``` 54 | 55 | (5) Now that our working project is the project directory , we can activate the project environment. 56 | 57 | Again, enter Pkg mode using `]`. You should see 58 | 59 | ```julia 60 | pkg> 61 | ``` 62 | 63 | And from here you can type `activate .`, where the `.` indicates the current folder. That is, 64 | 65 | ```julia 66 | pkg> activate . 67 | ``` 68 | 69 | You should now see 70 | 71 | ```julia 72 | Activating project at `/tmp/MyResearch` 73 | ``` 74 | 75 | (6) If you now type `status`, i.e. 76 | 77 | ```julia 78 | pkg> status 79 | ``` 80 | 81 | you should see an empty environment, e.g. 82 | 83 | ```julia 84 | Project MyResearch v0.1.0 85 | Status `/tmp/MyResearch/Project.toml` (empty project) 86 | ``` 87 | 88 | (7) From here we can add dependenices that we want, e.g. type `add Turing DataFrames StatsPlots DifferentialEquations Distributions Dates LinearAlgebra CSV` 89 | 90 | ```julia 91 | (MyResearch) pkg> add Turing DataFrames StatsPlots DifferentialEquations Distributions Dates LinearAlgebra CSV 92 | ``` 93 | 94 | You subsequently see the following output 95 | 96 | ```julia 97 | Resolving package versions... 98 | Updating `/tmp/adsfas/MyResearch/Project.toml` 99 | [336ed68f] + CSV v0.10.11 100 | [a93c6f00] + DataFrames v1.6.1 101 | [0c46a032] + DifferentialEquations v7.9.1 102 | [31c24e10] + Distributions v0.25.100 103 | [f3b207a7] + StatsPlots v0.15.6 104 | [fce5fe82] + Turing v0.29.1 105 | [ade2ca70] + Dates 106 | [37e2e46d] + LinearAlgebra 107 | Updating `/tmp/adsfas/MyResearch/Manifest.toml` 108 | ... 109 | ``` 110 | 111 | (8) If you run `status` you should now see something like 112 | 113 | ```julia 114 | (MyResearch) pkg> status 115 | Project MyResearch v0.1.0 116 | Status `/tmp/MyResearch/Project.toml` 117 | [336ed68f] CSV v0.10.11 118 | [a93c6f00] DataFrames v1.6.1 119 | [0c46a032] DifferentialEquations v7.9.1 120 | [31c24e10] Distributions v0.25.100 121 | [f3b207a7] StatsPlots v0.15.6 122 | [fce5fe82] Turing v0.29.1 123 | [ade2ca70] Dates 124 | [37e2e46d] LinearAlgebra 125 | ``` 126 | 127 | ### Editing code 128 | 129 | If we look at the files in the project, we'll see the following: 130 | 131 | ```sh 132 | ├── Manifest.toml 133 | ├── Project.toml 134 | └── src 135 | └── MyResearch.jl 136 | 137 | 1 directory, 3 files 138 | ``` 139 | 140 | We can see that in `src/` there is already a file called `MyResearch.jl`. 141 | 142 | Let's see what's already in it: 143 | 144 | ```julia 145 | module MyResearch 146 | 147 | greet() = print("Hello World!") 148 | 149 | end # module MyResearch 150 | ``` 151 | 152 | We can actually access this from our Julia session / REPL now! 153 | 154 | ```julia 155 | julia> using MyResearch 156 | 157 | julia> MyResearch.greet() 158 | Hello World! 159 | ``` 160 | 161 | If we then edit `src/MyResearch.jl` to, say, 162 | 163 | ```julia 164 | module MyResearch 165 | 166 | greet() = print("Hello MRC!") 167 | 168 | end # module MyResearch 169 | ``` 170 | 171 | and go back to the session / REPL, we can re-run our `greet` function: 172 | 173 | ```julia 174 | julia> MyResearch.greet() 175 | Hello MRC! 176 | ``` 177 | 178 | ## Live-coding 179 | 180 | The result of the live-coding you can find in: 181 | - `MyResearch/src/MyResearch.jl`: module `MyResearch` + `load_data`. 182 | - `MyResearch/scripts/inference.jl`: script where we did the following: 183 | - Perform inference on for the SIR model with influenza data from the presentation. 184 | - Condition the model only one the first 7 days, perform inference, and then use some of initial states from this to perform inference on the subsequent 7 days. 185 | -------------------------------------------------------------------------------- /2023-MRC-BSU-and-UKHSA/README.md: -------------------------------------------------------------------------------- 1 | # Workshop on Julia and Turing.jl MRC Biostatistics Unit & UKHSA (Cambridge UK, 2023) 2 | 3 | This directory contains a two-day workshop on Julia & Turing.jl held for the MRC Biostatistics Unit and UKHSA at the University of Cambridge on 21st and 22nd September, 2023. 4 | 5 | ## Outline 6 | - Part 1: basic Julia 7 | - Part 2: overview of scientific computing ecosystem in Julia + Turing.jl 8 | - Part 3: JuliaBUGS, a BUGS-compatible Julia implementation (a recent addition to the TuringLang ecosystem) 9 | - Part 4: Gaussian processes in Julia and Turing.jl 10 | - Part 5: Free-form live-coding to get set up with a small research project 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Turing-Workshops 2 | 3 | Turing Workshops 4 | 5 | ## Contents 6 | 7 | - [2023-Geilo-Winter-School](https://www.sintef.no/projectweb/geilowinterschool/2023-23rd-geilo-winter-school/): 23rd Geilo Winter School, Computational Statistics. 8 | - [2023-Intro-Turing](https://www.meetup.com/bda-group/events/290842675/): Bayesian Data Analysis Meetup Group. 9 | 10 | ## License 11 | 12 | This content is licensed under [Creative Commons Attribution-ShareAlike 4.0 Internacional](http://creativecommons.org/licenses/by-sa/4.0/). 13 | 14 | [![CC BY-SA 4.0](https://licensebuttons.net/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/) 15 | --------------------------------------------------------------------------------