[Turing.jl] How to generate samples ("observations") from a model

@model function mymodel2(y::AbstractVector{F}) where {F<:Real}
  T = length(y)
  θ ~ Uniform(0,1)
  y ~ MvNormal(θ * Diagonal(ones(T)))
  return rand(MvNormal(θ * Diagonal(ones(T))))
end

with

t = rand(mymodel(rand(10)))[1]
param = (; θ = t)
generated_quantities(mymodel2(y), param)

does the job.

However, it is not satisfying:

  • When doing inference e.g. chain2 = sample(mymodel2(y), MH(), 10000), the rand inside the model is useless and called every time the model is called.
  • This is probably inefficient if ones wants to use generated_quantities N-time compared to rand(MvNormal(θ * Diagonal(ones(T))), N).