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 |
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
#!/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
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
import numpy as np, numpy.linalg as linalg | |
def fast_svd(M, k): | |
p = k+5 | |
Y = np.dot(M, np.random.normal(size=(M.shape[1],p))) | |
Q,r = linalg.qr(Y) | |
B = np.dot(Q.T,M) | |
Uhat, s, v = linalg.svd(B, full_matrices=False) | |
U = np.dot(Q, Uhat) | |
return U.T[:k].T, s[:k], v[:k] |
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 | |
def symdirichlet(alpha, n): | |
v = np.zeros(n)+alpha | |
return np.random.dirichlet(v) | |
def exp_digamma(x): | |
if x < 0.1: | |
return x/100 |
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
import numpy as np | |
import random | |
import math | |
from scipy.special import gamma,gammaln | |
from scipy import weave | |
import sys | |
import collections | |
from libbayes import discrete, gamma_pdf, slice_sample, exp_pdf | |
from libbayes import single_collapsed_likelihood as scl | |
from toputils import get_words |
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() |