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