Skip to content

Instantly share code, notes, and snippets.

Created November 13, 2014 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/121e636c4bc781a6082e to your computer and use it in GitHub Desktop.
Save anonymous/121e636c4bc781a6082e to your computer and use it in GitHub Desktop.
Fed Yield curve (after @isomorphismes and @Michael_Toth) http://www.statschat.org.nz/2014/11/14/motion-and-context-in-graphics/
par(bg="#DCE6EC", mar=c(5,4,3,2), xpd=FALSE, mgp=c(2.8,0.3,0.5), font.main=2,
col.lab="black", col.axis="black", col.main="black", cex.axis=0.8,
cex.lab=0.8, cex.main=0.9, family="Helvetica", lend=1,
tck=0, las=1, bty="n")
opar <- par()
require(YieldCurve)
data(FedYieldCurve)
## GDPC96 (transformed to continuously compounded annual rate of change)
## and UNRATE from Fred, the St Louis Fed data site
gdp<-read.csv("~/Downloads/fredgraph.csv")
unrate<-read.csv("~/Downloads/fredgraph-unrate.csv")
unrate$date<-as.Date(unrate$observation_date)
gdp$date<-as.Date(gdp$observation_date)
maturities <- c(3/12,6/12,1,2,3,5,7,10)
howmany=NROW(FedYieldCurve)
require(animation)
saveGIF({
for (i in 1:howmany) {
par(bg="#DCE6EC", mar=c(5,4,3,2), xpd=FALSE, mgp=c(2.8,0.3,0.5), font.main=2,
col.lab="black", col.axis="black", col.main="black", cex.axis=0.8,
cex.lab=0.8, cex.main=0.9, family="Helvetica", lend=1,
tck=0, las=1, bty="n")
layout(matrix(1:2,nrow=2),heights=c(6,4))
plot(maturities, FedYieldCurve[i,], type="o",lwd=3, col="#333333", xlab="Maturities structure in years", ylab="Interest rates values",ylim=c(0,15) )
thisyear<- i+c(-12:-1)
thisyear<-thisyear[thisyear>0 & thisyear<=howmany]
for(j in thisyear)
lines(maturities,FedYieldCurve[j,],col=ifelse(j<i,"#66333333","#33663333"))
title(main=paste("Yield Curve: ", year(time(FedYieldCurve[i]))))
grid(NA, NULL, col="white", lty="solid", lwd=1.5)
axis(1, tick=FALSE, col.axis="black")
axis(2, tick=FALSE, col.axis="black")
plot(UNRATE~date,data=unrate,type="l",col="blue",ylim=range(unrate[,2],gdp[,2]),ylab="")
lines(GDPC96_CCA~date,data=gdp,col="forestgreen")
grid(NA, NULL, col="white", lty="solid", lwd=1)
abline(v=time(FedYieldCurve)[i],lwd=4,col="#00000020")
text(as.Date("1990-1-1"),-8,"GDP change",col="forestgreen")
text(as.Date("2002-1-1"),-8,"Unemployment",col="blue")
}
},interval=.05,movie.name="yield curve evolution.gif", ani.width=400,ani.height=500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment