|
| 1 | +# Example: Variational inference |
| 2 | + |
| 3 | +The real utility of `TransformedDistribution` becomes more apparent when using `transformed(dist, b)` for any bijector `b`. |
| 4 | +To get the transformed distribution corresponding to the `Beta(2, 2)`, we called `transformed(dist)` before. |
| 5 | +This is an alias for `transformed(dist, bijector(dist))`. |
| 6 | +Remember `bijector(dist)` returns the constrained-to-constrained bijector for that particular `Distribution`. |
| 7 | +But we can of course construct a `TransformedDistribution` using different bijectors with the same `dist`. |
| 8 | + |
| 9 | +This is particularly useful in _Automatic Differentiation Variational Inference (ADVI)_. |
| 10 | + |
| 11 | +## Univariate ADVI |
| 12 | + |
| 13 | +An important part of ADVI is to approximate a constrained distribution, e.g. `Beta`, as follows: |
| 14 | + |
| 15 | + 1. Sample `x` from a `Normal` with parameters `μ` and `σ`, i.e. `x ~ Normal(μ, σ)`. |
| 16 | + 2. Transform `x` to `y` s.t. `y ∈ support(Beta)`, with the transform being a differentiable bijection with a differentiable inverse (a "bijector"). |
| 17 | + |
| 18 | +This then defines a probability density with the same _support_ as `Beta`! |
| 19 | +Of course, it's unlikely that it will be the same density, but it's an _approximation_. |
| 20 | + |
| 21 | +Creating such a distribution can be done with `Bijector` and `TransformedDistribution`: |
| 22 | + |
| 23 | +```@example advi |
| 24 | +using Bijectors |
| 25 | +using StableRNGs: StableRNG |
| 26 | +rng = StableRNG(42) |
| 27 | +
|
| 28 | +dist = Beta(2, 2) |
| 29 | +b = bijector(dist) # (0, 1) → ℝ |
| 30 | +b⁻¹ = inverse(b) # ℝ → (0, 1) |
| 31 | +td = transformed(Normal(), b⁻¹) # x ∼ 𝓝(0, 1) then b(x) ∈ (0, 1) |
| 32 | +x = rand(rng, td) # ∈ (0, 1) |
| 33 | +``` |
| 34 | + |
| 35 | +It's worth noting that `support(Beta)` is the _closed_ interval `[0, 1]`, while the constrained-to-unconstrained bijection, `Logit` in this case, is only well-defined as a map `(0, 1) → ℝ` for the _open_ interval `(0, 1)`. |
| 36 | +This is of course not an implementation detail. |
| 37 | +`ℝ` is itself open, thus no continuous bijection exists from a _closed_ interval to `ℝ`. |
| 38 | +But since the boundaries of a closed interval has what's known as measure zero, this doesn't end up affecting the resulting density with support on the entire real line. |
| 39 | +In practice, this means that |
| 40 | + |
| 41 | +```@example advi |
| 42 | +td = transformed(Beta()) |
| 43 | +inverse(td.transform)(rand(rng, td)) |
| 44 | +``` |
| 45 | + |
| 46 | +will never result in `0` or `1` though any sample arbitrarily close to either `0` or `1` is possible. |
| 47 | +_Disclaimer: numerical accuracy is limited, so you might still see `0` and `1` if you're 'lucky'._ |
| 48 | + |
| 49 | +## Multivariate ADVI example |
| 50 | + |
| 51 | +We can also do _multivariate_ ADVI using the `Stacked` bijector. |
| 52 | +`Stacked` gives us a way to combine univariate and/or multivariate bijectors into a singe multivariate bijector. |
| 53 | +Say you have a vector `x` of length 2 and you want to transform the first entry using `Exp` and the second entry using `Log`. |
| 54 | +`Stacked` gives you an easy and efficient way of representing such a bijector. |
| 55 | + |
| 56 | +```@example advi |
| 57 | +using Bijectors: SimplexBijector |
| 58 | +
|
| 59 | +# Original distributions |
| 60 | +dists = (Beta(), InverseGamma(), Dirichlet(2, 3)) |
| 61 | +
|
| 62 | +# Construct the corresponding ranges |
| 63 | +function make_ranges(dists) |
| 64 | + ranges = [] |
| 65 | + idx = 1 |
| 66 | + for i in 1:length(dists) |
| 67 | + d = dists[i] |
| 68 | + push!(ranges, idx:(idx + length(d) - 1)) |
| 69 | + idx += length(d) |
| 70 | + end |
| 71 | + return ranges |
| 72 | +end |
| 73 | +
|
| 74 | +ranges = make_ranges(dists) |
| 75 | +ranges |
| 76 | +``` |
| 77 | + |
| 78 | +```@example advi |
| 79 | +# Base distribution; mean-field normal |
| 80 | +num_params = ranges[end][end] |
| 81 | +
|
| 82 | +d = MvNormal(zeros(num_params), ones(num_params)); |
| 83 | +
|
| 84 | +# Construct the transform |
| 85 | +bs = bijector.(dists) # constrained-to-unconstrained bijectors for dists |
| 86 | +ibs = inverse.(bs) # invert, so we get unconstrained-to-constrained |
| 87 | +sb = Stacked(ibs, ranges) # => Stacked <: Bijector |
| 88 | +
|
| 89 | +# Mean-field normal with unconstrained-to-constrained stacked bijector |
| 90 | +td = transformed(d, sb) |
| 91 | +y = rand(td) |
| 92 | +``` |
| 93 | + |
| 94 | +As can be seen from this, we now have a `y` for which `0.0 ≤ y[1] ≤ 1.0`, `0.0 < y[2]`, and `sum(y[3:4]) ≈ 1.0`. |
0 commit comments