Least squares with multi-dimensional output type

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

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:

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]
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 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.

Please link to your SO question if you cross-post in multiple places, the question was asked here: 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 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).

1 Like

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?

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:

out_data = [xp; yp]