Created
March 17, 2012 17:51
-
-
Save indapa/2063458 to your computer and use it in GitHub Desktop.
CPD computations for Bayesian network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#I'm taking the STanford on line course Probabilistic Graphical Models <pgm-class.org> | |
# They have a simple example of a Bayesian network for a Student letter of reference | |
# the code below was originally posted by a fellow classmate taking the class | |
# it really helped me in understanding how to do computations on conditional probability tables (CPDs) that | |
# form the heart of Bayesian networks | |
from numpy import * | |
P_I = array([ | |
[0.7,0.3] | |
]) | |
print "Probability of Intelligence: P(I)" | |
print P_I | |
P_D = array([ | |
[0.6,0.4] | |
]) | |
print "Probability of Intelligence: P(D)" | |
print P_D | |
print "Join Probability of Intelligence and Difficulty: P(I,D)" | |
P_ID = P_I.T.dot(P_D) | |
print P_ID | |
print "... flattened and copied out for easy multiplication with P(G|I,D)" | |
P_ID = array([P_ID.ravel()]).T | |
P_ID = hstack([P_ID,P_ID,P_ID]) | |
print P_ID | |
print "Conditional probability of grade P(G|I,D)" | |
P_G_ID = array([ | |
[0.3,0.4,0.3], | |
[0.05,0.25,0.7], | |
[0.9,0.08,0.02], | |
[0.5,0.3,0.2], | |
]) | |
print P_G_ID | |
print "Joint probability of grade, intelligence and difficulty P(G,I,D) = P(G|I,D)*P(I)*P(D) = P(G|I,D)*P(I,D)" | |
P_GID = P_G_ID * P_ID | |
P_GID = P_GID | |
print P_GID | |
print "Joint probability of grade and difficulty with intelligence = i0 P(G,I=i0,D) = P(G|I=i0,D)*P(D) = P(G|I=i0,D)*P(D)" | |
P_Dstack = hstack([P_D.T,P_D.T,P_D.T]) | |
P_Gi0D = P_G_ID[0:2,:] * P_Dstack | |
print P_Gi0D | |
print "Joint probability of grade and difficulty with intelligence = i1 P(G,I=i1,D) = P(G|I=i1,D)*P(D)" | |
P_Dstack = hstack([P_D.T,P_D.T,P_D.T]) | |
P_Gi1D = P_G_ID[2:,:] * P_Dstack | |
print P_Gi1D | |
P_Istack = hstack([P_I.T,P_I.T]) | |
P_S_I = array([ | |
[0.95,0.05], | |
[0.2,0.8], | |
]) | |
P_SI = P_S_I * P_Istack | |
P_L_G = array([ | |
[0.1,0.9], | |
[0.4,0.6], | |
[0.99,0.01], | |
]) | |
l1 = 0.9 * P_GID[:,0].sum() + 0.6 * P_GID[:,1].sum() + 0.01 * P_GID[:,2].sum() | |
l1_i0 = array(P_Gi0D.sum(axis=0)).dot(P_L_G[:,1]) | |
l1_i0_d0 = array(P_G_ID[0,:]).dot(P_L_G[:,1]) | |
l1_i1 = array(P_Gi1D.sum(axis=0)).dot(P_L_G[:,1]) | |
print "Probability of l1\n... (i.e. P(L = l1,G,I,D) = P(L = l1 | G) * P(G|I,D) * P(S|I) * P(I) * P(D) ):", l1 | |
print "Probability of l1 and i0\n... (i.e. P(L = l1,G,I = i0,D) = P(L = l1,| G,I = i0, D) * P(G|I = i0,D) * P(I = i0) * P(D) ):", l1_i0 | |
print "Probability of l1 and i1\n... (i.e. P(L = l1,G,I = i1,D) = P(L = l1,| G,I = i1, D) * P(G|I = i1,D) * P(I = i0) * P(D) ):", l1_i1 | |
print "Probability of l1 and i0 and d0\n... (i.e. P(L = l1,G,I = i0,D = d0) = P(L = l1,| G,I = i0, D = d0) * P(G|I = i0,D = d0) ):", l1_i0_d0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment