Blame view

src/ExternLib/StatisticFunctions/Toolbox.hh 15 KB
74a11471   Benjamin Renard   Add min, max, var...
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * Toolbox.hh
 *
 *  Created on: Jun 23, 2018
 *      Author: benjamin
 */

#ifndef TOOLBOX_HH_
#define TOOLBOX_HH_

#include "ParamData.hh"

namespace AMDA {
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
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
    namespace Parameters {
        namespace StatisticFunctions {

            template <typename Type>
            Type min(Type a, Type b) {
                if (!std::isfinite(a) || isNAN(a))
                    return b;
                if (!std::isfinite(b) || isNAN(b))
                    return a;
                return std::min(a, b);
            }

            template <typename Type>
            std::vector<Type> min(std::vector<Type> a, std::vector<Type> b) {
                std::vector<Type> res = a;
                if (a.size() != b.size()) {
                    BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size"));
                }
                for (unsigned int i = 0; i < a.size(); ++i) {
                    res[i] = StatisticFunctions::min(a[i], b[i]);
                }
                return res;
            }

            template <typename Type>
            Tab2DData<Type> min(Tab2DData<Type> a, Tab2DData<Type> b) {
                Tab2DData<Type> res(a);
                if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) {
                    BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions"));
                }
                for (int i = 0; i < a.getDim1Size(); ++i) {
                    for (int j = 0; j < a.getDim2Size(); ++j) {
                        res[i][j] = StatisticFunctions::min(a[i][j], b[i][j]);
                    }
                }
                return res;
            }

            template <typename Type>
            Type max(Type a, Type b) {
                if (!std::isfinite(a) || isNAN(a))
                    return b;
                if (!std::isfinite(b) || isNAN(b))
                    return a;
                return std::max(a, b);
            }

            template <typename Type>
            std::vector<Type> max(std::vector<Type> a, std::vector<Type> b) {
                std::vector<Type> res = a;
                if (a.size() != b.size()) {
                    BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size"));
                }
                for (unsigned int i = 0; i < a.size(); ++i) {
                    res[i] = StatisticFunctions::max(a[i], b[i]);
                }
                return res;
            }

            template <typename Type>
            Tab2DData<Type> max(Tab2DData<Type> a, Tab2DData<Type> b) {
                Tab2DData<Type> res(a);
                if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) {
                    BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions"));
                }
                for (int i = 0; i < a.getDim1Size(); ++i) {
                    for (int j = 0; j < a.getDim2Size(); ++j) {
                        res[i][j] = StatisticFunctions::max(a[i][j], b[i][j]);
                    }
                }
                return res;
            }

            template <typename Type>
            Type pow(Type a, double exp) {
                return std::pow(a, exp);
            }

            template <typename Type>
            std::vector<Type> pow(std::vector<Type> a, double exp) {
                std::vector<Type> res = a;
                for (unsigned int i = 0; i < a.size(); ++i) {
                    res[i] = StatisticFunctions::pow(a[i], exp);
                }
74a11471   Benjamin Renard   Add min, max, var...
98
                return res;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
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
            }

            template <typename Type>
            Tab2DData<Type> pow(Tab2DData<Type> a, double exp) {
                Tab2DData<Type> res = a;
                for (int i = 0; i < a.getDim1Size(); ++i) {
                    for (int j = 0; j < a.getDim2Size(); ++j) {
                        res[i][j] = StatisticFunctions::pow(a[i][j], exp);
                    }
                }
                return res;
            }

            template <typename Type>
            Type square(Type a) {
                return StatisticFunctions::pow(a, 2);
            }

            template <typename Type>
            std::vector<Type> square(std::vector<Type> a) {
                return StatisticFunctions::pow(a, 2);
            }

            template <typename Type>
            Tab2DData<Type> square(Tab2DData<Type> a) {
                return StatisticFunctions::pow(a, 2);
            }

            template <typename Type>
            Type root_square(Type a) {
                if (isNAN(a) || a < 0) {
                    Type res;
                    res << NotANumber();
                    return res;
                }
                return std::sqrt(a);
            }

            template <typename Type>
            std::vector<Type> root_square(std::vector<Type> a) {
                std::vector<Type> res = a;
                for (unsigned int i = 0; i < a.size(); ++i) {
                    res[i] = StatisticFunctions::root_square(a[i]);
                }
                return res;
            }

            template <typename Type>
            Tab2DData<Type> root_square(Tab2DData<Type> a) {
                Tab2DData<Type> res = a;
                for (int i = 0; i < a.getDim1Size(); ++i) {
                    for (int j = 0; j < a.getDim2Size(); ++j) {
                        res[i][j] = StatisticFunctions::root_square(a[i][j]);
                    }
                }
                return res;
            }

            template <typename Type>
            Type div(Type a, Type b) {
                if (isNAN(a) || isNAN(b) || (b == 0)) {
                    Type res;
                    res << NotANumber();
                    return res;
                }
                return a / b;
            }

            template <typename Type>
            std::vector<Type> div(std::vector<Type> a, std::vector<Type> b) {
                std::vector<Type> res = a;
                if (a.size() != b.size()) {
                    BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size"));
                }
                for (unsigned int i = 0; i < a.size(); ++i) {
                    res[i] = StatisticFunctions::div(a[i], b[i]);
                }
                return res;
            }

            template <typename Type>
            Tab2DData<Type> div(Tab2DData<Type> a, Tab2DData<Type> b) {
                Tab2DData<Type> res(a);
                if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) {
                    BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions"));
                }
                for (int i = 0; i < a.getDim1Size(); ++i) {
                    for (int j = 0; j < a.getDim2Size(); ++j) {
                        res[i][j] = StatisticFunctions::div(a[i][j], b[i][j]);
                    }
                }
                return res;
            }

            template <typename Type>
e336270a   Benjamin Renard   Fix correlation &...
194
195
            std::pair<double, double> getMean(std::list<std::pair<Type, Type>> &list) {
                std::pair<double, double> result(0, 0);
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
196
197
198
199
                std::pair<int, int> counter(0, 0);

                for (auto elt : list) {
                    if (!isNAN(elt.first)) {
e336270a   Benjamin Renard   Fix correlation &...
200
                        result.first += (double)elt.first;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
201
202
203
                        counter.first += 1;
                    }
                    if (!isNAN(elt.second)) {
e336270a   Benjamin Renard   Fix correlation &...
204
                        result.second += (double)elt.second;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
205
206
207
208
209
                        counter.second += 1;
                    }
                }
                if (counter.first != 0)
                    result.first /= counter.first;
e336270a   Benjamin Renard   Fix correlation &...
210
211
                else
                    result.first = NAN;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
212
213
214

                if (counter.second != 0)
                    result.second /= counter.second;
e336270a   Benjamin Renard   Fix correlation &...
215
216
                else
                    result.second = NAN;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
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

                return result;
            }

            template <typename Type>
            std::pair<Type, Type> getStd(std::list<std::pair<Type, Type>> &list) {
                std::pair<Type, Type> mean = getMean(list);
                std::pair<Type, Type> result(0, 0);
                int counter1 = 0;
                int counter2 = 0;
                for (auto elt : list) {
                    if (!isNAN(elt->first)) {
                        result.first += (elt->first - mean.first)*(elt->first - mean.first);
                        counter1 += 1;
                    }
                    if (!isNAN(elt->second)) {
                        result.second += (elt->second - mean.second)*(elt->second - mean.second);
                        counter2 += 1;
                    }
                }
                if (counter1 != 0) {
                    result.first /= counter1;
                } else {
                    result.first << NotANumber();
                }
                if (counter2 != 0) {
                    result.second /= counter2;
                } else {
                    result.second << NotANumber();
                }
                return std::sqrt(result);
            }

            template <typename Type>
e336270a   Benjamin Renard   Fix correlation &...
251
252
253
254
255
256
257
258
            std::vector<double>rankify(std::vector<Type>& X) {
                std::vector<double> Rank_X;
                if (X.empty()) {
                    return Rank_X;
                }
                Rank_X.resize(X.size());
                int i = 0;
                for (typename std::vector<Type>::iterator it1 = X.begin(); it1 != X.end(); ++it1) {
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
259
260
                    int r = 1, s = 1;

e336270a   Benjamin Renard   Fix correlation &...
261
262
263
264
265
266
                    for (typename std::vector<Type>::iterator it2 = X.begin(); it2 != X.end(); ++it2) {
                        if (it1 == it2) {
                            continue;
                        }
                        if (*it2 < *it1) r++;
                        if (*it1 == *it2) s++;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
267
268
                    }

403992c8   Hacene SI HADJ MOHAND   scalr ok tested
269
270
                    // Use Fractional Rank formula
                    // fractional_rank = r + (n-1)/2
e336270a   Benjamin Renard   Fix correlation &...
271
272
                    Rank_X[i] = (double) r + (s - 1) * 0.5;
                    ++i;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
273
274
275
276
277
278
279
                }

                // Return Rank Vector
                return Rank_X;
            }

            template <typename Type>
e336270a   Benjamin Renard   Fix correlation &...
280
281
            bool getCovariance(std::list<std::pair<Type, Type>> &list, double &result) {
                result = NAN;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
282
283
284
                if (list.empty()) {
                    return false;
                }
e336270a   Benjamin Renard   Fix correlation &...
285
286
287
288
                std::pair<double, double> mean = getMean(list);
                if (isNAN(mean.first) || isNAN(mean.second)) {
                    return false;
                }
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
289
                int counter = 0;
e336270a   Benjamin Renard   Fix correlation &...
290
                double sum = 0.;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
291
292
                for (auto elt : list) {
                    if (!isNAN(elt.first) && !isNAN(elt.second)) {
e336270a   Benjamin Renard   Fix correlation &...
293
                        sum += ((double)elt.first - mean.first)*((double)elt.second - mean.second);
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
294
295
296
297
                        counter += 1;
                    }
                }
                if (counter != 0)
e336270a   Benjamin Renard   Fix correlation &...
298
                    result = sum / counter;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
299
300
301
302
                return true;
            }

            template <typename Type>
e336270a   Benjamin Renard   Fix correlation &...
303
304
            bool getPearson(std::list<std::pair<Type, Type>> &list, double &result) {
                result = NAN;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
305
306
307
                if (list.empty()) {
                    return false;
                }
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
308
                int counter = 0;
e336270a   Benjamin Renard   Fix correlation &...
309
310
311
312
                double sum1 = 0, sum2 = 0;
                double sum1Sq = 0, sum2Sq = 0;
                double sum12 = 0;
                
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
313
314
                for (auto elt : list) {
                    if (!isNAN(elt.first) && !isNAN(elt.second)) {
e336270a   Benjamin Renard   Fix correlation &...
315
316
317
318
319
                        sum1 += (double)elt.first;
                        sum2 += (double)elt.second;
                        sum1Sq += (double)elt.first * (double)elt.first;
                        sum2Sq += (double)elt.second * (double)elt.second;
                        sum12 += (double)elt.first * (double)elt.second;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
320
321
322
                        counter += 1;
                    }
                }
e336270a   Benjamin Renard   Fix correlation &...
323
                if (counter > 1) {
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
324
325
                    result = (counter * sum12 - sum1 * sum2) /
                    (std::sqrt((counter * sum1Sq - sum1 * sum1)*(counter * sum2Sq - sum2 * sum2)));
e336270a   Benjamin Renard   Fix correlation &...
326
                }
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
327
328
329
330
                return true;
            }

            template <typename Type>
e336270a   Benjamin Renard   Fix correlation &...
331
332
333
            bool getSpearman(std::list<std::pair<Type, Type>> &list, double &result) {
                result = NAN;

403992c8   Hacene SI HADJ MOHAND   scalr ok tested
334
335
336
337
                if (list.empty()) {
                    return false;
                }

e336270a   Benjamin Renard   Fix correlation &...
338
                std::vector<double> X, Y;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
339
340
                for (auto elt : list) {
                    if (!isNAN(elt.first) && !isNAN(elt.second)) {
e336270a   Benjamin Renard   Fix correlation &...
341
342
                        X.push_back((double)elt.first);
                        Y.push_back((double)elt.second);
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
343
344
                    }
                }
e336270a   Benjamin Renard   Fix correlation &...
345
346
347
348
349
350
351
352

                if (X.empty()) {
                    return false;
                }

                std::vector<double> rank_X = rankify(X);
                std::vector<double> rank_Y = rankify(Y);
                std::list<std::pair<double, double>> rankList;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
353
                
e336270a   Benjamin Renard   Fix correlation &...
354
                for (int i = 0; i < X.size(); i++)
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
355
356
                    rankList.push_back(std::make_pair(rank_X[i], rank_Y[i]));
                
e336270a   Benjamin Renard   Fix correlation &...
357
                return getPearson(rankList, result);
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
358
359
360
            }

            template <typename Type>
e336270a   Benjamin Renard   Fix correlation &...
361
362
363
            bool getKendall(std::list<std::pair<Type, Type>> &list, double &result) {
                result = NAN;

403992c8   Hacene SI HADJ MOHAND   scalr ok tested
364
365
366
                if (list.empty()) {
                    return false;
                }
e336270a   Benjamin Renard   Fix correlation &...
367
368

                std::vector<double> X, Y;
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
369
370
                for (auto elt : list) {
                    if (!isNAN(elt.first) && !isNAN(elt.second)) {
e336270a   Benjamin Renard   Fix correlation &...
371
372
                        X.push_back((double)elt.first);
                        Y.push_back((double)elt.second);
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
373
374
                    }
                }
e336270a   Benjamin Renard   Fix correlation &...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

                if (X.empty()) {
                    return false;
                }
                
                std::vector<double> rank_x = rankify(X);
                std::vector<double> rank_y = rankify(Y);

                long nc = 0;
                long nd = 0;
                for (int i = 0; i < rank_x.size()-1; i++){
                    for (int j = i+1; j<rank_x.size(); j++){
                        if (((rank_x[i] > rank_x[j]) && (rank_y[i] >  rank_y[j])) || 
                            ((rank_x[i] < rank_x[j]) && (rank_y[i] <  rank_y[j])))
                            ++nc;
                        if (((rank_x[i] > rank_x[j]) && (rank_y[i] <  rank_y[j])) || 
                            ((rank_x[i] < rank_x[j]) && (rank_y[i] >  rank_y[j])))
                            ++nd;

bf7793bf   Hacene SI HADJ MOHAND   correcting kendall
394
395
396
                    }
                    
                }
e336270a   Benjamin Renard   Fix correlation &...
397
                result = ((double)(nc-nd)/(0.5*rank_x.size()*(rank_x.size()-1)));
403992c8   Hacene SI HADJ MOHAND   scalr ok tested
398
399
400
401
402
                return true;
            }

        } /* namespace StatisticFunctions */
    } /* namespace Parameters */
74a11471   Benjamin Renard   Add min, max, var...
403
404
} /* namespace AMDA */
#endif /* TOOLBOX_HH_ */