This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function format(s, args...) | |
# Python-style string formatting with floating point support | |
# Note that this is 1-based to be more Julian | |
result = deepcopy(s) | |
for (i, x) in enumerate(args) | |
q = Regex("{$i(:\.([0-9])+f)?}") | |
next = result | |
for m in eachmatch(q, result) | |
val = x | |
if m.captures[2] != nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Implementation of the ADMM convergence rate SDP from Nishihara et al., | |
ICML 2015, equation 11. | |
Code by Wesley Tansey and Sanmi Koyejo | |
7/31/2015 | |
''' | |
import cvxpy as cvx | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <set> | |
#include <iostream> | |
class A | |
{ | |
std::set<int> s; | |
public: | |
A() { s.insert(0); s.insert(1); } | |
std::set<int> getSet() { return s; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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 | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A O(nlogn) time implementation of the knockoff filter. | |
Author: Wesley Tansey | |
Date: 3/27/2020 | |
''' | |
import numpy as np | |
def knockoff_filter(knockoff_stats, alpha, offset=1.0, is_sorted=False): | |
'''Perform the knockoffs selection procedure at the target FDR threshold. |