Structural Time Series modeling in TensorFlow Probability

2023-11-12

在邯郸学步后,想要深入用好Tensorflow中的STS model,还是要静下心来,好好阅读点材料。

f ( t ) = f 1 ( t ) + f 2 ( t ) + . . . + f n ( t ) + ε ; ε ∼ N ( 0 , σ 2 ) f(t) = f_1(t) + f_2(t) + ... + f_n(t) + \varepsilon; \varepsilon \sim N(0, \sigma^2) f(t)=f1(t)+f2(t)+...+fn(t)+ε;εN(0,σ2)

1、 时间序列模型的用处,除了实现预则立之外,还可以有以下3方面作用:

  • 推断某一因素的因果影响,
  • 通过容易获取的信息推断出不容易获取信息或指标当前值,
  • 探测时间序列数据中的异常值。
    Methods for forecasting time series can also be applied to infer the causal impact of a feature launch or other intervention on user engagement metrics, to infer the current value of difficult-to-observe quantities like the unemployment rate from more readily available information, as well as to detect anomalies in time series data.

2、STS的核心是Variation Inference and Hamiltonian Monte Carlo,可以实现单点和不确定性的预测,可以利用GPUs实现平行计算及与深度神经网络的整合
TensorFlow Probability (TFP) now features built-in support for fitting and forecasting using structural time series models. This support includes Bayesian inference of model parameters using variational inference (VI) and Hamiltonian Monte Carlo (HMC), computing both point forecasts and predictive uncertainties. Because they’re built in TensorFlow, these methods naturally take advantage of vectorized hardware (GPUs and TPUs), can efficiently process many time series in parallel, and can be integrated with deep neural networks.

3、95% 预测置信区间。

We can see that the forecast uncertainty (shading ± 2 standard deviations) increases over time, as the linear trend model becomes less confident in its extrapolation of the slope. The mean forecast combines the seasonal variational with a linear extrapolation of the existing trend, which appears to slightly underestimate the accelerating growth in atmospheric CO2, but the true values are still within the 95% predictive interval.

4、分解各部分的作用
在不太理解算法内部原理的情况下,猜测从下图各部分中预测置信区间的大小看,hour-of-daytemperature在电量预测模型中起到的比较重要的作用;day-of-weekautogressive次之,其中autogressive贡献了大部分的预测不确定性。
在这里插入图片描述
We see that the model has quite reasonably identified a large hour-of-day effect and a much smaller day-of-week effect (the lowest demand appears to occur on Saturdays and Sundays), as well as a sizable effect from temperature, and that it produces relatively confident forecasts of these effects. Most of the predictive uncertainty comes from the autoregressive process, based on its estimate of the unmodeled (residual) variation in the observed series.

5、LinearRegression除了可以处理相关时间序列数据外,也可以处理节假日和特定日期。
For time series depending on additional, time-varying covariates. Regression components can also be used to encode holiday or other date-specific effects.

As a more advanced application, we might use the design matrix to encode holiday effects. For example, suppose we are modeling data from the month of December. We can combine day-of-week seasonality with special effects for Christmas Eve (Dec 24), Christmas (Dec 25), and New Year’s Eve (Dec 31), by constructing a design matrix with indicators for those dates. Source

holiday_indicators = np.zeros([31, 3])
holiday_indicators[23, 0] = 1  # Christmas Eve
holiday_indicators[24, 1] = 1  # Christmas Day
holiday_indicators[30, 2] = 1  # New Year's Eve

holidays = tfp.sts.LinearRegression(design_matrix=holiday_indicators,
                                    name='holidays')
day_of_week = tfp.sts.Seasonal(num_seasons=7,
                               observed_time_series=observed_time_series,
                               name='day_of_week')
model = tfp.sts.Sum(components=[holidays, seasonal],
                    observed_time_series=observed_time_series)

6、Seasonal要素. For time series depending on seasonal factors, such as the hour of the day, the day of the week, or the month of the year.

day_of_weekhour_of_day的处理方式:

day_of_week = tfp.sts.Seasonal(num_seasons=7,
                               num_steps_per_season=24,
                               observed_time_series=y,
                               name='day_of_week')
hour_of_day = tfp.sts.Seasonal(num_seasons=24,
                               num_steps_per_season=1,
                               observed_time_series=y,
                               name='hour_of_day')
model = tfp.sts.Sum(components=[day_of_week, hour_of_day],
                    observed_time_series=y)

month_of_year的处理方式:

