UsefulCode

From CCGB

Jump to: navigation, search

Some useful python code collected from online source by Ying.

For more pages from the Hardison Lab see HardisonLab.


Contents

Gibbs Sampling

A wikipedia page for "Gibbs Sampling": CLICK HERE

A simple python code to generate a bivariate normal distribution using Gibbs sampling is from: Dr. Darren Wilkinson

Python code

import sys
from math import *
from random import random

def genexp(lamb):
  return (-1.0/lamb)*log(random())

def gennor(mu,sigma):
  theta=random()*2*pi
  rsq=genexp(0.5)
  z=sqrt(rsq)*cos(theta)
  return mu+z*sigma

n=int(sys.argv[1])
rho=float(sys.argv[2])
x=0
y=0
sig=sqrt(1-rho*rho)
for i in range(n):
  x=gennor(rho*y,sig)
  y=gennor(rho*x,sig)
  print x,y
Personal tools