Skip to content

Commit

Permalink
Turing v0.36 and removal of Pathfinder code (#579)
Browse files Browse the repository at this point in the history
* Bump Turing.jl to 0.36 and update all mentions of Gibbs to match

* Replace Pathfinder example code with a link to Pathfinder docs

* Update Manifest.toml

* Bump Turing compat to 0.36.2

* Regenerate Manifest.toml with TuringBenchmarking.jl v0.5.8

* Update dependencies

* Host golf.dat file ourselves

---------

Co-authored-by: Penelope Yong <penelopeysm@gmail.com>
  • Loading branch information
mhauru and penelopeysm authored Jan 28, 2025
1 parent c939949 commit 523428a
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 271 deletions.
458 changes: 237 additions & 221 deletions Manifest.toml

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ AdvancedMH = "5b7e9947-ddc0-4b3f-9b55-0d8042f74170"
Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Expand Down Expand Up @@ -39,7 +38,6 @@ Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationNLopt = "4e6fcdb7-1186-4e1f-a706-475e75c168bb"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
Pathfinder = "b1d3bc72-d0e7-4279-b92f-7fa5d6d2d454"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -55,4 +53,4 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[compat]
Turing = "0.35"
Turing = "0.36.2"
2 changes: 1 addition & 1 deletion _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ website:
text: Team
right:
# Current version
- text: "v0.35"
- text: "v0.36"
menu:
- text: Changelog
href: https://turinglang.org/docs/changelog.html
Expand Down
4 changes: 2 additions & 2 deletions core-functionality/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ We can perform inference by using the `sample` function, the first argument of w
c1 = sample(gdemo(1.5, 2), SMC(), 1000)
c2 = sample(gdemo(1.5, 2), PG(10), 1000)
c3 = sample(gdemo(1.5, 2), HMC(0.1, 5), 1000)
c4 = sample(gdemo(1.5, 2), Gibbs(PG(10, :m), HMC(0.1, 5, :s²)), 1000)
c4 = sample(gdemo(1.5, 2), Gibbs(:m => PG(10), :s² => HMC(0.1, 5)), 1000)
c5 = sample(gdemo(1.5, 2), HMCDA(0.15, 0.65), 1000)
c6 = sample(gdemo(1.5, 2), NUTS(0.65), 1000)
```
Expand Down Expand Up @@ -452,7 +452,7 @@ end
simple_choice_f = simple_choice([1.5, 2.0, 0.3])
chn = sample(simple_choice_f, Gibbs(HMC(0.2, 3, :p), PG(20, :z)), 1000)
chn = sample(simple_choice_f, Gibbs(:p => HMC(0.2, 3), :z => PG(20)), 1000)
```

The `Gibbs` sampler can be used to specify unique automatic differentiation backends for different variable spaces. Please see the [Automatic Differentiation]({{<meta using-turing-autodiff>}}) article for more.
Expand Down
3 changes: 1 addition & 2 deletions developers/compiler/design-overview/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ not. Let `md` be an instance of `Metadata`:
`md.vns`, `md.ranges`, `md.dists`, `md.orders` and `md.flags`.
- `md.vns[md.idcs[vn]] == vn`.
- `md.dists[md.idcs[vn]]` is the distribution of `vn`.
- `md.gids[md.idcs[vn]]` is the set of algorithms used to sample `vn`. This is used in
the Gibbs sampling process.
- `md.gids[md.idcs[vn]]` is the set of algorithms used to sample `vn`. This was used by the Gibbs sampler. Since Turing v0.36 it is unused and will eventually be deleted.
- `md.orders[md.idcs[vn]]` is the number of `observe` statements before `vn` is sampled.
- `md.ranges[md.idcs[vn]]` is the index range of `vn` in `md.vals`.
- `md.vals[md.ranges[md.idcs[vn]]]` is the linearized vector of values of corresponding to `vn`.
Expand Down
4 changes: 2 additions & 2 deletions tutorials/gaussian-mixture-models/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ model = gaussian_mixture_model(x);
```

We run a MCMC simulation to obtain an approximation of the posterior distribution of the parameters $\mu$ and $w$ and assignments $k$.
We use a `Gibbs` sampler that combines a [particle Gibbs](https://www.stats.ox.ac.uk/%7Edoucet/andrieu_doucet_holenstein_PMCMC.pdf) sampler for the discrete parameters (assignments $k$) and a Hamiltonion Monte Carlo sampler for the continuous parameters ($\mu$ and $w$).
We use a `Gibbs` sampler that combines a [particle Gibbs](https://www.stats.ox.ac.uk/%7Edoucet/andrieu_doucet_holenstein_PMCMC.pdf) sampler for the discrete parameters (assignments $k$) and a Hamiltonian Monte Carlo sampler for the continuous parameters ($\mu$ and $w$).
We generate multiple chains in parallel using multi-threading.

```{julia}
Expand All @@ -123,7 +123,7 @@ setprogress!(false)

```{julia}
#| output: false
sampler = Gibbs(PG(100, :k), HMC(0.05, 10, :μ, :w))
sampler = Gibbs(:k => PG(100), (:μ, :w) => HMC(0.05, 10))
nsamples = 150
nchains = 4
burn = 10
Expand Down
20 changes: 20 additions & 0 deletions tutorials/gaussian-processes-introduction/golf.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
distance n y
2 1443 1346
3 694 577
4 455 337
5 353 208
6 272 149
7 256 136
8 240 111
9 217 69
10 200 67
11 237 75
12 202 52
13 192 46
14 174 54
15 167 28
16 201 27
17 195 31
18 191 33
19 147 20
20 152 24
17 changes: 3 additions & 14 deletions tutorials/gaussian-processes-introduction/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,9 @@ given distance.
### Let's download the data and take a look at it:

```{julia}
using CSV, DataDeps, DataFrames
ENV["DATADEPS_ALWAYS_ACCEPT"] = true
register(
DataDep(
"putting",
"Putting data from BDA",
"http://www.stat.columbia.edu/~gelman/book/data/golf.dat",
"fc28d83896af7094d765789714524d5a389532279b64902866574079c1a977cc",
),
)
fname = joinpath(datadep"putting", "golf.dat")
df = CSV.read(fname, DataFrame; delim=' ', ignorerepeated=true)
using CSV, DataFrames
df = CSV.read("golf.dat", DataFrame; delim=' ', ignorerepeated=true)
df[1:5, :]
```

Expand Down
2 changes: 1 addition & 1 deletion tutorials/hidden-markov-models/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ setprogress!(false)
```

```{julia}
g = Gibbs(HMC(0.01, 50, :m, :T), PG(120, :s))
g = Gibbs((:m, :T) => HMC(0.01, 50), :s => PG(120))
chn = sample(BayesHmm(y, 3), g, 1000);
```

Expand Down
4 changes: 2 additions & 2 deletions usage/automatic-differentiation/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ end
c = sample(
gdemo(1.5, 2),
Gibbs(
HMC(0.1, 5, :m; adtype=AutoForwardDiff(; chunksize=0)),
HMC(0.1, 5, :s²; adtype=AutoReverseDiff(false)),
:m => HMC(0.1, 5; adtype=AutoForwardDiff(; chunksize=0)),
:s² => HMC(0.1, 5; adtype=AutoReverseDiff(false)),
),
1000,
progress=false,
Expand Down
24 changes: 2 additions & 22 deletions usage/external-samplers/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,10 @@ As previously mentioned, the Turing wrappers can often limit the capabilities of
However, the native HMC sampler within Turing only allows the user to specify the type of the mass matrix despite the two options being possible within `AdvancedHMC`.
Thankfully, we can use Turing's support for external samplers to define an HMC sampler with a custom mass matrix in `AdvancedHMC` and then use it to sample our Turing model.

We will use the library `Pathfinder`[^2] ((`Pathfinder`'s GitHub)[https://github.com/mlcolab/Pathfinder.jl]) to construct our estimate of mass matrix.
We can use the library `Pathfinder`[^2] ([`Pathfinder`'s GitHub](https://github.com/mlcolab/Pathfinder.jl)) to construct our estimate of mass matrix.
`Pathfinder` is a variational inference algorithm that first finds the maximum a posteriori (MAP) estimate of a target posterior distribution and then uses the trace of the optimization to construct a sequence of multivariate normal approximations to the target distribution.
In this process, `Pathfinder` computes an estimate of the mass matrix the user can access.

The code below shows this can be done in practice.

```{julia}
using AdvancedHMC, Pathfinder
# Running pathfinder
draws = 1_000
result_multi = multipathfinder(model, draws; nruns=8)
# Estimating the metric
inv_metric = result_multi.pathfinder_results[1].fit_distribution.Σ
metric = DenseEuclideanMetric(Matrix(inv_metric))
# Creating an AdvancedHMC NUTS sampler with the custom metric.
n_adapts = 1000 # Number of adaptation steps
tap = 0.9 # Large target acceptance probability to deal with the funnel structure of the posterior
nuts = AdvancedHMC.NUTS(tap; metric=metric)
# Sample
chain = sample(model, externalsampler(nuts), 10_000; n_adapts=1_000)
```
You can see an example of how to use `Pathfinder` with Turing in [`Pathfinder`'s docs](https://mlcolab.github.io/Pathfinder.jl/stable/examples/turing/).

## Using new inference methods

Expand Down
2 changes: 1 addition & 1 deletion usage/sampler-visualisation/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ setprogress!(false)
Gibbs sampling tends to exhibit a "jittery" trajectory. The example below combines `HMC` and `PG` sampling to traverse the posterior.

```{julia}
c = sample(model, Gibbs(HMC(0.01, 5, :s²), PG(20, :m)), 1000)
c = sample(model, Gibbs(:s² => HMC(0.01, 5), :m => PG(20)), 1000)
plot_sampler(c)
```

Expand Down

0 comments on commit 523428a

Please sign in to comment.