month_of_year = tfp.sts.Seasonal(
  num_seasons=12,
  num_steps_per_season=[31, 28, 31, 30, 30, 31, 31, 31, 30, 31, 30, 31],
  drift_scale_prior=tfd.LogNormal(loc=-1., scale=0.1),
  initial_effect_prior=tfd.Normal(loc=0., scale=5.),
  name='month_of_year')

对于4年(包含闰月)的month_of_year处理方式类似如下,这一方式可用于处理农历.

num_days_per_month = np.array(
  [[31, 28, 31, 30, 30, 31, 31, 31, 30, 31, 30, 31],
   [31, 29, 31, 30, 30, 31, 31, 31, 30, 31, 30, 31],  # year with leap day
   [31, 28, 31, 30, 30, 31, 31, 31, 30, 31, 30, 31],
   [31, 28, 31, 30, 30, 31, 31, 31, 30, 31, 30, 31]])

month_of_year = tfp.sts.Seasonal(
  num_seasons=12,
  num_steps_per_season=num_days_per_month,
  drift_scale_prior=tfd.LogNormal(loc=-1., scale=0.1),
  initial_effect_prior=tfd.Normal(loc=0., scale=5.),
  name='month_of_year')

7、Autoregressive

8、LocalLinearTrend

9、SemiLocalLinearTread

10、LocalLevel



1. How to make the electricity demand model better?

为什么说在观察到温度与AR的spikes基本一致,就表明addtional featuesdata transformations有助于更好地捕捉到温度feature的作用?
A modeler might use this decomposition to understand how to improve the model. For example, they might notice that some spikes in temperature still seem to coincide with spikes in the AR residual, indicating that additional features or data transformations might help better capture the temperature effect.

关于这个问题,有人提出了类似的问题:
It was mentioned that since peaks in the residual correspond to peaks in temperature other features could be engineered to capture this - are you referring to say polynomial expansions of temperature? any other hints? 针对这个问题,Dave Moore的回复如下:
I think we were imagining polynomial expansions, though you could also consider other transformations like log, or one-hot vectors from quantizing temperature into bins, which at a fine enough discretization can represent arbitrary nonlinear functions. Or to be really fancy you could consider interactions between these (e.g., instead of a piecewise-constant response from quantized features, you’d use the one-hot bins as a masking pattern on the original series to get a piecewise-linear response, or on polynomial features to get a piecewise-polynomial response, etc).

1.1 How to do polynomial expansions for time series data?

模型中添加平方、立方和乘积等非线性features,往往是比较有用的。
Often it’s useful to add complexity to the model by considering nonlinear features of the input data. A simple and common method to use is polynomial features, which can get features’ high-order and interaction terms. Source

The way of creating polynominal features:

X 1 X_1 X1 X 2 X_2 X2
0 1
2 3
4 5
>>> poly = PolynomialFeatures(degree = 2)
>>> poly.fit_transform(X)
1 X 1 X_1 X1 X 2 X_2 X2 X 1 2 X_1^2 X12 X 1 X 2 X_1X_2 X1X2 X 2 2 X_2^2 X22
1 0 1 0 2 0^2 02 0 × 1 0 \times 1 0×1 1 2 1^2 12
1 2 3 2 2 2^2 22 2 × 3 2 \times 3 2×3 3 2 3^2 32
1 4 5 4 2 4^2 42 4 × 5 4 \times 5 4×5 5 2 5^2 52
>>> X = np.arange(6).reshape(3, 2)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> poly = PolynomialFeatures(2)
>>> poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.,  0.,  1.],
       [ 1.,  2.,  3.,  4.,  6.,  9.],
       [ 1.,  4.,  5., 16., 20., 25.]])

Polynomial expansions for time series:
Refer to the Polynomial Regression and Generating polynomial features, the polynomial expansions for time series maybe like the following code:

>>> from sklearn.preprocessing import PolynomialFeatures
>>> import numpy as np

>>> x = np.arange(5)
>>> x = x[:, np.newaxis]
>>> x
array([[0],
       [1],
       [2],
       [3],
       [4]])
>>> polynomial_features= PolynomialFeatures(degree=2)
>>> x_poly = polynomial_features.fit_transform(x)
>>> x_poly
array([[ 1.,  0.,  0.],
       [ 1.,  1.,  1.],
       [ 1.,  2.,  4.],
       [ 1.,  3.,  9.],
       [ 1.,  4., 16.]])
>>> polynomial_features= PolynomialFeatures(degree=3)
>>> x_poly = polynomial_features.fit_transform(x)
>>> x_poly
array([[ 1.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  2.,  4.,  8.],
       [ 1.,  3.,  9., 27.],
       [ 1.,  4., 16., 64.]])

1.2 Try to make the model better

