Mathematica Asked on March 3, 2021
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?
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*)
Answered by Daniel Huber on March 3, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP