🎯要点
- 宏观能耗场景模型参数化输入数据,分析可视化输出结果,使用场景时间序列数据模型及定量和定性指标
- 使用线图和箱线图、饼图、散点图、堆积条形图、桑基图等可视化模型输出结果
- 根据气体排放过程得出其时间序列关系,使用推断模型中计算工具量化气体排放量
- 推断模型中计算工具使用的数学工具是恒定比率、时间比率、分位数滚动窗口、均方根、线性插补等
🍪语言内容分比
🍇Python时间序列分析
时间序列数学模型
时间序列模型是假设多个时间序列或时间序列之间存在关系的模型。简单回归模型就是一个例子:
y
(
t
)
=
x
(
t
)
β
+
ε
(
t
)
(
1
)
y(t)=x(t) \beta+\varepsilon(t)\qquad(1)
y(t)=x(t)β+ε(t)(1)
其中
y
(
t
)
=
{
y
t
;
t
=
0
,
±
1
,
±
2
,
…
}
y(t)=\left\{y_t ; t=0, \pm 1, \pm 2, \ldots\right\}
y(t)={yt;t=0,±1,±2,…} 是一个序列,以时间下标
t
t
t 为索引,它是可观测信号序列
x
(
t
)
=
{
x
t
}
x(t)=\left\{x_t\right\}
x(t)={xt}和不可观测,独立且同分布的随机变量的白噪声序列
ε
(
t
)
=
{
ε
t
}
\varepsilon(t)=\left\{\varepsilon_t\right\}
ε(t)={εt} 的组合。
一种更通用的模型,我们称之为一般时间回归模型,它假设一种关系,其中包含任意数量的
x
(
t
)
、
y
(
t
)
x(t)、y(t)
x(t)、y(t) 和
ε
(
t
)
\varepsilon(t)
ε(t) 的连续元素。该模型可以由方程表示
∑
i
=
0
p
α
i
y
(
t
−
i
)
=
∑
i
=
0
k
β
i
x
(
t
−
i
)
+
∑
i
=
0
q
μ
i
ε
(
t
−
i
)
(
2
)
\sum_{i=0}^p \alpha_i y(t-i)=\sum_{i=0}^k \beta_i x(t-i)+\sum_{i=0}^q \mu_i \varepsilon(t-i)\qquad(2)
i=0∑pαiy(t−i)=i=0∑kβix(t−i)+i=0∑qμiε(t−i)(2)
通常认为
α
0
=
1
\alpha_0=1
α0=1 是理所当然的。 左侧前导系数的归一化将
y
(
t
)
y(t)
y(t) 标识为输出序列。方程中的任何和都可以是无限的,但如果模型要可行,则系数序列
{
α
i
}
,
{
β
i
}
\left\{\alpha_i\right\},\left\{\beta_i\right\}
{αi},{βi} 和
{
μ
i
}
\left\{\mu_i\right\}
{μi} 只能依赖于有限数量的参数。
虽然以(2)的形式写出一般模型很方便,但也通常用以下方程表示
y
(
t
)
=
∑
i
=
1
p
ϕ
i
y
(
t
−
i
)
+
∑
i
=
0
k
β
i
x
(
t
−
i
)
+
∑
i
=
0
q
μ
i
ε
(
t
−
i
)
y(t)=\sum_{i=1}^p \phi_i y(t-i)+\sum_{i=0}^k \beta_i x(t-i)+\sum_{i=0}^q \mu_i \varepsilon(t-i)
y(t)=i=1∑pϕiy(t−i)+i=0∑kβix(t−i)+i=0∑qμiε(t−i)
其中 ϕ i = − α i \phi_i=-\alpha_i ϕi=−αi 表示 i = 1 , … , p i=1, \ldots, p i=1,…,p。这会将序列 y ( t ) y(t) y(t) 的滞后版本与输入序列 x ( t ) x(t) x(t) 及其滞后一起放置在右侧上。
工程师倾向于将其描述为反馈模型,而经济学家更可能将其描述为具有滞后因变量的模型。
由于包含可观察的解释序列
x
(
t
)
x(t)
x(t),上述模型被称为回归模型。当
x
(
t
)
x(t)
x(t)被删除时,我们得到一个更简单的无条件线性随机模型:
∑
i
=
0
p
α
i
y
(
t
−
i
)
=
∑
i
=
0
q
μ
i
ε
(
t
−
i
)
\sum_{i=0}^p \alpha_i y(t-i)=\sum_{i=0}^q \mu_i \varepsilon(t-i)
i=0∑pαiy(t−i)=i=0∑qμiε(t−i)
这是自回归移动平均模型。
Python自回归综合移动平均线
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.arima_model import ARIMA
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
我们将使用包含特定日期飞机乘客数量的数据集。
df = pd.read_csv('air.csv', parse_dates = ['Month'], index_col = ['Month'])
df.head()
plt.xlabel('Date')
plt.ylabel('Number of air passengers')
plt.plot(df)
在建立模型之前,我们必须确保时间序列是平稳的。有两种主要方法可以确定给定时间序列是否平稳。
- 滚动统计:绘制滚动平均值和滚动标准差。如果时间序列随时间保持恒定(用肉眼观察线条是否笔直且平行于 x 轴),则时间序列是平稳的。
- 增强迪基-富勒检验:如果 p 值较低(根据原假设)并且 1%、5%、10% 置信区间的临界值尽可能接近增强迪基-富勒统计,则时间序列被视为平稳。
对于那些不理解平均值和滚动平均值之间区别的人来说,10 天滚动平均值会将前 10 天的收盘价平均作为第一个数据点。下一个数据点会删除最早的价格,加上第 11 天的价格并取平均值,依此类推。
rolling_mean = df.rolling(window = 12).mean()
rolling_std = df.rolling(window = 12).std()
plt.plot(df, color = 'blue', label = 'Original')
plt.plot(rolling_mean, color = 'red', label = 'Rolling Mean')
plt.plot(rolling_std, color = 'black', label = 'Rolling Std')
plt.legend(loc = 'best')
plt.title('Rolling Mean & Rolling Standard Deviation')
plt.show()
正如您所看到的,滚动平均值和滚动标准差随着时间的推移而增加。因此,我们可以得出结论,时间序列不是平稳的。
result = adfuller(df['Passengers'])
print('ADF Statistic: {}'.format(result[0]))
print('p-value: {}'.format(result[1]))
print('Critical Values:')
for key, value in result[4].items():
print('\t{}: {}'.format(key, value))
获取因变量的对数是降低滚动平均值增加速率的简单方法。
df_log = np.log(df)
plt.plot(df_log)
让我们创建一个函数来运行两个测试,以确定给定的时间序列是否平稳。
def get_stationarity(timeseries):
rolling_mean = timeseries.rolling(window=12).mean()
rolling_std = timeseries.rolling(window=12).std()
original = plt.plot(timeseries, color='blue', label='Original')
mean = plt.plot(rolling_mean, color='red', label='Rolling Mean')
std = plt.plot(rolling_std, color='black', label='Rolling Std')
plt.legend(loc='best')
plt.title('Rolling Mean & Standard Deviation')
plt.show(block=False)
result = adfuller(timeseries['Passengers'])
print('ADF Statistic: {}'.format(result[0]))
print('p-value: {}'.format(result[1]))
print('Critical Values:')
for key, value in result[4].items():
print('\t{}: {}'.format(key, value))