python 二次平滑_Python 指数平滑

# -*- coding: utf-8 -*-

'''

Exponential smooth(指数平滑)的手工实现(无第三方库)

Author : Kabuto_hui

Date : 2018.04.19

'''

#指数平滑算法

def exponential_smoothing(alpha, s):

'''

一次指数平滑

:param alpha: 平滑系数

:param s: 数据序列, list

:return: 返回一次指数平滑模型参数, list

'''

s_temp = [0 for i in range(len(s))]

s_temp[0] = ( s[0] + s[1] + s[2] ) / 3

for i in range(1, len(s)):

s_temp[i] = alpha * s[i] + (1 - alpha) * s_temp[i-1]

return s_temp

def compute_single(alpha, s):

'''

一次指数平滑

:param alpha: 平滑系数

:param s: 数据序列, list

:return: 返回一次指数平滑模型参数, list

'''

return exponential_smoothing(alpha, s)

def compute_double(alpha, s):

'''

二次指数平滑

:param alpha: 平滑系数

:param s: 数据序列, list

:return: 返回二次指数平滑模型参数a, b, list

'''

s_single = compute_single(alpha, s)

s_double = compute_single(alpha, s_single)

a_double = [0 for i in range(len(s))]

b_double = [0 for i in range(len(s))]

for i in range(len(s)):

a_double[i] = 2 * s_single[i] - s_double[i] #计算二次指数平滑的a

b_double[i] = (alpha / (1 - alpha)) * (s_single[i] - s_double[i]) #计算二次指数平滑的b

return a_double, b_double

def compute_triple(alpha, s):

'''

三次指数平滑

:param alpha: 平滑系数

:param s: 数据序列, list

:return: 返回三次指数平滑模型参数a, b, c, list

'''

s_single = compute_single(alpha, s)

s_double = compute_single(alpha, s_single)

s_triple = exponential_smoothing(alpha, s_double)

a_triple = [0 for i in range(len(s))]

b_triple = [0 for i in range(len(s))]

c_triple = [0 for i in range(len(s))]

for i in range(len(s)):

a_triple[i] = 3 * s_single[i] - 3 * s_double[i] + s_triple[i]

b_triple[i] = (alpha / (2 * ((1 - alpha) ** 2))) * ((6 - 5 * alpha) * s_single[i] - 2 * ((5 - 4 * alpha) * s_double[i]) + (4 - 3 * alpha) * s_triple[i])

c_triple[i] = ((alpha ** 2) / (2 * ((1 - alpha) ** 2))) * (s_single[i] - 2 * s_double[i] + s_triple[i])

return a_triple, b_triple, c_triple

if __name__ == "__main__":

alpha = 0.8

data = [i for i in range(100)]

sigle = compute_single(alpha, data)

print(alpha * data[-1] + (1 - alpha) * sigle[-1])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值