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

110 lines
2.9 KiB
C++

//
// Ìåòîä ýêñïîíåíöèàëüíîãî ñãëàæèâàíèÿ è ïðîãíîçèðîâàíèÿ:
// áåç òðåíäà, áåç ñåçîííîñòè
//
#include "StdAfx.h"
#include <iostream>
#include "NoTrendNoSeasonality.h"
#include "Aic.h"
#include "Param.h"
// êîíñòðóêòîð ñ çàäàííûìè íåïîñðåäñòâåííî ïàðàìåòðàìè
NoTrendNoSeasonality::NoTrendNoSeasonality(vector<double> timeSeries, int countPointForecast) {
this->x = timeSeries;
this->countPointForecast = countPointForecast;
this->partition();
}
NoTrendNoSeasonality::~NoTrendNoSeasonality() {
// îñâîáîæäàåòñÿ ïàìÿòü
std::vector<double> ().swap(S);
std::vector<double> ().swap(x);
std::vector<double> ().swap(forecast);
}
// èíèöèàëèçàöèÿ ìîäåëè, çàäàíèå ïåðâîíà÷àëüíûõ çíà÷åíèé
void NoTrendNoSeasonality::init() {
S.clear();
forecast.clear();
S.push_back(x[0]);
forecast.push_back(S[0]);
}
// ìåòîä çàäàíèÿ ïàðàìåòðîâ
void NoTrendNoSeasonality::setParam(string paramName, double value) {
if (paramName.compare("alpha") == 0) {
this->alpha = value;
}
}
// ñôîðìèðîâàòü ìîäåëü
void NoTrendNoSeasonality::createModel() {
this->init(); // èíèöèàëèçèðîâàòü ìîäåëü
double e = 0;
//âûïîëíÿåòñÿ ïðîõîä ìîäåëè ïî ñãëàæèâàíèþ è ïðîãíîçèðîâàíèþ countPointForecast òî÷åê
for (unsigned int t = 0; t < x.size()-1 + this->countPointForecast; t++) {
// ïîêà íå äîøëè äî êîíöà ðÿäà - ñãëàæèâàåì, èíà÷å ñòðîèì ïðîãíîç
if (t < x.size()) {
e = x[t]-forecast[t];
} else {
e = 0;
}
S.push_back(S[t]+alpha * e); // óðîâåíü
forecast.push_back(S[t+1]); // ïðîãíîç
}
}
// ñôîðìèðîâàòü ìîäåëü
void NoTrendNoSeasonality::createModelForEstimation() {
this->init(); // èíèöèàëèçèðîâàòü ìîäåëü
double e = 0;
//âûïîëíÿåòñÿ ïðîõîä ìîäåëè ïî ñãëàæèâàíèþ è ïðîãíîçèðîâàíèþ countPointForecast òî÷åê
for (unsigned int t = 0; t < xLearning.size()-1 + this->countPointForecast; t++) {
// ïîêà íå äîøëè äî êîíöà ðÿäà - ñãëàæèâàåì, èíà÷å ñòðîèì ïðîãíîç
if (t < xLearning.size()) {
e = xLearning[t]-forecast[t];
} else {
e = 0;
}
S.push_back(S[t]+alpha * e); // óðîâåíü
forecast.push_back(S[t+1]); // ïðîãíîç
}
}
// ìåòîä ïîëó÷åíèÿ ïðîãíîçà
vector<double> NoTrendNoSeasonality::getForecast() {
vector<double> result;
for (unsigned int i = forecast.size() - countPointForecast; i < forecast.size(); i++) {
result.push_back(forecast[i]);
}
return result;
}
// ìåòîä ïîëó÷åíèÿ îöåíêè ìîäåëè
double NoTrendNoSeasonality::calcEstimation(Aic *aic) {
return aic->getValue(2, this->xEstimation, this->getForecast());
}
// ìåòîä ïîëó÷åíèÿ îïòèìèçèðîâàííîãî çíà÷åíèÿ îäíîãî ïàðàìåòðà
// TODO: ðåàëèçîâàòü
Param* NoTrendNoSeasonality::optimize(Estimation *est) {
Param *optimal = new Param();
double minSmape = 99999;
for (double al = 0.1; al < 1; al+= 0.01) {
this->setParam("alpha", al);
this->createModelForEstimation();
double smapeValue = est->getValue(getXEstimation(), getForecast());
if (minSmape > smapeValue) {
minSmape = smapeValue;
optimal->alpha = al;
}
}
return optimal;
}