Skip to content

Instantly share code, notes, and snippets.

@fditraglia
Created May 17, 2013 21:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fditraglia/5602114 to your computer and use it in GitHub Desktop.
Save fditraglia/5602114 to your computer and use it in GitHub Desktop.
Naive Weighted Inflation Forecast
library(Quandl)
p <- 100 * Quandl('FRED/GDPDEF', collapse = 'quarterly', start_date = '1960-01-01', end_date = '2012-01-01', type = 'ts', transformation = 'rdiff')
#Michigan Inflation Expectations aren't in Quandl yet
library(quantmod)
getSymbols("MICH",src="FRED")
#Median expected price change next 12 months, Survey of Consumers.
#Convert to ts object
MICH.start <- as.numeric(format.Date(start(MICH), '%Y'))
MICH.start <- c(MICH.start, as.numeric(format.Date(start(MICH), '%m')))
MICH <- ts(MICH, start = MICH.start, freq = 12)
#Aggregate to quarterly frequency
survey.mean <- aggregate(MICH, FUN = mean, nfrequency = 4)
survey.median <- aggregate(MICH, FUN = median, nfrequency = 4)
#Survey measures 12 month expected price change. Convert to implied quarterly price change
survey.mean <- 100 * ((survey.mean/100 + 1)^(1/4) - 1)
survey.median <- 100 * ((survey.median/100 + 1)^(1/4) - 1)
#Calculate MSFE for simple strategies that take a linear combination of inflation survey. Note that this changes the forecast period to be when we have values for the survey.
f.start <- c(1973,2)
f.end <- c(2012, 1)
naive <- function(w = 0, step = 1){
survey.lag <- lag(survey.mean, -step)
survey.lag <- window(survey.lag, start = f.start, end = f.end)
p.lag <- lag(p, -step)
p.lag <- window(p.lag, start = f.start, end = f.end)
#Give the survey weight w
pred <- w * survey.lag + (1 - w) * p.lag
actual <- window(p, start = f.start, end = f.end)
return(mean((pred - actual)^2))
}#end naive
w <- seq(from = 0, to = 1, by = 0.01)
step <- 1:8
w.vector <- rep(w, each = length(step))
step.vector <- rep(step, times = length(w))
MSFE.naive <- mapply(naive, w = w.vector, step = step.vector)
parameters <- cbind(w.vector, step.vector)
colnames(parameters) <- c('w', 'step')
MSFE.naive <- cbind(parameters, MSFE.naive)
MSFE.naive <- data.frame(MSFE.naive)
MSFE.naive$step <- as.factor(MSFE.naive$step)
library(ggplot2)
qplot(w, MSFE.naive, col = step, data = MSFE.naive, geom = 'line', size = I(2)) + theme_bw() + xlab('Weight Given to Survey') + ylab('MSFE') + labs(colour = "Steps Ahead")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment