[Tatsuyoshi Okimoto "Zeitreihenanalyse von Wirtschafts- und Finanzdaten"](https://www.amazon.co.jp/gp/product/4254127928/ref=as_li_ss_tl?ie=UTF8&ref_=nav_ya_signin&&linkCode=ll1&tag=sankichi92-2 = cda8aa8cd64ad2bda6a911e37a8c9ca1) Ich löse das Problem am Ende des Kapitels "Verwenden eines Computers" mit ** Python **.
@aokikenichi hat einen Artikel darüber geschrieben, was er mit ** R ** gelöst hat, also habe ich beschlossen, das zu veröffentlichen, was ich zuvor mit Python gelöst habe.
Es war jedoch mühsam, das resultierende Diagramm einzufügen, daher schreibe ich hier nur den Code. Das Ausgabeergebnis finden Sie unter ** Hochladen des Jupyter-Notizbuchs in Gist **.
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
Insbesondere benutze ich oft pandas und statsmodels.tsa. Ich habe auf die folgenden Dokumente verwiesen.
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-Wert'], 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.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))
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=['Statistische Testmenge', 'p-Wert'])
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