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

**URL:** <https://discourse.julialang.org/t/turing-jl-how-to-generate-samples-observations-from-a-model/95258>\
**Category:** Statistics\
**Tags:** turing\
**Created:** [February 27, 2023, 9:43am UTC](https://discourse.julialang.org/t/turing-jl-how-to-generate-samples-observations-from-a-model/95258 "2023-02-27T09:43:47Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [February 27, 2023, 9:59am UTC](https://discourse.julialang.org/t/turing-jl-how-to-generate-samples-observations-from-a-model/95258/2 "2023-02-27T09:59:58Z")

</div>

```julia
@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

```julia
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)`.

---

_[View the full topic](https://discourse.julialang.org/t/turing-jl-how-to-generate-samples-observations-from-a-model/95258)._
