Skip to content

Instantly share code, notes, and snippets.

@kuznetsovin
Last active August 6, 2020 07:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuznetsovin/6794f1f915bc64d29e91ff3e0c71c2c7 to your computer and use it in GitHub Desktop.
Save kuznetsovin/6794f1f915bc64d29e91ff3e0c71c2c7 to your computer and use it in GitHub Desktop.
Exponential smoothing realization
#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