Skip to content

Instantly share code, notes, and snippets.

View alextp's full-sized avatar

Alexandre Passos alextp

View GitHub Profile
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
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] |]
@alextp
alextp / avisa.py
Created January 31, 2011 00:16
Avisa a pessoa para alongar o pescoço
#!/usr/bin/env python
# coding: utf-8
import pygtk
pygtk.require('2.0')
import gtk
import time
class Avisa(object):
def __init__(self):
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
@alextp
alextp / fast_svd.py
Created November 4, 2010 13:12
Gunnar Martinsson's fast svd
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]
# coding: utf-8
import theano
import theano.tensor as T
import theano.sparse
import numpy as np
class Arow(object):
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
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"
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
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()