Instantly share code, notes, and snippets.

import numpy as np
def _logsum(logx, logy):
Return log(x+y), avoiding arithmetic underflow/overflow.
logx: log(x)
logy: log(y)
# adapted from
import socket
import gobject
def server(host, port):
'''Initialize server and start listening.'''
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
def recv_count():
while True:
n = (yield)
print "T-minus", n
except GeneratorExit:
print "Kaboom!"
def ex1():
r = recv_count()
View hmm.tex
% (C) Mathieu Blondel, July 2010
#!/usr/bin/env python
Find the solution for the second order differential equation
u'' = -u
with u(0) = 10 and u'(0) = -5
using the Euler and the Runge-Kutta methods.
#!/usr/bin/env python
Find the operations needed to sum up to TARGET by using all 4 numbers in NUMBERS.
from itertools import permutations, product
NUMBERS = ["3","4","7","8"]
TARGET = 10.0
from random import random
Find pi by the Monte-Carlo method.
area of a circle = pi r^2
area of a square = (2r)^2 = 4 r^2
Perform random uniform sampling between -1 and 1.
The proportion of points in the unit circle is:
Exercises for the Markov Chain Monte-Carlo (MCMC) course available at
import numpy as np
import numpy.linalg as la
import pylab
from scipy import stats
(C) Mathieu Blondel - 2010
License: BSD 3 clause
Implementation of the collapsed Gibbs sampler for
Latent Dirichlet Allocation, as described in
Finding scientifc topics (Griffiths and Steyvers)
# Mathieu Blondel, September 2010
# License: BSD 3 clause
import numpy as np
from numpy import linalg
import cvxopt
import cvxopt.solvers
def linear_kernel(x1, x2):
return, x2)