# Optim.jl optimize over Distances.jl metric

**URL:** <https://discourse.julialang.org/t/optim-jl-optimize-over-distances-jl-metric/10542>\
**Category:** Optimization (Mathematical)\
**Created:** [April 25, 2018, 5:39pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-over-distances-jl-metric/10542 "2018-04-25T17:39:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcbaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcbaron/32/4027_2.png) [@mcbaron](https://discourse.julialang.org/u/mcbaron)\
**Post date:** [April 25, 2018, 5:39pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-over-distances-jl-metric/10542/1 "2018-04-25T17:39:55Z")

</div>

Hello all!

I’m wondering what’s the most efficient way to go about optimizing over a specific distance metric.

As of right now, I have two vectors, `x`, `y` and `bhattacharyya(x, y)` as loaded from Distances.jl. I’d like to call something like `optimize(bhattacharyya(x, y), x)` in order to find the adjustment to x that minimizes the Bhattacharyya distance between it and y.

If I run the above directly, Julia will crash out without a warning or error after some time.  
There’s a better way of coding what I’m trying to perform, but I don’t know how to go about that.  
Can someone direct me to some resources or tell me if there’s something glaring that I’m missing?

Thanks,

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [April 25, 2018, 8:16pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-over-distances-jl-metric/10542/2 "2018-04-25T20:16:35Z")

</div>

Isn’t y=x the argmin trivially?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 26, 2018, 1:36am UTC](https://discourse.julialang.org/t/optim-jl-optimize-over-distances-jl-metric/10542/3 "2018-04-26T01:36:09Z")

</div>

> [@mcbaron](#):
>
> If I run the above directly, Julia will crash out without a warning or error after some time.
> 
> There’s a better way of coding what I’m trying to perform, but I don’t know how to go about that.

Posting a minimal working example would be a good start. But I agree with @Juser, isn’t y=x the minimizer? Or is there some other constraint?

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [April 26, 2018, 9:00am UTC](https://discourse.julialang.org/t/optim-jl-optimize-over-distances-jl-metric/10542/4 "2018-04-26T09:00:45Z")

</div>

> [@mcbaron](#):
>
> As of right now, I have two vectors, x, y and bhattacharyya(x, y) as loaded from Distances.jl. I’d like to call something like optimize(bhattacharyya(x, y), x) in order to find the adjustment to x that minimizes the Bhattacharyya distance between it and y.

Is this literally what you type? Then it obviously won’t work, as `bhattacharyya(y,x)` is a function call, so you call `optimize` with a number and a vector. What you probably mean is something like

```
optimize(x->bhattacharyya(y,x), x)

```

but as they others note… Per definition, a metric d has d(x,y)\>= 0 and d(x,y)=0\<=\>x=y, so setting x to y will give you the global minimum, no need to use numerical optimization here.

---

<div class="post-metadata">

**Author:** ![mcbaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcbaron/32/4027_2.png) [@mcbaron](https://discourse.julialang.org/u/mcbaron)\
**Post date:** [April 26, 2018, 11:45am UTC](https://discourse.julialang.org/t/optim-jl-optimize-over-distances-jl-metric/10542/5 "2018-04-26T11:45:12Z")

</div>

you are all correct, I posed the problem poorly to myself in my own head. I did find my error in logic.

Thank you to @pkofod for pointing out my Julia syntax issue, the type mismatch was something that I hadn’t considered.

Again, My apologies for posting a poorly defined problem like this, but thank you all for your comments.
