Local Linear Model

tfp.sts.LocalLinearTrend is formal representation of a local linear trend model.Source

Local Linear Model本质上是分段线性拟合,线性拟合的核心是最小二乘法(Ordinary Least Squares)及其置信区间,即:分段拟合的直线对应的置信区间没有重合( If 95% confidence intervals for these two means are calculated (approximately) by adding or subtracting two standard errors, the intervals do not overlap, so the difference in means is statistically very significant.)。

此模型适合在短期内数据呈现的趋势及斜率一致且不变,但长期会变化的时间序列数据(This model is appropriate for data where the trend direction and magnitude (latent slope) is consistent within short periods but may evolve over time.)。

如何进行分段?
The leves are defined by Random Walk Model which is simple random walk model (good simulation). When faced with a time series that shows irregular growth, the best strategy may not be to try to directly predict the level of the series at each period (i.e., the quantity YtY_tYt​). Instead, it may be better to try to predict the change that occurs from one period to the next (i.e., the quantity Yt−Yt−1Y_t - Y_{t-1}Yt​−Yt−1​).
That is, it may be better to look at the first difference of the series, to see if a predictable pattern can be found there. For purposes of one-period-ahead forecasting, it is just as good to predict the next change as to predict the next level of the series, since the predicted change can be added to the current level to yield a predicted level. The simplest case of such a model is one that always predicts that the next change will be zero, as if the series is equally likely to go up or down in the next period regardless of what it has done in the past.

(Source)
这段话的是意思似乎是探测分段是先根据头几步的数据,生成一个linear model;然后根据这个linear model生成一个预测值,并计算预测值与真实值的差异;根据这个差异,若差异很小,则不分段,若差异较大则分段。这个理解似乎有些问题,往有缘的大神予以纠正。

In tfp.sts.LocalLinearTrend , the levels are defined by Gaussian random walk model (i.e. random walks with Gaussian steps) whose steps are continuous normal (i.e. Gaussian) random variables, rather than discrete random variables. While this loses the simplicity of the random walk on a lattice, it gains in uniformity; the distribution of values at each time step is always Gaussian.

tfp.sts.LocalLinearTrend中有2个关键词: level and slope, level 应该表示分段或数据分组, slope 指拟合直线的斜率。

Local Linear Model如下图所示:

