Skip to content

Instantly share code, notes, and snippets.

View michaelchughes's full-sized avatar

Mike Hughes michaelchughes

View GitHub Profile
@michaelchughes
michaelchughes / DebugHighDimDirichletLogProb
Created September 11, 2012 17:01
Script to understand calculation of log probability of the Dirichlet in high dimensions.
% Script to understand calculation of log probability of the Dirichlet
% in high dimensions.
% Requires: Minka's fastfit toolbox
% http://research.microsoft.com/en-us/um/people/minka/software/fastfit/
% For complete problem description, check out
% http://stats.stackexchange.com/questions/36111/calculating-log-prob-of-dirichlet-distribution-in-high-dimensions
concParam = 0.01;
EPS = 1e-15;
N = 100;
M = 4;
sig = .4;
% Create evenly spaced pts between 0 and M
% X is N x 1
X = linspace( 0, M, N )';
% Calc squared Euclidean distance
% between every pair of rows in X
@michaelchughes
michaelchughes / MatrixProductMEX.cpp
Last active March 13, 2018 12:53
Simple Mex function to take in two Matlab matrices and return their matrix product. R = MatrixProductMEX( X, Y); same as R = X*Y;
#include "Eigen/Dense"
#include "mex.h"
#include <iostream>
using namespace Eigen;
using namespace std;
typedef Map<MatrixXd> MexMat;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
@michaelchughes
michaelchughes / MatProdEigen.cpp
Last active December 12, 2015 08:59
Multiplies two random matrices using Eigen, and displays the elapsed time for the multiplication only (not the creation of random matrices). Recommended compiliation (using g++): >> g++-mp-4.7 MatProdEigen.cpp -o MatProdEigen -I/path/to/eigen/ -O3 -DNDEBUG To multiply a 10x3 and 3x55 matrix: >> ./MatProdEigen 10 3 55
#include "Eigen/Dense"
#include <iostream>
#include <time.h>
using namespace Eigen;
using namespace std;
double diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks)/CLOCKS_PER_SEC;
@michaelchughes
michaelchughes / DMMultLik.m
Created February 18, 2013 20:40
Is Matlab faster than Python for Bayesian Machine Learning? This is benchmark code for comparing the computation of a multinomial-dirichlet marginal likelihood. Compare the function "calc_marg_lik" in DMMultLik.py with the Matlab function "DMMultLik.m". DMMultLik.py is a script that will compare both of these for Ntrial runs on a given problem.
function logp = DMMargLik( Nvec, alphvec )
logZ_lik = sum( gammaln( Nvec+alphvec ) ) - gammaln( sum(Nvec+alphvec) );
logZ_prior = sum( gammaln( alphvec) ) - gammaln( sum( alphvec ) );
logp = logZ_lik - logZ_prior;
end
@michaelchughes
michaelchughes / RankOneUpdate.m
Created April 19, 2013 21:25
Rank one update of cholesky and log determinant of the posterior parameters of a Wishart distribution.
function [] = RankOneUpdateTest( D )
nTrial = 1000;
invW = randn( 3*D, D);
invW = invW'*invW;
x = randn(D,1);
cholinvW = chol(invW,'upper');
logdetinvW = 2*sum(log(diag(cholinvW)));
@michaelchughes
michaelchughes / Countdown.m
Created May 1, 2016 19:54
Simple demo script for launching Matlab programs on Brown CS grid
function [] = Countdown(start)
% Will just count down from provided input number to 1
% Printing each value along the way.
%
disp('Counting down...');
for i = start:-1:1
pause(0.5); % wait half a second
disp(i);
end
@michaelchughes
michaelchughes / calc_elbo_super_lda_for_single_doc.py
Created November 8, 2016 22:54
Example code to compute ELBO objective as function of single-doc doc-topic probas vector (theta)
import autograd.numpy as np
from autograd.scipy.special import gammaln, digamma
from autograd import grad, hessian
from scipy.optimize import fmin_l_bfgs_b
def L_theta(
theta_d_K=None,
N_d_K=None,
alpha_K=None,
@michaelchughes
michaelchughes / calc_bar_z_for_slda.py
Created December 14, 2016 22:49
How to compute variational expectations of empirical doc-topic probabilities
import numpy as np
import itertools
def calc_bar_z_K(resp_UK=None, wc_U=None):
''' Compute empirical doc-topic probability vector
Returns
-------
barz_K : 1D array, size K
barz_K[k] = fraction of tokens in doc explained by topic k
@michaelchughes
michaelchughes / transform_util_positive.py
Created February 17, 2017 18:05
Utility functions for softplus transform between positive reals and unconstrained reals.
''' Define invertible, differentiable transform for arrays of positive vals.
Utility funcs for transforming positive vals to and from unconstrained real line.
This transform is sometimes called the "softplus" transformation.
See: https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
To run automated tests to verify correctness of this script, do:
(Add -v flag for verbose output)
```