text
stringlengths
1
93.6k
from Functions.summarize import summarize
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import numpy as np
#-------------------------------------------------Set dataframe to full view
pd.set_option('display.expand_frame_repr', False)
#-------------------------------------------------User Inputs
vintage = '2016-06-29' # vintage dataset to use for estimation
country = 'US' # United States macroeconomic data
sample_start = dt.strptime("2000-01-01", '%Y-%m-%d').date().toordinal() + 366 # estimation sample
#-------------------------------------------------Load model specification and dataset.
# Load model specification structure `Spec`
Spec = load_spec('Spec_US_example.xls')
# Parse `Spec`
SeriesID = Spec.SeriesID
SeriesName = Spec.SeriesName
Units = Spec.Units
UnitsTransformed = Spec.UnitsTransformed
# Load data
datafile = os.path.join('data',country,vintage + '.xls')
X,Time,Z = load_data(datafile,Spec,sample_start)
# Summarize dataset
summarize(X,Time,Spec)
#-------------------------------------------------Plot data
# Raw vs transformed
idxSeries = np.where(Spec.SeriesID == "INDPRO")[0][0]
t_obs = ~np.isnan(X[:,idxSeries])
fig = make_subplots(rows=2, cols=1,
subplot_titles=("Raw Observed Data", "Transformed Data"))
fig.append_trace(go.Scatter(
x=[dt.fromordinal(i - 366).strftime('%Y-%m-%d') for i in Time[t_obs]],
y=Z[t_obs,idxSeries],
), row=1, col=1)
fig.append_trace(go.Scatter(
x=[dt.fromordinal(i - 366).strftime('%Y-%m-%d') for i in Time[t_obs]],
y=X[t_obs,idxSeries],
), row=2, col=1)
fig.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)'} ,
title_text="Raw vs Transformed Data",
showlegend=False)
fig.update_yaxes(title_text=Spec.Units[idxSeries], row=1, col=1)
fig.update_yaxes(title_text=Spec.UnitsTransformed[idxSeries], row=2, col=1)
fig.show()
#-------------------------------------------------Run dynamic factor model (DFM) and save estimation output as 'ResDFM'.
threshold = 1e-4 # Set to 1e-5 for more robust estimates
Res = dfm(X,Spec,threshold)
Res = {"Res": Res,"Spec":Spec}
with open('ResDFM.pickle', 'wb') as handle:
pickle.dump(Res, handle)
# TODO: Res and Spec should be separate, this will be fixed after the unit tests are created
#-------------------------------------------------Plot Loglik across number of steps
fig = go.Figure()
fig.add_trace(go.Scatter(x=np.arange(1,len(Res["Res"]["loglik"][1:])+1),
y=Res["Res"]["loglik"][1:],
mode='lines',
name="LogLik")
)
fig.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)'} ,
title_text="LogLik across number of steps taken",
showlegend=False
)
fig.update_yaxes(title_text="LogLik")
fig.update_xaxes(title_text="Number of steps")
fig.show()
#-------------------------------------------------Plot common factor and standardized data.
# select INDPRO data series
idxSeries = np.where(Spec.SeriesID == "INDPRO")[0][0]
# Create traces
fig = go.Figure()
for i in range(Res["Res"]["x_sm"].shape[1]):
fig.add_trace(go.Scatter(x=[dt.fromordinal(i - 366).strftime('%Y-%m-%d') for i in Time],
y=Res["Res"]["x_sm"][:,i],
mode='lines',
name=Spec.SeriesID[i],
line={'width':.9})