# [置顶] 时间序列(二)

592人阅读 评论(1)

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 14 11:57:34 2017

@author: DaiPuWei
"""
"""
时间序列一次指数平移法，以电器销售额的预测为例
"""

import pandas as pd
import math

def Index_Translation(data,alpha):
"""
一次指数平移法函数
data是样本数据
alpha为加权系数
"""

#数据数组长度
length = len(data)
forecast_data = []

#预测初值去顶
tmp = 0
if length > 20:
tmp = data[0]
else:
for i in range(2):
tmp = tmp + data[i]
tmp = tmp / 2
forecast_data.append(tmp)

#计算预测数据
for i in range(1,length):
tmp = 0
tmp = alpha * data[i-1] + (1-alpha) * forecast_data[i-1]
forecast_data.append(tmp)

#相对误差
error = 0
for i in range(length):
error = error + (data[i] - forecast_data[i]) * (data[i] - forecast_data[i])
error = error * 1.0 / (length - 2)
error = math.sqrt(error)

return error,forecast_data

def Index_Translation_Model(data,alpha):
"""
这是一次指数平移模型
data是样本数据
alpha为加权系数数组
forecast是需要预测的指标年份
"""

#把数预测据按相对误差进行装饰
tmp = []
for i in range(len(alpha)):
error = 0
forecast_data = []
error,forecast_data = Index_Translation(data,alpha[i])
tmp.append((error,alpha[i],forecast_data))

#按相对误差从小到大排序
tmp.sort(reverse = False)

#获取相关信息
t = tmp[0]
Alpha = t[1]
forecast_data = t[2]

#对要预测的数据进行预测
length = len(data)
last = data[length-1]
_forecast = forecast_data[length-1]
forecast = Alpha * last + (1-Alpha) * _forecast

return forecast

def run_main():
'''
这是主是函数
'''

#读取文件中的样例
sample = pd.read_excel('E:\\Program Files (x86)\\大学数学\\算法大全pdf\\第24章   时间序列模型\\电器销售额.xlsx')
data = sample[sample.columns[1]]
year = sample[sample.columns[0]]

#加权系数
alpha = []
for i in range(10):
alpha.append((i+1)*1.0/10)

#预测结果
forecast_result = Index_Translation_Model(data,alpha)
print(year[len(year)-1]+1,'年的电器销售额预测值为:',forecast_result)

if __name__ == '__main__':
run_main()

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 14 13:37:09 2017

@author: DaiPuWei
"""

"""
时间序列二次指数平滑法，以发电量预测为例
"""

import pandas as pd
import math

def Index_Translation(data,alpha):
"""
指数平移法函数
data是样本数据
alpha为加权系数
"""

#数据数组长度
length = len(data)
forecast_data = []

#预测初值去顶
tmp = 0
if length > 20:
tmp = data[0]
else:
for i in range(2):
tmp = tmp + data[i]
tmp = tmp / 2
forecast_data.append(tmp)

#计算预测数据
for i in range(1,length):
tmp = 0
tmp = alpha * data[i-1] + (1-alpha) * forecast_data[i-1]
forecast_data.append(tmp)

return forecast_data

def Second_Index_Translation_Model(data,alpha,year,forecast_year):
"""
这是一次指数平移模型
data是样本数据
alpha为加权系数
forecast是需要预测的指标年份
"""

#一次指数平滑
forecast_data1 = Index_Translation(data,alpha)

#二次指数平滑
forecast_data2 = Index_Translation(forecast_data1,alpha)

#对要预测的数据进行预测
A = 2 * forecast_data1[-1] - forecast_data2[-1]
B = alpha *(forecast_data1[-1] - forecast_data2[-1]) / (1 - alpha)
first_year = year[len(year)-1]
print('二次指数平滑模型为:')
print('Y(t)=',A,'+',B,'(T-',first_year,')')

forecast = A + B * (forecast_year-first_year)

return forecast

def run_main():
'''
这是主函数
'''

#读取数据集
sample = pd.read_excel('E:\\Program Files (x86)\\大学数学\\算法大全pdf\\第24章   时间序列模型\\1965-1985年发电总量统计.xlsx')
data = sample['发电总量']
year = sample['年份']

#加权系数
alpha = 0.3

