Outline

State Space Models

Inference and Likelihood

  1. Begin with \(p_{t-1}(x_{t-1}|\mathcal{Y}_{t-1})\) (at \(t=1\), this is \(p(x_0)\))
  2. Predict next period \(x\): \(p_t(x_{t}|\mathcal{Y}_{t-1})=\int f_{t}(x_t|x_{t-1})p_{t-1}(x_{t-1}|\mathcal{Y}_{t-1})dx_{t-1}\)
  3. Construct conditional likelihood: \(\ell(y_{t}|\mathcal{Y}_{T-1})= \int g_t(y_t|x_t)p_t(x_{t}|\mathcal{Y}_{t-1})dx_t\)
  4. Use Bayes rule to predict \(x_t\): \(p(x_t|\mathcal{Y}_t)=\frac{g_t(y_t|x_t)p_t(x_{t}|\mathcal{Y}_{t-1})}{\ell(y_{t}|\mathcal{Y}_{T-1})}\)
  5. Go back to 1

Linear State Space Model

Dynamic Regression Model

Multivariate Linear State Space Model

Building blocks of multivariate state space models

State Space Representation of ARMA models

Estimation of Linear State Space Models

Exponential Smoothing

Filtering vs Smoothing

Example: State Space Decomposition

library(fredr) #FRED Data
library(fpp2) # Forecasting tools
library(bsts) #Bayesian State Space Models
library(gridExtra) #Graph Display

fredr_set_key("8782f247febb41f291821950cf9118b6") #Key I obtained for this class

#One Year Percent Change in Consumer Price Index for All Urban Consumers
INFLATION<-fredr(series_id = "CPIAUCSL",units="pc1") 

#Fed Funds Rate
FEDFUNDS<-fredr(series_id = "FEDFUNDS") 
fedfunds<-ts(FEDFUNDS$value,frequency=12,start=c(1954,7),names="FFR")

inflation1<-ts(INFLATION$value,frequency=12,start=c(1947,1),names="CPI")
#Truncate to same window as Fed Funds Rate
inflation<-window(inflation1,start=c(1954,7))

#monetarydata<-as.ts(data.frame(inflation,fedfunds),frequency=12,start=c(1954,7))

# Estimate Exponential Smoothing Model of Inflation
# Choose components by AICc, estimate parameters by maximum likelihood
inflETSmodel<-ets(inflation)

#Construct 6 month forecast

inflETSfcst<-forecast(inflETSmodel,h=6)

Exponential Smoothing Component Decomposition: \(y_t\), \(\ell_t\), \(b_t\)

#Plot components and variance decomposition
autoplot(inflETSmodel)

# Build a Bayesian State Space Model of Inflation

#Start by adding components to specification object

ss<-AddSemilocalLinearTrend(
     state.specification = list(), #Initialize with empty list
     y=inflation) #Use data to define scale of prior

ss<-AddDynamicRegression(state.specification=ss,
                         inflation~fedfunds)

# Set number of MCMC draws, and number to discard as "burn in" in analysis
niter<-1000
burnins<-500
  
#Construct model posterior by MCMC sampling
model <- bsts(inflation, state.specification = ss, niter = niter)
## =-=-=-=-= Iteration 0 Mon Oct 28 00:36:32 2019 =-=-=-=-=
## =-=-=-=-= Iteration 100 Mon Oct 28 00:36:33 2019 =-=-=-=-=
## =-=-=-=-= Iteration 200 Mon Oct 28 00:36:34 2019 =-=-=-=-=
## =-=-=-=-= Iteration 300 Mon Oct 28 00:36:35 2019 =-=-=-=-=
## =-=-=-=-= Iteration 400 Mon Oct 28 00:36:36 2019 =-=-=-=-=
## =-=-=-=-= Iteration 500 Mon Oct 28 00:36:37 2019 =-=-=-=-=
## =-=-=-=-= Iteration 600 Mon Oct 28 00:36:38 2019 =-=-=-=-=
## =-=-=-=-= Iteration 700 Mon Oct 28 00:36:39 2019 =-=-=-=-=
## =-=-=-=-= Iteration 800 Mon Oct 28 00:36:40 2019 =-=-=-=-=
## =-=-=-=-= Iteration 900 Mon Oct 28 00:36:41 2019 =-=-=-=-=
coefdraw<-matrix(nrow=niter,ncol=length(fedfunds))
for (i in 1:niter){
  coefdraw[i,]<-model$dynamic.regression.coefficients[i,1,]
}

#Remove first 500 draws as burnin
coefdraws<-coefdraw[(burnins+1):niter,]

# Make forecast under contingency that fed funds rate stays constant for next 6 months
cffr<-last(fedfunds)
newffr<-c(cffr,cffr,cffr,cffr,cffr,cffr)

newdata<-data.frame(newffr)
colnames(newdata)<-c("fedfunds")


#Construct posterior predictive distribution by sampling
pred <- predict(model, newdata=newdata,
                horizon = 6, burn = burnins)

BSTS Filtered Decomposition into Components

#Time series of posterior distributions for trend and dynamic regression components
PlotBstsComponents(model)

BSTS Filtered Coefficients

#Time series of Coefficient posterior distributions
PlotDynamicDistribution(coefdraws) 

## NULL

Results and Interpretation

BSTS vs ETS Forecasts

#ETS
autoplot(inflETSfcst)

#BSTS
plot(pred)

Application: Macroeconomic Forecasting with DSGE Models

Jan 2019 NY Fed DSGE Forecasts and Estimated State

Posterior Predictive Mean and Intervals Filtered Posterior of a Latent Variable

Conclusions

References