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: docs/src/user-guide.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -177,6 +177,64 @@ node, y ~ NormalMeanVariance(mean, var)
177
177
178
178
Having a node reference can be useful in case the user wants to return it from a model and to use it later on to specify initial joint marginal distributions.
179
179
180
+
### Broadcasting syntax
181
+
182
+
!!! note
183
+
Broadcasting syntax requires at least v2.1.0 of `GraphPPL.jl`
184
+
185
+
GraphPPL support broadcasting for `~` operator in the exact same way as Julia itself. A user is free to write an expression of the following form:
186
+
187
+
```julia
188
+
y =datavar(Float64, n)
189
+
y .~NormalMeanVariance(0.0, 1.0) # <- i.i.d observations
190
+
```
191
+
192
+
More complex expression are also allowed:
193
+
194
+
```julia
195
+
m ~NormalMeanPrecision(0.0, 0.0001)
196
+
t ~Gamma(1.0, 1.0)
197
+
198
+
y =randomvar(Float64, n)
199
+
y .~NormalMeanPrecision(m, t)
200
+
```
201
+
202
+
```julia
203
+
A =constvar(...)
204
+
x =randomvar(n)
205
+
y =datavar(Vector{Float64}, n)
206
+
207
+
w ~Wishart(3, diageye(2))
208
+
x[1] ~MvNormalMeanPrecision(zeros(2), diageye(2))
209
+
x[2:end] .~ A .* x[1:end-1] # <- State-space model with transition matrix A
210
+
y .~MvNormalMeanPrecision(x, w) # <- Observations with unknown precision matrix
211
+
```
212
+
213
+
Note, however, that all variables that take part in the broadcasting operation must be defined before either with `randomvar` or `datavar`. The exception here is constants that are automatically converted to their `constvar` equivalent. If you want to prevent broadcasting for some constant (e.g. if you want to add a vector to a multivariate Gaussian distribution) use explicit `constvar` call:
214
+
215
+
```julia
216
+
# Suppose `x` is a 2-dimensional Gaussian distribution
217
+
z .~ x .+constvar([ 1, 1 ])
218
+
# Which is equivalent to
219
+
for i in1:n
220
+
z[i] ~ x[i] +constvar([ 1, 1 ])
221
+
end
222
+
```
223
+
224
+
Without explicit `constvar` Julia's broadcasting machinery would instead attempt to unroll for loop in the following way:
225
+
226
+
```julia
227
+
# Without explicit `constvar`
228
+
z .~ x .+ [ 1, 1 ]
229
+
# Which is equivalent to
230
+
array = [1, 1]
231
+
for i in1:n
232
+
z[i] ~ x[i] + array[i] # This is wrong if `x[i]` is supposed to be a multivariate Gaussian
233
+
end
234
+
```
235
+
236
+
Read more about how broadcasting machinery works in Julia in [the official documentation](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting).
237
+
180
238
### Node creation options
181
239
182
240
To pass optional arguments to the node creation constructor the user can use the `where { options... }` options specification syntax.
errstr ="The expression `$varexpr = datavar($(type))` is incorrect. datavar(::Type, [ dims... ]) requires `Type` as a first argument, but `$(type)` is not a `Type`."
# In case of broadcasted call we assume that variable has been created before otherwise it should throw an error
367
+
$(write_check_variable_existence(backend, model, short_id, "Cannot use variables named `$(short_id)` in the broadcasting call. `$(short_id)` sequence of variables must be created in advance."))
0 commit comments