如何使用自回归来绘制和可视化时间序列未来值的预测?

时间:2019-02-13 10:22:26

标签: python matplotlib

我想绘制时间序列及其未来价值的预测。我正在使用自回归模型。我不确定我使用的策略是否正确。但是我想在将来估计15个时间戳的时间序列数据。我希望能在不同的可能性之间达成共识:

import numpy as np
from numpy import convolve
import matplotlib.pyplot as plt
plt.style.use('ggplot')

def moving_average(y, period):
    buffer = []
    for i in range(period, len(y)):
        buffer.append(y[i - period : i].mean())
    return buffer

def auto_regressive(y, p, d, q, future_count):
    """
    p = the order (number of time lags)
    d = degree of differencing
    q = the order of the moving-average
    """
    buffer = np.copy(y).tolist()
    for i in range(future_count):
        ma = moving_average(np.array(buffer[-p:]), q)
        forecast = buffer[-1]
        for n in range(0, len(ma), d):
            forecast -= buffer[-1 - n] - ma[n]
        buffer.append(forecast)
    return buffer



y=[60, 2, 0, 0, 1, 1, 0, -1, -2, 0, -2, 6, 0, 2, 0, 4, 0, 1, 3, 2, 1, 2, 1, 0, 2, 2, 0, 1, 0, 1, 3, -1, 0, 2, 2, 1, 3, 2, 4, 2, 3, 0, 0, 2, 2, 0, 3, 1, 0, 2]
x=[1549984749, 1549984751, 1549984755, 1549984761, 1549984768, 1549984769, 1549984770, 1549984774, 1549984780, 1549984783, 1549984786, 1549984787, 1549984788,
   1549984794, 1549984797, 1549984855, 1549984923, 1549984930, 1549984955, 1549985006, 1549985008, 1549985027, 1549985086, 1549985091, 1549985101, 1549985115,
   1549985116, 1549985118, 1549985130, 1549985130, 1549985139, 1549985141, 1549985146, 1549985154, 1549985178, 1549985192, 1549985203, 1549985217, 1549985245,
   1549985288, 1549985311, 1549985316, 1549985425, 1549985447, 1549985460, 1549985463, 1549985489, 1549985561, 1549985595, 1549985610]
x=np.array(x)
print(np.size(x))
y=np.array(y)
print(np.size(y))

future_count = 15
predicted_15 = auto_regressive(y,20,1,2,future_count)

plt.plot(x[len(x) - len(predicted_15):], predicted_15)
plt.plot(x, y, 'o-')
plt.show()

但是我得到了这个错误:

    "have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (15,) and (65,)

1 个答案:

答案 0 :(得分:0)

由于predicted_15包含y以及您的预测值(因此y的长度为65),您会遇到错误。您只想绘制预测值(长度15)。

plt.plot(x[len(x) - len(predicted_15):], predicted_15[len(x):])

话虽如此,您需要考虑这些y预测值对应的x值。