Skip to content

Instantly share code, notes, and snippets.

View fonnesbeck's full-sized avatar

Chris Fonnesbeck fonnesbeck

  • Philadelphia Phillies
View GitHub Profile
@fonnesbeck
fonnesbeck / least_squares.py
Created April 14, 2011 13:15
Least squares with equality constraint (taken and modified from http://fseoane.net/blog/2011/least-squares-with-equality-constrain/)
import numpy as np
from scipy import linalg
def lse(A, b, B, d, cond=None):
"""
Equality-contrained least squares.
The following algorithm minimizes ||Ax - b|| subject to the
constrain Bx = d.
@fonnesbeck
fonnesbeck / iterator_class.py
Created July 12, 2011 21:46
Wrapping a class in an iterator
class AlwaysFive:
def __iter__(self):
while True:
yield 5
a = AlwaysFive()
it = iter(a)
@fonnesbeck
fonnesbeck / koala.py
Created April 6, 2012 00:52
PyMC model of koala sighting
# ====================================================================================
# = Koala sightings example from Link and Barker 2009. Original WinBUGS model below. =
# ====================================================================================
import numpy as np
from numpy.ma import masked_array
from pymc import *
# Prior model weights, derived from Bayes factors
pi = (0.016656, 0.052853, 0.045682, 0.087367, 0.091975, 0.221741, 0.19819,
@fonnesbeck
fonnesbeck / GPTutorial.ipynb
Created April 10, 2012 16:46
An iPython notebook containing a short PyMC tutorial on Gaussian Processes. Requires PyMC and iPython (>=0.12) to be installed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@fonnesbeck
fonnesbeck / simplegibbs.py
Created May 10, 2012 15:46
Simple Gibbs sampler, derived from http://bit.ly/IWhJ52
'''
Gibbs sampler for function:
f(x,y) = x x^2 \exp(-xy^2 - y^2 + 2y - 4x)
using conditional distributions:
x|y \sim Gamma(3, y^2 +4)
y|x \sim Normal(\frac{1}{1+x}, \frac{1}{2(1+x)})
'''
@fonnesbeck
fonnesbeck / metropolis.py
Created May 10, 2012 21:47
Simple self-tuning random walk Metropolis algorithm
"""
A random walk Metropolis algorithm with tuning, implemented for a linear model that
predicts price as a function of age.
"""
import numpy as np
from scipy.stats import distributions
rnorm = np.random.normal
@fonnesbeck
fonnesbeck / recovery.py
Created May 11, 2012 13:47
Random effects band recovery model in PyMC
from pymc import *
from numpy import array, resize, diag, exp, zeros, prod, concatenate
import pdb
# Data
Releases = array([12,12,10,14,17,21,9,30,25,41,45,79,97,80,62,80,74,74,46,45])
NRCperiods = len(Releases)
recdata = resize((
8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,
0,7,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,
@fonnesbeck
fonnesbeck / simplegibbs_cython.pyx
Created May 11, 2012 15:27
Simple Gibbs sampler in cython, adapted from http://bit.ly/J3vP63
'''
Gibbs sampler for function:
f(x,y) = x x^2 \exp(-xy^2 - y^2 + 2y - 4x)
using conditional distributions:
x|y \sim Gamma(3, y^2 +4)
y|x \sim Normal(\frac{1}{1+x}, \frac{1}{2(1+x)})
'''
@fonnesbeck
fonnesbeck / nests.dat
Created July 9, 2012 19:40
Simple nest dataset
species,eggs,parasite,lat,lon
WOTH, 2, 0, -82.32, 25.10
TOSO, 2, 0, -80.56, 26.66
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.