コード例 #1
0
import pymc3 as pm
import statsmodels.api as sm
import theano
import theano.tensor as tt
from pandas.plotting import register_matplotlib_converters
from pandas_datareader.data import DataReader

plt.style.use("seaborn")
register_matplotlib_converters()

# ### 2. Download and plot the data on US CPI
#
# We'll get the data from FRED:

cpi = DataReader("CPIAUCNS", "fred", start="1971-01", end="2018-12")
cpi.index = pd.DatetimeIndex(cpi.index, freq="MS")

# Define the inflation series that we'll use in analysis
inf = np.log(cpi).resample("QS").mean().diff()[1:] * 400
inf = inf.dropna()
print(inf.head())

# Plot the series
fig, ax = plt.subplots(figsize=(9, 4), dpi=300)
ax.plot(inf.index, inf, label=r"$\Delta \log CPI$", lw=2)
ax.legend(loc="lower left")
plt.show()

# ### 3. Fit the model with maximum likelihood
#
# Statsmodels does all of the hard work of this for us - creating and
コード例 #2
0
# In simple time series models, like AR(r) models, convergence is achieved
# fairly quickly, and this can limit the performance benefit to using the
# Chandrasekhar recursions. Herbst (2015) focuses instead on DSGE (Dynamic
# Stochastic General Equilibrium) models instead, which often have a large
# state vector and often a large number of periods to achieve convergence.
# In these cases, the performance gains can be quite substantial.

# #### Practical example
#
# As a practical example, we will consider monthly data that has a clear
# seasonal component. In this case, we look at the inflation rate of
# apparel, as measured by the consumer price index. A graph of the data
# indicates strong seasonality.

cpi_apparel = DataReader('CPIAPPNS', 'fred', start='1986')
cpi_apparel.index = pd.DatetimeIndex(cpi_apparel.index, freq='MS')
inf_apparel = np.log(cpi_apparel).diff().iloc[1:] * 1200
inf_apparel.plot(figsize=(15, 5))

# We will construct two model instances. The first will be set to use the
# Kalman filter recursions, while the second will be set to use the
# Chandrasekhar recursions. This setting is controlled by the
# `ssm.filter_chandrasekhar` property, as shown below.
#
# The model we have in mind is a seasonal autoregression, where we include
# the first 6 months as lags as well as the given month in each of the
# previous 15 years as lags. This implies that the state vector has
# dimension $m = 186$, which is large enough that we might expect to see
# some substantial performance gains by using the Chandrasekhar recursions.
#
# **Remark**: We set `tolerance=0` in each model - this has the effect of
コード例 #3
0
from pandas_datareader.data import DataReader
import pandas as pd
import matplotlib.pyplot as plt
from datetime import date

series_code = 'DEXJPUS'
data_source = 'fred'
startDate = date(1979, 1, 1)
jpy = DataReader(series_code, data_source=data_source, start=startDate)
jpy.index = pd.to_datetime(jpy.index)
jpy['usdjpy'] = jpy['DEXJPUS']
del jpy['DEXJPUS']
jpy['returns'] = jpy['usdjpy'].pct_change()
autocorrelation_daily = jpy['returns'].autocorr()
print("The daily autocorrelation is: " + str(autocorrelation_daily))
jpyWeekly = jpy.resample('W').last()
jpyWeekly['weekly_returns'] = jpyWeekly['usdjpy'].pct_change()
atuocorrelation_weekly = jpyWeekly['weekly_returns'].autocorr()
print("The weekly autocorrelation is: " + str(atuocorrelation_weekly))
jpyMonthly = jpy.resample('M').last()
jpyMonthly['returns'] = jpyMonthly['usdjpy'].pct_change()
autocorrelation_monthly = jpyMonthly['returns'].autocorr()
print("The monthly autocorrelation is: " + str(autocorrelation_monthly))
コード例 #4
0
from common import *

# ARMA(1, 1) model
from pandas_datareader.data import DataReader
cpi = DataReader('CPIAUCNS', 'fred', start='1971-01', end='2016-12')
cpi.index = pd.DatetimeIndex(cpi.index, freq='MS')
inf = np.log(cpi).resample('QS').mean().diff()[1:] * 400


def output():
    fig, ax = plt.subplots(figsize=(13, 3), dpi=300)
    ax.plot(inf.index, inf, label=r'$\Delta \log CPI$')
    ax.legend(loc='lower left')
    ax.yaxis.grid()
    fig.savefig(os.path.join(PNG_PATH, 'fig_2-inf.png'), dpi=300)
    fig.savefig(os.path.join(PDF_PATH, 'fig_2-inf.pdf'), dpi=300)


if __name__ == '__main__':
    output()
コード例 #5
0
import datetime as dt
from __future__ import absolute_import

pd.core.common.is_list_like = pd.api.types.is_list_like
from pandas_datareader.data import DataReader
import matplotlib.pyplot as plt
from numpy import loadtxt, where

end = dt.datetime.now()
start = end - dt.timedelta(days=5 * 356)

df = DataReader("MU", "iex", start, end)

# df.reset_index(inplace = True)

df.index = pd.to_datetime(df.index, format='%Y-%m-%d')
df['year'] = df.index.year.values
df['month'] = df.index.month.values
df['day'] = df.index.day.values

df.head()

# print(year)
df['date'] = df['year'].astype(str) + '-' + df['month'].astype(
    str) + '-' + df['day'].astype(str)

# df['date'] = pd.to_datetime(df[['year', 'month','day']])
df.date.values

df.tail()