CorrelationFunctions.hh
7.12 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: CorrelationFunctions.hh
* Author: hacene
*
* Created on September 27, 2021, 3:35 PM
*/
#ifndef CORRELATIONFUNCTIONS_HH
#define CORRELATIONFUNCTIONS_HH
#include "DicError.hh"
#include "AMDA_exception.hh"
#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"
#include <vector>
#include <iostream>
#include <iterator>
#include <c++/4.8.2/bits/stl_vector.h>
#include <c++/4.8.2/bits/stl_pair.h>
#include <list>
#include "Toolbox.hh"
#include "AbstractFunc.hh"
namespace AMDA {
namespace Parameters {
namespace StatisticFunctions {
enum COEFS {
COVARIANCE = 1,
PAERSON = 2,
SPEARMAN = 3,
KENDALL = 4,
};
static std::map<std::string, COEFS> coefsToStr = {
{"covariance", COEFS::COVARIANCE},
{"pearson", COEFS::PAERSON},
{"spearman", COEFS::SPEARMAN},
{"kendall", COEFS::KENDALL},
{"1", COEFS::COVARIANCE},
{"2", COEFS::PAERSON},
{"3", COEFS::SPEARMAN},
{"4", COEFS::KENDALL},
};
/**
*
* @param pProcess
* @param pTimeIntervalList
* @param firstParamInput
* @param secondParamInput
* @param windowtime
* @param type
*/
template <typename InputElemType, typename OutputElemType>
class Correlation : public Sm2ParamsAbstractFunc<InputElemType, OutputElemType> {
public:
Correlation(Process & pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& firstParamInput,
ParamDataSpec<InputElemType>& secondParamInput, double windowtime, std::string correlationType) :
Sm2ParamsAbstractFunc<InputElemType, OutputElemType> (pProcess, pTimeIntervalList, firstParamInput,
secondParamInput, windowtime), _correlationType(correlationType) {
}
virtual ~Correlation() {
}
virtual OutputElemType compute() {
return computeCorrelation(Abstract2ParamsFunc<InputElemType, OutputElemType>::_paramOutput);
}
double computeCorrelation(ParamDataSpec<double>* /*out*/) {
double result = 0.;
result << NotANumber();
if (Sm2ParamsAbstractFunc<InputElemType, OutputElemType>::_mem.empty()) {
return result;
}
std::list<std::pair<InputElemType, InputElemType>> list;
for (typename std::list<std::pair<double, std::pair < InputElemType, InputElemType>>>::iterator it = Sm2ParamsAbstractFunc<InputElemType, OutputElemType>::_mem.begin(); it != Sm2ParamsAbstractFunc<InputElemType, OutputElemType>::_mem.end(); ++it) {
if (isNAN(it->second.first) || isNAN(it->second.second))
continue;
list.push_back(it->second);
}
if (coefsToStr.find(_correlationType) == coefsToStr.end()) {
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction unknown correlation type " + _correlationType));
}
switch (coefsToStr[_correlationType]) {
case COEFS::COVARIANCE:
getCovariance(list, result);
break;
case COEFS::PAERSON:
getPearson(list, result);
break;
case COEFS::SPEARMAN:
getSpearman(list, result);
break;
case COEFS::KENDALL:
getKendall(list, result);
break;
default:
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction unknown correlation type :" + _correlationType));
}
return result;
}
std::vector<double> computeCorrelation(ParamDataSpec<std::vector<double> >* /*out*/) {
std::vector<double> result;
if (Sm2ParamsAbstractFunc<InputElemType, OutputElemType>::_mem.empty()) {
return result;
}
int n_ = Sm2ParamsAbstractFunc<InputElemType, OutputElemType>::_mem.begin()->second.first.size();
result.resize(n_);
result << NotANumber();
for(int i = 0; i < n_; ++i){
std::list<std::pair<double, double>> list;
for (typename std::list<std::pair<double, std::pair < InputElemType, InputElemType>>>::iterator it = Sm2ParamsAbstractFunc<InputElemType, OutputElemType>::_mem.begin(); it != Sm2ParamsAbstractFunc<InputElemType, OutputElemType>::_mem.end(); ++it) {
list.push_back(std::make_pair((double)it->second.first[i], (double)it->second.second[i]));
}
if (coefsToStr.find(_correlationType) == coefsToStr.end()) {
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction unknown correlation type " + _correlationType));
}
switch (coefsToStr[_correlationType]) {
case COEFS::COVARIANCE:
getCovariance(list, result[i]);
break;
case COEFS::PAERSON:
getPearson(list, result[i]);
break;
case COEFS::SPEARMAN:
getSpearman(list, result[i]);
break;
case COEFS::KENDALL:
getKendall(list, result[i]);
break;
default:
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction unknown correlation type :" + _correlationType));
}
list.clear();
}
return result;
}
private:
std::string _correlationType;
};
}
}
}
#endif /* CORRELATIONFUNCTIONS_HH */