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

Exercise: Airline Flights

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",vintage_dates = as.Date("2021-04-08")) 

#Fed Funds Rate
FEDFUNDS<-fredr(series_id = "FEDFUNDS",vintage_dates = as.Date("2021-04-08")) 
fedfunds<-ts(FEDFUNDS$value,frequency=12,start=c(1954,7),end=c(2021,2),names="FFR")

inflation1<-ts(INFLATION$value,frequency=12,start=c(1947,1),end=c(2021,2),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 Wed May 12 09:33:21 2021 =-=-=-=-=
## =-=-=-=-= Iteration 100 Wed May 12 09:33:22 2021 =-=-=-=-=
## =-=-=-=-= Iteration 200 Wed May 12 09:33:24 2021 =-=-=-=-=
## =-=-=-=-= Iteration 300 Wed May 12 09:33:25 2021 =-=-=-=-=
## =-=-=-=-= Iteration 400 Wed May 12 09:33:26 2021 =-=-=-=-=
## =-=-=-=-= Iteration 500 Wed May 12 09:33:28 2021 =-=-=-=-=
## =-=-=-=-= Iteration 600 Wed May 12 09:33:29 2021 =-=-=-=-=
## =-=-=-=-= Iteration 700 Wed May 12 09:33:31 2021 =-=-=-=-=
## =-=-=-=-= Iteration 800 Wed May 12 09:33:32 2021 =-=-=-=-=
## =-=-=-=-= Iteration 900 Wed May 12 09:33:33 2021 =-=-=-=-=
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

#BSTS
plot(pred)

#ETS
autoplot(inflETSfcst)

Application: Macroeconomic Forecasting with DSGE Models

Mar 2021 NY Fed DSGE Forecasts and Estimated State

Posterior Predictive Mean and Intervals Filtered Posterior of a Latent Variable

Conclusions

References