Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@ahwillia
ahwillia / circ_regression.py
Last active April 11, 2024 07:24
Linear Regression with a circular dependent variable
# The MIT License (MIT)
#
# Copyright (c) Alex H. Williams
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
@ahwillia
ahwillia / kron_vec_product.py
Last active April 10, 2024 12:23
Efficient computation of a Kronecker - vector product (with multiple matrices).
import numpy as np
import numpy.random as npr
from functools import reduce
# Goal
# ----
# Compute (As[0] kron As[1] kron ... As[-1]) @ v
# ==== HELPER FUNCTIONS ==== #
@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 / 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 / cv.py
Last active November 29, 2023 22:19
Cross-validation for matrix factorization models
import numpy as np
from numpy.random import randn, rand
from scipy.optimize import minimize
import matplotlib.pyplot as plt
from nnls import nnlsm_blockpivot as nnlstsq
import itertools
from scipy.spatial.distance import cdist
def censored_lstsq(A, B, M):
"""Solves least squares problem with missing data in B
@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 / make_nmf_fig.py
Last active April 27, 2023 12:55
PCA and NMF with cross-validation (using Numpy)
from pca_crossval import *
from tqdm import tqdm
import itertools
np.random.seed(1111)
m, n, r = 100, 101, 3
Utrue, Vtrue = rand(m,r), rand(r,n)
data = np.dot(Utrue, Vtrue) + .25*randn(m,n)
data[data < 0] = 0
ranks = []
@ahwillia
ahwillia / hclust_sort.py
Created June 22, 2020 23:26
Sort data points by hierarchical clustering
from sklearn.datasets import make_biclusters
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def resort_rows_hclust(U):
"""Sorts the rows of a matrix by hierarchical clustering
Parameters:
U (ndarray) : matrix of data
@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