Skip to content

Instantly share code, notes, and snippets.

View mbusigin's full-sized avatar

Matt Busigin mbusigin

View GitHub Profile
@mbusigin
mbusigin / gist:4700714
Last active December 12, 2015 02:39
CP/GDP vs future CP growth
dlag = function(p, k = 1)
{
return( lag(Delt(p, k=k), k=-k) )
}
getSymbols( c("CP", "GDP"), src="FRED" )
layout(1:2)
a = na.omit(merge(CP/GDP*100, dlag(CP, k=4*5)*100))
@mbusigin
mbusigin / 10y-oscillator.R
Created June 11, 2013 02:13
Generate chart with 10y yield oscillator (10y yield minus 260d moving average), +/- 1 stdev dashed lines
library(quantmod)
recplot = function(var, maintitle="", ylab="", ylim=NULL)
{
if ( exists("USRECD") == FALSE ) { getSymbols("USRECD", src="FRED", env=.GlobalEnv) }
a = na.locf(cbind(USRECD, var))
a = a[ .index(a) %in% .index(var) ]
par(oma=c(0,0,0,0))
par(xaxt="n", yaxt="n")
@mbusigin
mbusigin / fedfunds.R
Created June 21, 2013 01:51
Estimate Fed Funds rates.
##
## John Taylor proposed the following rule designed to guide monetary policy:
## i = r* + pi + 0.5 ( pi - pi*) + 0.5 ( y - y*)
## where i is the nominal federal funds rate, r* is the "natural" real federal funds rate (often taken to be 2%), pi is the rate of inflation, pi* is the target inflation rate (for example, 2%), y is the logarithm of real output, and y* is the logarithm of potential output.
## The two basic ideas here are to raise the federal funds rate to one-half the extent that inflation exceeds its target and to lower the federal funds rate to one-half of the percentage that real output falls below its potential.  Implicit in this formulation is that a reasonable rule of thumb applied consistently over time is more likely to achieve a good outcome than is aggressive manipulation of monetary policy.  It is widely believed that central banks have paid close attention to variations on this Taylor Rule in recent years.
## From: http://www.econmodel.com/classic/terms/taylor_rule.htm
calculateTaylor
@mbusigin
mbusigin / PCE_wage_dd.R
Created August 5, 2013 14:06
Calculate drawdown in PCE as % of Wages
plot_PCEWage_drawdown <- function()
{
if ( exists("PCE") == FALSE ) { getSymbols("PCE", src="FRED") }
if ( exists("A576RC1") == FALSE ) { getSymbols("A576RC1", src="FRED") }
pce_wage_dd = ( SMA(calculateDrawdown(PCE/A576RC1)) ) * 100
recplot(pce_wage_dd, main="Drawdown of (PCE as % of Wages)")
}
@mbusigin
mbusigin / mergeLongEnd.R
Last active December 21, 2015 04:39
30s, 20s and LTGOVTBD don't have a contiguous series. This piece of code merges the three in that order of preference. Potential improvement - estimate term premium to have consistent constant maturity of 30y.
mergeLongEnd = function()
{
if ( exists("DGS20") == FALSE ) { getSymbols("DGS20", src="FRED") }
if ( exists("DGS30") == FALSE ) { getSymbols("DGS30", src="FRED") }
if ( exists("LTGOVTBD") == FALSE ) { getSymbols("LTGOVTBD", src="FRED") }
a = merge(DGS30, LTGOVTBD, DGS20)
a$s = a$DGS30
a$s[is.na(a$s)] = a$DGS20[is.na(a$s)]
a$s[is.na(a$s)] = a$LTGOVTBD[is.na(a$s)]
return(a$s)
@mbusigin
mbusigin / ytd-return-barchart.R
Created October 12, 2013 17:10
R example of creating bar chart of ytd returns for a few assets
getSymbols(c("SPY", "TLT", "LQD"))
a = merge(SPY[,6], TLT[,6], LQD[,6])[ "2013" ]
b = (as.vector(tail(a, n=1)) / as.vector(head(a, n=1))) - 1
names(b) = c("SPY", "TLT", "LQD")
b2 = round(b[order(b)] * 100)
barplot( b2, names.arg=names(b2) )
text(1:3, 1, paste(b2, "%", sep=""))
@mbusigin
mbusigin / purchasing-power.R
Last active December 28, 2015 14:59
Backing the value of interest back into the purchasing power of the USD over time.
a = -Delt(CPIAUCNS, k=12)*100
a = na.omit( a[endpoints(a, on="years")] )
a = cumprod( (1+(a/100)) )
 
b = M2OWN - Delt(CPIAUCNS, k=12)*100
b = na.omit( b[endpoints(b, on="years")] )
b = cumprod( (1+(b/100)) )
 
b2 = TB3MS - Delt(CPIAUCNS, k=12)*100
b2 = na.omit( b2[endpoints(b2, on="years")] )
@mbusigin
mbusigin / purchasing-power-taxes.R
Last active December 28, 2015 18:08
Backing the value of interest into the purchasing power of the USD.... with taxes.
a = -Delt(CPIAUCNS, k=12)*100
a = na.omit( a[endpoints(a, on="years")] )
a = cumprod( (1+(a/100)) )
 
b = (M2OWN*0.6 - Delt(CPIAUCNS, k=12)*100)
b = na.omit( b[endpoints(b, on="years")] )
b = cumprod( (1+(b/100)) )
 
b2 = (TB3MS*0.6 - Delt(CPIAUCNS, k=12)*100)
b2 = na.omit( b2[endpoints(b2, on="years")] )
@mbusigin
mbusigin / llxc.R
Created December 10, 2013 02:53
Lead/Lag Cross Correlation between XTS objects
llxc = function( a, ll=-12:12, legend.loc="topleft", displayLegend=T, legend.cex=1.0, overplot=F )
{
if ( ncol(a) < 2 )
{
print( "Not enough columns")
return;
}
a = na.omit(a)
@mbusigin
mbusigin / Cumulative Utility of Household Debt.R
Created December 15, 2013 16:51
Estimating the cumulative utility of household debt to national income.
library(quantmod)
getSymbols(c("GDP", "CMDEBT"), src="FRED")
plot(cumsum(na.omit(diff(GDP) - diff(CMDEBT))))