Skip to content

Commit b80e488

Browse files
committed
istrans -> is_transformed
1 parent 3eb49c6 commit b80e488

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

developers/transforms/dynamicppl/index.qmd

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Note that this acts on _all_ variables in the model, including unconstrained one
6767
vi_linked = DynamicPPL.link(vi, model)
6868
println("Transformed value: $(DynamicPPL.getindex_internal(vi_linked, vn_x))")
6969
println("Transformed logp: $(DynamicPPL.getlogp(vi_linked))")
70-
println("Transformed flag: $(DynamicPPL.istrans(vi_linked, vn_x))")
70+
println("Transformed flag: $(DynamicPPL.is_transformed(vi_linked, vn_x))")
7171
```
7272

7373
Indeed, we can see that the new logp value matches with
@@ -82,7 +82,7 @@ The reverse transformation, `invlink`, reverts all of the above steps:
8282
vi = DynamicPPL.invlink(vi_linked, model) # Same as the previous vi
8383
println("Un-transformed value: $(DynamicPPL.getindex_internal(vi, vn_x))")
8484
println("Un-transformed logp: $(DynamicPPL.getlogp(vi))")
85-
println("Un-transformed flag: $(DynamicPPL.istrans(vi, vn_x))")
85+
println("Un-transformed flag: $(DynamicPPL.is_transformed(vi, vn_x))")
8686
```
8787

8888
### Model and internal representations
@@ -104,7 +104,7 @@ Note that `vi_linked[vn_x]` can also be used as shorthand for `getindex(vi_linke
104104
We can see (for this linked varinfo) that there are _two_ differences between these outputs:
105105

106106
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.
108108

109109
2. _The internal representation is a vector, whereas the model representation is a scalar._
110110
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
131131
While DynamicPPL provides the _functionality_ for transforming variables, the transformation itself happens at an even higher level, i.e. in the sampler itself.
132132
The HMC sampler in Turing.jl is in [this file](https://github.com/TuringLang/Turing.jl/blob/5b24cebe773922e0f3d5c4cb7f53162eb758b04d/src/mcmc/hmc.jl).
133133
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.
135135

136136
We can observe this by inserting print statements into the model.
137137
Here, `__varinfo__` is the internal symbol for the `VarInfo` object used in model evaluation:
@@ -145,7 +145,7 @@ setprogress!(false)
145145
println("-----------")
146146
println("model repn: $(DynamicPPL.getindex(__varinfo__, @varname(x)))")
147147
println("internal repn: $(DynamicPPL.getindex_internal(__varinfo__, @varname(x)))")
148-
println("istrans: $(istrans(__varinfo__, @varname(x)))")
148+
println("is_transformed: $(is_transformed(__varinfo__, @varname(x)))")
149149
end
150150
end
151151
@@ -154,10 +154,10 @@ sample(demo2(), HMC(0.1, 3), 3);
154154

155155

156156
(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`.
158158

159159
::: {.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.
161161
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)).
162162
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).
163163
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
169169
This is exactly the same as how we had to make sure to define `logq(y)` correctly in the toy HMC example above.
170170

171171
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.
173173

174174
## A deeper dive into DynamicPPL's internal machinery
175175

@@ -234,7 +234,7 @@ DynamicPPL.getindex_internal(vi_linked, vn_x)
234234
```
235235

236236
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.
238238

239239
<!-- 'wrappingWidth' setting required because of https://github.com/mermaid-js/mermaid-cli/issues/112#issuecomment-2352670995 -->
240240
```{mermaid}
@@ -246,7 +246,7 @@ graph TD
246246
A["x ~ LogNormal()"]:::boxStyle
247247
B["vn = <span style='color:#3B6EA8 !important;'>@varname</span>(x)<br>dist = LogNormal()<br>x, vi = ..."]:::boxStyle
248248
C["assume(vn, dist, vi)"]:::boxStyle
249-
D(["<span style='color:#3B6EA8 !important;'>if</span> istrans(vi, vn)"]):::boxStyle
249+
D(["<span style='color:#3B6EA8 !important;'>if</span> is_transformed(vi, vn)"]):::boxStyle
250250
E["f = from_internal_transform(vi, vn, dist)"]:::boxStyle
251251
F["f = from_linked_internal_transform(vi, vn, dist)"]:::boxStyle
252252
G["x, logjac = with_logabsdet_jacobian(f, getindex_internal(vi, vn, dist))"]:::boxStyle
@@ -267,7 +267,7 @@ graph TD
267267

268268
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.
269269

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.
271271
However, if it was linked, then the Jacobian term would be appropriately included, making sure that sampling proceeds correctly.
272272

273273
## Why do we need to do this at runtime?

0 commit comments

Comments
 (0)