# Regression of a set of (2-element) vectors on another set of (2-element) vectors

As a trivial example, here are two vectors:

f = {{1, 1}, {2, 2}, {3, 3}, {4, 4}}

s = {{-1, 2}, {-2, 4}, {-3, 6}, {-4, 8}}


It is instantly clear (apart from my sloppy formatting) that elements of s follow from elements of f according to s = M.f

where M = a matrix {{-1, 0}, {0, 2}}

I find the help details for the various forms of regression obscure, so my question is: more generally, how do I find the best fit matrix for such a (seemingly simple) correlation?

• f and s are matrices and not vectors... anyway, what you have is referred to as a Procrustes problem. Jan 26 at 13:43
• Would LinearSolve be helpful here? Or LeastSquares? Jan 26 at 13:54
• @J.M. Point taken. The problem stems from vector analysis, my data being orthogonal components of (speed, direction) vectors. Jan 26 at 14:00
• Anyway, FindGeometricTransform[] might interest you. Jan 26 at 14:12
• @J.M. Indeed it does! I think it solves my limited Procrustean problem, thank you. Jan 26 at 16:31

Note that your matrix M is not unique because your example data is not linear independent. Therefore, we may get a different M. Note further, due to the shape of your data you must write f.M not M.f.

To get a least square solution, we first define an symbolic matrix and define an error function: err that measures the differences between s and f.M. Then we minimize the error by adjusting the parameters of the symbolic matrix:

f = {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
s = {{-1, 2}, {-2, 4}, {-3, 6}, {-4, 8}};
mat = Array[Subscript[m, #1, #2] &, {2, 2}];
err = (s - f.mat)^2 // Flatten // Total;
sol = mat /. FindMinimum[err, Flatten[mat]][[2]]
s == f.sol

(*{{-4.53077, 0.778831}, {3.53077, 1.22117}}*)
(*True*)

• LeastSquares[f, s] would accomplish the same thing, wouldn't it? Jan 26 at 18:31
• No, LeastSquares solves m.x==b where m is known. @MarcoB Jan 26 at 19:30
• Daniel, try this: s == f . LinearSolve[f, s] or s == f . LeastSquares[f, s]. They will both return True, exactly like your solution. Jan 26 at 22:24
• In fact, if you use NMinimize instead of FindMinimum in your code, you will get the same result as LeastSquares. I think FindMinimum just finds another one of the many possible solutions. Jan 26 at 22:29
• @Marco Hi, that's correct. I found the belonging information under "Generalisation&Extensions" in the help. Was new to m, one never stops learning. Jan 27 at 9:50