Skip to content

Commit

Permalink
Add API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Oct 2, 2024
1 parent 81ddf9c commit 302a499
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 106 deletions.
6 changes: 6 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[deps]
Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterInterLinks = "d12716ef-a0f6-4df4-a9f1-a5a34e75c656"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
5 changes: 0 additions & 5 deletions docs/README.md

This file was deleted.

37 changes: 37 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Documenter
using Turing
# Need to import Distributions and Bijectors to generate docs for functions
# from those packages.
using Distributions
using Bijectors

using DocumenterInterLinks

links = InterLinks(
"DynamicPPL" => "https://turinglang.org/DynamicPPL.jl/stable/objects.inv",
"AbstractPPL" => "https://turinglang.org/AbstractPPL.jl/dev/objects.inv",
"ADTypes" => "https://sciml.github.io/ADTypes.jl/stable/objects.inv",
)

# Doctest setup
DocMeta.setdocmeta!(Turing, :DocTestSetup, :(using Turing); recursive=true)

makedocs(;
sitename="Turing",
modules=[Turing, Distributions, Bijectors],
pages=[
"Home" => "index.md",
"API" => "api.md",
"Submodule APIs" => [
"Inference" => "api/Inference.md",
"Optimisation" => "api/Optimisation.md",
]
],
checkdocs=:exports,
# checkdocs_ignored_modules=[Turing, Distributions, DynamicPPL, AbstractPPL, Bijectors],
doctest=false,
warnonly=true,
plugins=[links],
)

deploydocs(; repo="github.com/TuringLang/Turing.jl.git", push_preview=true)
168 changes: 168 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# API
## Module-wide re-exports

Turing.jl directly re-exports the entire public API of the following packages:

