Skip to content

Instantly share code, notes, and snippets.

@indapa
Created December 17, 2012 20:09
Show Gist options
  • Save indapa/4321661 to your computer and use it in GitHub Desktop.
Save indapa/4321661 to your computer and use it in GitHub Desktop.
import numpy as np
from Factor import *
from FactorOperations import *
from PGMcommon import *
#make Factor objects
D=Factor( [1], [2], [.6,.4],'Difficulty')
I=Factor( [5], [2], [.7,.3], 'Intelligence' )
G=Factor( [2, 5,1], [ 3, 2, 2], [.3,.4,.3,.9,.08,.02,.05,.25,.7,.5,.3,.2], 'Grade', )
S=Factor( [3,5], [2,2], [.95,.05,.2,.8], 'SAT')
L=Factor( [4,2], [2,3], [.1,.9,.4,.6,.99,.01], 'Letter' )
INPUTS=[D,G,S,L,I]
#ComputeMarginal returns a factor representing the marginal of a variable
#compute marignal probability of variable 4, which is Pr(Letter)
print ComputeMarginal( [4], INPUTS, [])
#Pr(L | Intelligence=smart)
print ComputeMarginal( [4], INPUTS, [1,1])
#Pr(L | Difficulty=easy, Intelligence=smart)
print ComputeMarginal( [4], INPUTS, [5,1,1,1])
#Pr(Intelligence=gifted|grade=C)
print ComputeMarginal([5], INPUTS, [2,3])
#Pr(Intelligence=gifted|letter=weak)
print ComputeMarginal([5], INPUTS, [4,1])
#Pr(Intelligence=gifted|letter=weak, grade=C)
print ComputeMarginal([5], INPUTS, [2,3,4,1])
#Pr(Intelligence=gifted|grade=C, SAT=high)
print ComputeMarginal([5], INPUTS, [2,3,3,2])
#Pr(Difficulty|grade=C)
print ComputeMarginal([1], INPUTS, [2,3])
#Pr(Difficulty|grade=C, SAT=high)
print ComputeMarginal([1], INPUTS, [2,3,3,2])
#Pr(Intelligence|grade=C,diffculty=hard
print ComputeMarginal([5], INPUTS, [2,3,1,2])
#Pr(Intelligence|grade=B)
print ComputeMarginal([5], INPUTS, [2,2])
#Pr(Intelligence|grade=B, difficulty=hard)
print ComputeMarginal([5], INPUTS, [2,2,1,2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment