# Least squares with multi-dimensional output type

**URL:** https://discourse.julialang.org/t/least-squares-with-multi-dimensional-output-type/42283
**Category:** Optimization (Mathematical)
**Created:** [June 30, 2020, 4:21am UTC](https://discourse.julialang.org/t/least-squares-with-multi-dimensional-output-type/42283 "2020-06-30T04:21:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![akahs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akahs/32/15377_2.png) [@akahs](https://discourse.julialang.org/u/akahs)
#### Post date: [June 30, 2020, 4:21am UTC](https://discourse.julialang.org/t/least-squares-with-multi-dimensional-output-type/42283/1 "2020-06-30T04:21:31Z")

</div>

I am trying to use LsqFit to find out a 2D geometric transformation parameter, say a mapping from (x,y) to (xp,yp):

```julia
x = [x for x=-2.:2. for y=-2.:2.]
y = [y for x=-2.:2. for y=-2.:2.]
in_data = [x y]
k = -0.02
@. xp = x + k*x*(x^2+y^2)
@. yp = y + k*y*(x^2+y^2)
out_data = [xp yp]

```

However, from the readme of LsqFit it’s not clear how to accommodate multi-dimensional dependent variable (or is it possible?). I tried to format the dependent variable in the same format as the independent variable, which is a nx2 matrix, as follows:

```julia
k0=[0.]
function model(x,p) 
    @. xp = x[:,1]+p[1]*x[:,1]*(x[:,1]^2+x[:,2]^2) 
    @. yp = x[:,2]+p[1]*x[:,2]*(x[:,1]^2+x[:,2]^2)
    return [xp yp]
end
ret = curve_fit(model, in_data, out_data, k0)

```

But this doesn’t work and I got the error message:

> LoadError: MethodError: no method matching mul!(::Array{Float64,1}, ::LinearAlgebra.Transpose{Float64,Array{Float64,2}}, ::Array{Float64,2}, ::Bool, ::Bool)

I saw [this post](https://discourse.julialang.org/t/multivariate-nonlinear-regression/25887) which addresses the multivariate input variable, but I still can’t figure out how to deal with the output variable.  
This example can actually be solved analytically, but I just wanted to find out how to do least squares for 2D points.  
Originally posted [here](https://stackoverflow.com/questions/62648069/use-lsqfit-for-multi-variate-output?noredirect=1#comment110793912_62648069).

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 30, 2020, 10:51am UTC](https://discourse.julialang.org/t/least-squares-with-multi-dimensional-output-type/42283/2 "2020-06-30T10:51:57Z")

</div>

Please link to your SO question if you cross-post in multiple places, the question was asked here: [julia - Use LsqFit for multi-variate output? - Stack Overflow](https://stackoverflow.com/questions/62648069/use-lsqfit-for-multi-variate-output)

As I said in the comment there, I don’t see how you find a minimum here without defining some sort of norm that quantifies the distance between candidate solutions? Check out the [tutorial](https://julianlsolvers.github.io/LsqFit.jl/latest/tutorial/) in the LsqFit docs to see how the package estimates parameters - basically by minimzing the sum of squared distances between your output data and f(input data, parameters).

---

<div class="post-metadata">

### Author: ![akahs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akahs/32/15377_2.png) [@akahs](https://discourse.julialang.org/u/akahs)
#### Post date: [June 30, 2020, 4:12pm UTC](https://discourse.julialang.org/t/least-squares-with-multi-dimensional-output-type/42283/3 "2020-06-30T16:12:31Z")

</div>

Added the link to the original post.  
I think conceptually the loss function is simply the sum of Euclidean distances between points. It should be quite straight forward to compute. Maybe LsqFit didn’t implement higher dimensional dependent variables?

---

<div class="post-metadata">

### Author: ![akahs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akahs/32/15377_2.png) [@akahs](https://discourse.julialang.org/u/akahs)
#### Post date: [July 1, 2020, 12:22am UTC](https://discourse.julialang.org/t/least-squares-with-multi-dimensional-output-type/42283/4 "2020-07-01T00:22:53Z")

</div>

Going to answer my own question:  
The vector output variable needs to be stacked together to form an 1D array. So the only changes needed is:

```julia
out_data = [xp; yp]

```
