# Function parameter estimation similar to Octave fminsearch

**URL:** https://discourse.julialang.org/t/function-parameter-estimation-similar-to-octave-fminsearch/69485
**Category:** Optimization (Mathematical)
**Tags:** package, optimization
**Created:** [October 9, 2021, 9:16pm UTC](https://discourse.julialang.org/t/function-parameter-estimation-similar-to-octave-fminsearch/69485 "2021-10-09T21:16:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![John\_Peach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_peach/32/23611_2.png) [@John\_Peach](https://discourse.julialang.org/u/John_Peach)
#### Post date: [October 9, 2021, 9:16pm UTC](https://discourse.julialang.org/t/function-parameter-estimation-similar-to-octave-fminsearch/69485/1 "2021-10-09T21:16:55Z")

</div>

I’m trying to solve a simple parameter estimation problem in Julia similar to how fminsearch works in Octave/Matlab. The [RAFF.jl](https://github.com/fsobral/RAFF.jl) package seemed like a nice approach, but perhaps there’s a better way?

For context, I have a photo of several lampposts in a public square that I know are all the same height (H), but I don’t know what the height is, and I know nothing about the camera, but I’m assuming that each pixel extends over a small angle such that there are r pixels per radian. I know the distance (Dst) from the camera to each lamppost so the problem is find r and H that minimize the difference between the tangent of the angle and H/Dst,

\underset{r,H}{\operatorname{argmin}} f(n,Dst) \sum\_{i=1}^k \left( \tan\left( \frac{n\_i}{r}\right) - \frac{H}{Dst\_i} \right)^2.

In Octave (n,Dst defined below):  
`f = @(x) sum((tan(n/x(1)) - x(2)./Dst).^2);`  
`C = fminsearch(f,[1500,12]) = [1503.360 12.900]`

The solutions generated by RAFF don’t match the Octave solution, so I’m guessing that either I’m using the wrong package, or not setting RAFF up properly. Here are the results:

```julia

# Data inputs to the problem
n = [765.0, 332.0, 217.0, 165.0, 283.0, 297.0, 814.0, 434.0, 289.0];
Dst = [22.71666667, 45.64, 71.58333333, 90.22666667, 59.63, 59.33666667, 23.82333333, 46.88333333, 72.88];

# Write data in format useful for RAFF.jl
x = [n Dst zeros(9,1)];

# Function to be minimized over parameters p
f(x,p) = (tan(x[1]/p[1]) - p[2]/x[2])^2
# Jacobian
begin
	gmodel!(g,x,p) = begin
		g[1] = 2*x[1]/p[1]^2*sec(x[1]/p[1])^2*(tan(x[1]/p[1]) - p[2]/x[2])
		g[2] = 2/p[2]*(tan(x[1]/p[1]) - p[2]/x[2])
	end
end

# Fit using RAFF
fit = raff(f,gmodel!,x,2; initguess = [1000.0, 10.0], ε=1.0e-10)

# Output (solution only)
Solution (.solution) = [1000.0, 10.0]

# Without Jacobian
fit = raff(f,x,2; initguess = [1000.0, 10.0], ε=1.0e-10)
Solution (.solution) = [56821.055712178044, 0.31544255532138893] 

# No initial guess
fit = raff(f,x,2)
Solution (.solution) = [-0.9778814405815958, -3.5093908435870933]

# Function re-written to use Dst as first column in x, n as second column (no zeros)
f1(x,p) = p[1]*atan(p[2]/x[1])
Solution (.solution) = [477.50199698046083, 39.851464869409654]

# Function re-written to use n as first column in x, Dst as second column
f2(x,p) = p[2]/(tan(x[1]/p[1]))
Solution (.solution) = [-0.8529985482107982, 5.582472558856189]

```

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [October 9, 2021, 9:35pm UTC](https://discourse.julialang.org/t/function-parameter-estimation-similar-to-octave-fminsearch/69485/2 "2021-10-09T21:35:53Z")

</div>

> [@John\_Peach](#):
>
> fminsearch

from the `fminsearch` Octave docs:

> : _x =_ **fminsearch** _(fun, x0)_
> 
> Find a value of x which minimizes the function fun.
> 
> The search begins at the point x0 and iterates using the Nelder & Mead Simplex algorithm (a derivative-free method). This algorithm is better-suited to functions which have discontinuities or for which a gradient-based search such as `fminunc` fails.
> 
> Options for the search are provided in the parameter options using the function `optimset` . Currently, `fminsearch` accepts the options: `"TolX"` , `"MaxFunEvals"` , `"MaxIter"` , `"Display"` . For a description of these options, see `optimset` .

so, Octave `fminsearch` uses Nelder Mead to find the optimum. RAFF.jl, on the other part:

> This package implements a robust method[1] to fit a given function (described by its parameters) to input data. The method is based on the LOVO algorithm [1] and also in a suitable voting strategy in order automatically eliminate outliers.  
> [1] Castelani, E. V., Lopes, R., Shirabayashi, W., & Sobral, F. N. C. (2019). RAFF.jl: Robust Algebraic Fitting Function in Julia. Journal of Open Source Software, 4(39), 1385. [https://doi.org/10.21105/joss.01385](https://doi.org/10.21105/joss.01385)  
> [2] Andreani, R., Martínez, J. M., Martínez, L., & Yano, F. S. (2009). Low order-value optimization and applications. _Journal of Global Optimization_ , 43(1), 1-22.

if you want a similar result to `fminsearch`, you need a package that provides Nelder Mead, some options:

- Optim.jl [Optim.jl](https://julianlsolvers.github.io/Optim.jl/stable/#algo/nelder_mead/)
- GalacticOptim.jl: [https://galacticoptim.sciml.ai/stable/local\_optimizers/local\_derivative\_free/](https://galacticoptim.sciml.ai/stable/local_optimizers/local_derivative_free/) (wraps Optim.jl, and a bunch of other solvers)
- NLopt.jl (wrapper to NLopt) [GitHub - JuliaOpt/NLopt.jl: Package to call the NLopt nonlinear-optimization library from the Julia language](https://github.com/JuliaOpt/NLopt.jl)  
[NLopt Algorithms - NLopt Documentation](https://nlopt.readthedocs.io/en/latest/NLopt_Algorithms/#nelder-mead-simplex)  
(i just posted the optimization packages that have nelder mead, if that algorithm is good enough is another discussion)

---

<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: [October 9, 2021, 9:38pm UTC](https://discourse.julialang.org/t/function-parameter-estimation-similar-to-octave-fminsearch/69485/3 "2021-10-09T21:38:00Z")

</div>

This is a least-square fitting problem, for which there are a couple Julia packages: [GitHub - JuliaNLSolvers/LsqFit.jl: Simple curve fitting in Julia](https://github.com/JuliaNLSolvers/LsqFit.jl), [https://github.com/matthieugomez/LeastSquaresOptim.jl](https://github.com/matthieugomez/LeastSquaresOptim.jl) … e.g. try the Levenberg–Marquardt algorithm in LsqFit.jl.

`fminsearch` in Matlab or Octave is a generic derivative-free optimization function using the Nelder–Mead simplex algorithm. There are many, many general optimization packages in Julia: [https://github.com/JuliaNLSolvers/Optim.jl](https://github.com/JuliaNLSolvers/Optim.jl), [https://github.com/JuliaNonconvex/Nonconvex.jl](https://github.com/JuliaNonconvex/Nonconvex.jl), [GitHub - JuliaOpt/NLopt.jl: Package to call the NLopt nonlinear-optimization library from the Julia language](https://github.com/JuliaOpt/NLopt.jl), [https://github.com/robertfeldt/BlackBoxOptim.jl](https://github.com/robertfeldt/BlackBoxOptim.jl), [https://github.com/emmt/OptimPack.jl](https://github.com/emmt/OptimPack.jl), not to mention higher-level “modeling” packages like [https://github.com/jump-dev/JuMP.jl](https://github.com/jump-dev/JuMP.jl). There are several implementations of Nelder–Mead among those packages, but in general I would tend not to use Nelder–Mead as my first (or second, or third) choice of algorithms these days.

---

<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: [October 9, 2021, 9:41pm UTC](https://discourse.julialang.org/t/function-parameter-estimation-similar-to-octave-fminsearch/69485/4 "2021-10-09T21:41:25Z")

</div>

> [@longemen3000](#):
>
> if you want a similar result to `fminsearch` , you need a package that provides Nelder Mead, some options:

No you don’t. You just need a package that is solving the same (nonlinear least-squares) optimization problem [\*]. RAFF is solving a [different “LOVO” optimization problem](https://arxiv.org/abs/1911.13078) that generalizes least-squares by eliminating “outlier” data points, and hence will yield a different result.

[\*] With the caveat that if your problem is nonconvex with multiple local minima, then different algorithms, different implementations of the “same” algorithm (e.g. different initialization strategies for Nelder–Mead), and different starting points may yield different minima. But with a suitable starting point any convergent local minimization algorithm should be able to locate the `fminsearch` minimum.

---

<div class="post-metadata">

### Author: ![John\_Peach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_peach/32/23611_2.png) [@John\_Peach](https://discourse.julialang.org/u/John_Peach)
#### Post date: [October 16, 2021, 6:33pm UTC](https://discourse.julialang.org/t/function-parameter-estimation-similar-to-octave-fminsearch/69485/5 "2021-10-16T18:33:43Z")

</div>

Thanks for the links. I only chose fminsearch in Octave because it was simple to implement and I was hoping to find something similar in Julia. After trying several of your suggested packages, I decided that the problem itself is ill-posed, especially with any measurement errors, so I’m rewriting the problem rather than the method of solution.
