文章目录

  • 1.定义
  • 2.移动平均法、指数平滑法和季节模型
  • 1.移动平均法
  • 2.二次移动平均法及趋势移动平均法



时间序列分类 机器学习 时间序列怎么分类_移动平均法

1.定义

时间序列是按时间顺序排列的、随时间变化且相互关联的数据序列。对时间序列进行观察研究,找寻它的发展规律,预测它将来的走势就是时间序列分析。

时间序列根据所研究的依据不同,可有不同的分类。

  1. 按所研究的对象的多少分,有一元时间序列和多元时间序列.
  2. 按时间的连续性可将时间序列分为离散时间序列和连续时间序列两种.
  3. 按序列的统计特性分,有平稳时间序列和非平稳时间序列.如果一个时间序列的概率分布与时间t无关,则称该序列为严格的(狭义的)平稳时间序列。如果序列的一、二阶矩存在,而且对任意时刻t满足:
  1. 均值为常数;
  2. 协方差为时间间隔τ的函数

则该该序列为宽平稳时间序列,也叫广义平稳时间序列

对于这方面的内容,详情可以多参考概率论的随机过程部分-有相似之处。

  1. 按时间序列的分布规律来分,有高斯型时间序列和非高斯型时间序列。

本章的主要内容是分析一元的时间序列分析。

2.移动平均法、指数平滑法和季节模型

1.移动平均法

移动平均法是常用的时间序列预测方法,由于其简单而具有很好的实用价值.

设观测序列为y1,···,yT,取移动平均的项数N<T.一次移动平均值计算公式为

时间序列分类 机器学习 时间序列怎么分类_python_02

则有:

时间序列分类 机器学习 时间序列怎么分类_时间序列分析_03

t+1期的预测值为时间序列分类 机器学习 时间序列怎么分类_python_04

其预测标准误差为:

时间序列分类 机器学习 时间序列怎么分类_数学建模_05

时间序列分类 机器学习 时间序列怎么分类_时间序列分类 机器学习_06

栗子:

汽车配件某年1~12年月份的化油器销售量(单位:只)统计数据见下表中第2行,试用一次移动平均法预测下一年1月份的销售量.

化油器销售量及一次移动平均法预测值表:

月份

1

2

3

4

5

6

7

8

9

10

11

12

预测

yi

423

358

434

445

527

429

502

480

384

427

446

N=3

405

412

469

467

461

452

469

455

430

419

N=5

437

439

452

466

473

444

444

448

分别取N=3,N=5,按预测公式

时间序列分类 机器学习 时间序列怎么分类_数学建模_07

时间序列分类 机器学习 时间序列怎么分类_时间序列分析_08

计算3个月和5个月移动平均预测值,分别见上表第三行和第四行。N=3时,预测的标准误差为56.5752;N=5时,预测的标准误差为39.8159.

通过预测后,可以看到,实际数据波动较大,经移动平均后,随机波动明显减少,且N越大,波动也越小。同时,也可以看到,一次移动平均法的预测标准误差还是有些大,对于实际数据波动较大的序列,一般较少采用此法进行预测。

代码实现:

import numpy as np
y=np.array([423,358,434,445,527,429,426,502,480,384,427,446])
def MoveAverage(y,N):
    Mt=['*']*N
    for i in range(N+1,len(y)+2):
        M=y[i-(N+1):i-1].mean()
        Mt.append(M)
    return Mt
yt3=MoveAverage(y,3) 
s3=np.sqrt(((y[3:]-yt3[3:-1])**2).mean())
yt5=MoveAverage(y,5)
s5=np.sqrt(((y[5:]-yt5[5:-1])**2).mean())
print('N=3时,预测值:',yt3,',预测的标准误差:',s3)
print('N=5时,预测值:',yt5,',预测的标准误差:',s5)

简单移动平均使用的是等量加权策略,可以利用卷积,相应代码如下:

def sma(arr,n):

	weights=np.ones(n)/n

	return np.convolve(weights,arr)[n-1:-n+1]
