This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def collapsed_likelihood(tprior, gmean, gvar, tcount): | |
p0 = sum(np.log(gamma_pdf(tp, gmean, gvar)) for tp in tprior) | |
p0 += tcount.shape[0]*(gammaln(sum(tprior))-sum(gammaln(tp) for tp in tprior)) | |
for d in xrange(tcount.shape[0]): | |
for i,t in enumerate(tcount[d]): | |
p0 += gammaln(t + tprior[i]) | |
p0 -= gammaln(sum(tcount[d]) + sum(tprior)) | |
return p0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def single_collapsed_likelihood(tprior, gmean, gvar, tcount): | |
"Assumes tprior is a scalar" | |
p0 = np.log(gamma_pdf(tprior, gmean, gvar)) | |
p0 += tcount.shape[0]*(gammaln(tprior*tcount.shape[1])-gammaln(tprior)*tcount.shape[1]) | |
for d in xrange(tcount.shape[0]): | |
for i,t in enumerate(tcount[d]): | |
p0 += gammaln(t + tprior) | |
p0 -= gammaln(sum(tcount[d]) + tprior*tcount.shape[1]) | |
return p0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hash_collapsed_likelihood(tprior, gmean, gvar, tcount, s1): | |
"Assumes a single hyperparameter" | |
p0 = np.log(gamma_pdf(tprior, gmean, gvar)) | |
s0 = len(tcount) | |
p0 += s0*(gammaln(tprior*s1)-sum(gammaln(tprior) for i in xrange(s1))) | |
for d in xrange(s0): | |
for i,t in tcount[d].items(): | |
p0 += gammaln(t + tprior) | |
p0 += (s1-len(tcount[d]))*gammaln(tprior) | |
p0 -= gammaln(sum(tcount[d]) + s1*tprior) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def slice_sample(likelihood, x0): | |
old_lik = likelihood(x0) | |
old_alpha = x0 | |
lnt = old_lik - np.random.exponential(1) | |
w = old_alpha/32. | |
L = max(0, old_alpha - w*random.random()) | |
R = L + w | |
K = 4 | |
while K > 0 and (lnt < likelihood(L) or lnt < likelihood(R)): | |
V = random.random() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def symdirichlet(alpha, n): | |
v = np.zeros(n)+alpha | |
return np.random.dirichlet(v) | |
import re | |
wre = re.compile(r"(\w)+") | |
def get_words(text, stop=True): | |
"A simple tokenizer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import theano | |
import theano.tensor as T | |
import theano.sparse | |
import numpy as np | |
class Arow(object): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np, scipy, scipy.sparse, numpy.linalg, scipy.optimize | |
from scipy import weave | |
def project_l1(lbda, sigma): | |
"Project positive vector lbda to have l1 norm sigma" | |
ll = -np.sort(-lbda) | |
cs = 0. | |
theta = 0 | |
prevtheta = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import pygtk | |
pygtk.require('2.0') | |
import gtk | |
import time | |
class Avisa(object): | |
def __init__(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open MathNet.Numerics; | |
open System | |
let a = Fn.Factorial 10 | |
let elementwiseProduct (a: float array) (b: float array) = [| for i in 0..a.Length-1 -> a.[i]*b.[i] |] | |
let arraySum (a: float array) (b: float array) = [|for i in 0..a.Length-1 -> a.[i]+b.[i] |] | |
let scalarProduct (a: float) (b: float array) = [| for i in 0..b.Length-1 -> a*b.[i] |] | |
let expectation (z: float array) (v: float array array) = | |
let prod = [| for i in 0..v.Length-1 -> scalarProduct z.[i] v.[i] |] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let gaussLik (a: float array) (b: float array) = | |
Math.Exp -0.5* (dist a b) | |
let sampleDiscrete (p: float array) = | |
let d = new Distributions.ArbitraryDistribution() | |
d.SetDistributionParameters(0, p) | |
d.NextInt32() | |
// Samples the z variable for a single theta |
OlderNewer