0
$\begingroup$

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?

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

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*)
$\endgroup$
7
  • 1
    $\begingroup$ LeastSquares[f, s] would accomplish the same thing, wouldn't it? $\endgroup$
    – MarcoB
    Jan 26 at 18:31
  • $\begingroup$ No, LeastSquares solves m.x==b where m is known. @MarcoB $\endgroup$ Jan 26 at 19:30
  • $\begingroup$ Daniel, try this: s == f . LinearSolve[f, s] or s == f . LeastSquares[f, s]. They will both return True, exactly like your solution. $\endgroup$
    – MarcoB
    Jan 26 at 22:24
  • $\begingroup$ 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. $\endgroup$
    – MarcoB
    Jan 26 at 22:29
  • $\begingroup$ @Marco Hi, that's correct. I found the belonging information under "Generalisation&Extensions" in the help. Was new to m, one never stops learning. $\endgroup$ Jan 27 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.