# Fitting hierarchical model with Turing NUTS error

**URL:** https://discourse.julialang.org/t/fitting-hierarchical-model-with-turing-nuts-error/111527
**Category:** General Usage
**Created:** [March 12, 2024, 3:55pm UTC](https://discourse.julialang.org/t/fitting-hierarchical-model-with-turing-nuts-error/111527 "2024-03-12T15:55:47Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![jpdelong](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@jpdelong](https://discourse.julialang.org/u/jpdelong)
#### Post date: [March 12, 2024, 3:55pm UTC](https://discourse.julialang.org/t/fitting-hierarchical-model-with-turing-nuts-error/111527/1 "2024-03-12T15:55:47Z")

</div>

Hi all - I am trying to fit a nonlinear hierarchical model to data using Turing. It works fine without the hierarchical part and it works with the MH sampler, but it fails using HMC and NUTS, with the following errors:

ERROR: DomainError with Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}}(NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN):  
Normal: the condition σ \>= zero(σ) is not satisfied.

## Here is my code:

using Turing, MCMCChains, StatsPlots, Distributions

spider = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2]  
num\_prey = [2 1 10 9 8 7 6 5 4 7 6 5 4 3 2 1 1 2 1 10 9 8 7 6 5 4 3 2]  
time2feed = [0.0122 0.020 0.0164 0.000219 0.00642 0.0107 0.0132 0.0132 0.0148 0.00684 0.00774 0.015 0.00765 0.00585 0.00996 0.00758 0.00752 0.00798 0.0223 0.0001597 0.00368 0.00459 0.00427 0.00466 0.00626 0.00562 0.00752 0.0205]

@model function t2c\_fun\_res\_h(num\_prey,spider,time2feed)  
n\_gr = length(unique(spider))

```
a ~ truncated(Normal(1/maximum(time2feed),0.1*(1/maximum(time2feed))),0.01,Inf)
σ_a ~ Exponential(20)

h ~ truncated(Normal(0.001,0.001),0.0001,maximum(time2feed))
σ_h ~ Exponential(std(time2feed))

a_spider ~ filldist(truncated(Normal(a,σ_a),0.01,Inf), n_gr)
h_spider ~ filldist(truncated(Normal(h,σ_h),0,Inf), n_gr)

for i in 1:length(time2feed)
    time2feed[i] ~ Exponential.((1 ./ ((a_spider[spider[i]]) .* num_prey[i]) + (h_spider[spider[i]])))
end

```

end

model\_h = t2c\_fun\_res\_h(num\_prey,spider,time2feed)

fr\_chain = sample(  
model\_h,  
NUTS(0.65),  
#HMC(0.05,10),  
init\_params = [50,13,0.001,0.005,52,0.004],  
1000  
);

Any thoughts on that error and how to fix it? My error trace suggests it has something to do with the line specifying a\_spider.

John
