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
Ensure only model parameters are included in the sorted parameter list (#292)
If a Julia code to compute log density can be generated for a model,
then the topo order of nodes in the model need to be updated to sync
with the generated Julia source.
The original code used the topologically sorted list of all nodes
(`pass.sorted_nodes`) to determine the final ordered list of parameters
for the `Model`. However, `pass.sorted_nodes` can include variables that
are purely "transformed data" - deterministic nodes computed entirely
from provided data constants at compile time (as described in
`docs/src/source_gen.md` under "Handling Mixed Data Transformation and
Deterministic Assignments"). For example, if `y = [1, 2, missing,
missing, 2]` and the model has `x[i] = y[i] + 1`, then `x[1]`, `x[2]`,
and `x[5]` are transformed data, while only `x[3]` and `x[4]` (dependent
on the missing `y` values) are actual deterministic parameters within
the model graph. Including transformed data nodes in the final
`parameters` list is incorrect, as they are constants and not part of
the model's parameter space to be evaluated or sampled.
This PR is a simple fix by filtering the `pass.sorted_nodes` list,
keeping only those `VarName`s that are already present in the
`parameters` set. This ensures the final `parameters` field of the
`Model` struct contains only true model parameters, correctly ordered
according to the graph's topology.
---------
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/src/source_gen.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -457,6 +457,8 @@ end
457
457
458
458
We made a simple change to the program to prepare for lowering: we need to distinguish between observations and model parameters (because they correspond to different code). We introduce a new operator into the program `\eqsim` to indicate that the left hand side is an observation.
459
459
460
+
### Handling Mixed Observations and Parameters
461
+
460
462
BUGS supports mixing observations and model parameters for different elements of the same array variable.
461
463
To support this, we introduce a guard to use conditional logic to decide what computation to do for different iteration of the same statement.
462
464
@@ -487,3 +489,27 @@ begin
487
489
end
488
490
end
489
491
```
492
+
493
+
### Handling Mixed Data Transformation and Deterministic Assignments
494
+
495
+
For instance
496
+
497
+
```julia
498
+
for i in1:5
499
+
x[i] = y[i] +1
500
+
end
501
+
```
502
+
503
+
if the data is
504
+
505
+
```julia
506
+
y = [1, 2, missing, missing, 2]
507
+
```
508
+
509
+
this is generally allowed in BUGS.
510
+
511
+
`x[1], x[2], x[5]` can be computed at compile time, so these are "transformed data".
512
+
`x[3], x[4]` need to be computed at evaluation time.
513
+
And only `x[3]` and `x[4]` are in the compiled graph.
514
+
515
+
For generated Julia program, if a statement can be eliminated because all the variables stemmed from this statement are "transformed data". While in the above case, where a statements corresponds to both transformed data and deterministic variables. It will be left in the generated program as is. In this case, there will be redundant computation.
0 commit comments