TL;DR: The question is about multiplication ACCURACY
I have to multiply matrices A
(100x8000), B
(8000x27) and C
(27x1).
Since matrices B
and C
are constant and A
is variable, I prefer to calculate it as: ABC = np.dot(A, np.dot(B, C))
. However I wonder, that it may be numerically worse (in terms of accuracy) than np.dot(np.dot(a, B), C)
.
What may be important: matrices A
and B
contain 8000 samples of (respectively) 100 and 27 correlated features.
Is there a numerically optimal (in terms of accuracy) order of the multiplication? If yes - how may I determine it?
Special Case
It may be assumed that both A
and B
matrices are nonnegative.
Moreover:
C = np.linalg.solve(cov(B, k), X)
where X
is a 27x1 matrix of 27 (possibly correlated) random variables of unknown distribution, cov = lambda X, k: np.dot(X.T, X) + k * np.eye(X.shape[1])
, and k
is a nonnegative constant minimizing the expression:
sum((X[i, 0] - np.dot(np.dot(B[:, [i]].T, drop(B, i)),
np.linalg.solve(cov(drop(B, i), k),
np.delete(X, i, axis=0))) **2
for i in range(27))
The drop()
function is defined as lambda X, i: np.delete(X, i, axis=1)
.
Even More Special Case
It may be assumed that np.cov(B.T, B)
is a covariance matrix of X
, which follows multivariate Gaussian distribution.
(AB)C
(which is considered accurate) for performance purposes. Unfortunately, I do not remember any recommendations on matrix multiplication order from my numerical analysis class. For sure I am going to run tests with different precision, but I am looking for more solid, theoretical background here. So far my only idea is to write naive expressions for elements ofABC
and estimate the error. – Hardeningk
doesn't seem to occur in the definition ofcov
– Appearance