Skip to content

Instantly share code, notes, and snippets.

View ahwillia's full-sized avatar

Alex Williams ahwillia

View GitHub Profile
@ahwillia
ahwillia / pairs.jl
Last active August 29, 2015 14:06
Scatterplot matrix in julia
# Thanks to Joe Kington
# http://stackoverflow.com/questions/7941207/is-there-a-function-to-make-scatterplot-matrices-in-matplotlib
using PyPlot
function pairs(data)
(nobs, nvars) = size(data)
(fig, ax) = subplots(nvars, nvars, figsize=(8,8))
subplots_adjust(hspace=0.05, wspace=0.05)
@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 / 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 / equations.tex
Last active June 2, 2018 21:38
Export a sequence of LaTeX equations to individual image files
% quad %
\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]
% optimization %
\usepackage{amsmath}
\begin{equation*}
\begin{aligned}
& \underset{x}{\text{minimize}}
& & f_0(x) \\
& \text{subject to}
@ahwillia
ahwillia / dtwplot.jl
Last active August 28, 2016 17:56
Problem I'm having with userplot
@userplot DTWPlot
@recipe function f(h::DTWPlot)
seq1, seq2 = h.args
i1,i2 = collect(1:length(seq1)),collect(1:length(seq2))
# set up the subplots
seriestype := :line
legend --> false
link := :both
@ahwillia
ahwillia / altgradpca.jl
Created September 11, 2016 06:19
Prototype for alternating gradient descent with Optim.jl
using Optim
# low-dimensional data embedded in high-dimension
data = randn(100,5)*randn(5,100);
# a container for the parameters we fit
immutable PCA{T<:Real}
X::Matrix{T}
Y::Matrix{T}
end
@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 / 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)
@ahwillia
ahwillia / randomized_pca.py
Last active May 10, 2017 17:51
Randomized Matrix Factorization in TensorFlow
import tensorflow as tf # works on version 1.0.0
import numpy as np
from tqdm import trange
# create fake data (low-rank matrix X)
A = np.random.randn(100, 3).astype(np.float32)
B = np.random.randn(3, 100).astype(np.float32)
X = np.dot(A, B)
# create tensorflow variables to predict low-rank decomposition
@ahwillia
ahwillia / cv_pca.py
Created June 20, 2017 21:21
Tensorflow PCA with cross-validation
import numpy as np
import tensorflow as tf
from tqdm import tqdm
# N, size of matrix. R, rank of data
N = 100
R = 5
# generate data
W_true = np.random.randn(N,R).astype(np.float32)