LinearRegression

计算包含计算的线性回归值的向量/矩阵。

vector vector::LinearRegression();



matrix matrix::LinearRegression(

  ENUM_MATRIX_AXIS  axis=AXIS_NONE      // 沿其计算回归的轴线

   );

参数

axis

[in]  指定沿其计算回归的轴线。ENUM_MATRIX_AXIS枚举值(AXIS_HORZ — 横轴, AXIS_VERT — 竖轴)。

返回值

包含计算的线性回归值的向量/矩阵。

 

注意

使用标准回归方程计算线性回归:y (x) = a * x + b,其中a是直线斜率,而b是其Y轴位移。

示例:

#include <Graphics\Graphic.mqh>



#define GRAPH_WIDTH  750

#define GRAPH_HEIGHT 350



//+------------------------------------------------------------------+

//| 脚本程序起始函数                                                   |

//+------------------------------------------------------------------+

void OnStart()

  {

   vector vector_a;

   vector_a.CopyRates(_Symbol,_Period,COPY_RATES_CLOSE,1,100);

   vector vector_r=vector_a.LinearRegression();



//--- 关闭图表显示

   ChartSetInteger(0,CHART_SHOW,false);



//--- 用于绘制图形的数组

   double x[];

   double y1[];

   double y2[];

   ArrayResize(x,uint(vector_a.Size()));

   ArrayResize(y1,uint(vector_a.Size()));

   ArrayResize(y2,uint(vector_a.Size()));

   for(ulong i=0; i<vector_a.Size(); i++)

     {

      x[i]=(double)i;

      y1[i]=vector_a[i];

      y2[i]=vector_r[i];

     }



//--- 图形标题

   string title="Linear regression "+_Symbol+","+EnumToString(_Period);



   long   chart=0;

   string name="LinearRegression";



//--- 创建图形

   CGraphic graphic;

   graphic.Create(chart,name,0,0,0,GRAPH_WIDTH,GRAPH_HEIGHT);

   graphic.BackgroundMain(title);

   graphic.BackgroundMainSize(12);



//--- 激活函数图

   CCurve *curvef=graphic.CurveAdd(x,y1,CURVE_POINTS_AND_LINES);

   curvef.Name("vector_a");

   curvef.LinesWidth(2);

   curvef.LinesSmooth(true);

   curvef.LinesSmoothTension(1);

   curvef.LinesSmoothStep(10);



//--- 激活函数的导数

   CCurve *curved=graphic.CurveAdd(x,y2,CURVE_LINES);

   curved.Name("vector_r");

   curved.LinesWidth(2);

   curved.LinesSmooth(true);

   curved.LinesSmoothTension(1);

   curved.LinesSmoothStep(10);

   graphic.CurvePlotAll();

   graphic.Update();



//--- 无限循环识别按下的键盘按键

   while(!IsStopped())

     {

      //--- 按下escape键退出程序

      if(TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)!=0)

         break;

      //--- 按下PdDn保存图形图片

      if(TerminalInfoInteger(TERMINAL_KEYSTATE_PAGEDOWN)!=0)

        {

         string file_names[];

         if(FileSelectDialog("Save Picture",NULL,"All files (*.*)|*.*",FSD_WRITE_FILE,file_names,"LinearRegression.png")<1)

            continue;

         ChartScreenShot(0,file_names[0],GRAPH_WIDTH,GRAPH_HEIGHT);

        }

      Sleep(10);

     }



//--- 清除

   graphic.Destroy();

   ObjectDelete(chart,name);

   ChartSetInteger(0,CHART_SHOW,true);

  }

ENUM_MATRIX_AXIS

用于在矩阵所有统计函数中指定轴线的枚举。

ID 描述
AXIS_NONE 没有指定坐标轴。对所有矩阵元素执行计算,类似向量(请参见Flat方法)。
AXIS_HORZ 横轴
AXIS_VERT 竖轴