"Measurement Time Series Analysis of Economic and Finance Data" Solving Chapter End Problems with Python

[Tatsuyoshi Okimoto "Measurement Time Series Analysis of Economic and Finance Data"](https://www.amazon.co.jp/gp/product/4254127928/ref=as_li_ss_tl?ie=UTF8&ref_=nav_ya_signin&&linkCode=ll1&tag=sankichi92-22&linkId = cda8aa8cd64ad2bda6a911e37a8c9ca1) I'm solving the end-of-chapter problem "using a computer" with ** Python **.

@aokikenichi wrote an article about what he solved with ** R ** as follows, so I decided to publish what I solved with Python before.

-"Measurement Time Series Analysis of Economic and Finance Data" Solving the End Problem with R-Chapter 1 Basic Concept of Time Series Analysis- -"Measurement time series analysis of economic and finance data" Solving the end problem with R-Chapter 2 ARMA process- -"Measurement time series analysis of economic and finance data" Solving the end-of-chapter problem with R-Chapter 4 VAR model-

However, it was troublesome to paste the resulting chart, so I am writing only the code here. For the output result, see ** Updating Jupyter Notebook to Gist **.

Library to use

import numpy as np
import pandas as pd
from statsmodels.tsa.api import stattools, AR, ARMA, VAR
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.stats.diagnostic import acorr_ljungbox
import matplotlib.pyplot as plt

In particular, I often use pandas and statsmodels.tsa. I referred to the following documents.

1. Basic concept of time series analysis

1.3

mu_sigma = [(0, 1), (2, 1), (-2, 1), (0, 2), (0, 3), (2, 2)]
white_noise = DataFrame()
for mu, sigma in mu_sigma:
    name = '$\mu={0}, \sigma={1}$'.format(mu, sigma)
    white_noise[name] = np.random.normal(mu, sigma, 100)
white_noise.plot(subplots=True, layout=(3,2), figsize=(12, 12))

1.5

economicdata = pd.read_excel('http://www.geocities.jp/tatsuyoshi_okimoto/books/tsa/economicdata.xls', index_col='date')

(1)

economicdata.plot(subplots=True, layout=(3,2), figsize=(12, 12))

(2)

economicdata_pct = np.log(economicdata).diff() * 100

(3)

economicdata_pct[['topix', 'exrate', 'indprod']].plot(subplots=True, figsize=(12, 12))

(4)

indprod = economicdata_pct.indprod.dropna()

plot_acf(indprod, lags=20)
def portmanteau_test(endog, lags=10):
    q_m, pvalues = acorr_ljungbox(endog, lags=lags)
    df = pd.DataFrame([q_m.round(2), pvalues.round(3)], index=['Q(m)', 'p-value'], columns=range(1, lags + 1))
    return df

print(portmanteau_test(indprod))

(5)

topix = economicdata_pct.topix.dropna()
print(portmanteau_test(topix))
exrate = economicdata_pct.exrate.dropna()
print(portmanteau_test(exrate))

2. ARMA process

2.5

(1)

def plot_acf_pacf(x, lags=20):
    fig = plt.figure(figsize=(12,4))
    ax1 = fig.add_subplot(121)
    plot_acf(x, ax=ax1, lags=lags)
    ax2 = fig.add_subplot(122)
    plot_pacf(x, ax=ax2, lags=lags)
    return fig

plot_acf_pacf(indprod)
ar4 = AR(indprod).fit(maxlag=4)
plot_acf(ar4.resid, lags=20)
arma12 = ARMA(indprod, (1,2)).fit()
plot_acf(arma12.resid, lags=20)
def arma_order_select(y, orders):
    df = pd.DataFrame(index=['AIC', 'SIC'])
    for order in orders:
        model = ARMA(y, order).fit()
        df[str(order)] = [round(model.aic, 1), round(model.bic, 1)]
    df['min'] = df.idxmin(axis=1)
    return df

orders = [(4,0), (0,3), (1,1), (2,1), (1,2), (2,2)]
print(arma_order_select(indprod, orders))

(2)

print(portmanteau_test(ar4.resid))
print(portmanteau_test(arma12.resid))

2.6

arma = pd.read_excel('http://www.geocities.jp/tatsuyoshi_okimoto/books/tsa/arma.xls')

(1)

y1 = arma.y1.values
plot_acf_pacf(y1)

(3)

order_select = stattools.arma_order_select_ic(y1, max_ar=2, max_ma=2,  ic=['aic', 'bic'])
print(order_select['aic'])
print(order_select['bic'])
print('AIC:', order_select['aic_min_order'], ', SIC:', order_select['bic_min_order'])

(4)

ar2 = AR(y1).fit(maxlag=2)
plot_acf(ar2.resid, lags=20)
print(portmanteau_test(ar2.resid))
arma21 = ARMA(y1, (2,1)).fit()
plot_acf(arma21.resid, lags=20)
print(portmanteau_test(arma21.resid))

(5)

y2 = arma.y2.values
plot_acf_pacf(y2)
order_select = stattools.arma_order_select_ic(y2, max_ar=2, max_ma=2, ic=['aic', 'bic'])
print(order_select['aic'])
print(order_select['bic'])
print('AIC:', order_select['aic_min_order'], ', SIC:', order_select['bic_min_order'])
arma21 = ARMA(y2, (2,1)).fit()
plot_acf(arma21.resid, lags=20)
print(portmanteau_test(arma21.resid))
y3 = arma.y3.values
plot_acf_pacf(y3)
orders = [(4,0), (8,0), (1,1), (1,2), (1,3), (2,1), (2,2), (2,3)]
print(arma_order_select(y3, orders))
arma23 = ARMA(y3, (2,3)).fit()
plot_acf(arma23.resid, lags=20)
print(portmanteau_test(arma23.resid))
arma11 = ARMA(y3, (1,1)).fit()
plot_acf(arma11.resid, lags=20)
print(portmanteau_test(arma11.resid))

4. VAR model

msci_day = pd.read_excel('http://www.geocities.jp/tatsuyoshi_okimoto/books/tsa/msci_day.xls', index_col='Date')
msci_pct = np.log(msci_day).diff().dropna() * 100

4.5

(1)

jp_uk_us = ['jp', 'uk', 'us']
var3 = VAR(msci_pct[jp_uk_us]).fit(3)

import itertools

df = pd.DataFrame(index=['Statistical test statistic', 'p-value'])
for pair in itertools.permutations(jp_uk_us, r=2):
    col = pair[1] + ' → ' + pair[0]
    res = var3.test_causality(*pair, verbose=False)
    df[col] = [res['statistic'].round(3), res['pvalue'].round(3)]
print(df)
var3.irf().plot(orth=True)
var3.fevd().plot()

4.6

(4)

model = VAR(msci_pct[['jp', 'fr', 'ca']])
model.select_order(10)
result = model.fit(maxlags=10, ic='aic')

(5)

result.test_causality('fr', 'jp')

result.test_causality('fr', 'ca')

(6)

result.irf().plot(impulse='fr', response='jp')

(7)

result.fevd().plot()

Recommended Posts

"Measurement Time Series Analysis of Economic and Finance Data" Solving Chapter End Problems with Python
Implementation of clustering k-shape method for time series data [Unsupervised learning with python Chapter 13]
Python: Time Series Analysis: Preprocessing Time Series Data
Time series analysis 3 Preprocessing of time series data
Plot CSV of time series data with unixtime value in Python (matplotlib)
Smoothing of time series and waveform data 3 methods (smoothing)
View details of time series data with Remotte
Format and display time series data with different scales and units with Python or Matplotlib
Data analysis with python 2
Data analysis with Python
Challenge principal component analysis of text data with Python
Analysis of measurement data ②-Histogram and fitting, lmfit recommendation-
I implemented "Basics of Time Series Analysis and State Space Model" (Hayamoto) with pystan
Get a large amount of Starbucks Twitter data with python and try data analysis Part 1
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python
Python for Data Analysis Chapter 4
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
Perform isocurrent analysis of open channels with Python and matplotlib
Get rid of dirty data with Python and regular expressions
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
Execution time measurement with Python With
[Python] Plot time series data
Graph time series data in Python using pandas and matplotlib
Python for Data Analysis Chapter 2
Python for Data Analysis Chapter 3
Recommended books and sources of data analysis programming (Python or R)
How to extract features of time series data with PySpark Basics
Comparison of time series data predictions between SARIMA and Prophet models
Practical exercise of data analysis with Python ~ 2016 New Coder Survey Edition ~
Practice of data analysis by Python and pandas (Tokyo COVID-19 data edition)
[Python] Get economic data with DataReader
About time series data and overfitting
Differentiation of time series data (discrete)
Data analysis starting with python (data visualization 1)
Coexistence of Python2 and 3 with CircleCI (1.0)
Data analysis starting with python (data visualization 2)
Rewrite the field creation node of SPSS Modeler with Python. Feature extraction from time series sensor data
Power of forecasting methods in time series data analysis Semi-optimization (SARIMA) [Memo]
Try to make foldl and foldr with Python: lambda. Also time measurement
[In-Database Python Analysis Tutorial with SQL Server 2017] Step 3: Data Exploration and Visualization
I have 0 years of programming experience and challenge data processing with python
Rehabilitation of Python and NLP skills starting with "100 Language Processing Knock 2015" (Chapter 1)
Forecasting time series data with Simplex Projection
Data pipeline construction with Python and Luigi
Predict time series data with neural network
Precautions when solving DP problems with Python
[Python] Accelerates loading of time series CSV
Time series analysis 4 Construction of SARIMA model
Tweet analysis with Python, Mecab and CaboCha
Recommendation of Altair! Data visualization with Python
Data analysis starting with python (data preprocessing-machine learning)
Easy introduction of python3 series and OpenCV3
Reading OpenFOAM time series data and sets data
Time series analysis # 6 Spurious regression and cointegration
Python practice data analysis Summary of learning that I hit about 10 with 100 knocks
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 1
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 2
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
[Basics of modern mathematical statistics with python] Chapter 2: Probability distribution and expected value