前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >时间序列(二)

时间序列(二)

作者头像
AI那点小事
发布2020-04-20 18:21:54
7410
发布2020-04-20 18:21:54
举报
文章被收录于专栏:AI那点小事

一次指数平滑法Python代码如下:

代码语言:javascript
复制
# -*- 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()

二次指数平滑法Python代码如下:

代码语言:javascript
复制
# -*- 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()

三次指数平滑法Python代码如下:

代码语言:javascript
复制
# -*- 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()

一次差分指数平滑法Python代码如下:

代码语言:javascript
复制
# -*- 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()

二阶差分指数平滑法

代码语言:javascript
复制
# -*- 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()

自适应滤波法Python代码如下:

代码语言:javascript
复制
# -*- 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

def Adaptive_Filtering(data,weight,error,N,K,index):
    '''
        一次自适应滤波预测
        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

def Adaptive_Filtering_Model(data,weight,error,N,K):
    """
        时间序列自适应滤波法模型
    """    

    #误差的最大值
    weight,error = Adaptive_Filtering(data,weight,error,N,K,N)
    error_max = max(error)

    #开始进行自适应滤波法,进行迭代修改数据
    '''
        第一次循环单独考虑
    '''
    for i in range(N+1,len(data)):
        weight,error = Adaptive_Filtering(data,weight,error,N,K,i)
    error_max = max(error)
    while error_max > 0.00001:
        for i in range(N,len(data)):
            weight,error = Adaptive_Filtering(data,weight,error,N,K,i)
        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')
    #模型运用
    Adaptive_Filtering_Model(data,weight,error,N,K)


if __name__ == '__main__':
    run_main()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档