import numpy as np
y=np.array([423,358,434,445,527,429,426,502,480,384,427,446])
n1=3; yt1=np.convolve(np.ones(n1)/n1,y)[n1-1:-n1+1] ##左开右闭 np.convolve(np.ones(n1)/n1,y,mode='valid')  同样适用-且更加具有普适性
s1=np.sqrt(((y[n1:]-yt1[:-1])**2).mean())
n2=5; yt2=np.convolve(np.ones(n2)/n2,y)[n2-1:-n2+1]
s2=np.sqrt(((y[n2:]-yt2[:-1])**2).mean())
print('N=3时,预测值:',yt1,',预测的标准误差:',s1)
print('N=5时,预测值:',yt2,',预测的标准误差:',s2)
np.convolve(a, v, mode='full'):
 Returns the discrete, linear convolution of two one-dimensional sequences.

    The convolution operator is often seen in signal processing, where it
    models the effect of a linear time-invariant system on a signal [1]_.  In
    probability theory, the sum of two independent random variables is
    distributed according to the convolution of their individual
    distributions.

    If `v` is longer than `a`, the arrays are swapped before computation.

    Parameters
    ----------
    a : (N,) array_like
        First one-dimensional input array.
    v : (M,) array_like
        Second one-dimensional input array.
    mode : {'full', 'valid', 'same'}, optional
        'full':
          By default, mode is 'full'.  This returns the convolution
          at each point of overlap, with an output shape of (N+M-1,). At
          the end-points of the convolution, the signals do not overlap
          completely, and boundary effects may be seen.

        'same':
          Mode 'same' returns output of length ``max(M, N)``.  Boundary
          effects are still visible.

        'valid':
          Mode 'valid' returns output of length
          ``max(M, N) - min(M, N) + 1``.  The convolution product is only given
          for points where the signals overlap completely.  Values outside
          the signal boundary have no effect.

    Returns
    -------
    out : ndarray
        Discrete, linear convolution of `a` and `v`.

    See Also
    --------
    scipy.signal.fftconvolve : Convolve two arrays using the Fast Fourier
                               Transform.
    scipy.linalg.toeplitz : Used to construct the convolution operator.
    polymul : Polynomial multiplication. Same output as convolve, but also
              accepts poly1d objects as input.

 By default, mode is 'full'.  This returns the convolution
          at each point of overlap, with an output shape of (N+M-1,). At
          the end-points of the convolution, the signals do not overlap
          completely, and boundary effects may be seen.
          默认情况下,模式为“完整”。 这将返回卷积
           在每个重叠点,总的输出形状为(N + M-1,)。 在
           卷积的端点,信号不重叠
           完全可以看到边界效应。
 使用栗子:
  Examples
    --------
    Note how the convolution operator flips the second array
    before "sliding" the two across one another:

    >>> np.convolve([1, 2, 3], [0, 1, 0.5])
    array([0. , 1. , 2.5, 4. , 1.5])

    Only return the middle values of the convolution.
    Contains boundary effects, where zeros are taken
    into account:

    >>> np.convolve([1,2,3],[0,1,0.5], 'same')
    array([1. ,  2.5,  4. ])

    The two arrays are of the same length, so there
    is only one position where they completely overlap:

    >>> np.convolve([1,2,3],[0,1,0.5], 'valid')
    array([2.5])

    了解什么是卷积:
  References
    ----------
    .. [1] Wikipedia, "Convolution",
        https://en.wikipedia.org/wiki/Convolution
            
            卷积公式可以描述为在时刻t处函数f(τ)的加权平均值,其中权重由g(–τ)给出,仅移动量t即可。随着t的变化,加权函数会强调输入函数的不同部分。

时间序列分类 机器学习 时间序列怎么分类_数学建模_09


时间序列分类 机器学习 时间序列怎么分类_移动平均法_10


离散序列的卷积求法

时间序列分类 机器学习 时间序列怎么分类_移动平均法_11

卷积关键词:权重-时间-叠加性-离散还是连续

2.二次移动平均法及趋势移动平均法

当预测变量的基本趋势发生变化时,一次移动平均法不能迅速适应这种变化.

当时间序列的变化为线性趋势时,一次移动平均法的滞后偏差使预测值偏低,不能进行合理的趋势外推。

二次移动平均法-就是对一次移动平均数再进行二次移动平均,再以一次移动平均值和二次移动平均值为基础建立预测模型,计算预测值的方法。

二次移动平均值计算公式为:

时间序列分类 机器学习 时间序列怎么分类_时间序列分析_12

当预测目标的基本趋势是在某一水平上下波动时,可用一次移动平均方法建立预测模型。当预测目标的基本趋势与某一线性模型相吻合时,常用二次移动平均法.但序列同时存在线性趋势和周期波动时,可用趋势移动平均法建立预测模型:

时间序列分类 机器学习 时间序列怎么分类_python_13

时间序列分类 机器学习 时间序列怎么分类_python_14