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
@lendle
lendle / projectsimplex.jl
Last active March 28, 2016 19:56
Project a vector of reals onto a simplex in julia
# Figure 1 algorithm from http://icml2008.cs.helsinki.fi/papers/361.pdf
# project a real vector onto a simplex in nlogn time
# v is the vector to project
# z is the value to which the projected vector must sum, one by default
function projectsimplex{T <: Real}(v::Array{T, 1}, z::T=one(T))
n=length(v)
µ = sort(v, rev=true)
#finding ρ could be improved to avoid so much temp memory allocation
ρ = maximum((1:n)[µ - (cumsum(µ) .- z) ./ (1:n) .> zero(T)])
θ = (sum(µ[1:ρ]) - z)/ρ
@lendle
lendle / PAM in julia.ipynb
Created February 5, 2014 09:29
Example of the `pam` clustering algorithm in julia. http://nbviewer.ipython.org/gist/lendle/8820029
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lendle
lendle / KernSmooth_example.ipynb
Last active July 4, 2016 13:57
KernSmooth.jl example
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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
@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;

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 / 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.
@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
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
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