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

700人阅读 评论(1)

Python代码如下:

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 13 11:20:10 2017

@author: DaiPuWei
"""

'''
时间序列简单平移法：以预测12月份的销售收入为例
'''

import math

def Forecast(profit,N,month):
'''
预测函数：profit为销售收入数据
N为预测周期，month为需要预测的月份
'''
result = []
if month >=1 and month <= 12:
#预测值
forecast= 0
for i in range(month-N-1,month-1):
forecast += profit[i]
forecast = forecast / 4
result.append(forecast)

#标准误差
error = 0
for i in range(N,len(profit)):
tmp = forecast - profit[i]
error = error + tmp * tmp
error = error / (len(profit) - N)
error = math.sqrt(error)
result.append(error)
else:
print('月份不正确，请重新输入月份(1-12)')
return result

def run_main():
'''
这是组函数
'''
#销售收入
profit = [553.8,574.6,606.9,649.8,705.1,772.0,816.4,892.7,963.9,1015.1,1102.7]

#以4为预测周期的预测值
forecast1 = Forecast(profit,4,12)

#以4为预测周期的预测值
forecast2 = Forecast(profit,5,12)

if forecast1[1] < forecast2[1]:
print('12月份的预测值为%g'%forecast1[0])
else:
print('12月份的预测值为%g'%forecast2[0])

if __name__ == '__main__':
run_main()

Python代码如下:

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 13 11:58:31 2017

@author: DaiPuWei
"""

'''
时间序列加权移动平均法:以预测1989年原煤产量为例
'''

import pandas as pd

def Weighted_Forecast(data,weight,N):
'''
data是统计数据
weight是权重
N是预测周期
index是要预测的下标
'''

#各个权重之和
sum_weight = sum(weight)

#预测值
forecast  = 0
length = len(data)

#预测年份
year = data.index[length-1]+1

j = 0
for i in range(length-N,length):
forecast = forecast + weight[j] * data[data.columns[0]][data.index[i]]
j = j + 1
forecast = forecast / sum_weight
print('%d年原煤产量未校对的预测值为:'%year)
print(forecast)

#data中数据的预测值
forecast_data = []
for i in range(N):
forecast_data.append(data[data.columns[0]][data.index[i]])
for i in range(N,length):
tmp = 0
k = 0
for j in range(i-N,i):
tmp = tmp + data[data.columns[0]][data.index[j]]*weight[k]
k = k + 1
forecast_data.append(tmp/sum_weight)
print('所有的预测值为:')
for i in range(length):
print(data.index[i],forecast_data[i])

#相对误差
error_rate = []
for i in range(length):
error_rate.append(1-forecast_data[i]/data[data.columns[0]][data.index[i]])
sum_error = sum(error_rate)
sum_data = sum(data[data.columns[0]][data.index[N:]])
avg_rate = 0

#校正预测值
forecast = forecast/(1-avg_rate)

return forecast

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

#读取数据
data = pd.read_excel('E:\\Program Files (x86)\\大学数学\\算法大全pdf\\第24章   时间序列模型\\原煤产量统计.xlsx')

#周期数
N = 3

#权重
weight = []
for i in range(N):
weight.append(i+1)

forecast = Weighted_Forecast(data,weight,N)
length = len(data)
year = data.index[length-1]+1
print('%d年的原煤产量预测值为:'%year)
print(forecast)

if __name__ == '__main__':
run_main()

Python代码如下:

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 13 17:56:56 2017

@author: DaiPuWei
"""

'''
时间序列趋势移动平均法，以1965-1985年发电总量为数据集合预测1986年与1987年
的发电总量
'''

import pandas as pd

def Forecast(data,N):
'''
预测函数：data为销售收入数据
N为预测周期,
'''
forecast_data = []
length = len(data)
for i in range(N,length):
tmp = 0
for j in range(i-N,i):
tmp = tmp + data[j]
tmp = tmp / N
forecast_data.append(tmp)
result = forecast_data[len(forecast_data)-1]

return result,forecast_data

def Model(data,N,year,init):
'''
模型为:y(t+T) = a + b * T
a = 2 * M1 - M2
b = 2* (M1 - M2) / (N - 2)
'''

#一次移动平均的预测值以及预测数据
M1,first_forecast = Forecast(data,N)

#二次移动平均的预测值以及预测数据
M2,Second_forecast = Forecast(first_forecast,N)

#趋势移动平均模型参数a,b
a = 2 * M1 - M2
b = 2 *(M1 - M2) / (N - 2)
print('预测模型为：y(T) = ',a,'+',b,'*(T-',init,')')

#预测值
forecast = a + b * (year -init)

return forecast

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

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

#预测周期
N = 6

#初始年份
init = sample['年份'][len(sample)-1]

#1986年预测值
forecast_1986 = Model(data,N,1986,init)
print('1986年发电总量的预测值为:%f'%forecast_1986)

#1987年预测值
forecast_1987 = Model(data,N,1987,init)
print('1987年发电总量的预测值为:%f'%forecast_1987)

if __name__ == '__main__':
run_main()

1
0

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