Instantly share code, notes, and snippets.

Wesley Tanseytansey

• Sort options
Created Aug 9, 2019
Heterogeneous (AKA multi-view) factor modeling in pytorch.
View multifactor.py
 ''' Heterogeneous factor modeling. This model fits a heterogeneous factor model where columns may be: 1) Binary 2) Categorical 3) Gaussian Everything is fit via alternating minimization and stochastic gradient descent. The code relies on pytorch for SGD and a demo is included.
Last active May 11, 2019
Pool adjacent violators algorithm for monotone matrix factorization
View factor_pav.py
 '''Pool adjacent violators algorithm for (column-)monotone matrix factorization. Applies the PAV algorithm to column factors of a matrix factorization: Given: M = W.V' Returns: V_proj, a projected version of V such that M[i] is monotone decreasing for all i. Author: Wesley Tansey Date: May 2019 '''
Last active May 8, 2019
Fast multivariate normal sampling for some common cases
View fast_mvn.py
 '''Fast sampling from a multivariate normal with covariance or precision parameterization. Supports sparse arrays. Params: - mu: If provided, assumes the model is N(mu, Q) - mu_part: If provided, assumes the model is N(Q mu_part, Q). This is common in many conjugate Gibbs steps. - sparse: If true, assumes we are working with a sparse Q - precision: If true, assumes Q is a precision matrix (inverse covariance) - chol_factor: If true, assumes Q is a (lower triangular) Cholesky decomposition of the covariance matrix (or of the precision matrix if precision=True).
Last active Feb 28, 2018
Solver for a nurse scheduling problem
View nurse_schedules.py
 ''' Program to generate valid time allocations of a mental ward staff. Given: Two staff lists. Each list applies for a specific window of time. Lists may contain non-empty intersections of employees. Each employee has a designation as RMN or HCA.
Created Jul 2, 2017
Fit negative binomial with weighted observations in R
View fitWeightedNegativeBinomial.R
 # Fit using a simple EM algorithm # observations are x # weights are w (must be same length as x) # returns (r, p) # r - dispersion parameter # p - probability of success weightedNegBinomFit <- function(x, w, maxsteps=30) { sum.wx = sum(x*w) sum.w = sum(w)
Created Feb 10, 2017
Unpack a lower triangular Cholesky matrix from a neural network output in Tensorflow
View chol_unpack.py
 import tensorflow as tf def unpack_cholesky(q, ndims): # Build the lower-triangular Cholesky from the flat buffer (assumes q shape is [batchsize, cholsize]) chol_diag = tf.nn.softplus(q[:,:ndims]) chol_offdiag = q[:,ndims:] chol_rows = [] chol_start = 0 chol_end = 1 for i in xrange(ndims):
Created Nov 9, 2016
terrible STL set iterator behavior
View wtfset.cpp
 #include #include class A { std::set s; public: A() { s.insert(0); s.insert(1); } std::set getSet() { return s; }
Created Jul 31, 2015
ADMM convergence checker in CVXPY