- [Distributions.jl](https://juliastats.org/Distributions.jl)
- [MCMCChains.jl](https://turinglang.org/MCMCChains.jl)
- [AbstractMCMC.jl](https://turinglang.org/AbstractMCMC.jl)
- [Bijectors.jl](https://turinglang.org/Bijectors.jl)
- [Libtask.jl](https://github.com/TuringLang/Libtask.jl)

Please see the individual packages for their documentation.

## Individual exports and re-exports

**All** of the following symbols are exported unqualified by Turing, even though the documentation suggests that many of them are qualified.
That means, for example, you can just write

```julia
using Turing

@model function my_model(...)

sample(my_model(), Prior(), 100)
```

instead of

```julia
sample(model, Turing.Inference.Prior(), 100)
```

even though [`Prior()`](@ref) is actually defined in the `Turing.Inference` module.

### Modelling

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `@model` | [`DynamicPPL.@model`](@extref) | Define a probabilistic model |
| `@varname` | [`AbstractPPL.@varname`](@extref) | Generate a `VarName` from a Julia expression |
| `@submodel` | [`DynamicPPL.@submodel`](@extref) | Define a submodel |

### Inference

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `sample` | [`StatsBase.sample`](https://turinglang.org/AbstractMCMC.jl/stable/api/#Sampling-a-single-chain) | Sample from a model |

### Samplers

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `Prior` | [`Turing.Inference.Prior`](@ref) | Sample from the prior distribution |
| `MH` | [`Turing.Inference.MH`](@ref) | Metropolis–Hastings |
| `Emcee` | [`Turing.Inference.Emcee`](@ref) | Affine-invariant ensemble sampler |
| `ESS` | [`Turing.Inference.ESS`](@ref) | Elliptical slice sampling |
| `Gibbs` | [`Turing.Inference.Gibbs`](@ref) | Gibbs sampling |
| `GibbsConditional` | [`Turing.Inference.GibbsConditional`](@ref) | A "pseudo-sampler" to provide analytical conditionals to `Gibbs` |
| `HMC` | [`Turing.Inference.HMC`](@ref) | Hamiltonian Monte Carlo |
| `SGLD` | [`Turing.Inference.SGLD`](@ref) | Stochastic gradient Langevin dynamics |
| `SGHMC` | [`Turing.Inference.SGHMC`](@ref) | Stochastic gradient Hamiltonian Monte Carlo |
| `HMCDA` | [`Turing.Inference.HMCDA`](@ref) | Hamiltonian Monte Carlo with dual averaging |
| `NUTS` | [`Turing.Inference.NUTS`](@ref) | No-U-Turn Sampler |
| `PolynomialStepsize` | [`Turing.Inference.PolynomialStepsize`](@ref) | A function to generate a polynomially decaying step size |
| `IS` | [`Turing.Inference.IS`](@ref) | Importance sampling |
| `SMC` | [`Turing.Inference.SMC`](@ref) | Sequential Monte Carlo |
| `PG` | [`Turing.Inference.PG`](@ref) | Particle Gibbs |
| `CSMC` | [`Turing.Inference.CSMC`](@ref) | The same as PG |
| `externalsampler` | [`Turing.Inference.externalsampler`](@ref) | Wrap an external sampler for use in Turing |

### Variational inference

These functions are not (yet) formally documented.
Please see the [variational inference tutorial](https://turinglang.org/docs/tutorials/09-variational-inference/) for a walkthrough on how to use these.

TODO: Generate docs for AdvancedVI 0.2, update the InterLinks object.inv, and update the table below to include `@extref`.

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `vi` | `AdvancedVI.vi` | Perform variational inference |
| `ADVI` | `AdvancedVI.ADVI` | Construct an instance of a VI algorithm |

### Automatic differentiation types

These are used to specify the automatic differentiation backend to use.

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `AutoForwardDiff` | [`ADTypes.AutoForwardDiff`](@extref) | ForwardDiff.jl backend |
| `AutoReverseDiff` | [`ADTypes.AutoReverseDiff`](@extref) | ReverseDiff.jl backend |
| `AutoZygote` | [`ADTypes.AutoZygote`](@extref) | Zygote.jl backend |
| `AutoTracker` | [`ADTypes.AutoTracker`](@extref) | Tracker.jl backend |
| `AutoTapir` | [`ADTypes.AutoTapir`](@extref) | Tapir.jl backend, only for ADTypes >= 1.0 |

### Debugging

```@docs
setprogress!
```

### Distributions

These distributions are defined in Turing.jl, but not in Distributions.jl.

```@docs
Flat
FlatPos
BinomialLogit
OrderedLogistic
LogPoisson
```

`BernoulliLogit` is part of Distributions.jl since version 0.25.77.
If you are using an older version of Distributions where this isn't defined, Turing will export the same distribution.

```@docs
Distributions.BernoulliLogit
```

### Tools to work with distributions

TODO: DistributionsAD needs docs

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `filldist` | `DistributionsAD.filldist` | Create a product distribution from a distribution and integers |
| `arraydist` | `DistributionsAD.arraydist` | Create a product distribution from an array of distributions |
| `NamedDist` | [`DynamicPPL.NamedDist`](@extref) | A distribution that carries the name of the variable |

### Predictions

```@docs
predict
```

### Re-exports from DynamicPPL / AbstractPPL

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `pointwise_loglikelihoods` | [`DynamicPPL.pointwise_loglikelihoods`](@extref) | Compute log likelihoods for each sample in a chain |
| `generated_quantities` | [`DynamicPPL.generated_quantities`](@extref) | Calculate additional quantities defined in a model |
| `logprior` | [`DynamicPPL.logprior`](@extref) | Compute log prior probability |
| `logjoint` | [`DynamicPPL.logjoint`](@extref) | Compute log joint probability |
| `LogDensityFunction` | [`DynamicPPL.LogDensityFunction`](@extref) | Wrap a Turing model to satisfy LogDensityFunctions.jl interface |
| `condition` | [`AbstractPPL.condition`](@extref) | Condition a model on data |
| `decondition` | [`AbstractPPL.decondition`](@extref) | Remove conditioning on data |
| `conditioned` | [`DynamicPPL.conditioned`](@extref) | Return the conditioned values of a model |
| `fix` | [`DynamicPPL.fix`](@extref) | Fix the value of a variable |
| `unfix` | [`DynamicPPL.unfix`](@extref) | Unfix the value of a variable |
| `OrderedDict` | [`OrderedCollections.OrderedDict`](https://juliacollections.github.io/OrderedCollections.jl/dev/ordered_containers/#OrderedDicts) | An ordered dictionary |

### Extra re-exports from Bijectors

Note that Bijectors itself does not export `ordered`.

```@docs
Bijectors.ordered
```

### Point estimates

| Exported symbol | Documentation | Description |
| --- | --- | --- |
| `maximum_a_posteriori` | [`Turing.Optimisation.maximum_a_posteriori`](@ref) | Find a MAP estimate for a model |
| `maximum_likelihood` | [`Turing.Optimisation.maximum_likelihood`](@ref) | Find a MLE estimate for a model |
| `MAP` | [`Turing.Optimisation.MAP`](@ref) | Type to use with Optim.jl for MAP estimation |
| `MLE` | [`Turing.Optimisation.MLE`](@ref) | Type to use with Optim.jl for MLE estimation |

6 changes: 6 additions & 0 deletions docs/src/api/Inference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# API: `Turing.Inference`

```@autodocs
Modules = [Turing.Inference]
Order = [:type, :function]
```
6 changes: 6 additions & 0 deletions docs/src/api/Optimisation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# API: `Turing.Optimisation`

```@autodocs
Modules = [Turing.Optimisation]
Order = [:type, :function]
```
5 changes: 5 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Turing.jl

This site contains the API documentation for the identifiers exported by Turing.jl.

If you are looking for usage examples and guides, please visit [https://turinglang.org/docs](https://turinglang.org/docs).
25 changes: 0 additions & 25 deletions docs/src/library/advancedhmc.md

This file was deleted.

45 changes: 0 additions & 45 deletions docs/src/library/api.md

This file was deleted.

25 changes: 0 additions & 25 deletions docs/src/library/bijectors.md

This file was deleted.

4 changes: 2 additions & 2 deletions src/Turing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export @model, # modelling
vi, # variational inference
ADVI,
sample, # inference
@logprob_str,
@prob_str,
@logprob_str, # TODO: Remove, see https://github.com/TuringLang/DynamicPPL.jl/issues/356
@prob_str, # TODO: Remove, see https://github.com/TuringLang/DynamicPPL.jl/issues/356
externalsampler,
AutoForwardDiff, # ADTypes
AutoReverseDiff,
Expand Down
6 changes: 5 additions & 1 deletion src/mcmc/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ function DynamicPPL.unflatten(vi::TypedVarInfo, θ::NamedTuple)
end
DynamicPPL.unflatten(vi::SimpleVarInfo, θ::NamedTuple) = SimpleVarInfo(θ, vi.logp, vi.transformation)

# Algorithm for sampling from the prior
"""
Prior()
Algorithm for sampling from the prior.
"""
struct Prior <: InferenceAlgorithm end

function AbstractMCMC.step(
Expand Down
11 changes: 11 additions & 0 deletions src/mcmc/emcee.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
### Sampler states
###

"""
Emcee(n_walkers::Int, stretch_length=2.0)
Affine-invariant ensemble sampling algorithm.
# Reference
Foreman-Mackey, D., Hogg, D. W., Lang, D., & Goodman, J. (2013).
emcee: The MCMC Hammer. Publications of the Astronomical Society of the
Pacific, 125 (925), 306. https://doi.org/10.1086/670067
"""
struct Emcee{space,E<:AMH.Ensemble} <: InferenceAlgorithm
ensemble::E
end
Expand Down
5 changes: 5 additions & 0 deletions src/mcmc/particle_mcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ function PG(nparticles::Int, space::Tuple)
return PG(nparticles, AdvancedPS.ResampleWithESSThreshold(), space)
end

"""
CSMC(...)
Equivalent to [`PG`](@ref).
"""
const CSMC = PG # type alias of PG as Conditional SMC

struct PGTransition{T,F<:AbstractFloat} <: AbstractTransition
Expand Down
Loading

0 comments on commit 302a499

Please sign in to comment.