Skip to content

Commit 4f238d1

Browse files
committed
Streamline 'Getting Started' page
1. Remove the section on posterior checks; this is the landing page and it's not necessary for people reading about the library for the first time to go through that. 2. Signpost the way to the rest of the documentation at the bottom. 3. Minor wording changes
1 parent 84c5ce9 commit 4f238d1

File tree

2 files changed

+30
-55
lines changed

2 files changed

+30
-55
lines changed

Diff for: tutorials/docs-00-getting-started/index.qmd

+29-54
Original file line numberDiff line numberDiff line change
@@ -16,96 +16,71 @@ Pkg.instantiate();
1616

1717
To use Turing, you need to install Julia first and then install Turing.
1818

19-
### Install Julia
19+
You will need to install Julia 1.7 or greater, which you can get from [the official Julia website](http://julialang.org/downloads/).
2020

21-
You will need to install Julia 1.3 or greater, which you can get from [the official Julia website](http://julialang.org/downloads/).
22-
23-
### Install Turing.jl
24-
25-
Turing is an officially registered Julia package, so you can install a stable version of Turing by running the following in the Julia REPL:
21+
Turing is officially registered in the [Julia General package registry](https://github.com/JuliaRegistries/General), which means that you can install a stable version of Turing by running the following in the Julia REPL:
2622

2723
```{julia}
24+
#| eval: false
2825
#| output: false
2926
using Pkg
3027
Pkg.add("Turing")
3128
```
3229

33-
You can check if all tests pass by running `Pkg.test("Turing")` (it might take a long time)
34-
35-
### Example
36-
37-
Here's a simple example showing Turing in action.
30+
### Example usage
3831

39-
First, we can load the Turing and StatsPlots modules
32+
First, we load the Turing and StatsPlots modules.
33+
The latter is required for visualising the results.
4034

4135
```{julia}
4236
using Turing
4337
using StatsPlots
4438
```
4539

46-
Then, we define a simple Normal model with unknown mean and variance
40+
We then specify our model, which is a simple Gaussian model with unknown mean and variance.
41+
Models are defined as ordinary Julia functions, prefixed with the `@model` macro.
42+
Each statement inside closely resembles how the model would be defined with mathematical notation.
43+
Here, both `x` and `y` are observed values, and are therefore passed as function parameters.
44+
`m` and `` are the parameters to be inferred.
4745

4846
```{julia}
4947
@model function gdemo(x, y)
5048
s² ~ InverseGamma(2, 3)
5149
m ~ Normal(0, sqrt(s²))
5250
x ~ Normal(m, sqrt(s²))
53-
return y ~ Normal(m, sqrt(s²))
51+
y ~ Normal(m, sqrt(s²))
5452
end
5553
```
5654

57-
Then we can run a sampler to collect results. In this case, it is a Hamiltonian Monte Carlo sampler
58-
59-
```{julia}
60-
chn = sample(gdemo(1.5, 2), NUTS(), 1000, progress=false)
61-
```
62-
63-
We can plot the results
55+
Suppose we observe `x = 1.5` and `y = 2`, and want to infer the mean and variance.
56+
We can pass these data as arguments to the `gdemo` function, and run a sampler to collect the results.
57+
Here, we collect 1000 samples using the No U-Turn Sampler (NUTS) algorithm.
6458

6559
```{julia}
66-
plot(chn)
60+
chain = sample(gdemo(1.5, 2), NUTS(), 1000, progress=false)
6761
```
6862

69-
In this case, because we use the normal-inverse gamma distribution as a conjugate prior, we can compute its updated mean as follows:
63+
We can plot the results:
7064

7165
```{julia}
72-
s² = InverseGamma(2, 3)
73-
m = Normal(0, 1)
74-
data = [1.5, 2]
75-
x_bar = mean(data)
76-
N = length(data)
77-
78-
mean_exp = (m.σ * m.μ + N * x_bar) / (m.σ + N)
66+
plot(chain)
7967
```
8068

81-
We can also compute the updated variance
69+
and obtain summary statistics by indexing the chain:
8270

8371
```{julia}
84-
updated_alpha = shape(s²) + (N / 2)
85-
updated_beta =
86-
scale(s²) +
87-
(1 / 2) * sum((data[n] - x_bar)^2 for n in 1:N) +
88-
(N * m.σ) / (N + m.σ) * ((x_bar)^2) / 2
89-
variance_exp = updated_beta / (updated_alpha - 1)
72+
mean(chain[:m]), mean(chain[:s²])
9073
```
9174

92-
Finally, we can check if these expectations align with our HMC approximations from earlier. We can compute samples from a normal-inverse gamma following the equations given [here](https://en.wikipedia.org/wiki/Normal-inverse-gamma_distribution#Generating_normal-inverse-gamma_random_variates).
75+
### Where to go next
9376

94-
```{julia}
95-
function sample_posterior(alpha, beta, mean, lambda, iterations)
96-
samples = []
97-
for i in 1:iterations
98-
sample_variance = rand(InverseGamma(alpha, beta), 1)
99-
sample_x = rand(Normal(mean, sqrt(sample_variance[1]) / lambda), 1)
100-
samples = append!(samples, sample_x)
101-
end
102-
return samples
103-
end
77+
::: {.callout-note title="Note on prerequisites"}
78+
Familiarity with Julia is assumed throughout the Turing documentation.
79+
If you are new to Julia, [Learning Julia](https://julialang.org/learning/) is a good starting point.
10480

105-
analytical_samples = sample_posterior(updated_alpha, updated_beta, mean_exp, 2, 1000);
106-
```
81+
The underlying theory of Bayesian machine learning is not explained in detail in this documentation.
82+
A thorough introduction to the field is [*Pattern Recognition and Machine Learning*](https://www.springer.com/us/book/9780387310732) (Bishop, 2006); an online version is available [here (PDF, 18.1 MB)](https://www.microsoft.com/en-us/research/uploads/prod/2006/01/Bishop-Pattern-Recognition-and-Machine-Learning-2006.pdf).
83+
:::
10784

108-
```{julia}
109-
density(analytical_samples; label="Posterior (Analytical)")
110-
density!(chn[:m]; label="Posterior (HMC)")
111-
```
85+
The next page on [Turing's core functionality](../../tutorials/docs-12-using-turing-guide/) explains the basic features of the Turing language.
86+
From there, you can either look at [worked examples of how different models are implemented in Turing](../../tutorials/00-introduction/), or [specific tips and tricks that can help you get the most out of Turing](../../tutorials/docs-17-mode-estimation/).

Diff for: tutorials/docs-12-using-turing-guide/index.qmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Turing's Core Functionality"
2+
title: "Core Functionality"
33
engine: julia
44
---
55

0 commit comments

Comments
 (0)