# \[DynamicPPL\] attempt to link a linked vi

**URL:** https://discourse.julialang.org/t/dynamicppl-attempt-to-link-a-linked-vi/87572
**Category:** Probabilistic Programming
**Tags:** question, turing
**Created:** [September 21, 2022, 12:58pm UTC](https://discourse.julialang.org/t/dynamicppl-attempt-to-link-a-linked-vi/87572 "2022-09-21T12:58:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [September 21, 2022, 12:58pm UTC](https://discourse.julialang.org/t/dynamicppl-attempt-to-link-a-linked-vi/87572/1 "2022-09-21T12:58:06Z")

</div>

I am getting this warning multiple times and the NUTS sampler doesn’t terminate. Can you please explain what is happening for an end user of Turing.jl? The warning message is not very helpful.

How can I further debug this issue? Is there a method in Turing.jl to debug whenever something goes wrong with a particular subset of samples?

---

<div class="post-metadata">

### Author: ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)
#### Post date: [November 14, 2022, 11:16am UTC](https://discourse.julialang.org/t/dynamicppl-attempt-to-link-a-linked-vi/87572/2 "2022-11-14T11:16:18Z")

</div>

Can you please provide a MWE of your model ?

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [November 14, 2022, 11:20am UTC](https://discourse.julialang.org/t/dynamicppl-attempt-to-link-a-linked-vi/87572/3 "2022-11-14T11:20:03Z")

</div>

I will try to come back to this issue, it is been 2 months…

---

<div class="post-metadata">

### Author: ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)
#### Post date: [November 16, 2022, 1:25pm UTC](https://discourse.julialang.org/t/dynamicppl-attempt-to-link-a-linked-vi/87572/4 "2022-11-16T13:25:14Z")

</div>

Hi. Because I also stumbled upon such an issue I investigated a bit my model and I managed to narrow it down to:

```julia
"""
data : `Vector` of data
len: length of data (for prior analyisis)
"""
@model function attemptlinkedvi(data, len=missing)
	if data === missing
		data = Vector{Int}(undef, len)
	end
	μ = 100
	σ = 100

	prior ~ truncated(Normal(μ,σ),0,Inf)
	data ~ filldist(Exponential(prior), 20)
end

```

This model fits the data fine.  
If I **substitute** `σ = 100` with `σ = Exponential(100)` and then I get unending complaints about _attempting to link a linked vi_.  
In both cases sampling the prior without respect to data works fine.

> **Warnings**
>
> ```julia
> ┌ Warning: The current proposal will be rejected due to numerical error(s).
> │ isfinite.((θ, r, ℓπ, ℓκ)) = (true, true, false, true)
> └ @ AdvancedHMC ~/.julia/packages/AdvancedHMC/iWHPQ/src/hamiltonian.jl:47
> ┌ Warning: [DynamicPPL] attempt to link a linked vi
> └ @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/varinfo.jl:821
> ┌ Warning: [DynamicPPL] attempt to link a linked vi
> └ @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/varinfo.jl:821
> ┌ Warning: The current proposal will be rejected due to numerical error(s).
> │ isfinite.((θ, r, ℓπ, ℓκ)) = (true, true, false, true)
> └ @ AdvancedHMC ~/.julia/packages/AdvancedHMC/iWHPQ/src/hamiltonian.jl:47
> ┌ Warning: [DynamicPPL] attempt to link a linked vi
> └ @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/varinfo.jl:821
> ┌ Warning: [DynamicPPL] attempt to link a linked vi
> └ @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/varinfo.jl:821
> ┌ Warning: The current proposal will be rejected due to numerical error(s).
> │ isfinite.((θ, r, ℓπ, ℓκ)) = (true, true, false, true)
> └ @ AdvancedHMC ~/.julia/packages/AdvancedHMC/iWHPQ/src/hamiltonian.jl:47
> ┌ Warning: [DynamicPPL] attempt to link a linked vi
> └ @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/varinfo.jl:821
> ┌ Warning: [DynamicPPL] attempt to link a linked vi
> └ @ DynamicPPL ~/.julia/packages/DynamicPPL/zPOYL/src/varinfo.jl:821
> ┌ Warning: The current proposal will be rejected due to numerical error(s).
> │ isfinite.((θ, r, ℓπ, ℓκ)) = (true, true, false, true)
> └ @ AdvancedHMC ~/.julia/packages/AdvancedHMC/iWHPQ/src/hamiltonian.jl:47
> 
> ```

I figured that it may be due to the `truncated`, i.e. MCMC samples the whole `Normal` and if the samplings are outside what the `truncated` requires it rejects the proposal.  
So I substituted the `truncated(Normal)` with a strictly positive distribution `Gamma`, i.e. :

```julia
@model function attemptlinkedvi(data, len=missing)
	if data === missing
		data = Vector{Int}(undef, len)
	end
	μ = 100
	σ ~ Exponential(100)

	prior ~ Gamma(μ/σ, σ)
	data ~ filldist(Exponential(prior), 20)
end

```

and it fits nicely !

Still I am not sure if my hypothesis was correct, but this certainly suggests that sampling truncated distributions comes with some pitfalls.

@juliohm Can you remember if you also had similar `truncated` distributions in your model maybe ?

It would be nice to have an opinion of a Turing developer.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [November 16, 2022, 4:23pm UTC](https://discourse.julialang.org/t/dynamicppl-attempt-to-link-a-linked-vi/87572/5 "2022-11-16T16:23:44Z")

</div>

Thank you @filchristou for the follow up. In the model I was developing there are no truncated distributions. I only had Normal and LogitNormal distributions.
