Drawing variables from truncated normal with mean as another variable

Hi All,

I want to drawing random variables from truncated normal with mean as another variable. However it didn’t work and I got the following wrong message. Is there anyone who can help me with it?

Gonzalo

using Turing, Distributions

@model function model(); alpha ~ Uniform(0,1); x = rand(Truncated(Normal(0, alpha),0,1)); end

sample(model(), NUTS(), 1000, thinning=10, discard_initial=1000);

Sampling 100%|████████████████████████████████████████████████████████████████████████████████| Time: 0:00:00
ERROR: MethodError: no method matching randnt(::TaskLocalRNG, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 1}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 1}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 1})
Closest candidates are:
randnt(::AbstractRNG, ::Float64, ::Float64, ::Float64) at ~/anaconda3/share/julia/packages/Distributions/UaWBm/src/truncated/normal.jl:158

You just need to rewrite it as follows, using the tilde to simulate x conditional on alpha:

 @model function model()
       alpha ~ Uniform(0, 1)
       x ~ truncated(Normal(0, alpha), 0, 1)
 end

chain = sample(model(), NUTS(), 1000)

Thank you so much. I am so wrong using rand.