Major Bug in matrix.eig method

 

While working on a machine learning model I noticed a weird behavior on the Eig built-in method. When given a matrix 

  matrix SBSW = 
   {{-3.474589321704841,1.106384365940048,-9.091976743274188,-3.925227000397125},
   {-5.522139525216183,2.366887770561694,-15.162350848809,-6.357512062676836},
   {8.394926132499894,-2.960067671029549,22.29211501976177,9.524129250463528},
   {7.803242470082926,-2.08028733774348,19.21770560160895,8.186644823336877}};
   
  matrix eigen_vectors;
  vector eigen_values;
  
  SBSW.Eig(eigen_vectors, eigen_values);
  Print("Eigen vectors:\n",eigen_vectors,"\nEigen Values:\n",eigen_values);

It returns: 

EM      0       11:40:19.381    LDA Test (EURUSD,H1)    Eigen vectors:
EE      0       11:40:19.381    LDA Test (EURUSD,H1)    [[0.264966756582992]]
MQ      0       11:40:19.381    LDA Test (EURUSD,H1)    Eigen Values:
RK      0       11:40:19.381    LDA Test (EURUSD,H1)    [28.94158684911083]

Expected Behaviour:

The Eigen values calculating function has nothing to do with changing the dimensions of the eigen vectors so the eigen_vectors matrix is expected to be a 4x4 matrix just like the original matrix, meanwhile the eigen values vector should be expected to have the size of 4

Since most of the Matrix and Vector methods are inspired by python libraries as a documentation explains https://www.mql5.com/en/articles/9805

I took the same code to python:

import numpy as np

# Define the matrix SBSW
SBSW = np.array([[-3.474589321704841, 1.106384365940048, -9.091976743274188, -3.925227000397125],
                 [-5.522139525216183, 2.366887770561694, -15.162350848809, -6.357512062676836],
                 [8.394926132499894, -2.960067671029549, 22.29211501976177, 9.524129250463528],
                 [7.803242470082926, -2.08028733774348, 19.21770560160895, 8.186644823336877]])

# Calculate eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eigh(SBSW)

# Print the results
print("Eigenvalues:")
print(eigenvalues)
print("\nEigenvectors:")
print(eigenvectors)

The results were accurate:

Eigenvalues:
[-8.95615569 -4.72319475  3.62293147 39.42747726]  

Eigenvectors:
[[ 0.84395444  0.29862804  0.35463855  0.26980307] 
 [ 0.35181789  0.20041461 -0.90459394 -0.13329652] 
 [ 0.05344724 -0.58687505 -0.22363457  0.77634312] 
 [-0.4013832   0.72541915 -0.07699924  0.55383183]]

I believe this is a major bug for all complex coders in MQL5, as this method is important in clustering  and in dimension reduction algorithms.

Documentation on MQL5: Matrix and Vector Methods / Transformations / Eig
Documentation on MQL5: Matrix and Vector Methods / Transformations / Eig
  • www.mql5.com
Eig - Transformations - Matrix and Vector Methods - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Omega J Msigwa:

While working on a machine learning model I noticed a weird behavior on the Eig built-in method. When given a matrix 

It returns: 

Expected Behaviour:

The Eigen values calculating function has nothing to do with changing the dimensions of the eigen vectors so the eigen_vectors matrix is expected to be a 4x4 matrix just like the original matrix, meanwhile the eigen values vector should be expected to have the size of 4

Since most of the Matrix and Vector methods are inspired by python libraries as a documentation explains https://www.mql5.com/en/articles/9805

I took the same code to python:

The results were accurate:

I believe this is a major bug for all complex coders in MQL5, as this method is important in clustering  and in dimension reduction algorithms.

As far as I am aware, vectors, matrices and complex types are still somewhat beta-implementations.

A possible solution could be to try and use ALGLIB from codebase, and use standard arrays as inputs.

This will of course impact performance, as these implementations do not use AVX or other SIMD instructions, but at least you get a working version of your code.

Another option could be to implement your own version of eig-values, using OpenCL kernels. As they are executed on an OpenCL device, they are fast, and they support more flexible data type handlings, as they use a very C/C++ like language.

Anyways, you should not expect bug fixes for MQL built-in data types very soon, as these are still not finished, nor are they well designed. - I personally hope very much, MQ redesigns these some day to actually be useful and functionally embedded into MQL, as they are currently more or less just a patch ontop of MQL.
 

Service desk just responded

 

There are more than 5 alghorithms to solve non-symmetric matrices. We don't know which algorithm used in Pyton

You can check correctness of obtained eigen vectors and eigen values.

Your example presented in documentation to demonstrate complex solution

See Note section

If a complex solution is encountered when calculating eigenvalues, the calculation is stopped and the error code is set to 4019 (ERR_MATH_OVERFLOW). Use the complex overload of the Eig method to obtain a complete solution in complex space

If a complex eigenvalue has an imaginary part equal to zero, then it is a real eigenvalue. This can be seen in the example below.

Documentation on MQL5: Matrix and Vector Methods / Transformations / Eig
Documentation on MQL5: Matrix and Vector Methods / Transformations / Eig
  • www.mql5.com
Compute the eigenvalues and right eigenvectors of a square matrix. Complex solution of eigenvalues and eigenvectors Parameters eigen_vectors [out]...