Commit 403992c897795b58c21c840fcfda30e2c831d42d
1 parent
f5c74402
Exists in
master
and in
63 other branches
scalr ok tested
Showing
6 changed files
with
571 additions
and
313 deletions
Show diff stats
src/ExternLib/StatisticFunctions/AMDAPlugin.cc
... | ... | @@ -74,7 +74,20 @@ extern "C" void registerPlugin(AMDA::Plugins::PluginManager & pm) { |
74 | 74 | ServicesServer::getInstance()->addProcessFactory("median", factProcessMedianFunc); |
75 | 75 | ServicesServer::getInstance()->linkProcessWithPlugin("median", pluginPath); |
76 | 76 | |
77 | - ProcessFactory factProcessCorrelation = boost::factory<CorrelationProcess*>(); | |
78 | - ServicesServer::getInstance()->addProcessFactory("statCorrelation", factProcessCorrelation); | |
79 | - ServicesServer::getInstance()->linkProcessWithPlugin("statCorrelation", pluginPath); | |
77 | + ProcessFactory factCovarianceCorrelation = boost::factory<CovarianceProcess*>(); | |
78 | + ServicesServer::getInstance()->addProcessFactory("covariance", factCovarianceCorrelation); | |
79 | + ServicesServer::getInstance()->linkProcessWithPlugin("covariance", pluginPath); | |
80 | + | |
81 | + ProcessFactory factPearsonCorrelation = boost::factory<PearsonProcess*>(); | |
82 | + ServicesServer::getInstance()->addProcessFactory("pearson", factPearsonCorrelation); | |
83 | + ServicesServer::getInstance()->linkProcessWithPlugin("pearson", pluginPath); | |
84 | + | |
85 | + ProcessFactory factKendallCorrelation = boost::factory<KendallProcess*>(); | |
86 | + ServicesServer::getInstance()->addProcessFactory("kendall", factKendallCorrelation); | |
87 | + ServicesServer::getInstance()->linkProcessWithPlugin("kendall", pluginPath); | |
88 | + | |
89 | + ProcessFactory factSpearmanCorrelation = boost::factory<SpearmanProcess*>(); | |
90 | + ServicesServer::getInstance()->addProcessFactory("spearman", factSpearmanCorrelation); | |
91 | + ServicesServer::getInstance()->linkProcessWithPlugin("spearman", pluginPath); | |
92 | + | |
80 | 93 | } | ... | ... |
src/ExternLib/StatisticFunctions/CorrelationFunctions.hh
... | ... | @@ -14,6 +14,8 @@ |
14 | 14 | #ifndef CORRELATIONFUNCTIONS_HH |
15 | 15 | #define CORRELATIONFUNCTIONS_HH |
16 | 16 | |
17 | +#include "DicError.hh" | |
18 | +#include "AMDA_exception.hh" | |
17 | 19 | #include "Parameter.hh" |
18 | 20 | #include "ParamData.hh" |
19 | 21 | #include "DataTypeMath.hh" |
... | ... | @@ -23,6 +25,7 @@ |
23 | 25 | #include <iterator> |
24 | 26 | #include <c++/4.8.2/bits/stl_vector.h> |
25 | 27 | #include <c++/4.8.2/bits/stl_pair.h> |
28 | +#include "Toolbox.hh" | |
26 | 29 | #include "AbstractFunc.hh" |
27 | 30 | |
28 | 31 | namespace AMDA { |
... | ... | @@ -32,15 +35,21 @@ namespace AMDA { |
32 | 35 | #define AVERAGE_TIME 1200 // (seconds) |
33 | 36 | #define MAX_GAP_SIZE 3600 // (seconds) |
34 | 37 | |
35 | - class CorrelationBase { | |
36 | - public: | |
37 | - | |
38 | - CorrelationBase() { | |
39 | - } | |
40 | - | |
41 | - virtual ~CorrelationBase() { | |
42 | - } | |
43 | - void virtual pushSecondParamData(ParamDataIndexInfo &pParamDataIndexInfo) = 0; | |
38 | + enum COEFS { | |
39 | + COVARIANCE = 1, | |
40 | + PAERSON = 2, | |
41 | + SPEARMAN = 3, | |
42 | + KENDALL = 4, | |
43 | + }; | |
44 | + static std::map<std::string, COEFS> coefsToStr = { | |
45 | + {"covariance", COEFS::COVARIANCE}, | |
46 | + {"pearson", COEFS::PAERSON}, | |
47 | + {"spearman", COEFS::SPEARMAN}, | |
48 | + {"kendall", COEFS::KENDALL}, | |
49 | + {"1", COEFS::COVARIANCE}, | |
50 | + {"2", COEFS::PAERSON}, | |
51 | + {"3", COEFS::SPEARMAN}, | |
52 | + {"4", COEFS::KENDALL}, | |
44 | 53 | }; |
45 | 54 | |
46 | 55 | template <typename InputElemType, typename OutputElemType> |
... | ... | @@ -207,8 +216,13 @@ namespace AMDA { |
207 | 216 | nanVal << NotANumber(); |
208 | 217 | std::pair<double, InputElemType> prev_value(NAN, nanVal); |
209 | 218 | std::pair<double, InputElemType> next_value(NAN, nanVal); |
219 | + InputElemType value = nanVal; | |
210 | 220 | for (auto it = input.begin(); it != input.end(); ++it) { |
211 | - if (isNAN(it->second)) | |
221 | + if (it->first == time) { | |
222 | + value = it->second; | |
223 | + return value; | |
224 | + break; | |
225 | + } else if (isNAN(it->second)) | |
212 | 226 | continue; |
213 | 227 | else if (it->first > max_t) { |
214 | 228 | next_value = *it; |
... | ... | @@ -219,8 +233,6 @@ namespace AMDA { |
219 | 233 | values_for_mean.push_back(*it); |
220 | 234 | } |
221 | 235 | } |
222 | - | |
223 | - InputElemType value = nanVal; | |
224 | 236 | if (!values_for_mean.empty()) { |
225 | 237 | //Compute mean |
226 | 238 | InputElemType sum = 0; |
... | ... | @@ -237,79 +249,9 @@ namespace AMDA { |
237 | 249 | |
238 | 250 | return value; |
239 | 251 | } |
240 | - | |
241 | - OutputElemType compute() { | |
242 | - return computeCorrelation(_mem, AbstractCorrelationFunc<InputElemType, OutputElemType>::_nanVal, _correlationType); | |
243 | - } | |
244 | - | |
245 | - | |
246 | - std::pair<OutputElemType,OutputElemType> getMean(std::list<std::pair<InputElemType, InputElemType>> &list) { | |
247 | - std::pair<OutputElemType,OutputElemType> result(0, 0); | |
248 | - std::pair<int, int> counter(0, 0); | |
249 | - | |
250 | - for (auto elt : list) { | |
251 | - if (!isNAN(elt.first)) { | |
252 | - result.first += elt.first; | |
253 | - counter.first += 1; | |
254 | - } | |
255 | - if (!isNAN(elt.second)) { | |
256 | - result.second += elt.second; | |
257 | - counter.second += 1; | |
258 | - } | |
259 | - } | |
260 | - if (counter.first != 0) | |
261 | - result.first /= counter.first; | |
262 | - | |
263 | - if (counter.second != 0) | |
264 | - result.second /= counter.second; | |
265 | - | |
266 | - return result; | |
267 | - } | |
268 | - | |
269 | - std::pair<OutputElemType,OutputElemType> getStd(std::list<std::pair<InputElemType, InputElemType>> &list) { | |
270 | - std::pair<OutputElemType,OutputElemType> mean = getMean(list); | |
271 | - std::pair<OutputElemType,OutputElemType> result(0, 0); | |
272 | - int counter1 = 0; | |
273 | - int counter2 = 0; | |
274 | - for (auto elt : list) { | |
275 | - if (!isNAN(elt->first)) { | |
276 | - result.first += (elt->first - mean.first)*(elt->first - mean.first); | |
277 | - counter1 += 1; | |
278 | - } | |
279 | - if (!isNAN(elt->second)) { | |
280 | - result.second += (elt->second - mean.second)*(elt->second - mean.second); | |
281 | - counter2 += 1; | |
282 | - } | |
283 | - } | |
284 | - if (counter1 != 0) { | |
285 | - result.first /= counter1; | |
286 | - } else { | |
287 | - result.first << NotANumber(); | |
288 | - } | |
289 | - if (counter2 != 0) { | |
290 | - result.second /= counter2; | |
291 | - } else { | |
292 | - result.second << NotANumber(); | |
293 | - } | |
294 | - return std::sqrt(result); | |
295 | - } | |
296 | 252 | |
297 | - bool getCovariance(std::list<std::pair<InputElemType, InputElemType>> &list, OutputElemType &result) { | |
298 | - if (list.empty()) { | |
299 | - return false; | |
300 | - } | |
301 | - std::pair<OutputElemType,OutputElemType> mean = getMean(list); | |
302 | - result = 0; | |
303 | - int counter =0; | |
304 | - for (auto elt : list) { | |
305 | - if(!isNAN(elt.first) && !isNAN(elt.second)){ | |
306 | - result += (elt.first-mean.first)*(elt.second-mean.second); | |
307 | - counter += 1; | |
308 | - } | |
309 | - } | |
310 | - if(counter != 0) | |
311 | - result /= counter; | |
312 | - return true; | |
253 | + OutputElemType compute() { | |
254 | + return computeCorrelation(_mem, AbstractCorrelationFunc<InputElemType, OutputElemType>::_nanVal, _correlationType); | |
313 | 255 | } |
314 | 256 | |
315 | 257 | OutputElemType computeCorrelation(std::list<std::pair<double, std::pair<InputElemType, InputElemType>>>&mem, OutputElemType& nanVal, std::string type) { |
... | ... | @@ -321,10 +263,30 @@ namespace AMDA { |
321 | 263 | for (typename std::list<std::pair<double, std::pair < InputElemType, InputElemType>>>::iterator it = mem.begin(); it != mem.end(); ++it) { |
322 | 264 | list.push_back(it->second); |
323 | 265 | } |
324 | - getCovariance(list, result); | |
266 | + if (coefsToStr.find(type) == coefsToStr.end()) { | |
267 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction unknown correlation type " + type)); | |
268 | + } | |
269 | + | |
270 | + switch (coefsToStr[type]) { | |
271 | + case COEFS::COVARIANCE: | |
272 | + getCovariance(list, result); | |
273 | + break; | |
274 | + case COEFS::PAERSON: | |
275 | + getPearson(list, result); | |
276 | + break; | |
277 | + case COEFS::SPEARMAN: | |
278 | + getSpearman(list, result); | |
279 | + break; | |
280 | + case COEFS::KENDALL: | |
281 | + getKendall(list, result); | |
282 | + break; | |
283 | + default: | |
284 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("StatisticFunctions::CorrelationFunction unknown correlation type :" + type)); | |
285 | + } | |
286 | + | |
325 | 287 | return result; |
326 | 288 | } |
327 | - | |
289 | + | |
328 | 290 | protected: |
329 | 291 | std::string _correlationType; |
330 | 292 | std::list<std::pair<double, std::pair<InputElemType, InputElemType>> > _mem; | ... | ... |
src/ExternLib/StatisticFunctions/CorrelationProcess.cc
... | ... | @@ -25,6 +25,8 @@ |
25 | 25 | #include "CorrelationProcess.hh" |
26 | 26 | #include "StatisticCorrelationCreator.hh" |
27 | 27 | #include "CorrelationFunctions.hh" |
28 | +#include <typeinfo> | |
29 | + | |
28 | 30 | using namespace std; |
29 | 31 | using namespace boost; |
30 | 32 | using namespace log4cxx; |
... | ... | @@ -32,10 +34,12 @@ using namespace log4cxx; |
32 | 34 | namespace AMDA { |
33 | 35 | namespace Parameters { |
34 | 36 | |
35 | - CorrelationProcess::CorrelationProcess(Parameter ¶meter):SingleParamProcess_CRTP(parameter) { | |
37 | + CorrelationProcess::CorrelationProcess(Parameter ¶meter) : SingleParamProcess_CRTP(parameter) { | |
38 | + _type = ""; | |
36 | 39 | } |
37 | 40 | |
38 | 41 | CorrelationProcess::CorrelationProcess(const CorrelationProcess& pProcess, Parameter ¶meter) : SingleParamProcess_CRTP(pProcess, parameter) { |
42 | + _type = ""; | |
39 | 43 | } |
40 | 44 | |
41 | 45 | CorrelationProcess::~CorrelationProcess() { |
... | ... | @@ -46,12 +50,19 @@ namespace AMDA { |
46 | 50 | void CorrelationProcess::establishConnection() { |
47 | 51 | |
48 | 52 | |
49 | - if (_attributList.size() < 3) { | |
53 | + if (_attributList.size() < 2) { | |
50 | 54 | BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("CorrelationProcess::parse require 3 attributes'"))); |
51 | 55 | } |
52 | 56 | |
53 | 57 | //Imf parameter |
54 | - _secondInputParam = _parameter.getParameterManager().getParameter(_attributList[0]); | |
58 | + ParameterCreatorFromExpression creator(_parameter.getParameterManager()); | |
59 | + std::string secondParamExpression; | |
60 | + | |
61 | + if(_attributList[0][0] == '$' || _attributList[0][0] == '#') | |
62 | + secondParamExpression=_attributList[0]; | |
63 | + else | |
64 | + secondParamExpression = "$" + _attributList[0]; | |
65 | + _secondInputParam = creator.getOneParameterFromExpression(_parameter,secondParamExpression ,true); | |
55 | 66 | if (_secondInputParam == nullptr) { |
56 | 67 | BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("CorrelationProcess::parse cannot retrieve imf param"))); |
57 | 68 | } |
... | ... | @@ -62,40 +73,76 @@ namespace AMDA { |
62 | 73 | |
63 | 74 | TimeStamp CorrelationProcess::init() { |
64 | 75 | _windowtime = atof(_attributList[1].c_str()); |
65 | - _type = _attributList[2]; | |
76 | + | |
77 | + if(_type.empty() && _attributList.size() >=3) | |
78 | + _type = _attributList[2]; | |
66 | 79 | |
67 | 80 | TimeStamp time = _parameterInput->init(this, _timeIntervalList); |
68 | 81 | _paramInput = _parameterInput->getParamData(this).get(); |
69 | 82 | |
70 | 83 | _secondInputParam->init(this, _timeIntervalList); |
71 | 84 | ParamData* lSecondParamInput = _secondInputParam->getParamData(this).get(); |
72 | - | |
73 | - StatisticCorrelationCreator lCreator(*this, _timeIntervalList, *_paramInput, *lSecondParamInput, _windowtime, _type); | |
74 | - _operation = lCreator.getOperation(); | |
75 | - _paramData = ParamDataSPtr(_operation->getParamOutput()); | |
76 | - _paramData->setMinSampling(_paramInput->getMinSampling()); | |
85 | + | |
86 | + StatisticCorrelationCreator lCreator(*this, _timeIntervalList, *_paramInput, *lSecondParamInput, _secondInputParam, _windowtime, _type); | |
87 | + _operation = lCreator.getOperation(); | |
88 | + _paramData = ParamDataSPtr(_operation->getParamOutput()); | |
89 | + _paramData->setMinSampling(_paramInput->getMinSampling()); | |
77 | 90 | return time; |
78 | 91 | } |
79 | - | |
80 | - unsigned int CorrelationProcess::write() { | |
81 | - getSecondParamData(); | |
82 | - return SingleParamProcess::write(); | |
92 | + | |
93 | +// Covariance | |
94 | + CovarianceProcess::CovarianceProcess(Parameter& parameter):CorrelationProcess(parameter){ | |
95 | + CorrelationProcess::_type = "covariance"; | |
96 | + } | |
97 | + | |
98 | + CovarianceProcess::CovarianceProcess(const CorrelationProcess& pProcess, Parameter& parameter):CorrelationProcess(pProcess, parameter){ | |
99 | + CorrelationProcess::_type = "covariance"; | |
100 | + } | |
101 | + | |
102 | + CovarianceProcess::~CovarianceProcess(){ | |
103 | + } | |
104 | + | |
105 | + | |
106 | + // Pearson | |
107 | + PearsonProcess::PearsonProcess(Parameter& parameter):CorrelationProcess(parameter){ | |
108 | + CorrelationProcess::_type = "pearson"; | |
109 | + } | |
110 | + | |
111 | + PearsonProcess::PearsonProcess(const CorrelationProcess& pProcess, Parameter& parameter):CorrelationProcess(pProcess, parameter){ | |
112 | + CorrelationProcess::_type = "pearson"; | |
83 | 113 | } |
114 | + | |
115 | + PearsonProcess::~PearsonProcess(){ | |
116 | + } | |
117 | + | |
118 | + | |
84 | 119 | |
85 | - void CorrelationProcess::getSecondParamData() { | |
86 | - | |
87 | - try { | |
88 | - ParamDataIndexInfo lParamDataIndexInfo; | |
89 | - lParamDataIndexInfo = _secondInputParam->getAsync(this).get(); | |
90 | - while ((!lParamDataIndexInfo._noMoreTimeInt && !lParamDataIndexInfo._timeIntToProcessChanged) || (lParamDataIndexInfo._nbDataToProcess > 0)) { | |
91 | - reinterpret_cast<StatisticFunctions::CorrelationBase*> (_operation)->pushSecondParamData(lParamDataIndexInfo); | |
92 | - if (lParamDataIndexInfo._timeIntToProcessChanged || lParamDataIndexInfo._noMoreTimeInt) | |
93 | - break; | |
94 | - lParamDataIndexInfo = _secondInputParam->getAsync(this).get(); | |
95 | - } | |
96 | - } catch (...) { | |
97 | - throw; | |
98 | - } | |
120 | +// Kendall | |
121 | + KendallProcess::KendallProcess(Parameter& parameter):CorrelationProcess(parameter){ | |
122 | + CorrelationProcess::_type = "kendall"; | |
123 | + } | |
124 | + | |
125 | + KendallProcess::KendallProcess(const CorrelationProcess& pProcess, Parameter& parameter):CorrelationProcess(pProcess, parameter){ | |
126 | + CorrelationProcess::_type = "kendall"; | |
127 | + } | |
128 | + | |
129 | + KendallProcess::~KendallProcess(){ | |
99 | 130 | } |
131 | + | |
132 | + // Spearman | |
133 | + SpearmanProcess::SpearmanProcess(Parameter& parameter):CorrelationProcess(parameter){ | |
134 | + CorrelationProcess::_type = "spearman "; | |
135 | + } | |
136 | + | |
137 | + SpearmanProcess::SpearmanProcess(const CorrelationProcess& pProcess, Parameter& parameter):CorrelationProcess(pProcess, parameter){ | |
138 | + CorrelationProcess::_type = "spearman "; | |
139 | + } | |
140 | + | |
141 | + SpearmanProcess::~SpearmanProcess(){ | |
142 | + } | |
143 | + | |
100 | 144 | } |
145 | + | |
146 | + | |
101 | 147 | } |
148 | + | ... | ... |
src/ExternLib/StatisticFunctions/CorrelationProcess.hh
... | ... | @@ -36,13 +36,6 @@ namespace AMDA { |
36 | 36 | */ |
37 | 37 | TimeStamp init(); |
38 | 38 | |
39 | - /* | |
40 | - | |
41 | - * Write data in dataParam. | |
42 | - */ | |
43 | - unsigned int write(); | |
44 | - | |
45 | - void getSecondParamData(); | |
46 | 39 | |
47 | 40 | protected: |
48 | 41 | |
... | ... | @@ -52,7 +45,6 @@ namespace AMDA { |
52 | 45 | |
53 | 46 | double _windowtime; |
54 | 47 | |
55 | - | |
56 | 48 | private: |
57 | 49 | /** |
58 | 50 | * @brief list of param name intput |
... | ... | @@ -60,6 +52,41 @@ namespace AMDA { |
60 | 52 | */ |
61 | 53 | ParameterSPtr _secondInputParam; |
62 | 54 | |
55 | + }; | |
56 | + | |
57 | + class CovarianceProcess : public CorrelationProcess { | |
58 | + public: | |
59 | + | |
60 | + CovarianceProcess(Parameter& parameter); | |
61 | + CovarianceProcess(const CorrelationProcess& pProcess, Parameter ¶meter); | |
62 | + virtual ~CovarianceProcess(); | |
63 | + | |
64 | + }; | |
65 | + | |
66 | + class PearsonProcess : public CorrelationProcess { | |
67 | + public: | |
68 | + | |
69 | + PearsonProcess(Parameter& parameter); | |
70 | + PearsonProcess(const CorrelationProcess& pProcess, Parameter ¶meter); | |
71 | + virtual ~PearsonProcess(); | |
72 | + | |
73 | + }; | |
74 | + | |
75 | + class SpearmanProcess : public CorrelationProcess { | |
76 | + public: | |
77 | + | |
78 | + SpearmanProcess(Parameter& parameter); | |
79 | + SpearmanProcess(const CorrelationProcess& pProcess, Parameter ¶meter); | |
80 | + virtual ~SpearmanProcess(); | |
81 | + | |
82 | + }; | |
83 | + | |
84 | + class KendallProcess : public CorrelationProcess { | |
85 | + public: | |
86 | + | |
87 | + KendallProcess(Parameter& parameter); | |
88 | + KendallProcess(const CorrelationProcess& pProcess, Parameter ¶meter); | |
89 | + virtual ~KendallProcess(); | |
63 | 90 | |
64 | 91 | }; |
65 | 92 | ... | ... |
src/ExternLib/StatisticFunctions/StatisticCorrelationCreator.hh
... | ... | @@ -8,6 +8,9 @@ |
8 | 8 | #ifndef STATISTICCORRELATIONCREATOR_HH_ |
9 | 9 | #define STATISTICCORRELATIONCREATOR_HH_ |
10 | 10 | |
11 | +#include "Operation.hh" | |
12 | +#include "ParameterManager.hh" | |
13 | +#include "ParamMgr.hh" | |
11 | 14 | #include "ParamData.hh" |
12 | 15 | #include "VisitorOfParamData.hh" |
13 | 16 | #include "AMDA_exception.hh" |
... | ... | @@ -27,8 +30,8 @@ namespace AMDA { |
27 | 30 | /** |
28 | 31 | * @brief Constructor. |
29 | 32 | */ |
30 | - StatisticCorrelationCreator(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamData& paramInput, ParamData& secondParamInput, double windowtime, std::string type_) | |
31 | - : _process(pProcess), _timeIntervalList(pTimeIntervalList), _paramData(paramInput), _secondParamInput(secondParamInput), _windowtime(windowtime), _type(type_) { | |
33 | + StatisticCorrelationCreator(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamData& paramInput, ParamData& secondParamData, ParameterSPtr secondInputParam, double windowtime, std::string type_) | |
34 | + : _process(pProcess), _timeIntervalList(pTimeIntervalList), _paramData(paramInput), _secondParamData(secondParamData), _secondInputParam(secondInputParam),_windowtime(windowtime), _type(type_) { | |
32 | 35 | _paramData.accept(*this); |
33 | 36 | } |
34 | 37 | |
... | ... | @@ -43,7 +46,7 @@ namespace AMDA { |
43 | 46 | * @overload VisitorOfParamData::visit(ParamDataScalaireFloat *) |
44 | 47 | */ |
45 | 48 | void visit(ParamDataScalaireFloat *) { |
46 | - createOperation<float>(); | |
49 | + createOperation<float>(); | |
47 | 50 | } |
48 | 51 | |
49 | 52 | /** |
... | ... | @@ -51,7 +54,7 @@ namespace AMDA { |
51 | 54 | */ |
52 | 55 | void visit(ParamDataScalaireDouble *) { |
53 | 56 | createOperation<double>(); |
54 | - | |
57 | + | |
55 | 58 | } |
56 | 59 | |
57 | 60 | /** |
... | ... | @@ -169,18 +172,37 @@ namespace AMDA { |
169 | 172 | |
170 | 173 | template <typename Type> |
171 | 174 | void createOperation() { |
172 | - _operation = new StatisticFunctions::Correlation<Type,Type>(_process, _timeIntervalList, dynamic_cast<ParamDataSpec<Type>&>(_paramData), | |
173 | - dynamic_cast<ParamDataSpec<Type>&>(_secondParamInput),_windowtime, _type); | |
175 | + _operation = new StatisticFunctions::Correlation<Type, Type>(_process, _timeIntervalList, dynamic_cast<ParamDataSpec<Type>&> (_paramData), | |
176 | + dynamic_cast<ParamDataSpec<Type>&> (_secondParamData), _windowtime, _type); | |
177 | + pushSecondParamData<Type>(); | |
178 | + | |
174 | 179 | } |
175 | 180 | |
176 | - template <typename Type> | |
181 | + template <typename Type> | |
182 | + void pushSecondParamData(){ | |
183 | + try { | |
184 | + ParamDataIndexInfo lParamDataIndexInfo; | |
185 | + lParamDataIndexInfo = _secondInputParam->getAsync(&_process).get(); | |
186 | + while ((!lParamDataIndexInfo._noMoreTimeInt && !lParamDataIndexInfo._timeIntToProcessChanged) || (lParamDataIndexInfo._nbDataToProcess > 0)) { | |
187 | + reinterpret_cast<StatisticFunctions::AbstractCorrelationFunc<Type, Type>*> (_operation)->pushSecondParamData(lParamDataIndexInfo); | |
188 | + if (lParamDataIndexInfo._timeIntToProcessChanged || lParamDataIndexInfo._noMoreTimeInt) | |
189 | + break; | |
190 | + lParamDataIndexInfo = _secondInputParam->getAsync(&_process).get(); | |
191 | + } | |
192 | + } catch (...) { | |
193 | + throw; | |
194 | + } | |
195 | + } | |
196 | + | |
197 | + template <typename Type> | |
177 | 198 | void createOperation1D() { |
178 | 199 | } |
179 | 200 | |
180 | 201 | Process &_process; |
181 | 202 | TimeIntervalListSPtr _timeIntervalList; |
182 | - ParamData &_paramData; | |
183 | - ParamData &_secondParamInput; | |
203 | + ParamData &_paramData; | |
204 | + ParamData &_secondParamData; | |
205 | + ParameterSPtr _secondInputParam; | |
184 | 206 | double _windowtime; |
185 | 207 | std::string _type; |
186 | 208 | Operation *_operation; | ... | ... |
src/ExternLib/StatisticFunctions/Toolbox.hh
... | ... | @@ -11,186 +11,373 @@ |
11 | 11 | #include "ParamData.hh" |
12 | 12 | |
13 | 13 | namespace AMDA { |
14 | -namespace Parameters { | |
15 | -namespace StatisticFunctions { | |
16 | - | |
17 | -template <typename Type> | |
18 | -Type min(Type a, Type b) { | |
19 | - if (!std::isfinite(a) || isNAN(a)) | |
20 | - return b; | |
21 | - if (!std::isfinite(b) || isNAN(b)) | |
22 | - return a; | |
23 | - return std::min(a,b); | |
24 | -} | |
25 | - | |
26 | -template <typename Type> | |
27 | -std::vector<Type> min(std::vector<Type> a, std::vector<Type> b) { | |
28 | - std::vector<Type> res = a; | |
29 | - if (a.size() != b.size()) { | |
30 | - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); | |
31 | - } | |
32 | - for (unsigned int i = 0; i < a.size(); ++i) { | |
33 | - res[i] = StatisticFunctions::min(a[i],b[i]); | |
34 | - } | |
35 | - return res; | |
36 | -} | |
37 | - | |
38 | -template <typename Type> | |
39 | -Tab2DData<Type> min(Tab2DData<Type> a, Tab2DData<Type> b) { | |
40 | - Tab2DData<Type> res(a); | |
41 | - if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { | |
42 | - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); | |
43 | - } | |
44 | - for (int i = 0; i < a.getDim1Size(); ++i) { | |
45 | - for (int j = 0; j < a.getDim2Size(); ++j) { | |
46 | - res[i][j] = StatisticFunctions::min(a[i][j],b[i][j]); | |
47 | - } | |
48 | - } | |
49 | - return res; | |
50 | -} | |
51 | - | |
52 | -template <typename Type> | |
53 | -Type max(Type a, Type b) { | |
54 | - if (!std::isfinite(a) || isNAN(a)) | |
55 | - return b; | |
56 | - if (!std::isfinite(b) || isNAN(b)) | |
57 | - return a; | |
58 | - return std::max(a,b); | |
59 | -} | |
60 | - | |
61 | -template <typename Type> | |
62 | -std::vector<Type> max(std::vector<Type> a, std::vector<Type> b) { | |
63 | - std::vector<Type> res = a; | |
64 | - if (a.size() != b.size()) { | |
65 | - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); | |
66 | - } | |
67 | - for (unsigned int i = 0; i < a.size(); ++i) { | |
68 | - res[i] = StatisticFunctions::max(a[i],b[i]); | |
69 | - } | |
70 | - return res; | |
71 | -} | |
72 | - | |
73 | -template <typename Type> | |
74 | -Tab2DData<Type> max(Tab2DData<Type> a, Tab2DData<Type> b) { | |
75 | - Tab2DData<Type> res(a); | |
76 | - if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { | |
77 | - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); | |
78 | - } | |
79 | - for (int i = 0; i < a.getDim1Size(); ++i) { | |
80 | - for (int j = 0; j < a.getDim2Size(); ++j) { | |
81 | - res[i][j] = StatisticFunctions::max(a[i][j],b[i][j]); | |
82 | - } | |
83 | - } | |
84 | - return res; | |
85 | -} | |
86 | - | |
87 | -template <typename Type> | |
88 | -Type pow(Type a, double exp) { | |
89 | - return std::pow(a,exp); | |
90 | -} | |
91 | - | |
92 | -template <typename Type> | |
93 | -std::vector<Type> pow(std::vector<Type> a, double exp) { | |
94 | - std::vector<Type> res = a; | |
95 | - for (unsigned int i = 0; i < a.size(); ++i) { | |
96 | - res[i] = StatisticFunctions::pow(a[i],exp); | |
97 | - } | |
98 | - return res; | |
99 | -} | |
100 | - | |
101 | -template <typename Type> | |
102 | -Tab2DData<Type> pow(Tab2DData<Type> a, double exp) { | |
103 | - Tab2DData<Type> res = a; | |
104 | - for (int i = 0; i < a.getDim1Size(); ++i) { | |
105 | - for (int j = 0; j < a.getDim2Size(); ++j) { | |
106 | - res[i][j] = StatisticFunctions::pow(a[i][j],exp); | |
107 | - } | |
108 | - } | |
109 | - return res; | |
110 | -} | |
111 | - | |
112 | -template <typename Type> | |
113 | -Type square(Type a) { | |
114 | - return StatisticFunctions::pow(a,2); | |
115 | -} | |
116 | - | |
117 | -template <typename Type> | |
118 | -std::vector<Type> square(std::vector<Type> a) { | |
119 | - return StatisticFunctions::pow(a,2); | |
120 | -} | |
121 | - | |
122 | -template <typename Type> | |
123 | -Tab2DData<Type> square(Tab2DData<Type> a) { | |
124 | - return StatisticFunctions::pow(a,2); | |
125 | -} | |
126 | - | |
127 | -template <typename Type> | |
128 | -Type root_square(Type a) { | |
129 | - if (isNAN(a) || a < 0) { | |
130 | - Type res; | |
131 | - res << NotANumber(); | |
132 | - return res; | |
133 | - } | |
134 | - return std::sqrt(a); | |
135 | -} | |
136 | - | |
137 | -template <typename Type> | |
138 | -std::vector<Type> root_square(std::vector<Type> a) { | |
139 | - std::vector<Type> res = a; | |
140 | - for (unsigned int i = 0; i < a.size(); ++i) { | |
141 | - res[i] = StatisticFunctions::root_square(a[i]); | |
142 | - } | |
143 | - return res; | |
144 | -} | |
145 | - | |
146 | -template <typename Type> | |
147 | -Tab2DData<Type> root_square(Tab2DData<Type> a) { | |
148 | - Tab2DData<Type> res = a; | |
149 | - for (int i = 0; i < a.getDim1Size(); ++i) { | |
150 | - for (int j = 0; j < a.getDim2Size(); ++j) { | |
151 | - res[i][j] = StatisticFunctions::root_square(a[i][j]); | |
152 | - } | |
153 | - } | |
154 | - return res; | |
155 | -} | |
156 | - | |
157 | -template <typename Type> | |
158 | -Type div(Type a, Type b) { | |
159 | - if (isNAN(a) || isNAN(b) || (b == 0)) { | |
160 | - Type res; | |
161 | - res << NotANumber(); | |
14 | + namespace Parameters { | |
15 | + namespace StatisticFunctions { | |
16 | + | |
17 | + template <typename Type> | |
18 | + Type min(Type a, Type b) { | |
19 | + if (!std::isfinite(a) || isNAN(a)) | |
20 | + return b; | |
21 | + if (!std::isfinite(b) || isNAN(b)) | |
22 | + return a; | |
23 | + return std::min(a, b); | |
24 | + } | |
25 | + | |
26 | + template <typename Type> | |
27 | + std::vector<Type> min(std::vector<Type> a, std::vector<Type> b) { | |
28 | + std::vector<Type> res = a; | |
29 | + if (a.size() != b.size()) { | |
30 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); | |
31 | + } | |
32 | + for (unsigned int i = 0; i < a.size(); ++i) { | |
33 | + res[i] = StatisticFunctions::min(a[i], b[i]); | |
34 | + } | |
35 | + return res; | |
36 | + } | |
37 | + | |
38 | + template <typename Type> | |
39 | + Tab2DData<Type> min(Tab2DData<Type> a, Tab2DData<Type> b) { | |
40 | + Tab2DData<Type> res(a); | |
41 | + if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { | |
42 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); | |
43 | + } | |
44 | + for (int i = 0; i < a.getDim1Size(); ++i) { | |
45 | + for (int j = 0; j < a.getDim2Size(); ++j) { | |
46 | + res[i][j] = StatisticFunctions::min(a[i][j], b[i][j]); | |
47 | + } | |
48 | + } | |
49 | + return res; | |
50 | + } | |
51 | + | |
52 | + template <typename Type> | |
53 | + Type max(Type a, Type b) { | |
54 | + if (!std::isfinite(a) || isNAN(a)) | |
55 | + return b; | |
56 | + if (!std::isfinite(b) || isNAN(b)) | |
57 | + return a; | |
58 | + return std::max(a, b); | |
59 | + } | |
60 | + | |
61 | + template <typename Type> | |
62 | + std::vector<Type> max(std::vector<Type> a, std::vector<Type> b) { | |
63 | + std::vector<Type> res = a; | |
64 | + if (a.size() != b.size()) { | |
65 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); | |
66 | + } | |
67 | + for (unsigned int i = 0; i < a.size(); ++i) { | |
68 | + res[i] = StatisticFunctions::max(a[i], b[i]); | |
69 | + } | |
70 | + return res; | |
71 | + } | |
72 | + | |
73 | + template <typename Type> | |
74 | + Tab2DData<Type> max(Tab2DData<Type> a, Tab2DData<Type> b) { | |
75 | + Tab2DData<Type> res(a); | |
76 | + if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { | |
77 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); | |
78 | + } | |
79 | + for (int i = 0; i < a.getDim1Size(); ++i) { | |
80 | + for (int j = 0; j < a.getDim2Size(); ++j) { | |
81 | + res[i][j] = StatisticFunctions::max(a[i][j], b[i][j]); | |
82 | + } | |
83 | + } | |
84 | + return res; | |
85 | + } | |
86 | + | |
87 | + template <typename Type> | |
88 | + Type pow(Type a, double exp) { | |
89 | + return std::pow(a, exp); | |
90 | + } | |
91 | + | |
92 | + template <typename Type> | |
93 | + std::vector<Type> pow(std::vector<Type> a, double exp) { | |
94 | + std::vector<Type> res = a; | |
95 | + for (unsigned int i = 0; i < a.size(); ++i) { | |
96 | + res[i] = StatisticFunctions::pow(a[i], exp); | |
97 | + } | |
162 | 98 | return res; |
163 | - } | |
164 | - return a/b; | |
165 | -} | |
166 | - | |
167 | -template <typename Type> | |
168 | -std::vector<Type> div(std::vector<Type> a, std::vector<Type> b) { | |
169 | - std::vector<Type> res = a; | |
170 | - if (a.size() != b.size()) { | |
171 | - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); | |
172 | - } | |
173 | - for (unsigned int i = 0; i < a.size(); ++i) { | |
174 | - res[i] = StatisticFunctions::div(a[i],b[i]); | |
175 | - } | |
176 | - return res; | |
177 | -} | |
178 | - | |
179 | -template <typename Type> | |
180 | -Tab2DData<Type> div(Tab2DData<Type> a, Tab2DData<Type> b) { | |
181 | - Tab2DData<Type> res(a); | |
182 | - if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { | |
183 | - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); | |
184 | - } | |
185 | - for (int i = 0; i < a.getDim1Size(); ++i) { | |
186 | - for (int j = 0; j < a.getDim2Size(); ++j) { | |
187 | - res[i][j] = StatisticFunctions::div(a[i][j],b[i][j]); | |
188 | - } | |
189 | - } | |
190 | - return res; | |
191 | -} | |
192 | - | |
193 | -} /* namespace StatisticFunctions */ | |
194 | -} /* namespace Parameters */ | |
99 | + } | |
100 | + | |
101 | + template <typename Type> | |
102 | + Tab2DData<Type> pow(Tab2DData<Type> a, double exp) { | |
103 | + Tab2DData<Type> res = a; | |
104 | + for (int i = 0; i < a.getDim1Size(); ++i) { | |
105 | + for (int j = 0; j < a.getDim2Size(); ++j) { | |
106 | + res[i][j] = StatisticFunctions::pow(a[i][j], exp); | |
107 | + } | |
108 | + } | |
109 | + return res; | |
110 | + } | |
111 | + | |
112 | + template <typename Type> | |
113 | + Type square(Type a) { | |
114 | + return StatisticFunctions::pow(a, 2); | |
115 | + } | |
116 | + | |
117 | + template <typename Type> | |
118 | + std::vector<Type> square(std::vector<Type> a) { | |
119 | + return StatisticFunctions::pow(a, 2); | |
120 | + } | |
121 | + | |
122 | + template <typename Type> | |
123 | + Tab2DData<Type> square(Tab2DData<Type> a) { | |
124 | + return StatisticFunctions::pow(a, 2); | |
125 | + } | |
126 | + | |
127 | + template <typename Type> | |
128 | + Type root_square(Type a) { | |
129 | + if (isNAN(a) || a < 0) { | |
130 | + Type res; | |
131 | + res << NotANumber(); | |
132 | + return res; | |
133 | + } | |
134 | + return std::sqrt(a); | |
135 | + } | |
136 | + | |
137 | + template <typename Type> | |
138 | + std::vector<Type> root_square(std::vector<Type> a) { | |
139 | + std::vector<Type> res = a; | |
140 | + for (unsigned int i = 0; i < a.size(); ++i) { | |
141 | + res[i] = StatisticFunctions::root_square(a[i]); | |
142 | + } | |
143 | + return res; | |
144 | + } | |
145 | + | |
146 | + template <typename Type> | |
147 | + Tab2DData<Type> root_square(Tab2DData<Type> a) { | |
148 | + Tab2DData<Type> res = a; | |
149 | + for (int i = 0; i < a.getDim1Size(); ++i) { | |
150 | + for (int j = 0; j < a.getDim2Size(); ++j) { | |
151 | + res[i][j] = StatisticFunctions::root_square(a[i][j]); | |
152 | + } | |
153 | + } | |
154 | + return res; | |
155 | + } | |
156 | + | |
157 | + template <typename Type> | |
158 | + Type div(Type a, Type b) { | |
159 | + if (isNAN(a) || isNAN(b) || (b == 0)) { | |
160 | + Type res; | |
161 | + res << NotANumber(); | |
162 | + return res; | |
163 | + } | |
164 | + return a / b; | |
165 | + } | |
166 | + | |
167 | + template <typename Type> | |
168 | + std::vector<Type> div(std::vector<Type> a, std::vector<Type> b) { | |
169 | + std::vector<Type> res = a; | |
170 | + if (a.size() != b.size()) { | |
171 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); | |
172 | + } | |
173 | + for (unsigned int i = 0; i < a.size(); ++i) { | |
174 | + res[i] = StatisticFunctions::div(a[i], b[i]); | |
175 | + } | |
176 | + return res; | |
177 | + } | |
178 | + | |
179 | + template <typename Type> | |
180 | + Tab2DData<Type> div(Tab2DData<Type> a, Tab2DData<Type> b) { | |
181 | + Tab2DData<Type> res(a); | |
182 | + if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { | |
183 | + BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); | |
184 | + } | |
185 | + for (int i = 0; i < a.getDim1Size(); ++i) { | |
186 | + for (int j = 0; j < a.getDim2Size(); ++j) { | |
187 | + res[i][j] = StatisticFunctions::div(a[i][j], b[i][j]); | |
188 | + } | |
189 | + } | |
190 | + return res; | |
191 | + } | |
192 | + | |
193 | + template <typename Type> | |
194 | + std::pair<Type, Type> getMean(std::list<std::pair<Type, Type>> &list) { | |
195 | + std::pair<Type, Type> result(0, 0); | |
196 | + std::pair<int, int> counter(0, 0); | |
197 | + | |
198 | + for (auto elt : list) { | |
199 | + if (!isNAN(elt.first)) { | |
200 | + result.first += elt.first; | |
201 | + counter.first += 1; | |
202 | + } | |
203 | + if (!isNAN(elt.second)) { | |
204 | + result.second += elt.second; | |
205 | + counter.second += 1; | |
206 | + } | |
207 | + } | |
208 | + if (counter.first != 0) | |
209 | + result.first /= counter.first; | |
210 | + | |
211 | + if (counter.second != 0) | |
212 | + result.second /= counter.second; | |
213 | + | |
214 | + return result; | |
215 | + } | |
216 | + | |
217 | + template <typename Type> | |
218 | + std::pair<Type, Type> getStd(std::list<std::pair<Type, Type>> &list) { | |
219 | + std::pair<Type, Type> mean = getMean(list); | |
220 | + std::pair<Type, Type> result(0, 0); | |
221 | + int counter1 = 0; | |
222 | + int counter2 = 0; | |
223 | + for (auto elt : list) { | |
224 | + if (!isNAN(elt->first)) { | |
225 | + result.first += (elt->first - mean.first)*(elt->first - mean.first); | |
226 | + counter1 += 1; | |
227 | + } | |
228 | + if (!isNAN(elt->second)) { | |
229 | + result.second += (elt->second - mean.second)*(elt->second - mean.second); | |
230 | + counter2 += 1; | |
231 | + } | |
232 | + } | |
233 | + if (counter1 != 0) { | |
234 | + result.first /= counter1; | |
235 | + } else { | |
236 | + result.first << NotANumber(); | |
237 | + } | |
238 | + if (counter2 != 0) { | |
239 | + result.second /= counter2; | |
240 | + } else { | |
241 | + result.second << NotANumber(); | |
242 | + } | |
243 | + return std::sqrt(result); | |
244 | + } | |
245 | + | |
246 | + template <typename Type> | |
247 | + std::vector<Type>rankify(std::vector<Type>& X) { | |
248 | + | |
249 | + int N = X.size(); | |
250 | + | |
251 | + // Rank Vector | |
252 | + std::vector<Type> Rank_X(N); | |
253 | + | |
254 | + for (int i = 0; i < N; i++) { | |
255 | + int r = 1, s = 1; | |
256 | + | |
257 | + // Count no of smaller elements | |
258 | + // in 0 to i-1 | |
259 | + for (int j = 0; j < N; j++) { | |
260 | + if (X[j] < X[i]) r++; | |
261 | + if (X[j] == X[i] && i != j) s++; | |
262 | + } | |
263 | + | |
264 | + // Count no of smaller elements | |
265 | + // in i+1 to N-1 | |
266 | + /** | |
267 | + for (int j = i + 1; j < N; j++) { | |
268 | + if (X[j] < X[i]) r++; | |
269 | + if (X[j] == X[i]) s++; | |
270 | + }*/ | |
271 | + | |
272 | + // Use Fractional Rank formula | |
273 | + // fractional_rank = r + (n-1)/2 | |
274 | + Rank_X[i] = (Type) r + (s - 1) * 0.5; | |
275 | + } | |
276 | + | |
277 | + // Return Rank Vector | |
278 | + return Rank_X; | |
279 | + } | |
280 | + | |
281 | + template <typename Type> | |
282 | + bool getCovariance(std::list<std::pair<Type, Type>> &list, Type &result) { | |
283 | + if (list.empty()) { | |
284 | + return false; | |
285 | + } | |
286 | + std::pair<Type, Type> mean = getMean(list); | |
287 | + result = 0; | |
288 | + int counter = 0; | |
289 | + for (auto elt : list) { | |
290 | + if (!isNAN(elt.first) && !isNAN(elt.second)) { | |
291 | + result += (elt.first - mean.first)*(elt.second - mean.second); | |
292 | + counter += 1; | |
293 | + } | |
294 | + } | |
295 | + if (counter != 0) | |
296 | + result /= counter; | |
297 | + return true; | |
298 | + } | |
299 | + | |
300 | + template <typename Type> | |
301 | + bool getPearson(std::list<std::pair<Type, Type>> &list, Type &result) { | |
302 | + if (list.empty()) { | |
303 | + return false; | |
304 | + } | |
305 | + result = 0; | |
306 | + int counter = 0; | |
307 | + Type sum1 = 0, sum2 = 0; | |
308 | + Type sum1Sq = 0, sum2Sq = 0; | |
309 | + Type sum12 = 0; | |
310 | + for (auto elt : list) { | |
311 | + if (!isNAN(elt.first) && !isNAN(elt.second)) { | |
312 | + sum1 += elt.first; | |
313 | + sum2 += elt.second; | |
314 | + sum1Sq += elt.first * elt.first; | |
315 | + sum2Sq += elt.second * elt.second; | |
316 | + sum12 += elt.first * elt.second; | |
317 | + counter += 1; | |
318 | + } | |
319 | + } | |
320 | + if (counter > 0) | |
321 | + result = (counter * sum12 - sum1 * sum2) / | |
322 | + (std::sqrt((counter * sum1Sq - sum1 * sum1)*(counter * sum2Sq - sum2 * sum2))); | |
323 | + return true; | |
324 | + } | |
325 | + | |
326 | + template <typename Type> | |
327 | + bool getSpearman(std::list<std::pair<Type, Type>> &list, Type &result) { | |
328 | + if (list.empty()) { | |
329 | + return false; | |
330 | + } | |
331 | + | |
332 | + std::vector<Type> X, Y; | |
333 | + for (auto elt : list) { | |
334 | + if (!isNAN(elt.first) && !isNAN(elt.second)) { | |
335 | + X.push_back(elt.first); | |
336 | + Y.push_back(elt.second); | |
337 | + } | |
338 | + } | |
339 | + int n = X.size(); | |
340 | + if (n == 0) | |
341 | + return true; | |
342 | + std::vector<Type> rank_X = rankify(X); | |
343 | + std::vector<Type> rank_Y = rankify(Y); | |
344 | + std::list<std::pair<Type, Type>> rankList; | |
345 | + | |
346 | + for (int i = 0; i < n; i++) | |
347 | + rankList.push_back(std::make_pair(rank_X[i], rank_Y[i])); | |
348 | + | |
349 | + result = 0; | |
350 | + getPearson(rankList, result); | |
351 | + return true; | |
352 | + } | |
353 | + | |
354 | + template <typename Type> | |
355 | + bool getKendall(std::list<std::pair<Type, Type>> &list, Type &result) { | |
356 | + if (list.empty()) { | |
357 | + return false; | |
358 | + } | |
359 | + result = 0; | |
360 | + std::vector<Type> X, Y; | |
361 | + for (auto elt : list) { | |
362 | + if (!isNAN(elt.first) && !isNAN(elt.second)) { | |
363 | + X.push_back(elt.first); | |
364 | + Y.push_back(elt.second); | |
365 | + } | |
366 | + } | |
367 | + int n = X.size(); | |
368 | + if (n == 0) | |
369 | + return true; | |
370 | + std::vector<Type> rank_x = rankify(X); | |
371 | + std::vector<Type> rank_y = rankify(Y); | |
372 | + Type sum = 0; | |
373 | + for (int i = 0; i < n; i++) | |
374 | + sum = (Type) (rank_x[i] - rank_y[i]); | |
375 | + result = (Type) sum / (n * (n - 1)); | |
376 | + | |
377 | + return true; | |
378 | + } | |
379 | + | |
380 | + } /* namespace StatisticFunctions */ | |
381 | + } /* namespace Parameters */ | |
195 | 382 | } /* namespace AMDA */ |
196 | 383 | #endif /* TOOLBOX_HH_ */ | ... | ... |