def test_bde2(self): myskl = Data(read_csv(open('myksl.dat'))) # > ksbln.prior <- jointprior(kslbn) # Imaginary sample size: 64 # > kslbn.fit <- getnetwork(learn(kslbn,myksl,ksbln.prior)) # deal scores # > score(nodes(kslbn.fit)$Smok) # [1] -637.9544 # > score(nodes(kslbn.fit)$Alc) # [1] -752.027 # > score(nodes(kslbn.fit)$Work) # [1] -463.1716 # > score(nodes(kslbn.fit)$Sex) # [1] -751.0772 # > score(nodes(kslbn.fit)$Year) # [1] -666.6585 # > score(kslbn.fit) # [1] -3270.889 node_scores = (-637.9544,-752.027,-463.1716,-751.0772,-666.6585) net_score = -3270.889 score = 0 for i, cpt in enumerate(self.cpts): cpt = cpt.get_counts_sql(myskl) this_score = cpt.bdeu_score(64) self.assertAlmostEqual(this_score,node_scores[i],places) score += this_score self.assertAlmostEqual(score,net_score,places)
def test_bde3(self): # just tests that myskl has the right counts myskl = Data(read_csv(open('myksl.dat'))) # > ksbln.prior <- jointprior(kslbn) # Imaginary sample size: 64 # > kslbn.fit <- getnetwork(learn(kslbn,myksl,ksbln.prior)) # deal scores # > score(nodes(kslbn.fit)$Smok) # [1] -637.9544 # > score(nodes(kslbn.fit)$Alc) # [1] -752.027 # > score(nodes(kslbn.fit)$Work) # [1] -463.1716 # > score(nodes(kslbn.fit)$Sex) # [1] -751.0772 # > score(nodes(kslbn.fit)$Year) # [1] -666.6585 # > score(kslbn.fit) # [1] -3270.889 node_scores = (-637.9544,-752.027,-463.1716,-751.0772,-666.6585) net_score = -3270.889 score = 0 for i, cpt in enumerate(self.cpts): child = cpt.child() parents = list(cpt.variables() - set([child])) this_score = myskl.family_score(child,parents,64.0) self.assertAlmostEqual(this_score,node_scores[i],places) score += this_score self.assertAlmostEqual(score,net_score,places)
def test_bigdata_sql(self): alarm = Data(self.rawdata) for varset in (['CO', 'MINVOLSET'], ['CO'], ['HRBP', 'EXPCO2', 'PRESS', 'VENTTUBE'], [ 'DISCONNECT', 'MINVOLSET', 'VENTMACH', 'VENTTUBE', 'VENTLUNG', 'VENTALV', 'ARTCO2', 'PRESS', 'SHUNT' ]): factor = alarm[varset] self.assertEqual(factor.z(), 1000)
def test_smalldata_sql(self): rawdata = read_csv(open('fake_data')) dat = Data(rawdata) self.assertEqual(dat.makeFactor([]).z(), 5) self.samefactor(dat.makeFactor(['bar']), Factor(['bar'], [2, 2, 1])) self.samefactor(dat.makeFactor(['blah']), Factor(['blah'], [3, 1, 1])) self.samefactor(dat.makeFactor(['foo']), Factor(['foo'], [3, 1, 1, 0])) self.samefactor( dat.makeFactor(['bar', 'foo']), Factor(['bar', 'foo'], [2, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0]))