Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
4f49a3e
Add all high level docs from Turing.jl
yebai Jan 6, 2023
912b1bc
Fix some permalinks.
yebai Jan 6, 2023
b9a8143
Converted Quickstart doc
cgbotta Jan 24, 2023
6a82b20
Converted contributing pages to jmd
cgbotta Jan 25, 2023
306f777
add project files
yebai Jan 26, 2023
892c301
Disable evaluating style examples
yebai Jan 26, 2023
712746e
Update and rename index.md to index.jmd
yebai Jan 26, 2023
1c72459
Rename index.jmd to using-turing.jmd
yebai Jan 26, 2023
8ccdb21
fix formatting for guide.md
yebai Jan 26, 2023
0ad33c2
Fixed formatting.
yebai Jan 26, 2023
2d1e31e
Update tutorials/for-developers-interface/interface.md
yebai Jan 26, 2023
ef95a28
Update tutorials/for-developers-abstractmcmc-turing/how_turing_implem…
yebai Jan 26, 2023
c70aece
Update tutorials/for-developers-abstractmcmc-turing/how_turing_implem…
yebai Jan 26, 2023
9e8c16d
update sample viz
yebai Jan 26, 2023
ff63088
add dummy julia code block to enable jmd building
yebai Jan 26, 2023
cb299b1
Bugfix for negative variance.
yebai Jan 26, 2023
bb392ad
More conversions
cgbotta Feb 2, 2023
a1879f6
Added dependencies and changed directory names
cgbotta Feb 8, 2023
341b21e
Fixed dependencies
cgbotta Apr 17, 2023
57d7894
Fixing dependencies
cgbotta Apr 17, 2023
c2efc56
Fixing dependencies
cgbotta Apr 17, 2023
af861a4
Fixing dependencies
cgbotta Apr 17, 2023
06b1f1a
Fixing dependencies
cgbotta Apr 17, 2023
f6d8ed9
Fixing dependencies
cgbotta Apr 17, 2023
98a4eb8
Fixing dependencies
cgbotta Apr 17, 2023
ee0c267
Fixing dependencies
cgbotta Apr 17, 2023
1d14387
Fixing dependencies
cgbotta Apr 17, 2023
80c455a
Fixing dependencies
cgbotta Apr 17, 2023
a4f4659
Fixing dependencies
cgbotta Apr 17, 2023
6aa8085
Fixing dependencies
cgbotta Apr 17, 2023
8f43c61
Fixing dependencies
cgbotta Apr 17, 2023
6e3433a
Fixing dependencies
cgbotta Apr 17, 2023
4cad629
Fixing dependencies
cgbotta Apr 17, 2023
051da51
Fixing dependencies
cgbotta Apr 17, 2023
2d1583f
other tutorials
cgbotta Apr 17, 2023
b47ab65
other tutorials
cgbotta Apr 17, 2023
96ecc01
other tutorials
cgbotta Apr 17, 2023
49dbdc5
Merge branch 'master' into hg/transfer-docs
yebai Jun 11, 2023
da8bbee
fix merge conflict with master
yebai Jul 6, 2023
41c8c1a
some edits to advanced
yebai Jul 6, 2023
b86ec7f
remove weave from deps
yebai Jul 6, 2023
8787423
improve compiler
yebai Jul 6, 2023
1ef0797
added julia formatting for a block which it was missing (#398)
torfjelde Jul 6, 2023
4ec62cb
AbstractMCMC fixes, rename some files, and update manifests (#399)
willtebbutt Jul 6, 2023
2f51b7b
no empty cell + hyperlink to bishop (#400)
yebai Jul 6, 2023
886d617
add numeric prefix to docs
yebai Jul 6, 2023
56f50cb
triger rebuild
yebai Jul 6, 2023
b3c814a
add Distributions to deps
yebai Jul 6, 2023
8c07963
minor fixes
yebai Jul 6, 2023
a0889fc
Minor edits
yebai Jul 6, 2023
3f0d51e
Update tutorials/docs-12-using-turing-guide/using-turing-guide.jmd
yebai Jul 6, 2023
919ba2f
Update tutorials/docs-09-using-turing-advanced/advanced.jmd
yebai Jul 6, 2023
51afa20
docs draft
JaimeRZP Jul 7, 2023
e2e1d1a
Apply suggestions from code review
JaimeRZP Jul 24, 2023
37c5863
pull
JaimeRZP Jul 24, 2023
b491bd5
upd branch
JaimeRZP Jul 24, 2023
db11514
env
JaimeRZP Jul 24, 2023
fb7fa6f
format
JaimeRZP Jul 24, 2023
a4a079b
better name
JaimeRZP Jul 24, 2023
45b96ce
undoing rename
JaimeRZP Jul 24, 2023
ad9fea4
undoing fck up
JaimeRZP Jul 24, 2023
dc5387a
working on Pathfinder example
JaimeRZP Jul 24, 2023
46773bd
Apply suggestions from code review
JaimeRZP Jul 25, 2023
150091b
citations
JaimeRZP Jul 25, 2023
2fbb15e
no propaganda
JaimeRZP Jul 25, 2023
3d0ccf4
flesing out the examples
JaimeRZP Jul 25, 2023
7eeaa20
format
JaimeRZP Jul 25, 2023
34a6ede
more concise
JaimeRZP Jul 25, 2023
a1680a3
format
JaimeRZP Jul 25, 2023
ee426db
advancedMH
JaimeRZP Jul 25, 2023
207d68b
for devs
JaimeRZP Jul 26, 2023
1af8100
Apply suggestions from code review
JaimeRZP Jul 27, 2023
1f1cb9d
Update tutorials/15-external-samplers/external-samplers.jmd
JaimeRZP Jul 27, 2023
2761834
bug
JaimeRZP Jul 27, 2023
5df99c3
Merge branch 'external_samplers_docs' of https://github.com/TuringLan…
JaimeRZP Jul 27, 2023
1b39ea7
Apply suggestions from code review
JaimeRZP Jul 28, 2023
5ed43db
upd
JaimeRZP Jul 28, 2023
4d13403
Update tutorials/15-external-samplers/external-samplers.jmd
JaimeRZP Jul 28, 2023
7796e11
Update tutorials/15-external-samplers/external-samplers.jmd
JaimeRZP Jul 28, 2023
24f1cc3
Update external-samplers.jmd
yebai Jul 30, 2023
6ec06d3
update manifest.toml
yebai Jul 30, 2023
f8e9b03
Update external-samplers.jmd
yebai Aug 17, 2023
7cc1703
update manifest file
yebai Aug 17, 2023
f79aa96
bug
JaimeRZP Sep 19, 2023
32284d5
upd deps
JaimeRZP Sep 20, 2023
1009fe5
Merge branch 'master' into external_samplers_docs
JaimeRZP Sep 20, 2023
cc451bb
Revert "upd deps"
JaimeRZP Sep 20, 2023
1b92ee7
Merge branch 'external_samplers_docs' of https://github.com/TuringLan…
JaimeRZP Sep 20, 2023
ef2b2eb
bump manifesto
JaimeRZP Sep 20, 2023
1700ebc
nearly there
JaimeRZP Sep 22, 2023
48fb69c
bump turing
JaimeRZP Oct 19, 2023
da45d17
Merge branch 'master' into external_samplers_docs
JaimeRZP Oct 19, 2023
2b48212
slight formatting error
JaimeRZP Oct 19, 2023
bae440f
Merge branch 'external_samplers_docs' of https://github.com/TuringLan…
JaimeRZP Oct 19, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more concise
  • Loading branch information
JaimeRZP committed Jul 25, 2023
commit 34a6ede35106f7026a26e70d38d8311d112f490c
53 changes: 16 additions & 37 deletions tutorials/15-external-samplers/external-samplers.jmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ weave_options:

# Using External Samplers

Turing provides several wrapped samplers from external sampling libraries, e.g. HMC smaplers from AdvancedHMC.jl.
These wrappers allow new users to seemlessly sample statistical models without leaving Turing.
`Turing` provides several wrapped samplers from external sampling libraries, e.g. HMC smaplers from `AdvancedHMC`.
These wrappers allow new users to seemlessly sample statistical models without leaving `Turing`
However, these wrappers might not always be complete, missing some functionality from the wrapped sampling library.
Moreover, users might want to use samplers currently not wrapped within Turing.
Moreover, users might want to use samplers currently not wrapped within `Turing`.

For these reasons, Turing also makes it easy to run external samplers on Turing models without any modifications or wrapping being necessary!
For these reasons, `Turing` also makes it easy to run external samplers on Turing models without any modifications or wrapping being necessary!
Throughout we will use a 10 dimensional Neal's funnel as a running example::

```julia
Expand All @@ -31,57 +31,36 @@ end
Now we sample the model to generate some observations, which we can then subsequently condition on.

```julia
Random.seed!(1)
(; x) = rand(funnel() | (θ=0,))
model = funnel() | (; x)
```

Users can use any sampler algorithm they wish to sample this model as long as it is of the type `AbstractMCMC.AbstractSampler`.
Before discussing how is this in done in practice, it is interesting to give a high level description of the process.
Imagine that we created an instance of external sampler that we will call `spl` such that `typeof(spl)<:AbstractMCMC.AbstractSampler`.
In order to avoid type ambiguity within Turing, at the moment is necessary to declare `spl` as an external sampler to Turing this is done as:
In order to avoid type ambiguity within Turing, at the moment is necessary to declare `spl` as an external sampler to Turing `espl = externalsampler(spl)` where `externalsampler(s::AbstractMCMC.AbstractSampler)` is a Turing function that types our external sampler adquately.

```julia
espl = externalsampler(spl)
```

where `externalsampler(s::AbstractMCMC.AbstractSampler)` is a Turing function that types our external sampler adquately.

At this point, sampling from our model is as easy as doing

```julia
chain = sample(model, espl, 1_000)
```

This process will use `AbstractMCMC` functions overloaded by Turing to generate chains identical to those generated by the native samplers.
Note that if the external sampler produces transitions that Turing cannot parse the bundling of the samples will be different or fail.

## AdvancedMH

A good point to start to show how this is done in practice is by looking at the sampling library `AdvancedMH` ((`AdvancedMH`'s GitHub)[[https://github.com/TuringLang/AdvancedMH.jl]) for Metropolis Hastings methods.
A good point to start to show how this is done in practice is by looking at the sampling library `AdvancedMH` ((`AdvancedMH`'s GitHub)[[https://github.com/TuringLang/AdvancedMH.jl]) for Metropolis Hastings (MH) methods.
Let's say that we want to use a random walk Metropolis Hastings sampler without specifying the proposal distributions.
We can do this in two different ways:
The code below constructs a MH sampler using a multivariate Gaussian distribution with zero mean and unit variance in `d` dimensions as a random walk proposal.

```julia
# Importing the sampling library
using AdvancedMH

rwmh_1 = RWMH(d)
rwmh_2 = RWMH(model)
rwmh = RWMH(d)
```

The first method uses a multivariate Gaussian distribution with zero mean and unit variance in `d` dimensions.
The second method uses the priors of the model as a proposal. Note that the second method requires Turing to be imported.

Let us now compare the performance and posteriors of these two samplers to the native MH sampler within Turing.
Sampling is then as easy as:

```julia
mh_chain_0 = sample(model, MH(), 10_000)
mh_chain_1 = sample(model, externalsampler(rwmh_1), 10_000)
mh_chain_2 = sample(model, externalsampler(rwmh_2), 10_000)
chain = sample(model, externalsampler(rwmh), 10_000)
```

## AdvancedHMC
This process will use `AbstractMCMC` functions overloaded by Turing to generate chains identical to those generated by the native samplers.
Note that if the external sampler produces transitions that Turing cannot parse the bundling of the samples will be different or fail.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this would do well with further elaboration; might even warrant it's own section, preferably with a simple example, as this is likely to be a source of confusion for the user.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid I don't know enough about the bundling process to know what's important information to transmit to the reader.
I also think that we can remove this sentence since user level readers will not interact with this and devs only really need to know how to define their transition object in their libraries which is covered later.
However, please feel free to fill in information you think it might be useful.



## Going beyond the `Turing` API

As previously mentioned the Turing wrappers can often limit the capabilities of the sampling libraries they wrap.
`AdvancedHMC`[^1] ((`AdvancedHMC`'s GitHub)[https://github.com/TuringLang/AdvancedHMC.jl]) is a clear example of this. A common practice when performing HMC is to provide an initial guess for the mass matrix.
Expand Down Expand Up @@ -113,7 +92,7 @@ nuts = AdvancedHMC.NUTS(n_adapts, tap; metric=metric)
chain = sample(model, externalsampler(nuts), 10_000)
```

## Micro-Canonical HMC
## Using new inference methods

So far we have used to Turing's support for external samplers to go beyond the capabilities of the wrappers.
Now we want to use this support to employ a sampler not supported within Turing's ecosystem yet.
Expand Down