-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
AdaBoost.mqh
305 lines (242 loc) · 11.5 KB
/
AdaBoost.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//+------------------------------------------------------------------+
//| AdaBoost.mqh |
//| Copyright 2023, Omega Joctan |
//| https://www.mql5.com/en/users/omegajoctan |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Omega Joctan"
#property link "https://www.mql5.com/en/users/omegajoctan"
//+------------------------------------------------------------------+
//| defines |
//+------------------------------------------------------------------+
#include <MALE5\MatrixExtend.mqh>
#include <MALE5\Naive Bayes\Naive Bayes.mqh>
//+------------------------------------------------------------------+
//| Model class |
//+------------------------------------------------------------------+
#include <MALE5\Decision Tree\tree.mqh>
#include <MALE5\Linear Models\Logistic Regression.mqh>
//+------------------------------------------------------------------+
//| AdaBoost class for Decision Tree |
//+------------------------------------------------------------------+
namespace DecisionTree
{
class AdaBoost
{
protected:
vector m_alphas;
vector classes_in_data;
int m_random_state;
bool m_boostrapping;
uint m_min_split, m_max_depth;
CDecisionTreeClassifier *weak_learners[]; //store weak_learner pointers for memory allocation tracking
CDecisionTreeClassifier *weak_learner;
uint m_estimators;
public:
AdaBoost(uint min_split, uint max_split, uint n_estimators=50, int random_state=42, bool bootstrapping=true);
~AdaBoost(void);
void fit(matrix &x, vector &y);
int predict(vector &x);
vector predict(matrix &x);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
AdaBoost::AdaBoost(uint min_split, uint max_split, uint n_estimators=50, int random_state=42, bool bootstrapping=true)
:m_estimators(n_estimators),
m_random_state(random_state),
m_boostrapping(bootstrapping),
m_min_split(min_split),
m_max_depth(max_split)
{
ArrayResize(weak_learners, m_estimators); //Resizing the array to retain the number of base weak_learners
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
AdaBoost::~AdaBoost(void)
{
for (uint i=0; i<m_estimators; i++) //Delete the forest | all trees
if (CheckPointer(weak_learners[i]) != POINTER_INVALID)
delete(weak_learners[i]);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void AdaBoost::fit(matrix &x,vector &y)
{
m_alphas.Resize(m_estimators);
classes_in_data = MatrixExtend::Unique(y); //Find the target variables in the class
ulong m = x.Rows(), n = x.Cols();
vector weights(m); weights = weights.Fill(1.0) / m; //Initialize instance weights
vector preds(m);
vector misclassified(m);
//---
matrix data = MatrixExtend::concatenate(x, y);
matrix temp_data;
matrix x_subset;
vector y_subset;
double error = 0;
for (uint i=0; i<m_estimators; i++)
{
temp_data = data;
MatrixExtend::Randomize(temp_data, this.m_random_state, this.m_boostrapping);
if (!MatrixExtend::XandYSplitMatrices(temp_data, x_subset, y_subset)) //Get randomized subsets
{
ArrayRemove(weak_learners,i,1); //Delete the invalid weak_learner
printf("%s %d Failed to split data",__FUNCTION__,__LINE__);
continue;
}
//---
weak_learner = new CDecisionTreeClassifier(this.m_min_split, m_max_depth);
weak_learner.fit(x_subset, y_subset); //fiting the randomized data to the i-th weak_learner
preds = weak_learner.predict(x_subset); //making predictions for the i-th weak_learner
for (ulong j=0; j<m; j++)
misclassified[j] = (preds[j] != y_subset[j]);
error = (misclassified * weights).Sum() / (double)weights.Sum();
//--- Calculate the weight of a weak learner in the final weak_learner
double alpha = 0.5 * log((1-error) / (error + 1e-10));
//--- Update instance weights
weights *= exp(-alpha * y_subset * preds);
weights /= weights.Sum();
//--- save a weak learner and its weight
this.m_alphas[i] = alpha;
this.weak_learners[i] = weak_learner;
printf("Building Estimator [%d/%d] Accuracy Score %.3f",i+1,m_estimators,Metrics::accuracy_score(y_subset,preds));
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int AdaBoost::predict(vector &x)
{
// Combine weak learners using weighted sum
vector weak_preds(m_estimators),
final_preds(m_estimators);
for (uint i=0; i<this.m_estimators; i++)
weak_preds[i] = this.weak_learners[i].predict(x);
return (int)weak_preds[(this.m_alphas*weak_preds).ArgMax()]; //Majority decision
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector AdaBoost::predict(matrix &x)
{
vector ret_v(x.Rows());
for (ulong i=0; i<ret_v.Size(); i++)
ret_v[i] = this.predict(x.Row(i));
return ret_v;
}
}
//+------------------------------------------------------------------+
//| Adaboost for Logistic Regression |
//+------------------------------------------------------------------+
namespace LogisticRegression
{
class AdaBoost
{
protected:
vector m_alphas;
vector classes_in_data;
int m_random_state;
bool m_boostrapping;
uint m_min_split, m_max_depth;
CLogisticRegression *weak_learners[]; //store weak_learner pointers for memory allocation tracking
CLogisticRegression *weak_learner;
uint m_estimators;
public:
AdaBoost(uint n_estimators=50, int random_state=42, bool bootstrapping=true);
~AdaBoost(void);
void fit(matrix &x, vector &y);
int predict(vector &x);
vector predict(matrix &x);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
AdaBoost::AdaBoost(uint n_estimators=50, int random_state=42, bool bootstrapping=true)
:m_estimators(n_estimators),
m_random_state(random_state),
m_boostrapping(bootstrapping)
{
ArrayResize(weak_learners, m_estimators); //Resizing the array to retain the number of base weak_learners
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
AdaBoost::~AdaBoost(void)
{
for (uint i=0; i<m_estimators; i++) //Delete the forest | all trees
if (CheckPointer(weak_learners[i]) != POINTER_INVALID)
delete(weak_learners[i]);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void AdaBoost::fit(matrix &x,vector &y)
{
m_alphas.Resize(m_estimators);
classes_in_data = MatrixExtend::Unique(y); //Find the target variables in the class
ulong m = x.Rows(), n = x.Cols();
vector weights(m); weights = weights.Fill(1.0) / m; //Initialize instance weights
vector preds(m);
vector misclassified(m);
//---
matrix data = MatrixExtend::concatenate(x, y);
matrix temp_data;
matrix x_subset;
vector y_subset;
double error = 0;
for (uint i=0; i<m_estimators; i++)
{
temp_data = data;
MatrixExtend::Randomize(temp_data, this.m_random_state, this.m_boostrapping);
if (!MatrixExtend::XandYSplitMatrices(temp_data, x_subset, y_subset)) //Get randomized subsets
{
ArrayRemove(weak_learners,i,1); //Delete the invalid weak_learner
printf("%s %d Failed to split data",__FUNCTION__,__LINE__);
continue;
}
//---
weak_learner = new CLogisticRegression();
weak_learner.fit(x_subset, y_subset); //fiting the randomized data to the i-th weak_learner
preds = weak_learner.predict(x_subset); //making predictions for the i-th weak_learner
for (ulong j=0; j<m; j++)
misclassified[j] = (preds[j] != y_subset[j]);
error = (misclassified * weights).Sum() / (double)weights.Sum();
//--- Calculate the weight of a weak learner in the final weak_learner
double alpha = 0.5 * log((1-error) / (error + 1e-10));
//--- Update instance weights
weights *= exp(-alpha * y_subset * preds);
weights /= weights.Sum();
//--- save a weak learner and its weight
this.m_alphas[i] = alpha;
this.weak_learners[i] = weak_learner;
printf("Building Estimator [%d/%d] Accuracy Score %.3f",i+1,m_estimators,Metrics::accuracy_score(y_subset,preds));
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int AdaBoost::predict(vector &x)
{
// Combine weak learners using weighted sum
vector weak_preds(m_estimators),
final_preds(m_estimators);
for (uint i=0; i<this.m_estimators; i++)
weak_preds[i] = this.weak_learners[i].predict(x);
return (int)weak_preds[(this.m_alphas*weak_preds).ArgMax()]; //Majority decision
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector AdaBoost::predict(matrix &x)
{
vector ret_v(x.Rows());
for (ulong i=0; i<ret_v.Size(); i++)
ret_v[i] = this.predict(x.Row(i));
return ret_v;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+