[PYTHON] I implemented "Basics of Time Series Analysis and State Space Model" (Hayamoto) with pystan

What i did

-** Implemented the code in the stan chapter of "Basics of Time Series Analysis and State Space Models R and Stan Theory and Implementation" using pystan **

Who is the target of this article

-** I read Hayabusa because I wanted to know time series analysis, but I only understand python! Write in python! Person **

Introduction

Whenever you look up an introductory book on time series analysis, you will always find "Basics of time series analysis and state-space models: theory and implementation learned with R and Stan", so-called Hayabusa. In this book, "time series analysis such as AR model and MA model", "Gauss linear state space model", and "state space model by Bayesian estimation" are explained in a very detailed manner. ** The R and stan languages are used in the book, which is a bit of a pain for someone like me who can only use python. ** **

However, you can also use stan in python, so you can implement the same thing as introduced in the book. ** Therefore, I will introduce the Bayesian estimation by Hayashimoto's stan, which was actually implemented using python. ** **

Implementation is done using google colab. In google colab, it can be used only by importing by pystan. In other words, no initial settings are required, which is very convenient. ** Click here for the code. ** ** https://github.com/nakanakana12/hayabusa-bon

Implementation result

As for the code, refer to github, but here I will briefly introduce only the result.

Local level model estimation by Stan

The result of estimating the state space of the local level model (random walk with noise added) is as follows. ダウンロード (11).png

Blue is the state and orange is the observed value. The 95% section of the state estimated using stan is a blue net. Compared to the graph in the book, it seems to be implemented correctly.

Application: Models with complex observation equations

In the next chapter, we will model a slightly more complex equation. The blue line is the observed value, and the orange line is the value when there is no process error. This is also implemented correctly. ダウンロード (13).png

Summary

--Implemented Hayabusa's stan part using python. --Compare with the results of the book and confirm that it seems to be implemented correctly.

I hope it helps you even a little.

Reference site

--Hayamoto Support Page: https://logics-of-blue.com/tsa-ssm-book-support/ --R and stan code and data: https://github.com/logics-of-blue/book-tsa-ssm-foundation

Recommended Posts

I implemented "Basics of Time Series Analysis and State Space Model" (Hayamoto) with pystan
I tried time series analysis! (AR model)
Time series analysis 4 Construction of SARIMA model
Transfer function / state space model of RLC series circuit and simulation by Python
Time series analysis 1 Basics
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
"Measurement Time Series Analysis of Economic and Finance Data" Solving Chapter End Problems with Python
How to extract features of time series data with PySpark Basics
[Statistics] [Time series analysis] Plot the ARMA model and grasp the tendency.
Time series analysis 3 Preprocessing of time series data
Implement a model with state and behavior (3) --Example of implementation by decorator
Time series analysis 2 Stationary, ARMA / ARIMA model
Implement a model with state and behavior
Time series analysis # 6 Spurious regression and cointegration
Python: Time Series Analysis: Building a SARIMA Model
Python: Time Series Analysis: Stationarity, ARMA / ARIMA Model
Smoothing of time series and waveform data 3 methods (smoothing)
View details of time series data with Remotte
I tried morphological analysis and vectorization of words
I read and implemented the Variants of UKR
Transfer function / state space model of spring / mass / damper system and simulation by Python
I just wanted to extract the data of the desired date and time with Django
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to implement time series prediction with GBDT
I implemented collaborative filtering (recommendation) with redis and python
I read "Basics of Electric Circuits and Transmission Lines"
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
I learned the basics of reinforcement learning and played with Cart Pole (implementing simple Q Learning)