>>> import numpy as np
>>> import statsmodels.api as sm
>>> import matplotlib.pyplot as plt
>>> from statsmodels.sandbox.regression.predstd import wls_prediction_std
>>>
>>> np.random.seed(9876789)
>>> nsample = 50
>>> groups = np.zeros(nsample, int)
>>> groups[20:40] = 1
>>> groups[40:] = 2
>>> #dummy = (groups[:,None] == np.unique(groups)).astype(float)
...
>>> dummy = sm.categorical(groups, drop=True)
>>> x = np.linspace(0, 20, nsample)
>>> # drop reference category
... X = np.column_stack((x, dummy[:,1:]))
>>> X = sm.add_constant(X, prepend=False)
>>>
>>> beta = [1., 3, -3, 10]
>>> y_true = np.dot(X, beta)
>>> e = np.random.normal(size=nsample)
>>> y = y_true + e
>>> print(X[:5,:])
[[0.         0.         0.         1.        ][0.40816327 0.         0.         1.        ][0.81632653 0.         0.         1.        ][1.2244898  0.         0.         1.        ][1.63265306 0.         0.         1.        ]]
>>> print(y[:5])
[ 9.15948411 12.00565852 11.28186857 10.71633086 14.56695876]
>>> print(groups)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 2 2 2 2 2 2 2 2 2 2]
>>> print(dummy[:5,:])
[[1. 0. 0.][1. 0. 0.][1. 0. 0.][1. 0. 0.][1. 0. 0.]]
>>> res2 = sm.OLS(y, X).fit()
>>> print(res2.summary())OLS Regression Results
==============================================================================
Dep. Variable:                      y   R-squared:                       0.968
Model:                            OLS   Adj. R-squared:                  0.966
Method:                 Least Squares   F-statistic:                     459.4
Date:                Fri, 31 Jan 2020   Prob (F-statistic):           2.78e-34
Time:                        15:46:13   Log-Likelihood:                -73.569
No. Observations:                  50   AIC:                             155.1
Df Residuals:                      46   BIC:                             162.8
Df Model:                           3
Covariance Type:            nonrobust
==============================================================================coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
x1             0.8866      0.072     12.379      0.000       0.742       1.031
x2             3.7504      0.680      5.514      0.000       2.381       5.119
x3            -1.3477      1.108     -1.216      0.230      -3.578       0.883
const         10.6017      0.371     28.592      0.000       9.855      11.348
==============================================================================
Omnibus:                        1.111   Durbin-Watson:                   2.314
Prob(Omnibus):                  0.574   Jarque-Bera (JB):                1.155
Skew:                           0.309   Prob(JB):                        0.561
Kurtosis:                       2.583   Cond. No.                         96.3
==============================================================================Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
>>> prstd, iv_l, iv_u = wls_prediction_std(res2)
>>>
>>> fig, ax = plt.subplots(figsize=(8,6))
>>>
>>> ax.plot(x, y, 'o', label="Data")
[<matplotlib.lines.Line2D object at 0x7f4716c0c0d0>]
>>> ax.plot(x, y_true, 'b-', label="True")
[<matplotlib.lines.Line2D object at 0x7f4716c0c1d0>]
>>> ax.plot(x, res2.fittedvalues, 'r--.', label="Predicted")
[<matplotlib.lines.Line2D object at 0x7f4716c0cdd0>]
>>> ax.plot(x, iv_u, 'r--')
[<matplotlib.lines.Line2D object at 0x7f4716c0cd90>]
>>> ax.plot(x, iv_l, 'r--')
[<matplotlib.lines.Line2D object at 0x7f4716c1d810>]
>>> legend = ax.legend(loc="best")
>>> plt.show()

(Code Source)

Reference

  1. tfp.sts.LocalLinearTrend
  2. Ordinary Least Squares
  3. Statsmodels Examples
  4. Linear trend model of Duke University

Semi-Local Linear Model

tfp.sts.SemiLocalLinearTrend
Local Linear Model 和 Semi-Local Linear Model之间的主要差别是分段或进化的方法不同,前者通过Gaussian Random Walk Model,后者通过AR1 model。Semi-Local Linear Model在长期预报的情况下,将会生成比Local Linear Model更合理的置信区间。
Unlike the random walk used in LocalLinearTrend, a stationary AR1 process (coefficient in (-1, 1)) maintains bounded variance over time, so a SemiLocalLinearTrend model will often produce more reasonable uncertainties when forecasting over long timescales.(Source)

Local Level Model

tfp.sts.LocalLevel
这个模型只生成分段,不做线性趋势分析,也是通过Gaussian random walk model.

The local level model posits a level evolving via a Gaussian random walk. The latent state is [level]. We observe a noisy realization of the current level: f[t] = level[t] + Normal(0., observation_noise_scale) at each timestep.(Source)

