# Extract Turing model to LogDensityProblems and transform variables

**URL:** https://discourse.julialang.org/t/extract-turing-model-to-logdensityproblems-and-transform-variables/103201
**Category:** Probabilistic Programming
**Tags:** turing
**Created:** [August 25, 2023, 6:49pm UTC](https://discourse.julialang.org/t/extract-turing-model-to-logdensityproblems-and-transform-variables/103201 "2023-08-25T18:49:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![YuvalBernard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuvalbernard/32/52483_2.png) [@YuvalBernard](https://discourse.julialang.org/u/YuvalBernard)
#### Post date: [August 25, 2023, 6:49pm UTC](https://discourse.julialang.org/t/extract-turing-model-to-logdensityproblems-and-transform-variables/103201/1 "2023-08-25T18:49:40Z")

</div>

Hi all,  
As some sort of continuation of [Recommended way to extract logprob and build gradients in Turing.jl?](https://discourse.julialang.org/t/recommended-way-to-extract-logprob-and-build-gradients-in-turing-jl/88279)  
I wish to extract the log probability density from a Turing model (to use with Pathfider.jl for example),  
and I encountered the LogDensityFunction implementation of DynamicPPL, which is

```julia
f = LogDensityFunction(model) 

```

This reexports the model within the LogDensityProblems interface.  
However, is it possible to transform the model variables to unconstrained Euclidean space **beforehand** , so that MCMC can be performed without manually transforming each parameter using [TransformVariables.jl](https://github.com/tpapp/TransformVariables.jl)?

I’ve stumbled upon the link!! function from DynamicPPL, but I’ve got no clue how to implement it.

---

<div class="post-metadata">

### Author: ![sethaxen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sethaxen/32/35604_2.png) [@sethaxen](https://discourse.julialang.org/u/sethaxen)
#### Post date: [August 25, 2023, 11:00pm UTC](https://discourse.julialang.org/t/extract-turing-model-to-logdensityproblems-and-transform-variables/103201/2 "2023-08-25T23:00:10Z")

</div>

You can use Turing directly with Pathfinder, similar to the examples in [Turing usage · Pathfinder.jl](https://mlcolab.github.io/Pathfinder.jl/stable/examples/turing/)

But this should also work for you (adapted from [https://github.com/TuringLang/Turing.jl/blob/4affc28b341f4763bd1abc8523e4e209e9f6aa6e/src/contrib/inference/abstractmcmc.jl#L38-L48](https://github.com/TuringLang/Turing.jl/blob/4affc28b341f4763bd1abc8523e4e209e9f6aa6e/src/contrib/inference/abstractmcmc.jl#L38-L48)):

```julia

julia> using DynamicPPL, LogDensityProblems, LogDensityProblemsAD, ForwardDiff, Distributions

julia> @model function foo()
           x ~ Gamma()
       end;

julia> model = foo();

julia> ℓ = DynamicPPL.LogDensityFunction(model);

julia> LogDensityProblems.logdensity(ℓ, [-10.0]) # constrained
-Inf

julia> DynamicPPL.link!!(ℓ.varinfo, model);

julia> LogDensityProblems.logdensity(ℓ, [-10.0]) # unconstrained
-10.000045399929762

julia> ℓ_with_grad = LogDensityProblemsAD.ADgradient(Val(:ForwardDiff), ℓ);

julia> using Pathfinder

julia> pfr = pathfinder(ℓ_with_grad);

```

---

<div class="post-metadata">

### Author: ![YuvalBernard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuvalbernard/32/52483_2.png) [@YuvalBernard](https://discourse.julialang.org/u/YuvalBernard)
#### Post date: [August 26, 2023, 7:46am UTC](https://discourse.julialang.org/t/extract-turing-model-to-logdensityproblems-and-transform-variables/103201/3 "2023-08-26T07:46:57Z")

</div>

Thanks! works like a charm 🙂
