# New Bayesian Statistics tutorials with Turing

**URL:** https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825
**Category:** Probabilistic Programming
**Tags:** turing
**Created:** [May 9, 2021, 2:02pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825 "2021-05-09T14:02:01Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [May 9, 2021, 2:02pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/1 "2021-05-09T14:02:01Z")

</div>

I’ve just made some Bayesian Statistics tutorials using Julia and Turing. The content is fully opensourced in GitHub and has a very permissive Creative Commons License (which means you can copy and alter the crap out it…).

My hope is to have more people into Bayesian statistics. The content is aimed towards social scientists and PhD candidates in social sciences. I chose to provide an intuitive approach rather than focusing on rigorous mathematical formulations. I’ve made it to be how I would have liked to be introduced to Bayesian statistics.

Check it out in: [https://storopoli.io/Bayesian-Julia/](https://storopoli.io/Bayesian-Julia/) or in [https://github.com/storopoli/Bayesian-Julia](https://github.com/storopoli/Bayesian-Julia)

---

<div class="post-metadata">

### Author: ![EvoArt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evoart/32/25357_2.png) [@EvoArt](https://discourse.julialang.org/u/EvoArt)
#### Post date: [May 9, 2021, 3:50pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/2 "2021-05-09T15:50:40Z")

</div>

Brilliant. Thanks for this. Turing is ideal for learning bayesian stats and probabilistic programming. I love that (much like Julia) it has a high level, easy syntax but you can dig deeper to access greater complexity and performance when needed.

---

<div class="post-metadata">

### Author: ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)
#### Post date: [May 9, 2021, 4:09pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/3 "2021-05-09T16:09:14Z")

</div>

I looks very interesting, I’ll read it when I have some time,  
Thank you.

I think you missed the “beta distribution”, very common in Bayesian Statistics.

And I have a quick question…  
Most frequentist statistics only use the mean and the variance of the sample.  
What about bayesian statistics models? Do they use just the mean and the variance, or do they also make use of all the information of all the measures, i.e. all the moments?

---

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [May 9, 2021, 4:50pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/4 "2021-05-09T16:50:55Z")

</div>

We mostly use the full posterior density. And then you can calculate any moment or any other statistic you might fancy.

---

<div class="post-metadata">

### Author: ![TBulka](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@TBulka](https://discourse.julialang.org/u/TBulka)
#### Post date: [May 9, 2021, 11:21pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/5 "2021-05-09T23:21:49Z")

</div>

This looks really interesting and I’ll definitively will work through the material in the next days.

---

<div class="post-metadata">

### Author: ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)
#### Post date: [June 5, 2021, 8:04pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/6 "2021-06-05T20:04:40Z")

</div>

@Storopoli, quick question: in [https://storopoli.io/Bayesian-Julia/pages/4\_Turing/](https://storopoli.io/Bayesian-Julia/pages/4_Turing/) there’s this model:

```julia
@model dice_throw(y) = begin
    #Our prior belief about the probability of each result in a six-sided dice.
    #p is a vector of length 6 each with probability p that sums up to 1.
    p ~ Dirichlet(6, 1)

    #Each outcome of the six-sided dice has a probability p.
    for i in eachindex(y)
        y[i] ~ Categorical(p)
    end
end;

```

And then it says:

> Instead, I’ve opted for a Dirichlet with a weekly informative prior towards a “fair” dice which is encoded as a `Dirichlet(6,1)`.

- What’s a “weekly informative prior towards a “fair” dice”?
- Why do we choose `Dirichlet(6, 1)` and not, say, `Dirichlet(6, 8)`? As far as I can tell, all of them have mean 0.166667:

```julia
all(
	all(mean(Dirichlet(6, a)) .≈ 1/6)
	for a ∈ 0.01:.001:30
) == true

```

---

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [June 5, 2021, 8:36pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/7 "2021-06-05T20:36:12Z")

</div>

> [@ForceBru](#):
>
> What’s a “weekly informative prior towards a “fair” dice”?

This is going to be philosophical question? I don’t know, i think a 6-length vector of 1/6?

> [@ForceBru](#):
>
> Why do we choose `Dirichlet(6, 1)` and not, say, `Dirichlet(6, 8)` ? As far as I can tell, all of them have mean 0.166667:

Good question. I honestly don’t know. Looking the help:

```julia
help?> Dirichlet
search: Dirichlet DirichletMultinomial

  Dirichlet

  The Dirichlet distribution (http://en.wikipedia.org/wiki/Dirichlet_distribution) is often used as the conjugate prior for Categorical
  or Multinomial distributions. The probability density function of a Dirichlet distribution with parameter \alpha = (\alpha_1, \ldots,
  \alpha_k) is:

  f(x; \alpha) = \frac{1}{B(\alpha)} \prod_{i=1}^k x_i^{\alpha_i - 1}, \quad \text{ with }
B(\alpha) = \frac{\prod_{i=1}^k \Gamma(\alpha_i)}{\Gamma \left( \sum_{i=1}^k \alpha_i \right)},
\quad x_1 + \cdots + x_k = 1

  # Let alpha be a vector
  Dirichlet(alpha) # Dirichlet distribution with parameter vector alpha
  
  # Let a be a positive scalar
  Dirichlet(k, a) # Dirichlet distribution with parameter a * ones(k)

```

and then:

```julia
julia> mean(Dirichlet(6,1))
6-element Fill{Float64}: entries equal to 0.16666666666666666

julia> mean(Dirichlet(6,8))
6-element Fill{Float64}: entries equal to 0.16666666666666666

```

---

<div class="post-metadata">

### Author: ![EvoArt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evoart/32/25357_2.png) [@EvoArt](https://discourse.julialang.org/u/EvoArt)
#### Post date: [June 5, 2021, 8:56pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/8 "2021-06-05T20:56:16Z")

</div>

Dirichlet(6,8) implies you are quite confident that it’s a fair dice roll. Whereas (6,1) says “I don’t really know”

---

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [June 5, 2021, 8:56pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/9 "2021-06-05T20:56:23Z")

</div>

The `alpha` in a Dirichlet is a “concentration”. Maybe this helps:

```julia

julia> std(rand(Dirichlet(6,1),1000),dims=2)
6×1 Matrix{Float64}:
 0.14305158706755236
 0.14043323912305447
 0.12959278663394877
 0.14360349478154502
 0.14403746553339422
 0.13586448356346292

julia> std(rand(Dirichlet(6,8),1000),dims=2)
6×1 Matrix{Float64}:
 0.05263278171058433
 0.0531789315822342
 0.0524370633051767
 0.05355511158379274
 0.05450811672204576
 0.05293590862720139

```

A concentration of `alpha=1` gives a uniform distribution over the simplex. Also, it can help to remember that the Dirichlet distribution generalizes a Beta distribution, where `alpha` and `beta` play the same role.

---

<div class="post-metadata">

### Author: ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)
#### Post date: [June 5, 2021, 9:00pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/10 "2021-06-05T21:00:20Z")

</div>

> [@Storopoli](#):
>
> i think a 6-length vector of 1/6?

Oh, I thought that was a term that I didn’t understand.

I messed around with the Dirichlet distribution at [https://ben18785.shinyapps.io/distribution-zoo/](https://ben18785.shinyapps.io/distribution-zoo/), and it looks like when the vector of parameters is all ones, like `Dirichlet(ones(3))` or `Dirichlet(3, 1)`, we get a sort of “uniform”, flat distribution.

It seems to behave similarly to the Beta distribution: when _both_ its parameters are high, its PDF starts looking more and more like normal with mean 0.5. When all n Dirichlet’s parameters are equal and increase, the PDF also concentrates more and more around the middle: 1/n.

So it looks to me that `Dirichlet(ones(n))` has the flattest PDF and is thus the most “uniform” among Dirichlet distributions with other parameters

---

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [June 5, 2021, 9:16pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/11 "2021-06-05T21:16:13Z")

</div>

Yeah I was aiming for a uniform flat distribution. And also what a great demonstration by @cscherrer thanks!

---

<div class="post-metadata">

### Author: ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)
#### Post date: [June 5, 2021, 9:22pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/12 "2021-06-05T21:22:11Z")

</div>

BTW, @Storopoli, these are really nice tutorials - way easier to understand than those in Turing’s documentation. So thank you!

---

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [June 5, 2021, 9:27pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/13 "2021-06-05T21:27:38Z")

</div>

Hey @ForceBru thanks! I was invited to give a talk to Stuttgart’s Julia Meetup Group ([Login to Meetup | Meetup](https://www.meetup.com/stuttgart-julia-programming-language-meetup-gruppe/events/278545048/))

I will cover some stuff that I haven’t gone on the tutorials. It will be next Saturday and open to everybody. Will be in english.

Edit: I’ve found that Turing’s examples are more geared towards computer science/machine learning folks. Not so much as someone who had classical stats training and then learned all over again with Bayesian statistics.

---

<div class="post-metadata">

### Author: ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)
#### Post date: [June 5, 2021, 9:39pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/14 "2021-06-05T21:39:52Z")

</div>

Cool, just signed up for the talk!

---

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [May 20, 2022, 7:10pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/15 "2022-05-20T19:10:19Z")

</div>

updated link here: [Bayesian Statistics using Julia and Turing](https://storopoli.github.io/Bayesian-Julia)

---

<div class="post-metadata">

### Author: ![sree\_datta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sree_datta/32/32723_2.png) [@sree\_datta](https://discourse.julialang.org/u/sree_datta)
#### Post date: [June 29, 2022, 9:21pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/16 "2022-06-29T21:21:06Z")

</div>

Hi @Storopoli I a new user of Turing and Julia for Bayesian modeling. I was trying to replicate a basic Linear Regression model in your GitHub link (Children’s IQ Score example). I could replicate the model but could not plot the posterior results / trace using `StatsPlots`.

When I type `using StatsPlots` and try to run in the Jupyter Notebook it gives errors stating that one of StatsPlots dependencies `Arpack` has not been installed correctly. See this link (I posted this under New To Julia) [Arpack error with StatsPlots](https://discourse.julialang.org/t/arpack-error-when-trying-to-use-statsplots-in-a-bayesian-linear-regression-example/83493)

Any pointers in this regard? I’m coming from R and Python based Bayesian Models using `brms and rstan` as well as `pymc`. Are there any alternatives to `StatsPlots` that would allow me to plot the Bayesian model results?

---

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [June 29, 2022, 10:29pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/17 "2022-06-29T22:29:08Z")

</div>

That is definitely a install error. Are you using Windows?  
You can plot them with Makie and AlgebraOfGraphics. See here: [Rewriting MCMCChains with Makie.jl+AlgebraOfGraphics · Issue #306 · TuringLang/MCMCChains.jl · GitHub](https://github.com/TuringLang/MCMCChains.jl/issues/306#issuecomment-877480205).

In fact I am going to use my “winter break” (summer break in the northern hemisphere) to try to move all plots to Makie + AlgebraOfGraphics. Issue: [Move images from `Plots.jl` to `Makie.jl` + `AlgebraOfGraphics.jl` · Issue #46 · storopoli/Bayesian-Julia · GitHub](https://github.com/storopoli/Bayesian-Julia/issues/46).

---

<div class="post-metadata">

### Author: ![sree\_datta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sree_datta/32/32723_2.png) [@sree\_datta](https://discourse.julialang.org/u/sree_datta)
#### Post date: [June 30, 2022, 12:14am UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/18 "2022-06-30T00:14:56Z")

</div>

Thank you so much for a quick response @Storopoli

I’m on Ubuntu 21.04 using `Julia 1.7.3`. After posting my question on alternative graphing libraries, I found online reference to using Turing and ArviZ together. For the time being, I’m using ArviZ.

I have tried to install `StatsPlots` and dependencies multiple times by following different instructions on Github and Discourse based on other Linux users experiences, but so far no success. I have also opened an issue at `github/StatsPlots`.

I will check the reference on using `Makie.jl` - thanks again for your help.

---

<div class="post-metadata">

### Author: ![sree\_datta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sree_datta/32/32723_2.png) [@sree\_datta](https://discourse.julialang.org/u/sree_datta)
#### Post date: [June 30, 2022, 11:51pm UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/19 "2022-06-30T23:51:45Z")

</div>

@Storopoli I have another question for you: Does `Turing` have a front-end equivalent such as `brms` for `rstan` in R or such as `bambi` for `pymc` in Python? Both `brms` and `bambi` permit a simple formula based approach to specifying advanced Bayesian multi-level models for example.

The `brms` model specification is very much like that of `lme4` in R and `bambi`, inspired by `brms` follows a specification very close to `StatsModels`. in Python. For myself and a lot of others, both of these interfaces (`brms` and `bambi`) made Bayesian Modeling more accessible and simpler.

Would love to see something like that for `Turing` as well.

---

<div class="post-metadata">

### Author: ![wc4wc4wc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wc4wc4wc4/32/23038_2.png) [@wc4wc4wc4](https://discourse.julialang.org/u/wc4wc4wc4)
#### Post date: [July 1, 2022, 4:54am UTC](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825/20 "2022-07-01T04:54:14Z")

</div>

I would check out [TuringGLM](https://beta.turing.ml/TuringGLM.jl/stable/) if I were you.

[Next page](https://discourse.julialang.org/t/new-bayesian-statistics-tutorials-with-turing/60825.md?page=2)
