ts-aggregator/project_template/Preparator.cpp
2022-12-13 12:36:06 +04:00

181 lines
4.8 KiB
C++

#include "stdafx.h"
#include "Preparator.h"
#include <iostream>
#include <math.h>
using namespace std;
int Preparator::sgn(double val){
if (val > 0)
return 1;
if (val == 0)
return 0;
return -1;
}
void Preparator::setLog(bool log){
IsLogarithm = log;
}
void Preparator::setBoxCox(bool boxcox){
IsBoxCox = boxcox;
}
void Preparator::setBoxCoxGamma(double gamma){
if (gamma > 0 && gamma < 1){
boxCoxGamma = gamma;
}
}
vector<double> Preparator::boxcox(vector<double> timeSeria){
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] = (pow(timeSeria[i], boxCoxGamma) - 1) / boxCoxGamma;
}
return timeSeria;
}
vector<double> Preparator::deboxcox(vector<double> timeSeria){
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] = sgn(boxCoxGamma * timeSeria[i] + 1) *
pow((abs(boxCoxGamma * timeSeria[i] + 1)), 1 / boxCoxGamma);
}
return timeSeria;
}
Preparator::Preparator(bool _IsLogarithm, bool _IsBoxCox){
IsBoxCox = _IsBoxCox;
IsLogarithm = _IsLogarithm;
}
vector<double> Preparator::logarithm(vector<double> timeSeria){
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] = log(timeSeria[i]);
}
return timeSeria;
}
vector<double> Preparator::delogarithm(vector<double> timeSeria){
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] = exp(timeSeria[i]);
}
return timeSeria;
}
vector<double> Preparator::shift(vector<double> timeSeria){
double timeSeriaShiftValue = timeSeria[0];
for (int i = 0; i < timeSeria.size(); i++){
if (timeSeria[i] < timeSeriaShiftValue && timeSeria[i] < 0){
timeSeriaShiftValue = timeSeria[i];
}
}
if (timeSeriaShiftValue < 0){
timeSeriaShiftValue = fabs(timeSeriaShiftValue);
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] += timeSeriaShiftValue;
}
}
return timeSeria;
}
vector<double> Preparator::deShift(vector<double> timeSeria){
if (timeSeriaShiftValue < 0){
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] -= timeSeriaShiftValue;
}
}
return timeSeria;
}
vector<double> Preparator::normalizeTimeSeria(vector<double> timeSeria){
timeSeria = shift(timeSeria);
if (IsLogarithm)
timeSeria = logarithm(timeSeria);
if (IsBoxCox)
timeSeria = boxcox(timeSeria);
maxTimePoint = timeSeria[0];
for (int i = 0; i < timeSeria.size(); i++){
if (timeSeria[i] > maxTimePoint)
maxTimePoint = timeSeria[i];
}
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] /= maxTimePoint;
}
return timeSeria;
}
vector<double> Preparator::deNormalizeTimeSeria(vector<double> timeSeria){
//shift(timeSeria);
for (int i = 0; i < timeSeria.size(); i++){
timeSeria[i] *= maxTimePoint;
}
if (IsLogarithm)
timeSeria = delogarithm(timeSeria);
if (IsBoxCox)
timeSeria = deboxcox(timeSeria);
deShift(timeSeria);
return timeSeria;
}
//bool Preparator::TestCommon(vector<double> timeSeria, vector<double>(*direct)(vector<double>),
// vector<double>(*inverse)(vector<double>)){
// vector<double> initTimeSeria = timeSeria;
// timeSeria = direct(inverse(timeSeria));
// double res = 0;
// for (int i = 0; i < timeSeria.size(); i++){
// res += initTimeSeria[i] - timeSeria[i];
// }
// return res == 0;
//}
double Preparator::TestPreparator(vector<double> timeSeria){
double resLog = TestLogarithm(timeSeria);
cout << "log func works with " << resLog << " error" << endl;
double resBoxCox = TestBoxCox(timeSeria);
cout << "BoxCox func works with " << resBoxCox << " error" << endl;
double resShift = TestShift(timeSeria);
cout << "Shift func works with " << resShift << " error" << endl;
double resNormalize = TestNormalize(timeSeria);
cout << "Normalize func works with " << resShift << " error" << endl;
return resNormalize + resLog + resBoxCox + resShift;
}
double Preparator::TestLogarithm(vector<double> timeSeria){
vector<double> initTimeSeria = timeSeria;
timeSeria = delogarithm(logarithm(timeSeria));
double res = 0;
for (int i = 0; i < timeSeria.size(); i++){
res += initTimeSeria[i] - timeSeria[i];
}
return res;
}
double Preparator::TestBoxCox(vector<double> timeSeria){
vector<double> initTimeSeria = timeSeria;
timeSeria = deboxcox(boxcox(timeSeria));
double res = 0;
for (int i = 0; i < timeSeria.size(); i++){
res += initTimeSeria[i] - timeSeria[i];
}
return res;
}
double Preparator::TestShift(vector<double> timeSeria){
vector<double> initTimeSeria = timeSeria;
timeSeria = deboxcox(boxcox(timeSeria));
double res = 0;
for (int i = 0; i < timeSeria.size(); i++){
res += initTimeSeria[i] - timeSeria[i];
}
return res;
}
double Preparator::TestNormalize(vector<double> timeSeria){
timeSeriaShiftValue = 0;
maxTimePoint = 0;
IsLogarithm = false;
IsBoxCox = false;
boxCoxGamma = 0.5;
vector<double> initTimeSeria = timeSeria;
timeSeria = deboxcox(boxcox(timeSeria));
double res = 0;
for (int i = 0; i < timeSeria.size(); i++){
res += initTimeSeria[i] - timeSeria[i];
}
return res;
}