Dear Statalist users,
I am experiencing that multiplying two matrices can give different results depending on how you multiply those matrices.
For exemple, I have a matrix X and a matrix b, where X is N x k and b is a k x k, and I want to compute X*b.
I can just write xb = X*b in mata or I can loop over the columns of b, multiply X with each column of b and store the results in a new matrix xb (which is k x k).
Why I want to use the second method can be justified by the context of the problem. I would expect to find exactly the same results though. But it is apparently not the case in the current version of Stata.
I have written below an example which shows that there are differences. Although those differences are very small they may matter for your problem.
I am using Stata MP 18 on Windows 10 and Windows Server 2016. I ran the same program with an earlier version of Stata (12) and the issue did not show up. Running the program in version 18 with version control doesn't fix the problem.
Has someone any insight on why I obtain different results?
I am experiencing that multiplying two matrices can give different results depending on how you multiply those matrices.
For exemple, I have a matrix X and a matrix b, where X is N x k and b is a k x k, and I want to compute X*b.
I can just write xb = X*b in mata or I can loop over the columns of b, multiply X with each column of b and store the results in a new matrix xb (which is k x k).
Why I want to use the second method can be justified by the context of the problem. I would expect to find exactly the same results though. But it is apparently not the case in the current version of Stata.
I have written below an example which shows that there are differences. Although those differences are very small they may matter for your problem.
I am using Stata MP 18 on Windows 10 and Windows Server 2016. I ran the same program with an earlier version of Stata (12) and the issue did not show up. Running the program in version 18 with version control doesn't fix the problem.
Has someone any insight on why I obtain different results?
Code:
mata: ------------------------------------------------- mata (type end to exit) -------------------------------------------------------------- : N = 20 : k = 3 : rseed(101) : X = runiform(N,k) : b = runiform(k,k) : : xb1 = X*b : xb0 = J(rows(xb1),cols(xb1),.) : for (i = 1; i <= k ; i++) > { > xb0[,i] = X*b[,i] > > } : : // xb0 = cross(X',b) : xb0:!=xb1 1 2 3 +-------------+ 1 | 1 1 0 | 2 | 1 0 0 | 3 | 0 0 0 | 4 | 0 0 0 | 5 | 0 0 0 | 6 | 1 0 0 | 7 | 1 0 0 | 8 | 0 0 0 | 9 | 1 1 0 | 10 | 0 1 0 | 11 | 0 0 0 | 12 | 0 0 1 | 13 | 1 1 1 | 14 | 0 0 1 | 15 | 1 0 1 | 16 | 1 0 0 | 17 | 0 1 0 | 18 | 0 0 0 | 19 | 0 1 0 | 20 | 0 0 0 | +-------------+ : mreldif(xb0,xb1) 9.06980e-17 : : end ----------------------------------------------------------------------------------------------------------------------------------------
Comment