Skip to content

Instantly share code, notes, and snippets.

View joshbode's full-sized avatar

Josh Bode joshbode

  • Melbourne, Victoria, Australia
View GitHub Profile
@joshbode
joshbode / gist:3336974
Created August 13, 2012 04:27
Nullable Reference Class Fields
nullable = function(name) {
union_name = paste(name, 'NULL', sep='_')
tryCatch({
# existing definition
getClass(union_name)
return(union_name)
},
error=function(e) {
# new definition
@joshbode
joshbode / gist:3403318
Created August 20, 2012 11:20
Hack Hadoop (1.0.3) to compile native libraries on OS X (so rmr does not fall back to non-native)
deps:
brew install md5sha1sum automake snappy hadoop
tweak OS X:
# export JAVA_HOME=$(/usr/libexec/java_home)
hack jvm:
# sudo mkdir -p ${JAVA_HOME}/lib/{i386,x86_64}/server
# sudo ln -s ${JAVA_HOME}/{../Libraries,lib/i386/server}/libjvm.dylib
# sudo ln -s ${JAVA_HOME}/{../Libraries,lib/x86_64/server}/libjvm.dylib
@joshbode
joshbode / gist:3429572
Created August 22, 2012 21:29
PyConAU 2012 - Rough Notes

Title: PyCon AU Notes

Lazy Testing

  • WebTest
  • WSGIProxy
  • FunkLoad

Science and Engineering

@joshbode
joshbode / gist:3476326
Created August 26, 2012 08:46
Find number of matching words in phrases.
# match on intersecting word set
matching_words = function(x, y) {
x = unlist(strsplit(x, ' ', fixed=TRUE))
y = strsplit(y, ' ', fixed=TRUE)
# get intersections
results = sapply(y, function(b) { length(intersect(x, b)) })
# break ties in intersection counts based on simplicity (length) of original phrase
@joshbode
joshbode / LGR.r
Last active October 11, 2015 19:08
Robust Non-Linear Regression
library(robustbase)
# black-scholes LGR function
LGR = function (x, LGR_0, sigma) {
d_1 = (log(x) + sigma ^ 2 / 2.0) / sigma
d_2 = d_1 - sigma
return(LGR_0 * ifelse(x > 0, pnorm(-d_2) - x * pnorm(-d_1), 1.0))
@joshbode
joshbode / beta.r
Last active October 12, 2015 14:28
Beta Distribution
library(gtools)
step_length = 0.01
x = seq(0.0, 1.0, step_length)
a = 20.0
b = 2.0
beta_mean = a / (a + b)
@joshbode
joshbode / remark_latex.html
Created November 13, 2012 13:49
LaTeX in remark
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="https://github.com/downloads/gnab/remark/remark-0.4.2.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured">
remark.on('ready', function () {
MathJax.Hub.Config({
@joshbode
joshbode / hexmode_matrix.R
Last active December 11, 2015 04:48
Generate a summary matrix of a series of hexmode flags.
require(plyr)
# determine the flags that make up the number
find_flags = function(x, dense=TRUE) {
# collapse flags if more than one provided
if (length(x) > 1) {
x = Reduce(`|.hexmode`, x)
}
# nothing to do if nothing has been set...
@joshbode
joshbode / analytic.R
Last active December 11, 2015 17:58
SQL analytic-like functions in R
library(plyr)
# compute columns - similar to transform and plyr::mutate
compute = function(.data, ..., .append=FALSE) {
cols = as.list(substitute(list(...))[-1])
cols = cols[names(cols) != '']
env = parent.frame()
# evaluate the columns
@joshbode
joshbode / condition.r
Last active December 12, 2015 01:28
Dynamically evaluate stored conditions
# evaluate a condition
cond = function(expr, d=parent.frame()) {
Reduce(`&`, lapply(expr, eval, d))
}
# example
l = list(
'Condition 1'=expression(x > 0),
'Condition 2'=expression(y < 0)