Skip to content

Instantly share code, notes, and snippets.

@redpony
redpony / gist:2341487
Created April 9, 2012 04:46
Constant time sampling from multinomials (linear time initialization)
#ifndef _ALIAS_SAMPLER_H_
#define _ALIAS_SAMPLER_H_
#include <vector>
#include <limits>
// Placed in the public domain by Chris Dyer <cdyer@cs.cmu.edu>
// April 9, 2012
//
// R. A. Kronmal and A. V. Peterson, Jr. (1977) On the alias method for
@redpony
redpony / logval.h
Created April 16, 2012 18:17
C++ class to represent real numbers in the log domain
#ifndef LOGVAL_H_
#define LOGVAL_H_
// represent values internally in the log domain (much larger effective range than float,
// double, or long double). useful for very small probabilities or very large unnormalized
// probabilities while avoiding underflows/overflows.
//
// this should be used as if it were a double or float, i.e. for doubles a and b:
// a * b = LogVal(a) * LogVal(b)
// a + b = LogVal(a) + LogVal(b)
@redpony
redpony / logdet.cc
Last active May 13, 2023 12:49
Computing log(M.determinant()) in Eigen C++ is risky for large matrices since it may overflow or underflow. This gist uses LU (or, if applicable, Cholesky) decompositions to do the risky components in the log space.
// set use_cholesky if M is symmetric - it's faster and more stable
// for dep paring it won't be
template <typename MatrixType>
inline typename MatrixType::Scalar logdet(const MatrixType& M, bool use_cholesky = false) {
using namespace Eigen;
using std::log;
typedef typename MatrixType::Scalar Scalar;
Scalar ld = 0;
if (use_cholesky) {
LLT<Matrix<Scalar,Dynamic,Dynamic>> chol(M);