Example #1
1
from gPy.LearningUtils import *
from gPy.Examples import asia
from gPy.K2 import *
from gPy.Models import CBN

# generate 500 samples of the Asia network
data = CausalWorld(asia)
data.observe(5000)

# for comparison
true_model = asia
true_adg = asia.adg()

# search for the ADG
found_adg = K2(max_parents_family=3).search(data, true_adg.topological_order())
# fit a model to the data using the ADG
found_model = CBN.from_adg_data(found_adg, data)

print 'True ADG\n', true_adg
print 'Found ADG\n', found_adg
print 'Structural Hamming distance:',shd(found_adg,true_adg)
print 'BDeu scores of fitted models of'
print 'found ADG:', bdeu(found_adg,data)
print ' true ADG:', bdeu(true_adg,data)
print 'KL-divergence of fitted found model from true model:',dkl(true_model,found_model)

Example #2
1
from gPy.FKMCMC import fk_exp_graph
from gPy.Models import CBN

# generate 500 samples of the Asia network
data = CausalWorld(asia)
data.observe(500)

# for comparison
true_model = asia
true_adg = asia.adg()

# produce a sample of ADGs using K2 to search
search = K2(max_parents_family=3)
# generate 5*2 = 10 ADGs from every 7th step of the MCMC using 2
# `independent' order chains
found_adgs = fk_exp_graph(search, data,
                            num_samples = 5,
                            sample_every = 7,
                            num_orderings = 2)

# fit models to the data using the ADGs
found_models = [CBN.from_adg_data(found_adg, data) for found_adg in found_adgs]

print 'Structural Hamming distance:',[shd(found_adg,true_adg) for found_adg in found_adgs]
print 'BDeu scores of fitted models of'
print 'found ADG:', [bdeu(found_adg,data) for found_adg in found_adgs]
print ' true ADG:', bdeu(true_adg,data)
print 'KL-divergence of fitted found model from true model:',
print [dkl(true_model,found_model) for found_model in found_models]

Example #3
0
## independencies into a PDAG. Note that this may fail if the found conditional
## independencies are inconsistent with an ADG!

pdag = ICPattern(ci)

## Meek's algorithm is used to resolve this PDAG into a particular ADG

found_adg = pdag.orient()

## or for short:
## found_adg = ICPattern(PCCI(G2Separator(data))).orient()
##
## Note that G2Separator can be replaced with X2Separator or GraphSeparator
## and PCCI can be replaced with GraphCI.  The PDAG is the essential graph
## representing the Markov equivalence class of the conditional independencies.
## (This perhaps goes some way to explaining the complexity --- it depends what
## you are interested in.)
##
## Note also that you may want to scale the p-value with the sample size and
## that there is a BFSeparator but this is likely mathematically unsound.

# fit a model to the data using the ADG
found_model = CBN.from_adg_data(found_adg, data)

print 'Structural Hamming distance:', shd(found_adg, true_adg)
print 'BDeu scores of fitted models of'
print 'found ADG:', bdeu(found_adg, data)
print ' true ADG:', bdeu(true_adg, data)
print 'KL-divergence of fitted found model from true model:', dkl(
    true_model, found_model)