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
@@ -104,7 +104,7 @@ Note that `vi_linked[vn_x]` can also be used as shorthand for `getindex(vi_linke
104
104
We can see (for this linked varinfo) that there are _two_ differences between these outputs:
105
105
106
106
1._The internal representation has been transformed using the bijector (in this case, the log function)._
107
-
This means that the `istrans()` flag which we used above doesn't modify the model representation: it only tells us whether the internal representation has been transformed or not.
107
+
This means that the `is_transformed()` flag which we used above doesn't modify the model representation: it only tells us whether the internal representation has been transformed or not.
108
108
109
109
2._The internal representation is a vector, whereas the model representation is a scalar._
110
110
This is because in DynamicPPL, _all_ internal values are vectorised (i.e. converted into some vector), regardless of distribution. On the other hand, since the model specifies a univariate distribution, the model representation is a scalar.
@@ -131,7 +131,7 @@ Before that, though, we'll take a quick high-level look at how the HMC sampler i
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
133
In the first step of sampling, it calls `link` on the sampler.
134
-
This transformation is preserved throughout the sampling process, meaning that `istrans()` always returns true.
134
+
This transformation is preserved throughout the sampling process, meaning that `is_transformed()` always returns true.
135
135
136
136
We can observe this by inserting print statements into the model.
137
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, `istrans` is always kept as `true`.
157
+
You can see that during the three sampling steps, `is_transformed` is always kept as `true`.
158
158
159
159
::: {.callout-note}
160
-
The first two model evaluations where `istrans` is `false` occur prior to the actual sampling.
160
+
The first two model evaluations where `is_transformed` is `false` occur prior to the actual sampling.
161
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
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
163
Both of these steps occur with all samplers in Turing.jl, so are not specific to the HMC example shown here.
@@ -169,7 +169,7 @@ The biggest prerequisite for this to work correctly is that the potential energy
169
169
This is exactly the same as how we had to make sure to define `logq(y)` correctly in the toy HMC example above.
170
170
171
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 `istrans` flag and adds the Jacobian term accordingly.
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.
173
173
174
174
## A deeper dive into DynamicPPL's internal machinery
The purpose of having all of these machinery is to allow other parts of DynamicPPL, such as the tilde pipeline, to handle transformed variables correctly.
237
-
The following diagram shows how `assume` first checks whether the variable is transformed (using `istrans`), and then applies the appropriate transformation function.
237
+
The following diagram shows how `assume` first checks whether the variable is transformed (using `is_transformed`), and then applies the appropriate transformation function.
238
238
239
239
<!-- 'wrappingWidth' setting required because of https://github.com/mermaid-js/mermaid-cli/issues/112#issuecomment-2352670995 -->
240
240
```{mermaid}
@@ -246,7 +246,7 @@ graph TD
246
246
A["x ~ LogNormal()"]:::boxStyle
247
247
B["vn = <span style='color:#3B6EA8 !important;'>@varname</span>(x)<br>dist = LogNormal()<br>x, vi = ..."]:::boxStyle
Here, `with_logabsdet_jacobian` is defined [in the ChangesOfVariables.jl package](https://juliamath.github.io/ChangesOfVariables.jl/stable/api/#ChangesOfVariables.with_logabsdet_jacobian), and returns both the effect of the transformation `f` as well as the log Jacobian term.
269
269
270
-
Because we chose `f` appropriately, we find here that `x` is always the model representation; furthermore, if the variable was _not_ linked (i.e. `istrans` was false), the log Jacobian term will be zero.
270
+
Because we chose `f` appropriately, we find here that `x` is always the model representation; furthermore, if the variable was _not_ linked (i.e. `is_transformed` was false), the log Jacobian term will be zero.
271
271
However, if it was linked, then the Jacobian term would be appropriately included, making sure that sampling proceeds correctly.
0 commit comments