~ubuntu-branches/ubuntu/karmic/model-builder/karmic

« back to all changes in this revision

Viewing changes to model_builder/Bayes/gibbs.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-04-10 17:05:04 UTC
  • Revision ID: james.westby@ubuntu.com-20070410170504-y884ntvt656218me
Tags: upstream-0.4.0
Import upstream version 0.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding:iso8859-1 -*-
 
2
#-----------------------------------------------------------------------------
 
3
# Name:        gibbs.py
 
4
# Purpose:     This simple routine implements a simple Gibbs sampler.
 
5
#
 
6
# Author:      Fl�vio Code�o Coelho
 
7
#
 
8
# Created:     2003/25/09
 
9
# RCS-ID:      $Id: module1.py $
 
10
# Copyright:   (c) 2003
 
11
# Licence:     GPL
 
12
# Acknowledgements:   Whatever
 
13
#-----------------------------------------------------------------------------
 
14
 
 
15
from math import *
 
16
from RandomArray import *
 
17
from matplotlib.pylab import * 
 
18
 
 
19
 
 
20
n=10000
 
21
rho=.99 #correlation
 
22
#Means
 
23
m1 = 10
 
24
m2 = 20
 
25
#Standar deviations
 
26
s1 = 1
 
27
s2 = 1
 
28
#Initialize vectors
 
29
x=zeros(n, Float)
 
30
y=zeros(n, Float)
 
31
sd=sqrt(1-rho**2)
 
32
# the core of the method: sample recursively from two normal distributions
 
33
# Tthe mean for the current sample, is updated at each step.
 
34
for i in range(1,n):
 
35
  x[i] = normal(m1+rho*(y[i-1]-m2)/s2,s1*sd)
 
36
  y[i] = normal(m2+rho*(x[i-1]-m1)/s1,s2*sd)
 
37
 
 
38
scatter(x,y,marker='d',c='r')
 
39
title('Amostrador de Gibbs')
 
40
xlabel('x')
 
41
ylabel('y')
 
42
grid()
 
43
 
 
44
show()