Add l o g ( t e m p e r a t u r e ) log(temperature) log(temperature) transformation to build_model.

temperature_log_effect = sts.LinearRegression(
      design_matrix=tf.reshape(np.log(temperature),
                               (-1, 1)), name='temperature_log_effect')
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')
  # added feature
  temperature_log_effect = sts.LinearRegression(
      design_matrix=tf.reshape(np.log(temperature),
                               (-1, 1)), name='temperature_log_effect')

  autoregressive = sts.Autoregressive(
      order=1,
      observed_time_series=observed_time_series,
      name='autoregressive')
  model = sts.Sum([hour_of_day_effect,
                   day_of_week_effect,
                   temperature_effect,
                   # added feature
                   temperature_log_effect,
                   autoregressive],
                   observed_time_series=observed_time_series)
  return model

模型预测精度有了较大改善,如下图所示:
官网model的预测结果:
only temperature


添加了 l o g ( t e m p e r a t u r e ) log(temperature) log(temperature)后model的预测结果:
temperature + log(temperature)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Structural Time Series modeling in TensorFlow Probability 的相关文章

  • 12 papers to understand QA system with Deep Learning

    由于最近入手NLP任务 需要看一些paper 本文对最近两周看的paper做个总结 适用于有deep learning背景 希望了解NLP应用的同学 主要针对NLP方向 问答系统 QA 和翻译 Machine Translation 本文提
  • 主成分分析(Principal Component Analysis,PCA)详解

    PCA是非常重要的统计方法 其实际应用非常广泛 但是很多讲解太过于公式化 很难让初学者消化 本文将从一个实际例子出发 并对数学公式原理及推导过程作出详细解释 即使你的数学基础比较差 在看完这篇博客之后 相信你会对PCA会有一个透彻的认知 P
  • 正则化(Regularization)

    过拟合问题 Overfitting 当我们对一个问题建立线性回归模型或逻辑回归模型时 不恰当的选择特征会导致过拟合问题 过拟合问题是指当我们选择了很多的特征值时 模型对数据集的每一个example都符合的很好 但是对新的example却预测
  • 如何动态调试Python的第三方库

    注意 本文方法仅限于调试安装时附带py源码的库 如sklearn 引入 用sklearn中的sklearn feature extraction text TfidfTransformer来获取TF特征 但发现sklearn的计算结果与我手
  • 机器学习、深度学习、图像检索 的一些优秀博客

    机器学习 深度学习 图像检索 的一些优秀博客 1 http www cnblogs com ooon 2 http yongyuan name blog
  • 详解随机梯度下降法(Stochastic Gradient Descent,SGD)

    深度学习最常用的优化方法就是随机梯度下降法 但是随机梯度下降法在某些情况下会失效 这是为什么呢 带着这个问题我们接着往下看 一个经典的例子就是假设你现在在山上 为了以最快的速度下山 且视线良好 你可以看清自己的位置以及所处位置的坡度 那么沿
  • 深度学习系列之ANN

    到此 ANN网络从最基础的单层感知器 到为深度网络作模板延伸的BP网络 将模型结构 参数训练 算法都举例讲解的很透彻 为下面的CNN网络的学习打下坚实的基础 这个在线编辑器 体验太差了 好好写一篇长文章 想知道博客上与大家交流 转换过程太麻
  • R资源大全

    0 前言 虽然很早就知道R被微软收购 也很早知道R在统计分析处理方面很强大 开始一直没有行动过 直到 直到12月初在微软技术大会 看到我软的工程师演示R的使用 我就震惊了 然后最近在网上到处了解和爬一些R的资料 看着看着就入迷了 这就是个大
  • 论文阅读笔记4——MOTR: End-to-End Multiple-Object Tracking with TRansformer(利用Transformer进行多目标跟踪)

    首个完全基于端到端 Transformer 的多目标追踪模型 21年5月上传到arxiv上 论文 论文 代码 代码 space 1 Abstract 摘要大概由两部分组成 一是过去启发式算法的缺陷 二是MOTR的基本思路 他说MOT问题的关
  • 【CS229 lecture19】微分动态规划

    首先声明一下 这节课基本没听懂 但是还是把课程笔记写下 lecture19 微分动态规划 继续强化学习算法的讨论 Agenda 课程中段我曾讲过调试learning algorithm 今天再来将强化学习的部分 The motivating
  • 深度学习知识体系学习大全 牛!!

    搬来了大牛的博客 点击直接前往 https www yuque com angsweet machine learning jian jie 配一张大牛的思维导图 具体内容点进去都能看到 数学 机器学习 语言 算法 深度学习 书籍推荐 东西
  • 深度学习 vs 概率图模型 vs 逻辑学

    深度学习 vs 概率图模型 vs 逻辑学 发表于 2015 04 30 21 55 1359次阅读 来源 quantombone 0 条评论 作者 Tomasz Malisiewicz 深度学习 deep learning 图模型 人工智能
  • 机器学习简介

    介绍 机器学习是人工智能 AI 的一个子领域 机器学习的目标通常是理解数据的结构并将该数据拟合到人们可以理解和利用的模型中 尽管机器学习是计算机科学的一个领域 但它与传统的计算方法不同 在传统计算中 算法是计算机用来计算或解决问题的显式编程
  • 感知机分类学习

    感知机 perceptron 是一种二类分类的线性分类模型 也就是说 使用于将数据分成两类的 并且数据要线性可分的情况 线性可分是指存在一个超平面能够将空间分成两部分 每一部分为一类 感知机的目的就在于找这样的一个超平面 假设输入数据形式为
  • 二值分类模型的评价指标

    二值分类模型的评价指标主要有 Precision Recall F Score ROC and AUC ROC Receiver Operating Characteristic ROC曲线的横坐标为false positive rate
  • 【数据预处理】Pandas缺失的数据处理

    目录 缺少数据基础 何时 为何 数据丢失 被视为 缺失 的值 日期时间 插入缺失数据 缺少数据的计算 Sum Prod of Empties Nans GroupBy中的NA值 清理 填写缺失数据 填充缺失值 fillna 用PandasO
  • 【特征工程】特征选择与特征学习

    特征选择与特征学习 在机器学习的具体实践任务中 选择一组具有代表性的特征用于构建模型是非常重要的问题 特征选择通常选择与类别相关性强 且特征彼此间相关性弱的特征子集 具体特征选择算法通过定义合适的子集评价函数来体现 在现实世界中 数据通常是
  • LightGBM参数介绍

    Xgboost和LightGBM部分参数对照 Xgboots LightGbm booster default gbtree boosting default gbdt eta default 0 3 learning rate defau
  • 机器学习里的 kernel 是指什么?

    转自我的知乎回答 机器学习里的 kernel 是指什么 我换个角度来解释这个问题 机器学习在做回归或者分类时有一个很朴实的想法 预测 x 的值 那就在训练集 X 中寻找那些与 x 相似的样本 再把这些样本的值加权作为预测值 这里有两个问题
  • 【下降算法】最速下降法、Newton法、共轭梯度法

    文章目录 1 一维搜索 2 最速下降法 最速下降法特征 最速下降法的优缺点 3 Newton法 算法基本思想 牛顿法和梯度下降法的效率对比 4 共轭梯度法 1 一维搜索 最优化问题一般选择某一组变量 然后在满足一定的限制条件下 求出使目标值

