Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@ahwillia
ahwillia / multi_set_perm_match.py
Last active June 26, 2021 22:39
Greedy heuristic for finding K-permutations that match a set of K matrices
import numpy as np
from scipy.optimize import linear_sum_assignment
from sklearn.utils import check_random_state
import scipy.sparse
def perm_alignment(X, Y):
"""
Given two matrix X and Y. Returns sparse matrix P, holding permutation
matrix that minimizes norm(X @ P - Y).
@ahwillia
ahwillia / supervised_pca.py
Created May 5, 2021 18:37
Supervised PCA model via manifold optimization
"""
Supervised PCA model.
Ritchie, A., Balzano, L., Kessler, D., Sripada, C. S., & Scott, C.
(2020). Supervised PCA: A Multiobjective Approach. arXiv:2011.05309.
"""
import numpy as onp
import autograd.numpy as np
from pymanopt.manifolds import Grassmann, Euclidean, Product
@ahwillia
ahwillia / pytorch_nmf.py
Last active March 28, 2021 00:06
Simple Nonnegative Matrix Factorization in Pytorch
import numpy as np
import torch
import matplotlib.pyplot as plt
from torch_nonneg_linesearch import nonneg_projected_gradient_step
# Data dimensions
m, n = 100, 101
rank = 3
# Data matrix, detached from the graph.
@ahwillia
ahwillia / elastic_net.py
Created November 8, 2017 02:13
Elastic Net Regression via ADMM in Python
import numpy as np
import scipy.linalg
def elastic_net(A, B, x=None, l1=1, l2=1, lam=1, tol=1e-6, maxiter=10000):
"""Performs elastic net regression by ADMM
minimize ||A*x - B|| + l1*|x| + l2*||x||
Args:
A (ndarray) : m x n matrix
@ahwillia
ahwillia / simple_cmap.py
Created February 18, 2021 23:36
Simple formula for constructing a matplotlib colormap
from matplotlib.colors import LinearSegmentedColormap, colorConverter
def simple_cmap(colors, name='none'):
"""Create a colormap from a sequence of rgb values.
cmap = simple_cmap([(1,1,1), (1,0,0)]) # white to red colormap
cmap = simple_cmap(['w', 'r']) # white to red colormap
cmap = simple_cmap(['r', 'b', 'r']) # red to blue to red
"""
# check inputs
@ahwillia
ahwillia / pca_alt_min.py
Created October 30, 2016 06:52
Alternating Minimization in Tensorflow (PCA example)
import numpy as np
import tensorflow as tf
# N, size of matrix. R, rank of data
N = 100
R = 5
# generate data
W_true = np.random.randn(N,R)
C_true = np.random.randn(R,N)
@ahwillia
ahwillia / permtest.py
Last active February 7, 2021 18:31
Two-sample permutation test in Python
"""
A simple implementation of a permutation test among two
independent samples.
"""
import numpy as np
from sklearn.utils.validation import check_random_state
from more_itertools import distinct_permutations
from scipy.stats import percentileofscore
from math import factorial
@ahwillia
ahwillia / kronshuff.py
Last active October 18, 2020 18:21
Kronecker vector product via Shuffle algorithm
"""
References:
- B. Plateau, On the stochastic structure of parallelism and synchronization models for distributed algorithms.
Perform. Eval. Rev., 13 (1985), pp. 147–154.
- Dayar, T., & Orhan, M. C. (2015). On vector-Kronecker product multiplication with rectangular factors.
SIAM Journal on Scientific Computing, 37(5), S526-S543.
"""
@ahwillia
ahwillia / matlab.py
Created October 17, 2020 18:22
Helper function for loading nested MATLAB structs into python
import scipy.io as spio
import numpy as np
def loadmat(filename):
'''
this function should be called instead of direct spio.loadmat
as it cures the problem of not properly recovering python dictionaries
from mat files. It calls the function check keys to cure all entries
which are still mat-objects
'''
@ahwillia
ahwillia / cred_interval.jl
Last active May 8, 2020 19:21
Calculate Credible (Highest Posterior Density, HPD) Intervals in Julia using Distributions.jl
using PyPlot
using Distributions
function credible_interval(D::UnivariateDistribution; c=0.95, nx=1000)
# Discretize over the support
r = support(D)
lb,ub = r.lb,r.ub
# Histogram approximation of area under pdf
x = linspace(lb,ub,nx)