# Code from Chapter 15 of Machine Learning: An Algorithmic Perspective # by Stephen Marsland (http://seat.massey.ac.nz/personal/s.r.marsland/MLBook.html) # You are free to use, change, or redistribute the code in any way you wish for # non-commercial purposes, but please maintain the name of the original author. # This code comes with no warranty of any kind. # Stephen Marsland, 2008 # A Gibbs sampler for the Exam Panic dataset from numpy import * Pb = array([[0.5,0.5]]) Pr_b = array([[0.3,0.7],[0.8,0.2]]) Pa_b = array([[0.1,0.9],[0.5,0.5]]) Pp_ra = array([[0,1],[0.8,0.2],[0.6,0.4],[1,0]]) """ P(b|rap)=P(b|ra)=P(ra|b)*P(b)/P(ra)=P(r|b)*P(a|b)*P(b)/P(ra) r a P(b) T T 0.3*0.1*0.5/0.215=0.0698 T F 0.3*0.9*0.5/0.335=0.4030 F T 0.7*0.1*0.5/0.085=0.4118 F F 0.7*0.9*0.5/0.365=0.8630 """ def pb_rap(values): if random.rand()