forked from MegaJoctan/MALE5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross_validation.mqh
142 lines (121 loc) · 5.1 KB
/
cross_validation.mqh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//+------------------------------------------------------------------+
//| cross_validation.mqh |
//| Copyright 2022, Fxalgebra.com |
//| https://www.mql5.com/en/users/omegajoctan |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Fxalgebra.com"
#property link "https://www.mql5.com/en/users/omegajoctan"
//+------------------------------------------------------------------+
//| defines |
//+------------------------------------------------------------------+
#include <MALE5\Tensors.mqh>
class CCrossValidation_kfold
{
CTensors *folds_tensor;
void XandYSplitMatrices(const matrix &matrix_,matrix &xmatrix,vector &y_vector,int y_column=-1);
void RemoveCol(matrix &mat, ulong col);
uint k_folds;
public:
CCrossValidation_kfold(matrix &data_matrix, uint k_folds=5);
~CCrossValidation_kfold(void);
matrix fold(uint index);
uint fold_size;
matrix fold_x(uint index);
vector fold_y(uint index);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CCrossValidation_kfold::CCrossValidation_kfold(matrix &data_matrix, uint k_folds_=5)
{
this.k_folds = k_folds_;
folds_tensor = new CTensors(k_folds);
ulong rows = data_matrix.Rows();
fold_size = (int)MathCeil(rows/k_folds);
matrix temp_tensor(fold_size, data_matrix.Cols());
int start=0;
for (ulong i=0; i<k_folds; i++)
{
for (ulong j=start, count=0; j<fold_size+start; j++, count++)
{
temp_tensor.Row(data_matrix.Row(j), count);
}
folds_tensor.TensorAdd(temp_tensor, i); //Obtained size=k data matrix
start += (int)fold_size;
}
//#ifdef DEBUG_MODE
// Print("total ",rows," fold_size ",fold_size," k_folds ",k_folds);
// folds_tensor.TensorPrint();
//#endif
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CCrossValidation_kfold::~CCrossValidation_kfold(void)
{
delete(folds_tensor);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
matrix CCrossValidation_kfold::fold(uint index)
{
if (index+1 > this.k_folds)
{
matrix ret={};
Print("k-fold index out of range");
return (ret);
}
return folds_tensor.Tensor(index);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
matrix CCrossValidation_kfold::fold_x(uint index)
{
matrix x; vector y;
matrix fold_matrix = this.fold(index);
this.XandYSplitMatrices(fold_matrix, x, y);
return (x);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector CCrossValidation_kfold::fold_y(uint index)
{
matrix x; vector y;
matrix fold_matrix = this.fold(index);
this.XandYSplitMatrices(fold_matrix, x, y);
return (y);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CCrossValidation_kfold::XandYSplitMatrices(const matrix &matrix_,matrix &xmatrix,vector &y_vector,int y_column=-1)
{
y_column = int( y_column==-1 ? matrix_.Cols()-1 : y_column);
y_vector = matrix_.Col(y_column);
xmatrix.Copy(matrix_);
RemoveCol(xmatrix, y_column); //Remove the y column
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CCrossValidation_kfold::RemoveCol(matrix &mat, ulong col)
{
matrix new_matrix(mat.Rows(),mat.Cols()-1); //Remove the one Column
for (ulong i=0, new_col=0; i<mat.Cols(); i++)
{
if (i == col)
continue;
else
{
new_matrix.Col(mat.Col(i),new_col);
new_col++;
}
}
mat.Copy(new_matrix);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+