在下面
build_model
的代码中涉及到tfp中的
sts.Autoregressive
.很长时间不用,恍惚中忘记了何为
Autoregressive
?通过阅读
Autoregressive Model: Definition & The AR Process
学习到以下几点:
-
auto
不是英文单词,而是表示self的希腊语.这可能也说明这个模型有多么经典. -
autoregressive model旨在寻找当前时刻的值,与相近历史值的线性关系,因此又叫做
markov model
.与过去几个值,由
order
这个参数来指定. -
autoregressive model本身是一个包含随机信息的过程,可以很好的预测未来趋势,但无法得到准确的point estimation.(The AR process is an example of a
stochastic process
, which have degrees of uncertainty or randomness built in. The randomness means that you might be able to
predict future trends pretty well with past data
,
but you’re never going to get 100 percent accuracy
. Usually, the process gets “close enough” for it to be useful in most scenarios.)
数学公式还是最简洁的表达方式,AR model表示如下:
y
t
=
φ
1
y
t
−
1
+
φ
2
y
t
−
2
+
.
.
.
+
φ
p
y
t
−
p
+
A
t
+
δ
y_t = \varphi_1 y_{t-1} + \varphi_2 y_{t-2} + … + \varphi_p y_{t-p} + A_t+ \delta
y
t
=
φ
1
y
t
−
1
+
φ
2
y
t
−
2
+
.
.
.
+
φ
p
y
t
−
p
+
A
t
+
δ
-
yt
−
1
y_{t-1}
y
t
−
1
,
yt
−
2
y_{t-2}
y
t
−
2
, …,
yt
−
p
y_{t-p}
y
t
−
p
are the past series values. -
At
A_t
A
t
is white noise (i.e. randomness) -
δ\delta
δ
is defined by the following equation:
δ
=
(
1
−
∑
i
=
1
p
ϕ
i
)
μ
\delta = (1 – \sum_{i=1}^p \phi_i) \mu
δ
=
(
1
−
i
=
1
∑
p
ϕ
i
)
μ
where
μ
\mu
μ
is the process mean.
为在markdown中写出上述公式,参照了
LaTeX Math Symbols
and
Motivating Examples
.
def build_model(observed_time_series):
hour_of_day_effect = sts.Seasonal(
num_seasons=24,
observed_time_series=observed_time_series,
name='hour_of_day_effect')
day_of_week_effect = sts.Seasonal(
num_seasons=7, num_steps_per_season=24,
observed_time_series=observed_time_series,
name='day_of_week_effect')
temperature_effect = sts.LinearRegression(
design_matrix=tf.reshape(temperature - np.mean(temperature),
(-1, 1)), name='temperature_effect')
autoregressive = sts.Autoregressive(
order=1, # scalar Python positive int specifying the number of past timesteps to regress on.
observed_time_series=observed_time_series,
name='autoregressive')
model = sts.Sum([hour_of_day_effect,
day_of_week_effect,
temperature_effect,
autoregressive],
observed_time_series=observed_time_series)
return model
代码来自
Structural Time Series Modeling Case Studies: Atmospheric CO2 and Electricity Demand