Skip to content

Instantly share code, notes, and snippets.

View mbusigin's full-sized avatar

Matt Busigin mbusigin

View GitHub Profile
@mbusigin
mbusigin / cdf_pdf.R
Last active August 29, 2015 13:57
Combination Cumulative Distribution Function and Frequency Distribution graph.
cdf_pdf <- function(v, legend.loc="topleft", legend.cex=1, name="")
{
if ( name == "" )
{
name = names(v)[1]
}
a = as.vector(v)
lv = as.numeric(last(v))
hist(v, col="grey90", main="", xlab="", ylab="", )
library(quantmod)
getSymbols( c("LNU00000012", "LNS12000091"), src="FRED" )
plot(diff(LNU00000012, lag=12*5)*1.93+ 385.70445)
lines(lag(diff(LNS12000091, lag=12*5), k=-12*20), col="blue", lty=4)
points(last(na.omit(lag(diff(LNS12000091, lag=12*5), k=-12*20))), pch=16, col="blue")
legend("topright", c("5y Demographic projection of Growth of 35-44 employees", "5y Actual Growth of 35-44 employees"), col=c("black", "blue"), lty=c(1, 4), cex=.6)
@mbusigin
mbusigin / miles-vs-pop-vs-lf.R
Last active August 29, 2015 14:02
Scatter of 12m average miles driven %-y/y vs population & labour force
getSymbols(c("M12MTVUSM227NFWA", "CNP16OV", "CLF16OV"), src="FRED")
a = na.omit(merge(Delt(M12MTVUSM227NFWA, k=12), Delt(CNP16OV, k=12), Delt(CLF16OV, k=12)))
plot(as.vector(a[,2]), as.vector(a[,1]), type="p", xlim=c(-.01, .04), ylim=c(-0.04, .08), pch=14, cex=.5, col="red", xlab="x", ylab="M12MTVUSM227NFWA %-y/y")
m2 = lm(a[,1] ~ a[,2])
abline(m2, col="red")
points(as.vector(a[,3]), as.vector(a[,1]), pch=16, col="dodgerblue3", cex=.5)
m3 = lm(a[,1] ~ a[,3])
abline(m3, col="dodgerblue3")
legend("topleft", c("x = CNP16OV %-y/y (r=0.326)", "x = CLF16OV %-y/y (r=0.55)"), col=c("red", "dodgerblue3"), pch=c(14, 16), cex=0.5)
@mbusigin
mbusigin / scatter.R
Last active August 29, 2015 14:03
Chronographically coloured, pathed scatter plot
##
## Creates graphs like:
## https://twitter.com/mbusigin/status/483370672828055553
##
colourized_scatter <- function(a)
{
plot(matrix(a, ncol=2), type="l", xlab=names(a)[1], ylab=names(a)[2])
points(matrix(a, ncol=2), col=rainbow(nrow(a)), pch=16, cex=.5)
lines(lowess(a), lty=3)
@mbusigin
mbusigin / portfolio.R
Created July 19, 2014 19:58
Estimate a portfolio's return and risk
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) ))
}
@mbusigin
mbusigin / RV vs FV
Created October 12, 2014 18:11
Relating Future Volatility with Realized Volatility
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 ) )
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 / fred.R
Created July 12, 2015 12:48
R function to retrieve FRED series as xts object
##
## 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)
@mbusigin
mbusigin / multiplot.R
Created July 12, 2015 14:23
Plot xts object with multiple columns
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 / 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))