#模型预测
forecast_year = 1986
forecast = Second_Index_Translation_Model(data,alpha,year,forecast_year)
print(forecast_year,'年的发电总量的预测值为',forecast)

forecast_year = 1987
forecast = Second_Index_Translation_Model(data,alpha,year,forecast_year)
print(forecast_year,'年的发电总量的预测值为',forecast)

if __name__ == '__main__':
run_main()

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 14 17:34:30 2017

@author: DaiPuWei
"""

"""
时间序列三次指数平滑法，以固定资产他投资总额预测为例
"""

import pandas as pd

def Index_Translation(data,alpha):
"""
指数平移法函数
data是样本数据
alpha为加权系数
"""

#数据数组长度
length = len(data)
forecast_data = []

#预测初值去顶
tmp = 0
if length > 20:
tmp = data[0]
else:
for i in range(3):
tmp = tmp + data[i]
tmp = tmp / 3
forecast_data.append(tmp)

#计算预测数据
for i in range(1,length):
tmp = 0
tmp = alpha * data[i-1] + (1-alpha) * forecast_data[i-1]
forecast_data.append(tmp)

return forecast_data

def Third_Index_Translation_Model(data,alpha,year,forecast_year):
"""
这是一次指数平移模型
data是样本数据
year是样本年份
alpha为加权系数
forecast是需要预测的指标年份
"""

#一次指数平滑
forecast_data1 = Index_Translation(data,alpha)

#二次指数平滑
forecast_data2 = Index_Translation(forecast_data1,alpha)

#三次指数平滑
forecast_data3 = Index_Translation(forecast_data2,alpha)

#对要预测的数据进行预测
A = 3 * forecast_data1[-1] - 3 * forecast_data2[-1] + forecast_data3[-1]
B = alpha *((6 - 5 * alpha) * forecast_data1[-1] - 2*(5 - 4 * alpha) * forecast_data2[-1] + (4 - 3 * alpha) * forecast_data3[-1]) / (2*(1 - alpha)*(1 - alpha))
C = alpha * alpha * (forecast_data1[-1] - 2 * forecast_data2[-1] + forecast_data3[-1])
first_year = year[len(year)-1]
print('二次指数平滑模型为:')
print('Y(t)=',A,'+',B,'(T-',first_year,')+',C,'*(T-',first_year,')^2')

forecast = A + B * (forecast_year-first_year) + C * (forecast_year-first_year) * (forecast_year-first_year)

return forecast

def run_main():
"""
这是主函数
"""

#读取数据集
sample = pd.read_excel('E:\\Program Files (x86)\\大学数学\\算法大全pdf\\第24章   时间序列模型\\固定资产总额.xlsx')
data = sample[sample.columns[1]]
year = sample[sample.columns[0]]

#加权系数
alpha = 0.3

#模型预测
forecast_year = 1989
forecast = Third_Index_Translation_Model(data,alpha,year,forecast_year)
print(forecast_year,'年的发电总量的预测值为',forecast)

forecast_year = 1990
forecast = Third_Index_Translation_Model(data,alpha,year,forecast_year)
print(forecast_year,'年的发电总量的预测值为',forecast)

if __name__ == '__main__':
run_main()

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 14 19:58:25 2017

@author: DaiPuWei
"""

"""
时间序列一阶差分指数平滑法，以锅炉燃料消耗量预测为例
"""

import pandas as pd

def Difference_Index(data,alpha):
'''
一阶差分函数
data是样本数据
alpha为加权系数
'''

#差分数值
length = len(data)
first_difference = []
for i in range(length-1):
difference = data[i+1] - data[i]
first_difference.append(difference)

#差分指数平滑
forecast_difference = []
forecast_difference.append(first_difference[0])
for i in range(1,len(first_difference)):
forecast = alpha * first_difference[i-1] + (1-alpha) * forecast_difference[i-1]
forecast_difference.append(forecast)
forecast = forecast_difference[-1] + data[len(data)-1]

return forecast

def First_Difference_Index_Model(data,year,alpha):
"""
一阶差分指数平滑法模型
"""

#模型预测
forecast = Difference_Index(data,alpha)
print(year[len(year)-1]+1,'年的锅炉燃料消耗量预测值为:',forecast)

def run_main():
'''
这是主函数
'''

#读取数据集
sample = pd.read_excel('E:\\Program Files (x86)\\大学数学\\算法大全pdf\\第24章   时间序列模型\\锅炉燃料消耗量.xlsx')
data = sample[sample.columns[1]]
year = sample[sample.columns[0]]

