You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: developers/transforms/dynamicppl/index.qmd
+10-36Lines changed: 10 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -126,50 +126,24 @@ This sequence of events is summed up in the following diagram, where `f(..., arg
126
126
In the final part of this article, we will take a more in-depth look at the internal DynamicPPL machinery that allows us to convert between representations and obtain the correct probability densities.
127
127
Before that, though, we will take a quick high-level look at how the HMC sampler in Turing.jl uses the functions introduced so far.
128
128
129
-
## Case study: HMC in Turing.jl
129
+
## HMC in Turing.jl
130
130
131
131
While DynamicPPL provides the _functionality_ for transforming variables, the transformation itself happens at an even higher level, i.e. in the sampler itself.
132
132
The HMC sampler in Turing.jl is in [this file](https://github.com/TuringLang/Turing.jl/blob/5b24cebe773922e0f3d5c4cb7f53162eb758b04d/src/mcmc/hmc.jl).
133
-
In the first step of sampling, it calls `link` on the sampler.
134
-
This transformation is preserved throughout the sampling process, meaning that `is_transformed()` always returns true.
135
-
136
-
We can observe this by inserting print statements into the model.
137
-
Here, `__varinfo__` is the internal symbol for the `VarInfo` object used in model evaluation:
(Here, the check on `if x isa AbstractFloat` prevents the printing from occurring during computation of the derivative.)
157
-
You can see that during the three sampling steps, `is_transformed` is always kept as `true`.
158
-
159
-
::: {.callout-note}
160
-
The first two model evaluations where `is_transformed` is `false` occur prior to the actual sampling.
161
-
One occurs when the model is checked for correctness (using [`DynamicPPL.check_model_and_trace`](https://github.com/TuringLang/DynamicPPL.jl/blob/ba490bf362653e1aaefe298364fe3379b60660d3/src/debug_utils.jl#L582-L612)).
162
-
The second occurs because the model is evaluated once to generate a set of initial parameters inside [DynamicPPL's implementation of `AbstractMCMC.step`](https://github.com/TuringLang/DynamicPPL.jl/blob/ba490bf362653e1aaefe298364fe3379b60660d3/src/sampler.jl#L98-L117).
163
-
Both of these steps occur with all samplers in Turing.jl, so are not specific to the HMC example shown here.
164
-
:::
165
133
134
+
In the first step of sampling, it calls `link` on the sampler.
166
135
What this means is that from the perspective of the HMC sampler, it _never_ sees the constrained variable: it always thinks that it is sampling from an unconstrained distribution.
167
136
168
137
The biggest prerequisite for this to work correctly is that the potential energy term in the Hamiltonian—or in other words, the model log density—must be programmed correctly to include the Jacobian term.
169
138
This is exactly the same as how we had to make sure to define `logq(y)` correctly in the toy HMC example above.
170
139
171
-
Within Turing.jl, this is correctly handled because a statement like `x ~ LogNormal()` in the model definition above is translated into `assume(LogNormal(), @varname(x), __varinfo__)`, defined [here](https://github.com/TuringLang/DynamicPPL.jl/blob/ba490bf362653e1aaefe298364fe3379b60660d3/src/context_implementations.jl#L225-L229).
172
-
If you follow the trail of function calls, you can verify that the `assume` function does indeed check for the presence of the `is_transformed` flag and adds the Jacobian term accordingly.
140
+
Within Turing.jl, this is correctly handled by calling
0 commit comments