Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@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 / tensorflow_pca.py
Created October 27, 2016 07:25
PCA in TensorFlow
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 / 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 / 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 / plot_equation.py
Last active March 31, 2020 18:59
Plot a LaTeX equation as a matplotlib figure
# Majority of credit goes to Chris Holdgraf, @choldgraf, and this StackOverflow
# post: http://stackoverflow.com/questions/5320205/matplotlib-text-dimensions
import pylab as plt
import numpy as np
def plot_equation(eq, fontsize=50, outfile=None, padding=0.1, **kwargs):
"""Plot an equation as a matplotlib figure.
Parameters
----------
@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)
@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 / 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 / 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 / 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)