Skip to content

Instantly share code, notes, and snippets.

@mbusigin
mbusigin / cyclical_adjustment.R
Created Sep 7, 2017
R function to return cyclically-adjusted series using labour & GDP output gaps
View cyclical_adjustment.R
labour_output_gap = function()
{
if ( exists("UNRATE") == FALSE ) { fred("UNRATE", environment()) }
if ( exists("NROU") == FALSE ) { fred("NROU", environment()) }
a = na.trim(na.locf(merge(UNRATE, NROU)))
a = a[endpoints(a)]
return(UNRATE - NROU)
}
@mbusigin
mbusigin / multiplot.R
Created Sep 2, 2017
R function for multiplotting (overplotting), either on the same, or differing axes.
View multiplot.R
multiplot = function(r, legend.loc="topleft", legend.cex=1, overplot=F, plotSecondAxis=F, s_lty=1, plotfunc=recplot, legend=T, pointMax=F, pointLast=F, legendLast=F, legendLastRoundDigits=0 )
{
r = na.omit(r)
for ( x in names(r) )
{
n = r[,x]
m = n
r[,x] = m
}
@mbusigin
mbusigin / MultiComboBox.js
Last active Aug 9, 2017
A multi-selection ComboBox for Qooxdoo.
View MultiComboBox.js
/*
* This is a multi-select dropdown combobox deriviative!
*
* I had seen that some other folks had been looking for similar functionality, and although the Qooxdoo
* community was helpful, and explained what kinds of things they'd have to do to implement it, no finished class
* emerged. So, I had the need, and here it is. Please spin any improvements back to me!
*
* Matthew Busigin, CIO @ Hover Networks, Inc (mbusigin <at> hovernetworks <dot> com)
*
* We're overriding a bunch of stuff here:
@mbusigin
mbusigin / Baa-spread-impute.R
Created Oct 10, 2016
Impute the Baa Spread using LQD, IEF and inverted 10s.
View Baa-spread-impute.R
##
## Unfortunately, FRED no longer will be providing a whole bunch of interest rate series, principally Moody's.
## I use the Baa spread as the bedrock of my analysis, and although I can get a colleague to export the data from a BB terminal,
## I really needed to create a proxy so I can get data more quickly than that.
##
## The idea here is to impute the Baa spread from market inputs that we still have access to: 10s30s (because Baa spread has a
## duration spread embedded in it), the IEF/LQD ratio, and inverted 10s, in order to correct for convexity.
##
## The result is pretty decent: from 1986-2016, we get an R^2 of 0.9, RMSE of 0.26. I'm sure it could be improved upon, so let me know
## if you have any other ideas.
View plotOilOERInflationModel.R
function()
{
if ( exists("CPIAUCSL") == FALSE ) { fred("CPIAUCSL", environment()) }
if ( exists("MCOILWTICO") == FALSE ) { fred("MCOILWTICO", environment()) }
if ( exists("CUSR0000SEHC") == FALSE ) { fred("CUSR0000SEHC", environment()) }
recplot( (MCOILWTICO/CPIAUCSL)*-1.5736 + Delt(CUSR0000SEHC, k=12*5)* 6.5426 + 1.5954 )
lines( cagr(dlag(CPIAUCSL, k=12*5), 5), col="dodgerblue3" )
}
@mbusigin
mbusigin / multiplot.R
Created Jul 12, 2015
Plot xts object with multiple columns
View multiplot.R
multiplot = function(r, legend.loc="topleft", legend.cex=1, overplot=F, plotSecondAxis=F, s_lty=1, plotfunc=recplot, legend=T, pointMax=F, pointLast=F, legendLast=F, legendLastRoundDigits=0 )
{
r = na.omit(r)
for ( x in names(r) )
{
n = r[,x]
m = n
r[,x] = m
}
@mbusigin
mbusigin / fred.R
Created Jul 12, 2015
R function to retrieve FRED series as xts object
View fred.R
##
## The quantmod function, getSymbols.FRED(), has been broken by the recent upgrade to FRED to https only.
## I've taken that function and tweaked it to work.
##
fred <- function (Symbols, env, return.class = "xts", ...)
{
importDefaults("getSymbols.FRED")
this.env <- environment()
for (var in names(list(...))) {
assign(var, list(...)[[var]], this.env)
View neoclassical.R
plotNeoclassicalGrowth = function()
{
if ( exists("GDP") == FALSE ) { getSymbols("GDP", src="FRED") }
if ( exists("GDPC96") == FALSE ) { getSymbols("GDPC96", src="FRED") }
if ( exists("CLF16OV") == FALSE ) { getSymbols("CLF16OV", src="FRED") }
if ( exists("GPDI") == FALSE ) { getSymbols("GPDI", src="FRED") }
if ( exists("COFC") == FALSE ) { getSymbols("COFC", src="FRED") }
a = na.omit(merge(Delt(CLF16OV, k=12), (GPDI-COFC)/GDP, Delt(GDPC96, k=4) - Delt(CLF16OV, k=12) - ((GPDI-COFC)/CPIAUCSL)/GDP)) * 100
z = a
@mbusigin
mbusigin / RV vs FV
Created Oct 12, 2014
Relating Future Volatility with Realized Volatility
View RV vs FV
getSymbols("^GSPC", from="1950-01-01")
p = 10
a = na.omit(merge( volatility(GSPC, n=p)*100, lag(volatility(GSPC, n=p), k=-p)*100 ))
names(a) = c("hv", "rv")
medianvalues = c()
meanvalues = c()
maxvalues = c()
minvalues = c()
keys = c()
for ( x in seq( 0, max(a$hv), 2 ) )
@mbusigin
mbusigin / portfolio.R
Created Jul 19, 2014
Estimate a portfolio's return and risk
View portfolio.R
create.portfolio <- function( tickers=c('SPY', 'AGG', 'TLT', 'GLD'), weights=NULL, er=NULL, ev=NULL )
{
e = new.env()
getSymbols(tickers, from="1950-01-01", env=e)
a = NULL
for ( i in 1:length(tickers) )
{
a = cbind(a, annualReturn( get(tickers[i], e) ))
}
You can’t perform that action at this time.