Skip to content

Instantly share code, notes, and snippets.

View lendle's full-sized avatar

Sam Lendle lendle

  • UC Berkeley
  • Berkeley, CA
View GitHub Profile
tmle_dynamic_ate <- function(logitQnA1, logitQnA0, gn1, A, Y, d1=1, d0=0, Q_is_logit=TRUE, gbound=0.01){
n <- length(Y)
if (length(d1) == 1) d1 <- rep(d1, n)
if (length(d0) == 1) d0 <- rep(d0, n)
if (length(logitQnA1) != n ||
length(logitQnA0) != n ||
length(gn1) != n ||
length(d1) != n ||
length(d0) != n ||
length(A) != n) stop("inputs should all have the same length")
immutable NullableVector{T}
isnull::Vector{Uint8}
values::Vector{T}
end
Base.length(nv::NullableVector) = length(nv.isnull)
function Base.getindex(nv::NullableVector{Float64}, i::Integer)
return bool(nv.isnull[i]) ? Nullable{Float64}() : Nullable{Float64}(nv.values[i])
end
julia> function testgetindex(v)
@inbounds x = v[1]
x
end
testgetindex (generic function with 1 method)
julia> @code_llvm testgetindex(v)
define double @"julia_testgetindex;42178"(%jl_value_t*) {
top:
module firmod
type FIR{in_t,coef_t}
in::Vector{in_t}
coef::Vector{coef_t}
function FIR (coef::Vector{coef_t})
new (zeros (in_t, size (coef)), copy (coef))
end
end
import Base.convert
bitstype 64 IBMFloat64
# ibm float format is hex based:
# * bit 1 = sign
# * bits 2-8 = exponent to the power of 16, with bias of 64
# * bits 9-64 = mantissa: no implied leading 1 (unlike ieee),
# typically normalised (hex of bits 9-12 is non-zero), though this need
# not be the case
@lendle
lendle / SL_caret_wrappers.R
Created July 3, 2014 00:28
Create SuperLearner wrappers that run caret models
make_caret_wrappers <- function(alg_names) {
#creates an environment that stors SL wrappers. For a given <name> in alg_names, the wrapper will be
# named SL.caret.<name>
# alg_names should be a list of names of models provided by the caret pacakge.
# see http://caret.r-forge.r-project.org/modelList.html
# only set up for binomial family
# also, set up to use single split CV internally within caret. This can be changed by fiddling with fitControl, below
@lendle
lendle / Earth Example.ipynb
Last active August 29, 2015 14:03
Earth Example
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This is a markdown file

```jlcon other stuff on this line

julia> 1+3 4

julia> error("oops") ERROR: oops in error at error.jl:21

@lendle
lendle / colVars.R
Created June 3, 2014 20:14
Calculates the variance of columns of a matrix in a single pass, implemented with Rcpp. Much faster than `apply(x, 2, var)`
library(Rcpp)
cppFunction('NumericVector colVars(NumericMatrix x) {
int nrow = x.nrow(), ncol = x.ncol();
NumericVector out(ncol);
for (int j = 0; j < ncol; j++) {
double mean = 0;
double M2 = 0;
int n;
double delta, xx;
@lendle
lendle / hdps.R
Last active August 29, 2015 14:01
Implementation of the variable selection part of the HD-PS algorithm in R
hdps_screen <- function(outcome, treatment, covars, keep=0, indexes=TRUE) {
#outcome and treatment are binary vectors of length n
#covars is a matrix of binary variabesl of size n x p,
# where p is the number of covariates for in a paritular data dimension
#keep: number of covariates to keep
# indexes: return indexes or subest of covars
#
#returns indexes of kept covariates or matrix of kept covars
#indexes are sorted by abs(log(multiplicitive bias))
#if keep is 0 or greatner than the number of covars for which multiplicitive bias