#加权系数
alpha = 0.4

#一阶差分指数平滑法预测
First_Difference_Index_Model(data,year,alpha)

if __name__ == '__main__':
run_main()

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 14 20:06:47 2017

@author: DaiPuWei
"""

"""
时间序列二阶差分指数平滑法，以锅炉燃料消耗量预测为例
"""

import pandas as pd

def Difference_Index(data,alpha):
'''
差分函数
data是样本数据
alpha为加权系数
'''

#差分数值
length = len(data)
first_difference = []
for i in range(length-1):
difference = data[i+1] - data[i]
first_difference.append(difference)

#差分指数平滑
forecast_difference = []
forecast_difference.append(first_difference[0])
for i in range(1,len(first_difference)):
forecast = alpha * first_difference[i-1] + (1-alpha) * forecast_difference[i-1]
forecast_difference.append(forecast)

return forecast_difference

def Second_Difference_Index_Model(data,year,alpha):
"""
一阶差分指数平滑法模型
"""

#一阶差分指数平滑
forecast_difference1 = Difference_Index(data,alpha)

#二阶差分指数平滑
forecast_difference2 = Difference_Index(forecast_difference1,alpha)

#模型预测
forecast = forecast_difference1[-1] + forecast_difference2[-1] + data[len(data)-1]
print(year[len(year)-1]+1,'年的锅炉燃料消耗量预测值为:',forecast)

def run_main():
'''
这是主函数
'''

#读取数据集
sample = pd.read_excel('E:\\Program Files (x86)\\大学数学\\算法大全pdf\\第24章   时间序列模型\\锅炉燃料消耗量.xlsx')
data = sample[sample.columns[1]]
year = sample[sample.columns[0]]

#加权系数
alpha = 0.4

#二阶差分指数平滑法预测
Second_Difference_Index_Model(data,year,alpha)

if __name__ == '__main__':
run_main()

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 15 10:16:52 2017

@author: DaiPuWei
"""

"""
时间序列自适应滤波法
"""

def Forecast(data,weight,N,index):
'''
对数据进行预测
'''

tmp = []
forecast = 0
for i in range(index-N,index):
tmp.append(data[i])
tmp = tmp[::-1]
for i in range(len(tmp)):
forecast = forecast +tmp[i] * weight[i]

return forecast,tmp

'''
一次自适应滤波预测
data是样本数据
weight是权值数组
error为误差数组
N是权值数组长度
K为学习系数
index是需要预测数据的下标
'''

#进行预测
forecast ,tmp = Forecast(data,weight,N,index)

#进行数据修正
_error = data[index] - forecast
error[index] = _error
#data[index] = forecast
for i in range(N):
weight[i] = weight[i] + 2 *K * error[index] * tmp[i]

return weight,error

"""
时间序列自适应滤波法模型
"""

#误差的最大值
error_max = max(error)

#开始进行自适应滤波法，进行迭代修改数据
'''
第一次循环单独考虑
'''
for i in range(N+1,len(data)):
error_max = max(error)
while error_max > 0.00001:
for i in range(N,len(data)):
error_max = max(error)

#对数据进行预测
forecast,tmp = Forecast(data,weight,N,len(data))
j = len(data)+1
print('第',j,'期数据预测值为:',forecast)

def run_main():
'''
这是主函数
'''

#10个测试数据
data = []
for i in range(10):
tmp = (i + 1)* 1.0 /10
data.append(tmp)

#权值以及权值个数
weight = [0.5,0.5]
N = len(weight)

#学习系数
K = 0.9

#误差数组
error = []
for i in range(len(data)):
error.append(0)

print('权值为:',weight)
print('数据为:',data,'\n')
#模型运用

if __name__ == '__main__':
run_main()

1
0

个人资料
• 访问：155551次
• 积分：4927
• 等级：
• 排名：第6843名
• 原创：329篇
• 转载：0篇
• 译文：0篇
• 评论：80条
博客专栏
 数据结构与算法 文章：71篇 阅读：32636
 面试在线编程专栏 文章：71篇 阅读：31064
 数学建模专栏 文章：11篇 阅读：31980
 机器学习数据挖掘笔记 文章：10篇 阅读：10067
最新评论