Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@ahwillia
ahwillia / kron_vec_product.py
Last active February 17, 2025 08:46
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 / circ_regression.py
Last active April 16, 2025 14:53
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 / cca.py
Created February 11, 2020 23:14
Ridge CCA
import numpy as np
from sklearn.utils.extmath import randomized_svd
def partial_whiten(X, alpha, eigval_tol=1e-7):
"""
Return regularized whitening transform for a matrix X.
Parameters
----------
@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 / 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 / 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 / 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 / msplines.py
Last active July 21, 2025 21:54
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 / pytorch_nmf.py
Last active May 17, 2024 08:26
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 / 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