Skip to content

Instantly share code, notes, and snippets.

@fonnesbeck
fonnesbeck / cprob.tex
Created March 24, 2010 23:59
Conditional probability statement in LaTeX
Y_i \sim \left\{ \begin{array}{l}
0, \text{with probability } (1-\psi) \\
\text{Poisson}(\mu), \text{with probability } \psi
\end{array} \right.
@fonnesbeck
fonnesbeck / hmm.py
Created March 25, 2010 00:01
Hidden Markov model in PyMC
import numpy as np
import pymc
import pdb
def unconditionalProbability(Ptrans):
"""Compute the unconditional probability for the states of a
Markov chain."""
m = Ptrans.shape[0]
@fonnesbeck
fonnesbeck / gist:342991
Created March 25, 2010 00:02
Convert date to SQLite in Numbers
=CONCATENATE(YEAR(F2), "-", IF(LEN(MONTH(F2))<2, "0",), MONTH(F2), "-", IF(LEN(DAY(F2))<2, "0",), DAY(F2))
@fonnesbeck
fonnesbeck / gist:342992
Created March 25, 2010 00:03
Equal in distribution
\,{\buildrel d \over =}\,
@fonnesbeck
fonnesbeck / rtpois.R
Created March 25, 2010 00:05
Truncated Poisson random numbers in R
rtpois <- function(N, lambda, k) qpois(runif(N, ppois(k, lambda), 1), lambda)
@fonnesbeck
fonnesbeck / factor2numeric.R
Created March 25, 2010 00:06
Convert factor levels to numeric in R. Slightly more efficient than "as.numeric(as.character(f))"
as.numeric(levels(f))[f]
@fonnesbeck
fonnesbeck / price.py
Created March 25, 2010 00:07
Simple Bayesian hierarchical model in PyMC. Coded by A. MacNeil.
"""
Simple hierarchical linear model of price as a function of age.
"""
from pymc import *
from numpy import array
# Data
age = array([13, 14, 14,12, 9, 15, 10, 14, 9, 14, 13, 12, 9, 10, 15, 11, 15, 11, 7, 13, 13, 10, 9, 6, 11, 15, 13, 10, 9, 9, 15, 14, 14, 10, 14, 11, 13, 14, 10])
price = array([2950, 2300, 3900, 2800, 5000, 2999, 3950, 2995, 4500, 2800, 1990, 3500, 5100, 3900, 2900, 4950, 2000, 3400, 8999, 4000, 2950, 3250, 3950, 4600, 4500, 1600, 3900, 4200, 6500, 3500, 2999, 2600, 3250, 2500, 2400, 3990, 4600, 450,4700])/1000.
@fonnesbeck
fonnesbeck / mmd_template.txt
Created April 14, 2010 20:23
MultiMarkdown document template for TextMate
Title: MultiMarkdown Document
Subtitle: Adventures in MultiMarkdown
Author: ${TM_FULLNAME}
Affiliation: ${TM_ORGANIZATION_NAME}
Email: ${TM_EMAIL}
Web: ${TM_URL}
Date: ${TM_DATE}
Copyright: ${TM_YEAR} ${TM_ORGANIZATION_NAME}
This work is licensed under a Creative Commons License.
http://creativecommons.org/licenses/by-nc-sa/3.0/
@fonnesbeck
fonnesbeck / fill_missing.py
Created February 8, 2011 15:18
Nice Python idiom for replacing missing values in a data iterable
missing = -999
filled_data = [(x, missing)[x is ''] for x in raw_data]
@fonnesbeck
fonnesbeck / zip.py
Created March 17, 2011 18:03
Zero-inflated Poisson example using simulated data.
#!/usr/bin/env python
"""
zip.py
Zero-inflated Poisson example using simulated data.
Created by Chris Fonnesbeck on 2008-06-06.
Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
"""