Local Linear Model, Semi Local Linear Model and Local Level Model of TFP.STS相关推荐

  1. ML:MLOps系列讲解之《基于ML的软件的三个层次之02 Model: Machine Learning Pipelines——2.6 ML Model serialization forma》解读

    ML:MLOps系列讲解之<基于ML的软件的三个层次之02 Model: Machine Learning Pipelines--2.6 ML Model serialization forma ...

  2. ValueError: This model has not yet been built. Build the model first by calling build() or calling f

    ValueError: This model has not yet been built. Build the model first by calling build() or calling f ...

  3. php model module,Yii2用Gii自动生成Module+Model+CRUD

    1. 开启gii模块 common/config/main-local.php加入下面代码 return [ 'modules' => [ 'gii' => [ 'class' => ...

  4. Ebean报错java.lang.ClassCastException: com.project.model.xxx cannot be cast to com.project.model.xxx

    使用Ebean查询数据库时报错: 接口接收前端传过来的id代码: 报错内容: com.project.model.NavManage cannot be cast to com.project.mod ...

  5. Chapter 1 (Linear Equations in Linear Algebra): System of linear equations (线性方程组)

    本文为<Linear algebra and its applications>的读书笔记 目录 System of linear equations (线性方程组) Matrix Not ...

  6. java model类作用_SPRING框架中ModelAndView、Model、ModelMap区别及详细分析

    注意:如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面. 首先介绍ModelMap[Model]和ModelAndView的作用 Model 是一个接口, 其实现类为Exte ...

  7. html读取model的值,Js和Thymeleaf如何获取model中的值

    简述 在大多数的项目架构中,使用SPringBoot发布微服务,前端采用Thymeleaf做为Html模版,使用Jquery做为动态脚本,那么Thymeleaf和Jquery是如何获取Model中的数 ...

  8. linux usr local权限,OS X权限被拒绝/ usr/local/lib

    我正在寻找任何建议/直觉/线索/答案的权限问题,一直困扰着我,因为我切换到新的Macbook Pro.这是两难.某些程序在安装过程中将库复制到/ usr/local/lib下,并在运行这些程序时遇到了 ...

  9. 02基于注解开发SpringMVC项目(jar包,异步,request,参数传递,多选的接收,Model传参,map传参,model传参,ajax,重定向,时间日期转换)

     1 所需jar包 项目结构如下: 2 web.xml配置文件的内容如下: <?xmlversion="1.0"encoding="UTF-8"?&g ...

  10. python local global_Python 变量作用域 LEGB (上)—— Local,Global,Builtin

    Python 变量作用域的规则是 LEGB LEGB含义解释: L -- Local(function):函数内的名字空间 E -- Enclosing function locals:外部嵌套函数的 ...

最新文章

  1. php表单退出怎么写,PHP提交表单失败后如何保留填写的信息
  2. 功能安全-26262(2018) part5
  3. 单片微型计算机与一般微型计算机相比,单片机习题与思考题.doc
  4. Hibernate4实战 之 第四部分:关系映射
  5. 数据分析面试必考的AB-Test知识点整理
  6. 创业95%失败不是因项目本身
  7. Scrapy的安装介绍
  8. Android 创建自己的Camera App
  9. pycharm导入opencv库失败解决方法
  10. 哈哈哈,第一次做codeforce
  11. 专场介绍 | 第12届中国R会议(北京)生物信息专场
  12. python中eval()函数的作用及使用方法
  13. 峰值电流检测电路设计/自己备忘
  14. gic_architecture_specification解读一
  15. Erebus以Linux勒索软件的方式重出江湖,勒索韩国公司100万美元
  16. HTML 常用转义字符
  17. C语言打印倒三角形代码
  18. 基于web_socket_channel 实现弹幕通信
  19. C语言从入门到精通——进阶6 C语言文件操作
  20. Centos7的安装与模板机的制作

热门文章

  1. 产品思考 - 不剪发的Tony老师
  2. BadDet: Backdoor Attacks on Object Detection——面向目标检测的后门攻击
  3. 激光位移传感器与其他位移传感器比较
  4. 语言处理 之 fastspeech2,ar,nar研究
  5. 使用PowerCli来创建自定义ESXi ISO镜像
  6. asp.net 各种小窍门
  7. 计算机创建修改ip知识,恢复系统后让每台计算机自动修改IP和计算机名的方法...
  8. Qt 使用-自定义菜单栏
  9. 对标大厂标准,C站(CSDN)软件工程师能力认证正式上线
  10. 以前的windows安装文件可以删除吗_你知道C盘哪些文件是可以删除吗?