Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / rojo.py
Last active August 29, 2019 18:02
Fast solver for a symmetric tridiagonal circulant linear system in Python.
import numpy as np
from scipy.linalg import solve_circulant, circulant
from numpy.testing import assert_array_almost_equal
import numba
@numba.jit(nopython=True, cache=True)
def rojo_method(c, a, f, x, z):
"""
Solves symmetric, tridiagonal circulant system, assuming diagonal
@ahwillia
ahwillia / bandpass.py
Created February 20, 2019 23:29
Bandpass filter in python... (I have no idea why scipy does not provide this)
def bandpass(x, lowcut, highcut, fs, order=5, axis=-1, kind='butter'):
"""
Parameters
----------
x : ndarray
1d time series data
lowcut : float
Defines lower frequency cutoff (e.g. in Hz)
highcut : float
Defines upper frequency cutoff (e.g. in Hz)
@ahwillia
ahwillia / poiss_tf.py
Created September 26, 2018 23:04
Computing hessian-vector products in tensorflow
"""
Computing hessian-vector products in tensorflow.
For simplicity, we demonstrate the idea on a Poisson regression model.
"""
import tensorflow as tf
import numpy as np
from scipy.optimize import minimize
@ahwillia
ahwillia / poiss_reg.py
Last active September 26, 2018 22:35
Poisson Regression via scipy.optimize
"""
A simple implementation of Poisson regression.
"""
import numpy as np
from scipy.optimize import minimize
n = 1000 # number of datapoints
p = 5 # number of features