Last active
August 6, 2020 07:50
-
-
Save kuznetsovin/6794f1f915bc64d29e91ff3e0c71c2c7 to your computer and use it in GitHub Desktop.
Exponential smoothing realization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <list> | |
const float ErrIncorrectAlpha = -1.0; | |
/* | |
Реализация молели экспоненциального сглаживания. | |
@param[in] time_series Временной ряд. | |
@param[in] alpha Коэффициент сглаживания между 0 и 1. | |
@param[in] s0 инициализированное первое значение S. | |
@return Прогнозное значение. | |
*/ | |
float exponential_smoothing(std::list<float> time_series, float alpha, float s0) { | |
if (alpha > 1 || alpha < 0) { | |
// если параметр alpha задан не корректно, возвращается ошибка | |
return ErrIncorrectAlpha; | |
} | |
float predict_value = s0; | |
float fact_value; | |
for(std::list<float>::iterator r = time_series.begin(); r != time_series.end(); r++){ | |
fact_value = *r; | |
predict_value = alpha * fact_value + (1 - alpha) * predict_value; | |
} | |
return predict_value; | |
} | |
int main() | |
{ | |
// тестовые значения | |
std::list<float> ts = {9325.18, 9043.94, 8441.49, 8504.89}; | |
float predict_value; | |
predict_value = exponential_smoothing(ts, 0.43, 9552.29); | |
if (predict_value == ErrIncorrectAlpha) { | |
std::cout << "Alpha must be between 0 and 1" << std::endl; | |
} else { | |
std::cout << "Predict value: " << predict_value << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment