欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > python实现三次指数平滑法(二次指数平滑预测算法)

python实现三次指数平滑法(二次指数平滑预测算法)

2024/10/24 13:25:37 来源:https://blog.csdn.net/lm_is_dc/article/details/141439152  浏览:    关键词:python实现三次指数平滑法(二次指数平滑预测算法)

python实现三次指数平滑法(二次指数平滑预测算法)

1、简介

1、指数平滑法是一种常用的时间序列预测算法,有一次、二次和三次平滑,通过加权系数来调整历史数据权重;
2、主要思想是:预测值是以前观测值的加权和,且对不同的数据给予不同的权数,新数据给予较大的权数,旧数据给予较小的权数
3、一次平滑适用于无明显趋势的数据;
4、二次平滑用于修正线性趋势;
5、三次平滑处理二次曲线趋势;
6、加权系数a的大小影响预测的敏感性和平滑程度。

2、应用

应用:中短期经济发展趋势预测,股票趋势预测,成绩预测,信号变化预测,产品销量预测,时间序列预测。

3、代码

# -*- coding: utf-8 -*-"""
@contact: 微---信 1257309054
@file: test.py
@time: 2024/8/22 21:46
@author: LDC
"""def get_once_predict(history_value, smoothed_value, alpha, decimal_point=0):'''获取平滑后预测值:param history_value: 历史值:param smoothed_value: 平滑值:param alpha: 加权系数:param decimal_point: 小数点位数:return:'''if decimal_point == 0:return round(alpha * history_value + (1 - alpha) * smoothed_value)return round(alpha * history_value + (1 - alpha) * smoothed_value, decimal_point)def once_smoothing(history_data, alpha, s0=None, decimal_point=0):'''预测一次指数平滑数据:param history_data: 历史数据:param alpha: 加权系数:param s0: 初始值:param decimal_point: 小数点位数:return: 一次平滑结果'''history_data_len = len(history_data)if s0 == None:# 如果不给定始值,就取前三个值均值if 3 < history_data_len:s0 = sum(history_data[:3]) / 3elif 1 < history_data_len:s0 = history_data[0]else:return history_datas = [s0]smoothed_value = s0for data in history_data:smoothed_value = get_once_predict(data, smoothed_value, alpha, decimal_point=decimal_point)s.append(smoothed_value)return sdef second_smoothing(once_data, alpha, decimal_point=0):'''预测二次指数平滑数据:param once_data: 一次指数平滑后结果:param alpha: 加权系数:return: 二次平滑结果'''second_data = once_smoothing(once_data[1:], alpha, once_data[0], decimal_point)return second_datadef get_second_predict_n(n, s1, s2, alpha, decimal_point=0):'''获取二次指数平滑第n个预测值:param n: 未来第n个值:param s1: 一次指数平滑最后的值:param s2: 二次指数平滑最后的值:param alpha: 加权系数:param decimal_point: 小数点位数:return: 第n个预测值'''a = 2 * s1 - s2b = alpha / (1 - alpha) * (s1 - s2)# y = round(a + b * n, 2)if decimal_point == 0:y = round(a + b * n)else:y = round(a + b * n, decimal_point)return ydef third_smoothing(second_data, alpha, s3, decimal_point=0):'''预测三次指数平滑数据:param second_data: 二次指数平滑后结果:param alpha: 加权系数:param decimal_point: 小数点位数:return:'''second_data[0] = s3second_data = second_smoothing(second_data, alpha, decimal_point)return second_datadef get_third_predict_n(n, s1, s2, s3, alpha, decimal_point=0):'''获取三次平滑第n个预测值:param n: 未来第n个:param s1: 一次指数平滑最后的值:param s2: 二次指数平滑最后的值:param s2: 三次指数平滑最后的值:param alpha: 加权系数:param decimal_point: 小数点位数:return: 第n个预测值'''a = round(3 * s1 - 3 * s2 + s3, 2)b = round((alpha / (2 * pow(1 - alpha, 2))) * ((6 - 5 * alpha) * s1 - 2 * (5 - 4 * alpha) * s2 + (4 - 3 * alpha) * s3), 2)c = round(pow(alpha, 2) / (2 * pow(1 - alpha, 2)) * (s1 - 2 * s2 + s3), 2)if decimal_point == 0:y = round(a + b * n + c * pow(n, 2))else:y = round(a + b * n + c * pow(n, 2), decimal_point)print('非线性预测模型的系数a,b,c', a, b, c)return yif __name__ == '__main__':history_data = [225.2, 249.9, 263.2, 293.6, 318.9, 356.7, 363.3, 424.2, 466.5, 582.0, 750.0]alpha = 0.3# # 一次指数平滑s0 = round(sum(history_data[:3]) / 3, 1)decimal_point = 1  # 1位小数点once_data = once_smoothing(history_data, alpha, s0, decimal_point)print('α为0.3的一次平滑指数结果', once_data)# 二次指数平滑second_data = second_smoothing(once_data, alpha, decimal_point)print('α为0.3的二次平滑指数结果', second_data)# 三次指数平滑s3 = 244.5  # 三次平滑初始值third_data = third_smoothing(second_data, alpha, s3, decimal_point)print('α为0.3的三次平滑指数结果', third_data)next_1_value = get_third_predict_n(1, once_data[-1], second_data[-1], third_data[-1], alpha, decimal_point=0)print('2007年销量', next_1_value)next_2_value = get_third_predict_n(2, once_data[-1], second_data[-1], third_data[-1], alpha, decimal_point=0)print('2008年销量', next_2_value)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com