Skip to content

Instantly share code, notes, and snippets.

View mbusigin's full-sized avatar

Matt Busigin mbusigin

View GitHub Profile
@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 / 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 / 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)
#!/usr/bin/perl -W
# From populationpyramid.net's data source https://github.com/madewulf/PopulationPyramid.net/tree/master/static/data/generated
# Takes the JSON as stdin, returns a CSV with the M/O ratio
use strict;
use JSON;
use Data::Dumper;
my $json = JSON->new;
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 / 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="", )
@mbusigin
mbusigin / performance-barchart.R
Last active January 1, 2016 03:49
Plotting performance barcharts
library(quantmod)
library(PerformanceAnalytics)
# getSymbols(c("SPY", "TLT", "LQD"))
bars = 60
a = merge(Delt(SPY[,6], k=bars), Delt(TLT[,6], k=bars), Delt(LQD[,6], k=bars))
b = last(a)
names(b) = c("SPY", "TLT", "LQD")
b2 = round(as.vector(b), digits=2)
barplot( b2, names.arg=names(b) )
text(1:3, 0, paste(b2, "%", sep=""))
@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))))
@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 / 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")] )