Created
May 17, 2013 21:31
-
-
Save fditraglia/5602114 to your computer and use it in GitHub Desktop.
Naive Weighted Inflation Forecast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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