添加图片注释,不超过 140 字(可选)
行 2 乘以列 1。 输出将是 x 的总和,因为来自 行 2 中的值 1 与 列 1 中的值 x 相乘时不起作用。
(, 下载次数: 9)
上传
点击文件名下载附件
添加图片注释,不超过 140 字(可选)
最后一部分是 x 值的平方和。因为它是 row2 的包含值 x 与 colum2 的包含值 x 乘积后之和 (, 下载次数: 10)
上传
点击文件名下载附件
添加图片注释,不超过 140 字(可选)
如您所见,在这种情况下,矩阵的输出是一个 2x2 矩阵。我们看看这在现实世界中是如何工作的,利用来自线性回归中第一篇文章中的数据集合 。 我们提取数据,并将其放入一个数组中,自变量为 x,而因变量为 y。//inside MatrixRegTest.mq5 script#include "MatrixRegression.mqh";#include "LinearRegressionLib.mqh";CSimpleMatLinearRegression matlr;CSimpleLinearRegression lr; //+------------------------------------------------------------------+//| Script program start function |//+------------------------------------------------------------------+void OnStart() { //double x[] = {651,762,856,1063,1190,1298,1421,1440,1518}; //stands for sales //double y[] = {23,26,30,34,43,48,52,57,58}; //money spent on ads//--- double x[], y[]; string file_name = "NASDAQ_DATA.csv", delimiter = ","; lr.GetDataToArray(x,file_name,delimiter,1); lr.GetDataToArray(y,file_name,delimiter,2); }于此,我已经导入了我们在第一篇文章中创建的 CsimpleLinearRegression 函数库。CSimpleLinearRegression lr; 因为我们可能需要调用某些函数,例如提取数据放到数组。我们来寻找 xTx
MatrixMultiply(xT,m_xvalues,xTx,tr_cols,tr_rows,tr_rows,tr_cols); Print("xTx");MatrixPrint(xTx,tr_cols,tr_cols,5); //remember?? the output of the matrix will be the row1 and col2 marked in red
如果您注意数组 xT[] 的话,您可能发现赫兹量化软件只是复制了 x 值,并将它们存储在这个 xT[]数组中,它只是为了澄清,正如我早前所说的,我们所用的从 csv 文件收集数据并存于数组方式,调用函数 GetDataToArray() 得到的数据已经过转置。然后我们将数组 xT[] 乘以已转置的 m_xvalues[] ,m_xvalues 是在函数库中定义的全局数组,用于存储 x 值。 这是我们的 MatrixMultiply() 函数的内部。
void CSimpleMatLinearRegression::MatrixMultiply(double &A[],double &B[],double &output_arr[],int row1,int col1,int row2,int col2) {//--- double MultPl_Mat[]; //where the multiplications will be stored if (col1 != row2) Alert("Matrix Multiplication Error, \n The number of columns in the first matrix is not equal to the number of rows in second matrix"); else { ArrayResize(MultPl_Mat,row1*col2); int mat1_index, mat2_index; if (col1==1) //Multiplication for 1D Array { for (int i=0; i<row1; i++) for(int k=0; k<row1; k++) { int index = k + (i*row1); MultPl_Mat[index] = A * B[k]; } //Print("Matrix Multiplication output"); //ArrayPrint(MultPl_Mat); } else { //if the matrix has more than 2 dimensionals for (int i=0; i<row1; i++) for (int j=0; j<col2; j++) { int index = j + (i*col2); MultPl_Mat[index] = 0; for (int k=0; k<col1; k++) { mat1_index = k + (i*row2); //k + (i*row2) mat2_index = j + (k*col2); //j + (k*col2) //Print("index out ",index," index a ",mat1_index," index b ",mat2_index); MultPl_Mat[index] += A[mat1_index] * B[mat2_index]; DBL_MAX_MIN(MultPl_Mat[index]); } //Print(index," ",MultPl_Mat[index]); } ArrayCopy(output_arr,MultPl_Mat); ArrayFree(MultPl_Mat); } } }
诚恳地讲,这种乘法看起来既混乱又丑陋,尤其是在这样的情况下