# Optimization.jl interface for Turing models

**URL:** <https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473>\
**Category:** Optimization (Mathematical)\
**Tags:** question, turing, sciml\
**Created:** [December 12, 2023, 1:32am UTC](https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473 "2023-12-12T01:32:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [December 12, 2023, 1:32am UTC](https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473/1 "2023-12-12T01:32:09Z")

</div>

I feel like I’ve seen an example somewhere of how to construct an `Optimization.OptimizationFunction` from a Turing model, but am not finding it at the moment. Can anyone help me find it? Did I imagine this functionality?

I can use the Optim.jl interface for MLE/MAP estimation no problem, but would like to try fitting some models with the wider galaxy of solvers that are easily available through Optimization.jl.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [January 31, 2024, 4:05am UTC](https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473/2 "2024-01-31T04:05:59Z")

</div>

Giving this a bump…the example below lets me create an `OptimizationFunction` and `OptimizationProblem` from the Turing model, but throws a `StackOverflowError` when I try to `solve` it. Is this a bug, or just functionality that isn’t implemented?

```julia

using Turing
using Optimization, OptimizationOptimJL

@model function example(x, y)
    a ~ Normal()
    b ~ Normal()
    σ ~ Gamma()

    μ = a .+ b .* x
    y ~ MvNormal(μ, σ)
end

x = rand(10)
y = 1 .+ 2x + randn(10)

m = example(x, y)
f = OptimizationFunction(m)
p = OptimizationProblem(f, zeros(3))
solve(p, NelderMead())

```

---

<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:** [January 31, 2024, 9:22am UTC](https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473/3 "2024-01-31T09:22:10Z")

</div>

There is an exported interface, but it’s not documented:

```julia
julia> p = optim_problem(m, MAP());

julia> solve(p.prob, NelderMead())
retcode: Success
u: 3-element Vector{Float64}:
 1.250061897354349
 1.5422820485580477
 1.055306850982959

```

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [January 31, 2024, 9:30am UTC](https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473/4 "2024-01-31T09:30:56Z")

</div>

Thanks! I knew I’d seen it somewhere. Would a documentation PR be wanted, or is this interface still experimental?

---

<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:** [January 31, 2024, 3:04pm UTC](https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473/5 "2024-01-31T15:04:48Z")

</div>

I don’t know of there’s a reason. I’d recommend opening an issue first to find out before putting in the work.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [February 1, 2024, 12:06am UTC](https://discourse.julialang.org/t/optimization-jl-interface-for-turing-models/107473/6 "2024-02-01T00:06:28Z")

</div>

> <https://github.com/TuringLang/Turing.jl/issues/2164>
>
> Moving this question here from \[Discourse\](https://discourse.julialang.org/t/opt…imization-jl-interface-for-turing-models/107473). The \`optim\_problem\` interface to Optimization.jl is currently not documented anywhere. Is this intentional (e.g. because the interface is a moving target), or would a documentation PR be welcome?
