Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@ahwillia
ahwillia / elliptical_slice_sampler.py
Last active February 20, 2024 21:41
Elliptical Slice Sampler in JAX
"""
NOTE: This code has not been rigorously tested.
"""
import matplotlib.pyplot as plt
import jax.numpy as jnp
import jax
from tqdm import trange
def elliptical_slice_update(x, log_density, sigmas, key):
@ahwillia
ahwillia / cosine_basis.py
Created October 28, 2023 18:46
raised cosine basis functions
import matplotlib.pyplot as plt
import numpy as np
def raised_cos(t, loc, scale, amp):
"""
Raised, 1d-cosine basis functions tiling [0, 2 * pi)
These functions have the property of summing to a
constant amplitude at all points (i.e. uniform
tiling of space).
@ahwillia
ahwillia / metric_repair.py
Last active September 4, 2023 17:44
L2 Metric Repair
import numba
import numpy as np
from scipy.spatial.distance import pdist, squareform
from math import comb
@numba.jit(nopython=True)
def index(n, i, j):
"""
Computes linear index of (i, j) from the (n x n) distance matrix.
"""
@ahwillia
ahwillia / mmd_test.py
Created November 13, 2022 15:27
A kernel two-sample test for equality of distributions (Gretton et al. 2012)
import numpy as np
from scipy.spatial.distance import cdist, pdist
def mmd_two_sample_test(X, Y):
"""
Implements Gretton's test for equality of
distributions in high-dimensional settings
using concentration bounds on the maximum
mean discrepancy (MMD). This function uses
the unbiased estimator of the MMD (see
@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 / msplines.py
Last active April 8, 2024 16:56
Generate M-spline functions in Python
"""
Python code to generate M-splines.
References
----------
Ramsay, J. O. (1988). Monotone regression splines in action.
Statistical science, 3(4), 425-441.
"""
import numpy as np
@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 / 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