def setUp(self): self.factA = pgmf.DiscreteFactor(scope=np.array([1]), card=np.array([2]), val=np.array([0.11, 0.89])) self.factB = pgmf.DiscreteFactor(scope=np.array([2, 1]), card=np.array([2, 2]), val=np.array([0.59, 0.41, 0.22, 0.78])) self.factC = pgmf.DiscreteFactor(scope=np.array([3, 2]), card=np.array([2, 2]), val=np.array([0.39, 0.61, 0.06, 0.94])) self.factorList = [self.factA, self.factB, self.factC]
def setUp(self): self.fact1 = pgmf.DiscreteFactor(scope=np.array([1]), card=np.array([2]), val=np.array([0.11, 0.89])) self.fact2 = pgmf.DiscreteFactor(scope=np.array([2, 1]), card=np.array([2, 2]), val=np.array([0.59, 0.41, 0.22, 0.78])) self.fact3 = pgmf.DiscreteFactor(scope=np.array([3, 2]), card=np.array([2, 2]), val=np.array([0.39, 0.61, 0.06, 0.94])) self.factors = [self.fact1, self.fact2, self.fact3] self.pgm = pgmg.PGM(self.factors) self.evid = np.array([[1, 1]])
def test_add_factor(self): pgmCopy = copy.deepcopy(self.pgm) newFactor = pgmf.DiscreteFactor(scope=np.array([4]), card=np.array([2]), val=np.array([0.5, 0.5])) pgmCopy.add_factor(newFactor) factors = [self.fact1, self.fact2, self.fact3, newFactor] rvarTofIdx = { 1: [self.fact1, self.fact2], 2: [self.fact2, self.fact3], 3: [self.fact3], 4: [newFactor] } rvars = [1, 2, 3, 4] factors_ = pgmCopy.factors rvarToFactor_ = pgmCopy.rvarToFactor rvars_ = pgmCopy.rvars self.assertItemsEqual(rvars, rvars_) for fact, fact_ in zip(factors, factors_): np.testing.assert_equal(fact.scope, fact_.scope) np.testing.assert_equal(fact.card, fact_.card) np.testing.assert_almost_equal(fact.val, fact_.val, decimal=5)
def test_copy(self): factCopy = self.factA factCopy_ = pgmf.DiscreteFactor() factCopy_.copy(self.factA) #* np.testing.assert_equal(factCopy.scope, factCopy_.scope) np.testing.assert_equal(factCopy.card, factCopy_.card) np.testing.assert_almost_equal(factCopy.val, factCopy_.val, decimal=5)
def test_marginalize(self): var = np.array([2]) factMarg = pgmf.DiscreteFactor(scope=np.array([1]), card=np.array([2]), val=np.array([1, 1])) factMarg_ = copy.deepcopy(self.factB) factMarg_.marginalize(var) np.testing.assert_equal(factMarg.scope, factMarg_.scope) np.testing.assert_equal(factMarg.card, factMarg_.card) np.testing.assert_almost_equal(factMarg.val, factMarg_.val, decimal=5)
def test_reduce(self): evid = np.array([[2, 0], [3, 1]]) factRed = np.array([ pgmf.DiscreteFactor(scope=np.array([1]), card=np.array([2]), val=np.array([0.11, 0.89])), pgmf.DiscreteFactor(scope=np.array([2, 1]), card=np.array([2, 2]), val=np.array([0.59, 0, 0.22, 0])), pgmf.DiscreteFactor(scope=np.array([3, 2]), card=np.array([2, 2]), val=np.array([0, 0.61, 0, 0])) ]) factRed_ = copy.deepcopy(self.factorList) for fact, fact_ in zip(factRed, factRed_): fact_.reduce(evid) np.testing.assert_equal(fact.scope, fact_.scope) np.testing.assert_equal(fact.card, fact_.card) np.testing.assert_almost_equal(fact.val, fact_.val, decimal=5)
def test_compute_factor_product(self): factP = pgmf.DiscreteFactor(scope=np.array([1, 2]), card=np.array([2, 2]), val=np.array( [0.0649, 0.1958, 0.0451, 0.6942])) factP_ = copy.deepcopy(self.factA) factP_.compute_factor_product(self.factB) #* np.testing.assert_equal(factP.scope, factP_.scope) np.testing.assert_equal(factP.card, factP_.card) np.testing.assert_almost_equal(factP.val, factP_.val, decimal=5)
def test_compute_joint_distribution(self): joint = pgmf.DiscreteFactor(scope=np.array([1, 2, 3]), card=np.array([2, 2, 2]), val=np.array([ 0.025311, 0.076362, 0.002706, 0.041652, 0.039589, 0.119438, 0.042394, 0.652548 ])) joint_ = self.pgm.compute_joint_distribution() np.testing.assert_equal(joint.scope, joint_.scope) np.testing.assert_equal(joint.card, joint_.card) np.testing.assert_almost_equal(joint.val, joint_.val, decimal=5)
def test_compute_marginal_bf(self): margFactor = pgmf.DiscreteFactor(scope=np.array([2, 3]), card=np.array([2, 2]), val=np.array( [0.0858, 0.0468, 0.1342, 0.7332])) margFactor_ = self.pgm.compute_marginal_bf(np.array([2, 3]), self.evid) margFactor_.val = margFactor_.val / np.sum(margFactor_.val) np.testing.assert_equal(margFactor.scope, margFactor_.scope) np.testing.assert_equal(margFactor.card, margFactor_.card) np.testing.assert_almost_equal(margFactor.val, margFactor_.val, decimal=5)
def compute_factorlist_product(self, factors): """ Computes the factor product of the factors given in a list Parameters ---------- factors list list of factor objects Returns ------- factorProd pgmFactor product of all the factors in the provided list """ factorProd = pgmf.DiscreteFactor() for factor in set(factors): # the 'set' is very important! factorProd.compute_factor_product(factor) return factorProd
import numpy as np import pgmGraph as pgmg import pgmFactor as pgmf import pgmUtils as pgmu # Example 1: Markov Net toy example from Coursera #------------------------------------------------- A = 1 B = 2 C = 3 D = 4 factor1 = pgmf.DiscreteFactor(scope=np.array([1, 2]), card=np.array([2, 2]), val=np.array([30, 1, 5, 10])) factor2 = pgmf.DiscreteFactor(scope=np.array([2, 3]), card=np.array([2, 2]), val=np.array([100, 1, 1, 100])) factor3 = pgmf.DiscreteFactor(scope=np.array([3, 4]), card=np.array([2, 2]), val=np.array([1, 100, 100, 1])) factor4 = pgmf.DiscreteFactor(scope=np.array([1, 4]), card=np.array([2, 2]), val=np.array([100, 1, 1, 100])) factors = [factor1, factor2, factor3, factor4] markovNet = pgmg.PGM(factors) # Computing P(A,B) using variable elimination of individual factors markovMargDist_ve = markovNet.compute_min_neighbor_marginal(np.array([1, 2]))