随机推荐

  • 和为 K 的最少斐波那契数字数目(贪心)

    题目描述 给你数字 k 请你返回和为 k 的斐波那契数字的最少数目 其中 每个斐波那契数字都可以被使用多次 斐波那契数字定义为 F1 1 F2 1 Fn Fn 1 Fn 2 其中 n gt 2 数据保证对于给定的 k 一定能找到可行解 示例
  • 增强网关设计与使用

    增强网关 目的 整合错误码 对外显示友好 对内便于快速定位问题 记录出错请求 依照错误码制定处理策略 设计 状态码格式 示例 E01001B002 解析 E 统一前缀 表明异常 01 应用标识 001 功能域 B 错误类型 002 错误码
  • vue 3.0新特性之reactive与ref

    vue 3 0新特性 参考 https www cnblogs com Highdoudou p 9993870 html https www cnblogs com ljx20180807 p 9987822 html 性能优化 观察者机
  • Allegro自动备份PCB设计文件的方法

    受到误删原理图的影响 立刻把PCB的自动备份功能设置一下 和原理图备份不一样的是PCB备份文件和源文件的格式相同 只是名称不一样 这个名称是自己设置的 步骤如下 点击 Setup gt User Preferences 弹出 User Pr
  • Linux 端 Kaggle 数据集下载:API 下载

    Linux 端 Kaggle 数据集下载 API 下载 一 准备好 kaggle json 文件 1 登录 Kaggle 官网 2 点击右上角头像 gt Your Profile gt Account gt Create New Token
  • Pandas_设置单元格条件格式1——指定值字体变色、指定值设置背景色

    转载于 https www cnblogs com wodexk p 10801344 html
  • 普通工程师和高级工程师的差别在哪里?如何快速突破?

    作者 王拥军 编辑 迷鹿 王拥军 毕业于天津大学计算机系 拥有从计算机硬件到操作系统安全 从后台服务器到客户端的全平台工作经历 目前在腾讯自选股从事互联网证券软件研发管理 对上市公司及创业团队的产品 文化 经营等具有独到的见解 个人公众号
  • linux设置系统时间

    我们一般使用 date s 命令来修改系统时间 比如将系统时间设定成20066年10月19日的命令如下 date s 10 19 2006 将系统时间设定成下午1点12分0秒的命令如下 date s 13 12 00 注意 这里说的是系统时
  • 【字节面试题】小于n的最大数

    标题 小于n的最大数 题目描述 给定一个数你 入23121 给定一个数组A如 2 4 9 求由A中元素组成的 小于n的最大数 如小于23121的最大数是22999 思路 1 把数组排序 2 把int转换成字节数组 从第一个开始变量 如2 从
  • App6种常见的数据加载设计

    设计师在进行APP设计的设计时 往往会更加专注于界面长什么样 界面和界面之间怎么跳转 给予用户什么样的操作反馈 却偏偏特别容易忽略掉一个比较重要的环节 就是APP数据加载中的设计 所以会导致我们看到的APP 往往有着华丽的启动界面 然后就是
  • Python3, 19行代码,让微信登录页面地球转起来,涨见识了。

    19行代码动态展示微信地图 1 引言 2 代码实战 2 1 思路 2 2 示例 2 2 1 经纬度 2 2 2 制作gif 3 总结 1 引言 小屌丝 鱼哥 最近在干啥嘞 小鱼 干活呗 不然能干啥 小屌丝 嘿嘿 小鱼 你这笑的 怎么 那么
  • FPGA_分频(信号使能分频与计数器分频)(奇偶分频)

    时钟对于 FPGA 是非常重要的 但板载晶振提供的时钟信号频率是固定的 不一定满 足工程需求 所以分频和倍频还是很有必要的 一 计数器分频 这里通过计数的方式来实现分频 1 通过计数器来实现6分频 两种方式 第一种直接通过计数方式直接获取获
  • 华为OD机试 Java 实现【整型数组合并】【牛客练习题】

    一 题目描述 将两个整型数组按照升序合并 并且过滤掉重复数组元素 输出时相邻两数之间没有空格 二 输入描述 输入说明 按下列顺序输入 输入第一个数组的个数 输入第一个数组的数值 输入第二个数组的个数 输入第二个数组的数值 三 输出描述 输出
  • Qt添加第三方字体

    最近开发项目时 据说不能用系统自带的微软雅黑字体 于是找一个开源的字体 思源黑体 这个是google和Adobe公司合力开发的可以免费使用 本篇记录一下Qt使用第三方字体的方式 字体从下载之家下载http www downza cn sof
  • C#文件后缀名详解

    sln 解决方案文件 为解决方案资源管理器提供显示管理文件的图形接口所需的信息 csproj 项目文件 创建应用程序所需的引用 数据连接 文件夹和文件的信息 aspx Web 窗体页由两部分组成 视觉元素 HTML 服务器控件和静态文本 和
  • 什么是P = NP?问题

    文章目录 引言 天才基本法 什么是P NP问题 P NP 成立吗 总结 提示 以下是本篇文章正文内容 Java系列学习将会持续更新 引言 今天我们先放松一下 这篇文章并不是Java课程的学习 而是带大家认识一个学术问题 但是请大家放心 这里
  • libevent多线程使用事项

    在linux平台上使用c开发网络程序的同志们一般情况下都对鼎鼎大名的libevent非常的熟悉了 但是一些新进入此领域的new new people们对此都是一头雾水 原本的迷茫再加上开源软件一贯的 帮助文件 缺失作风 让我们这些新手们显的
  • 免费C/C++编译器

    不好意思 等到现在才想到要写这篇文章 怎么说呢 情况是这样的 刚开始我学习C语言时 是想在机器上安装visual c 的 因为Turbo C太古老了 用起来不方便 所以很自然地想安装vc 不过不知道大家有没有发现vc很大 而且有些机子就是安
  • 线程是什么意思

    线程是操作系统能够进行运算调度的最小单位 它被包含在进程之中 是进程中的实际运作单位 一条线程指的是进程中一个单一顺序的控制流 一个进程中可以并发多个线程 每条线程并行执行不同的任务 在Unix System V及SunOS中也被称为轻量进
  • Structural Time Series modeling in TensorFlow Probability

    在邯郸学步后 想要深入用好Tensorflow中的STS model 还是要静下心来 好好阅读点材料 f t f 1