LinearRegression
Tính toán một vector/ma trận với các giá trị hồi quy tuyến tính đã tính.
cpp
vector vector::LinearRegression();
matrix matrix::LinearRegression(
ENUM_MATRIX_AXIS axis=AXIS_NONE // trục mà hồi quy được tính toán
);
1
2
3
4
5
2
3
4
5
Tham số
axis
[in] Chỉ định trục mà hồi quy được tính toán. Giá trị của liệt kê ENUM_MATRIX_AXIS
(AXIS_HORZ — trục ngang, AXIS_VERT — trục dọc).
Giá trị trả về
Vector hoặc ma trận với các giá trị hồi quy tuyến tính đã tính.
Ghi chú
Hồi quy tuyến tính được tính bằng phương trình hồi quy tiêu chuẩn: y(x) = a * x + b, trong đó a là độ dốc của đường thẳng, còn b là độ dịch chuyển trên trục Y.
Ví dụ:
cpp
#include <Graphics\Graphic.mqh>
#define GRAPH_WIDTH 750
#define GRAPH_HEIGHT 350
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
vector vector_a;
vector_a.CopyRates(_Symbol,_Period,COPY_RATES_CLOSE,1,100);
vector vector_r=vector_a.LinearRegression();
//--- switch off chart show
ChartSetInteger(0,CHART_SHOW,false);
//--- arrays for drawing a graph
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];
}
//--- graph title
string title="Linear regression "+_Symbol+","+EnumToString(_Period);
long chart=0;
string name="LinearRegression";
//--- create graph
CGraphic graphic;
graphic.Create(chart,name,0,0,0,GRAPH_WIDTH,GRAPH_HEIGHT);
graphic.BackgroundMain(title);
graphic.BackgroundMainSize(12);
//--- activation function graph
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);
//--- derivatives of activation function
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();
//--- endless loop to recognize pressed keyboard buttons
while(!IsStopped())
{
//--- press escape button to quit program
if(TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)!=0)
break;
//--- press PdDn to save graph picture
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);
}
//--- clean up
graphic.Destroy();
ObjectDelete(chart,name);
ChartSetInteger(0,CHART_SHOW,true);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
ENUM_MATRIX_AXIS
Liệt kê để chỉ định trục trong tất cả các hàm thống kê cho ma trận.
ID | Mô tả |
---|---|
AXIS_NONE | Trục không được chỉ định. Tính toán được thực hiện trên tất cả các phần tử ma trận, như thể nó là một vector (xem phương thức Flat). |
AXIS_HORZ | Trục ngang |
AXIS_VERT | Trục dọc |