# Log determinant objective

**URL:** <https://discourse.julialang.org/t/log-determinant-objective/23927>\
**Category:** Optimization (Mathematical)\
**Created:** [May 6, 2019, 7:31pm UTC](https://discourse.julialang.org/t/log-determinant-objective/23927 "2019-05-06T19:31:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ksun46](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksun46/32/9437_2.png) [@Ksun46](https://discourse.julialang.org/u/Ksun46)\
**Post date:** [May 6, 2019, 7:31pm UTC](https://discourse.julialang.org/t/log-determinant-objective/23927/1 "2019-05-06T19:31:28Z")

</div>

Any recommendations on how to formulate a log determinant objective in JuMP? Thanks.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [May 7, 2019, 7:50am UTC](https://discourse.julialang.org/t/log-determinant-objective/23927/2 "2019-05-07T07:50:25Z")

</div>

The current recommendation is to use the log-det cone:  
[http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.LogDetConeTriangle](http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.LogDetConeTriangle)  
It is automatically reformulated into exponential cone and PSD constraints with  
[http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.Bridges.LogDetBridge](http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.Bridges.LogDetBridge)  
To maximize the volume of an ellipsoid with rotated second-order cone (RSOC) instead of exponential cone, there is also  
[http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.RootDetConeTriangle](http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.RootDetConeTriangle)  
which is automatically reformulated into RSOC and PSD constraints with  
[http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.Bridges.RootDetBridge](http://www.juliaopt.org/MathOptInterface.jl/stable/apireference/#MathOptInterface.Bridges.RootDetBridge)  
Some solvers such as [https://github.com/blegat/SDPT3.jl](https://github.com/blegat/SDPT3.jl) supports having log-det in the objective directly but this package is not finished yet.

I have created a package to make it easy write optimization problems with ellipsoids: [https://github.com/blegat/SetProg.jl](https://github.com/blegat/SetProg.jl)  
See [JuMP-dev 2019 | Benoit Legat | Set Programming with JuMP - YouTube](https://www.youtube.com/watch?v=hV3G-eNLNjk)

---

<div class="post-metadata">

**Author:** ![Ksun46](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksun46/32/9437_2.png) [@Ksun46](https://discourse.julialang.org/u/Ksun46)\
**Post date:** [May 15, 2019, 2:46am UTC](https://discourse.julialang.org/t/log-determinant-objective/23927/3 "2019-05-15T02:46:28Z")

</div>

Thanks! I will look into it.

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [December 16, 2021, 8:52pm UTC](https://discourse.julialang.org/t/log-determinant-objective/23927/4 "2021-12-16T20:52:06Z")

</div>

> [@blegat](#):
>
> The current recommendation is to use the log-det cone

How does one use this? My objective looks like this:

```julia
@NLobjective(
        model, Max,
        log(det(B)) + # produces "Unexpected array..."
        sum(
            (
                -log(σ^2) / 2 - ϵ[i]^2 / 2
                + log( 2Φ(a * ϵ[i]) )
            )
            for i ∈ eachindex(ϵ)
        )
    )

```

How do I incorporate “log-det cone” into it? Are there any modern best practices considering optimization of functions _including_ determinants (not _just_ the determinant itself)?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 16, 2021, 9:30pm UTC](https://discourse.julialang.org/t/log-determinant-objective/23927/5 "2021-12-16T21:30:16Z")

</div>

> [@ForceBru](#):
>
> `log(det(B))`

Offtopic: Don’t compute log determinants this way — you can easily overflow the limits of floating-point arithmetic. The LinearAlgebra package has functions `logdet` and `logabsdet` that you can employ.

No idea how `logdet` interacts with JuMP, but log determinants also have a [nice formula for their derivative](https://statisticaloddsandends.wordpress.com/2018/05/24/derivative-of-log-det-x/) which is useful in optimization.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 16, 2021, 9:49pm UTC](https://discourse.julialang.org/t/log-determinant-objective/23927/6 "2021-12-16T21:49:32Z")

</div>

> How does one use this?

Here’s an example, but it isn’t supported by Ipopt or NLopt, you’ll need to reformulate your entire problem as a conic optimization problem and use a solver like Mosek or SCS.

> **[Standard form · MathOptInterface](https://jump.dev/MathOptInterface.jl/stable/reference/standard_form/#MathOptInterface.LogDetConeSquare)**
>
> Documentation for MathOptInterface.

```julia
# max log(det(x))
#
# is equivalent to
# max t
# s.t. t <= log(det(X))

model = Model()
@variable(model, X[1:3, 1:3])
@variable(model, t)
@constraint(model, [t; 1; vec(X)] in MOI.LogDetConeSquare(3))
@objective(model, Max, t)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 16, 2021, 10:31pm UTC](https://discourse.julialang.org/t/log-determinant-objective/23927/7 "2021-12-16T22:31:38Z")

</div>

(Or you could just implement your own gradient using the formula I mentioned and then call NLopt or whatever optimization software you want. Using a modeling language is a two-edged sword — it can be convenient, but it is also limiting.)

Note also that ChainRules includes [rules for `logdet`](https://github.com/JuliaDiff/ChainRules.jl/blob/a6c9ade4ef003a64c00fa6fe61a668c4f8390a63/src/rulesets/LinearAlgebra/dense.jl#L106-L152), so you can use automatic differentiation (Zygote etcetera) to find your derivatives, which then makes it a lot easier to call arbitrary optimization packages.
