Local blog for Japanese speaking developers
TensorFlow Probability による構造時系列(STS)モデリング
2019年4月22日月曜日
この記事は Dave Moore、Jacob Burnim と TFP チームによる TensorFlow - Medium の記事 "
Structural Time Series modeling in TensorFlow Probability
" を元に翻訳・加筆したものです。詳しくは元記事をご覧ください。
投稿者: Dave Moore、Jacob Burnim と TFP チーム
本投稿では、
TensorFlow Probability
の新しいライブラリ
tfp.sts
について紹介します。このライブラリは、構造時系列モデルを使って時系列を予測します [3]。
概要
「予想とは難しいものだ。特に未来に関しては」
—
Karl Kristian Steincke
未来の出来事の予想はどうしても不確かなものになりますが、予測なしに将来の計画を立てることはできません。ウェブサイトの所有者は、サイトにアクセスするユーザーの数を予測して十分なハードウェア リソースを準備しなければなりません。また将来の収益や費用も予想する必要があります。企業は消費者製品の将来の需要を予測し、十分な在庫を確保する必要があります。電力会社は、電気の需要を予測し、その情報に基づいて電力の購買契約を結んだり、新しい発電所を建設したりする必要があります。
時系列を予測する手法は、ある機能をリリースした際のユーザー エンゲージメントへの影響など、何らかの変化が起きた場合の影響の推論に利用できます [1]。また、現在の失業率のような観測が難しい値をより入手しやすい情報から推論したり [2]、時系列データの異常値を検出したりするためにも用いられます。
構造時系列モデル
構造時系列(STS)モデル [3] は時系列を扱う確率モデルの一種で、次のような多くの標準的な時系列モデルの考え方を取り入れて汎用化したものです。
自己回帰プロセス
移動平均
ローカル線形トレンド
周期性
外部共変量(関心対象の時系列に関係する可能性がある他の時系列)に対する回帰と変数選択
STS モデルは、観測された時系列をより簡単な構成要素の合計として表現します。
それぞれの構成要素は、特定の構造を仮定して得られる時系列です。たとえば、いくつかの共変量時系列に対して、ある要素は周期的影響(例: 曜日による影響)を、別の要素はローカル線形トレンドを、さらに別の要素は線形依存性を表せます。
構造時系列を使うと、モデル作成者がデータの生成プロセスについての仮定を表現できるので、かなり少ないデータ(例えばデータが数 10 個しかない入力系列ひとつ)からでも妥当な予測をしやすくなります。また、過去のデータや将来の予測を構造化された要素へ分解、視覚化すれば、モデルによる予想の解釈も可能です。さらに確率的なモデリングによって、欠落したデータを自然に処理でき、一定の原則に基づいて「不確かさ」を定量的に扱えます。
TensorFlow Probability の構造時系列
TensorFlow Probability(TFP)は現在、構造時系列モデルを使ったフィッティングや予測を標準でサポートしています。変分推論(VI)やハミルトニアン・モンテカルロ法(HMC)を使ったモデル パラメーターのベイズ推論などがサポートされており、点推定と不確かさの推定の両方を計算できます。TensorFlow を使って構築されているので、これらの手法はそのままでもベクトル ハードウェア(GPU や TPU)のメリットを活用でき、多くの時系列を効率的かつ並列に処理したり、ディープ ニューラル ネットワークと統合したりできます。
例: CO2 濃度の予測
実際の構造時系列の例として、ハワイのマウナロア観測所で月ごとに記録した大気中の CO2 濃度について考えてみます [5]。
このように、この時系列には、長期的なトレンドと季節による年次変動の両方が含まれています。この 2 つの構成要素は、わずか数行の TFP コードを使って構造時系列モデルで直接表現できます。
https://medium.com/media/b72c9fa1a15fcffe2c2ac48761b75325/href
ここでは、ローカル線形トレンドモデルを使います。このモデルは、トレンドが線形で、ランダム ウォークにしたがって時間とともにゆっくりと変化する傾きを持つと仮定しています。モデルをデータにフィッティングさせると、モデルの仮定に基づいた確率的予測が生成されます。
この線形トレンドモデルでは、時間とともに予測の不確実性(標準偏差 ± 2 で網掛け)が増加しています。これは、傾きを補間する際の信頼度が小さくなっていくためです。予測の中央値は、既存トレンドを線形的に補間したものと、季節変動を組み合わせたものになっています。大気中の CO2 濃度の増加は加速しており、その点がわずかに過小評価されていますが、実際の値は 95% の予測区間内にあります。
この例のコード
は、すべて Github で公開されています。
例: 電気の需要予測
次に、もう少し複雑な例について考えてみます。オーストラリアのビクトリア州の電力需要予測です。上のグラフは、2014 年最初の 6 週間の 1 時間ごとの記録です([4] のデータ、
https://github.com/robjhyndman/fpp2-package
で公開されているもの)。
ここでは。冷房用の電気の需要と相関関係がある気温のデータを外部からのデータとして利用します(オーストラリアでは 1 月は夏であることに注意しましょう)。この気温データを STS モデルに組み込みます。STS モデルには、線形回帰を通して外部共変量を含めることができます。
https://medium.com/media/a5b96322ddf96550cdada23445f62c21/href
ここでは、複数の周期的影響を含めている点に注意してください。時間と曜日による影響に加え、説明できないその他の影響をモデリングするための自己回帰コンポーネントも加えています。単純なランダム ウォークも使えますが、分散を一定に抑えるため自己回帰コンポーネントを選んでいます。
このモデルによる予測は完璧ではなく、まだモデリングできていない何らかの要因がありそうです。しかし、まったくの的外れではなく、不確定性も妥当な範囲に収まっているように見えます。予測を個々の要素に分解して視覚化すると、これをよりよく理解することができます(グラフの y 軸のスケールは要素によって異なります)。
このモデルは、時間による大きな影響と、曜日による小さな影響(土曜日と日曜日に需要が最も低くなる)、さらに気温によるかなり大きな影響を相当正確に特定しています。これらの影響からは、かなり信頼性の高い予測を行っています。予想の不確定性の大部分は、観測された時系列におけるモデリングできない(その他の)変動を予測する自己回帰プロセスによるものです。
モデル作成者は、このような個々の要素への分解によってモデルの改善方法を知ることができます。たとえば上図では、自己回帰のスパイクは気温のスパイクと重なっている部分があります。つまり、特徴量やデータ変換をさらに追加すれば、気温による影響をもう少し正確にモデル化できるかもしれません。
この例のコード
は、すべて Github で公開されています。
TensorFlow Probability STS ライブラリ
以上の例からわかるように、TFP ではモデルの構成要素を組み合わせて STS モデルを構築できます。STS は、次のようなモデルの要素を提供しています。
Autoregressive
、
LocalLinearTrend
、
SemiLocalLinearTread
、
LocalLevel
: ランダム ウォークやその他のプロセスにしたがって変化する大きさや傾きを持つ時系列のモデリング
Seasonal
: 時間、曜日、月などの周期的要因に基づく時系列のモデリング
LinearRegression
: 時間によって変化するその他の共変量に基づく時系列のモデリング。休日など個々の日付に固有の影響も表せる
STS は、
変分推論
と
ハミルトニアン・モンテカルロ法
を使って結果の時系列モデルをフィッティングする手法を提供します。
TFP ホームページ
で、コードやドキュメント、その他の例をご覧ください。
構造時系列は、Google 内部でもいくつかの重要な時系列に応用されています。構造時系列が皆さんにも役立つことを期待しています。最新の Tensorflow Probability のお知らせや、その他の TFP に関するディスカッションに興味がある方は、
tfprobability@tensorflow.org
フォーラムに参加してください。
参考文献
[1] Brodersen, K. H., Gallusser, F., Koehler, J., Remy, N., & Scott, S. L. (2015). Inferring causal impact using Bayesian structural time-series models.
The Annals of Applied Statistics
,
9
(1), 247–274.
[2] Choi, H., & Varian, H. (2012). Predicting the present with Google Trends. Economic Record, 88, 2–9.
[3] Harvey, A. C. (1989).
Forecasting, structural time series models and the Kalman filter
. Cambridge University Press.
[4] Hyndman, R.J., & Athanasopoulos, G. (2018). Forecasting: principles and practice, 2nd edition, OTexts: Melbourne, Australia. OTexts.com/fpp2. Accessed on February 23, 2019.
[5] Keeling, C. D., Piper, S. C., Bacastow, R. B., Wahlen, M., Whorf, T. P., Heimann, M., & Meijer, H. A. (2001). Exchanges of atmospheric CO2 and 13CO2 with the terrestrial biosphere and oceans from 1978 to 2000. I. Global aspects, SIO Reference Series, №01–06, Scripps Institution of Oceanography, San Diego.
Reviewed by
Kaz Sato - Staff Developer Advocate, Google Cloud
ラベル
.app
1
.dev
1
#11WeeksOfAndroid
13
#11WeeksOfAndroid Android TV
1
#Android11
3
#DevFest16
1
#DevFest17
1
#DevFest18
1
#DevFest19
1
#DevFest20
1
#hack4jp
3
11 weeks of Android
2
A/B Testing
1
A4A
4
Accelerator
3
Accessibility
1
Actions on Google
16
Activation Atlas
1
Addy Osmani
1
ADK
2
AdMob
32
Ads
67
Ads API
66
Advanced Protection Program
3
AdWords API
14
Agency
1
AI
13
AIY
3
AIY Vision Kit
2
ALPN
1
AMP
116
AMP Cache
9
AMP Camp
2
AMP CSS
1
AMP Extension
1
AMP Fest
1
AMP for Email
4
AMP Optimizer
1
AMP Packager
1
AMP Playground
1
AMP Plugin
1
AMP SSR
1
AMP Story
4
AMP Toolbox
1
amp-bind
1
amp.dev
1
AMPHTML Ads
1
Analytics
9
Andorid
12
Android
393
Android 10
1
Android 11
20
Android 11 Compatibility
1
Android 11 final release
1
Android 11 meetups
1
Android 9
1
Android App Bundle
1
Android App Development
23
Android Architecture
1
Android Architecture Components
1
Android Auto
1
Android Design Support Library
1
Android Developer
14
Android Developer Story
4
Android Developers
13
Android Enterprise
5
Android for cars
2
Android Go
1
Android Jetpack
6
Android N
18
Android O
14
Android Open Source Project
1
Android P
7
Android Pay
1
android privacy
1
Android Q
13
android security
2
Android Security Year in Review
1
Android Studio
47
Android Studio 4.1
1
Android Support Library
6
Android Things
15
Android Tools
2
Android TV
11
Android Vitals
4
Android Wear
29
android11
6
androidmarket
3
androidstudio
1
AndroidX
6
Angular
2
Angular 2
2
AngularJS
2
Anthos
2
AoG
1
aosp
1
API
26
APIExpert
45
apk
2
APM
1
app
3
App Action
1
App Bundle
2
app engine
23
App Indexing
7
App Invites
6
App Maker
2
AppCompat
2
Apps Flutter eBay
1
Apps Script
11
aprilfool
4
AR
2
Architecture Components
7
ARCore
1
ArtTech
1
Associate Android Developer Certificate
1
Audio
7
Auth Code
1
Authentication
9
AuthSub
2
Autofill
4
AutoML
1
Autotrack
2
award
1
Awareness API
1
Beacons
6
bento
1
BERT
1
Best Practices
1
beta
1
Better Ads Standards
3
BigQuery
10
Billing
1
Biometrics
1
BLE
4
Blink
1
Blockly
1
blogger
1
BodyPix
1
Brillo
1
Brotli
2
Budou
1
Buildbetterapps
2
Calendar
3
campaign
1
Campus
1
Canvas
1
Cardboard
4
Career
1
Case Study
2
CCPA
1
CDS 2020
3
CDS Recap 2020
3
Certificate
2
chrome
189
Chrome Custom Tab
4
Chrome Dev Summit
5
chrome extension
14
Chrome for Android
2
Chrome for iOS
3
Chrome OS
7
Chrome Tech Talk Night
4
Chromebook
4
Chromecast
7
chromewebstore
9
Chromium
7
CLI
1
ClientLogin
3
Closure Compiler
1
Cloud
23
Cloud AI Platform
1
Cloud Firestore
5
Cloud Functions
9
Cloud IoT Device SDK
1
Cloud Next
9
Cloud OnAir
3
Cloud OnBoard
4
Cloud PubSub
1
Cloud Storage
1
Cloud Study Jams
3
Cloud Summit
1
Cloud Test Lab
2
Cloudflare
1
CNN
1
Coalition for Better Ads
2
CocoaPods
1
code review
1
codejam
5
codelab
5
Codepen
1
Colaboratory
1
Community
5
compatibility
1
Compose
1
compute engine
3
Contests
1
Context
1
controls
1
Conversation API
1
conversations
2
conversion
1
Cookie
10
Coral
2
COVID-19
2
Crash Reporting
2
Crashlytics
2
Custom Element
1
Custom Model
1
CWV
1
dark theme
1
Dart
2
DataCenter
1
Daydream
4
Deep Learning
4
Delegation
1
Demo Party
1
Design Patterns
1
Design Sprint
3
DesignBytes
1
Designer
1
DevArt
3
DevBytes
6
Developer
14
Developer Console
4
Developer Preview
6
Developer Relations
2
Developer Review
1
Developers Story
4
DevFest
10
DevFestX
3
DevOps
1
devtools
4
Dialogflow
1
Differential privacy
1
Digital Goods API
1
DirectShare
1
Discover
1
DNS-over-HTTPS
4
Domain
1
Doodle
1
DoubleClick
4
Doze モード
1
drive
2
DX
1
Dynamic Links
3
EarlGrey
1
Easter Egg
1
ECMAScript 2015
1
Eddystone
4
Edge
1
egypt
1
Encryption
1
English
2
Envoy
1
error
1
ES2015
1
ES2016
1
ES6
2
ES7
1
Event
6
Featured
25
FIDO
5
filter
1
final release
1
Firebase
120
Firebase Admin SDK
6
Firebase Analytics
10
Firebase Auth
4
Firebase Cloud Messaging
10
Firebase Crashlytics
2
Firebase Database
5
Firebase Libraries
1
Firebase Notifications
1
Firebase Performance
3
Firebase Remote Config
6
Flash
1
Flutter
6
Flutter App Development
1
font
3
fraud
1
G Suite
19
game
42
Game Developers Conference 2018
1
Game Developers Conference 2019
1
Gboard
1
GCP
17
GCPUG
1
GDC
1
GDD11JP
56
GDD2010JP
23
GDE
2
GDG
21
GDG Cloud
1
Geo
55
Gingerbread
1
GLIDE
5
Gmail
5
Gmail API
2
Go
1
golang
5
goo.gl
1
Google
7
Google Analytics
3
Google API
1
Google Apps
14
Google Apps Script
4
Google Assistant
10
Google Assistant SDK
2
Google Cast
8
Google Cloud
34
Google Cloud Day
4
Google Cloud INSIDE Digital
2
Google Cloud INSIDE Games & Apps
8
Google Cloud INSIDE Media
1
Google Cloud INSIDE Retail
3
Google Cloud Messaging
11
Google Cloud Platform
16
Google Code-in
1
Google Developer Experts
2
Google Developers Academy
1
Google Developers live
5
Google Developers Summit
2
Google Drive
6
Google Earth
1
Google Fit
2
Google for Games
2
Google for Mobile
2
Google for Startups
4
Google for Work
1
Google I/O
17
Google Impact Challenge
1
Google Maps
62
Google Maps Platform
50
Google ML Summit
2
Google Open Source Peer Bonus
1
Google Pay
4
Google Photo
1
Google Play
147
Google Play App Safety
1
Google Play Billing
1
Google Play Console
15
Google Play developer distribution agreement
1
Google Play Developer Policies
2
Google Play Game Services
10
Google Play Instant
1
Google Play Services
23
Google Play Store
1
Google Play アプリ署名
1
Google Plus
14
Google Search
7
Google Sheets API
3
Google Sign-In
12
Google Slides API
5
Google Summer of Code
1
Google Tag Manager
1
Google Trust Services
2
Google マップ
4
google_ads_api_v6
1
Google+
2
Googleapps
10
GoogleGames
1
GoogleI/O
28
GoogleLabs
1
GooglePlay
3
GoogleTV
1
GPS
1
Gradle
1
gRPC
2
GTUG
5
GWT
2
hack4jp
2
hackathon
7
Hangouts Chat
2
Hosting
3
hotel
1
HTML5
17
HTML5Rocks
1
HTTP/2
5
HTTPS
14
I/O Extended
4
ID Token
1
Identity
15
Identity Toolkit
1
IGF2010
4
IGF2020
2
Ignite
4
IME
11
Indie Game
7
Indie Games Festival
7
Indie Games Festival 2018
2
Indie Games Festival 2019
12
Indie Games Festival 2020
7
Inevitable ja Night
30
Insights
1
Instagram
1
Instant Apps
6
intern
2
Invites
1
IO19
3
iOS
22
IoT
7
IPv6
1
Issue Tracker
2
IWD
1
Japanese
6
Japanese Developer
1
Japanese Input
1
JavaScript
11
Jetpack
5
Jetpack Compose
3
K-12
1
Key Transparency
1
Knowledge Graph
1
Kotlin
24
Kotlin Android Extensions
1
Kotlin Beginners
3
Kotlin Vocabulary
2
Kubernetes
2
l10n
8
latest
18
latest news
1
LaunchPad
2
lifull
1
Lighthouse
1
LINE
1
Local AI
1
Location
1
Lollipop
10
Machine Learning
29
MAD Skills
2
MADSkills
2
Maker Faire Tokyo
1
Marshmallow
10
Material
1
Material Design
31
MDL
1
MDN
1
metrics
1
MIDI
2
mikan
1
Mixed Contents
4
ML
1
ML Kit
12
Mobile
15
Mobile Bootcamp
4
Mobile Sites certification
1
Mobile Vision
4
mod_pagespeed
1
Model Maker
1
monetization
2
monetize
3
Mozc
15
Music
1
NativeDriver
2
NativeScript
1
Navigation
1
NBU
1
ndk
3
Nearby
5
News
1
Next Extended
1
Next Tokyo
3
Nexus
2
Nexus S
1
NFC
1
Node.js
3
notifications
2
Noto CJK
1
Now in Android
13
NPAPI
2
NPN
1
oauth
12
open source
1
OpenAI
1
opencensus
1
OpenGL
4
OpenID
3
OpenID Connect
4
OpenSocial
1
opensource
18
OpenTitan
1
Optimization
1
Page Experience
1
Password Manager
2
Payment
8
Payment Handler API
1
Payment Request API
1
PDF
1
PEM
33
people
2
People API
3
Performance
14
Performance budget
1
permissions
1
PersonFinder
1
Physical Web
3
Pi
1
Pixel
1
Place Picker
1
Platform Stability
1
Play Billing
2
Play Billing Library
2
Play Console
2
Player Analytics
4
Playtime 2017
1
Policy
8
policy compliance
2
policy violations
2
Polymer
7
pricing
1
privacy
13
Privacy Sandbox
15
Progressive Web Apps
14
project hosting
1
Promise
1
Promo code
1
Protocol Buffers
1
PRPL
1
publicdata
1
Push API
1
Push Notification
6
PWA
3
Python
2
QUIC
2
QWIKLABS
3
RAIL
1
React
1
React Native
2
Realtime Database
9
Recap Live Japan 2019
3
reCaptcha
1
Redux
1
release
2
Remote Config
3
Remote Display API
1
Requirements
1
Resonance Audio
1
Rewarded Video Ads
2
Runtime Permission
1
Safe Browsing
3
safety
1
Sample Code
2
Santa Tracker
1
schema.org
1
script
1
secur
1
security
72
Service Worker
4
SHA-1
1
sketchup
1
SmartLock for Passwords
4
social
4
Social Good
1
Social Media
1
software development
1
SPDY
3
speak2tweet
1
Spreadsheet
3
SSR
1
stable release
1
startup
7
Storage
2
story
2
streetview
3
Study Jams
9
subscriptions
5
sunset
1
Swift
2
SwiftShader
1
Symantec
1
tag
1
tapple
1
Task
4
Team Drive
1
techtalk
13
TensorFlow
41
TensorFlow Federated
1
TensorFlow Lite
8
TensorFlow Object Detection API
1
TensorFlow Probability
2
TensorFlow.js
4
test
4
Test Lab
6
TF Certificate
2
TFX
1
TLS
1
ToS
1
trace
1
Transliteration
1
Trusted Web Activity
1
Twitter
1
Udacity
20
Unity
3
update
1
UX
5
v3
1
v6.1
1
V8
3
VP9
1
VR
11
Vulkan
2
Watch Face
2
wave
2
Wear OS
3
Weave
1
Web
34
Web Animations
1
Web Components
7
Web Manifest
1
Web Packaging
3
Web Stories
2
Web Story
3
Web Vitals
5
web.dev
1
WebAssembly
5
WebGL
1
WebM
1
WebMusic
5
WebRTC
1
WebView
1
Women Techmakers Scholars Program
1
Wordpress
1
workmanager
1
WTM
6
Xcode
1
YouTube
17
YouTube API
1
インタビュー
1
コードサンプル
1
プライバシー
1
機械学習
3
言論の自由
1
節電
3
定期購入
1
東日本大震災
9
日本語入力
41
ブログ アーカイブ
2021
3
2
1
2020
12
11
10
9
8
7
6
5
4
3
2
1
2019
12
11
10
9
8
7
6
5
4
3
2
1
2018
12
11
10
9
8
7
6
5
4
3
2
1
2017
12
11
10
9
8
7
6
5
4
3
2
1
2016
12
11
10
9
8
7
6
5
4
3
2
1
2015
12
11
10
9
8
7
6
5
4
3
2
2014
12
11
10
9
8
7
6
5
4
3
2
1
2013
12
11
10
9
8
7
6
5
4
3
2
1
2012
12
11
10
9
8
7
6
5
4
3
2
1
2011
12
11
10
9
8
7
6
5
4
3
2
1
2010
12
11
Feed
Follow @googledevjp
"プロダクトに関するご意見は
プロダクトフォーラム
にお願いします"