# Code from Chapter 16 of Machine Learning: An Algorithmic Perspective (2nd Edition)
# by Stephen Marsland (http://stephenmonika.net)
# 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, 2014
# A Gibbs sampler for the Exam Panic dataset
import numpy as np
Pb = np.array([[0.5,0.5]])
Pr_b = np.array([[0.3,0.7],[0.8,0.2]])
Pa_b = np.array([[0.1,0.9],[0.5,0.5]])
Ps_ra = np.array([[0,1],[0.8,0.2],[0.6,0.4],[1,0]])
"""
P(b|ras)=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_ras(values):
if np.random.rand()