Beispiel #1
0
 def tryModel(self, model):
     world = CausalWorld(model.copy())
     world.observe(1000)
     # and again to check updates...
     world.observe(1000)
     adg = model.adg()
     data = world.intervention_data(frozenset())
     for child in adg.topological_order():
         family_bdeu = data.family_score(child, adg.parents(child))
         self.assertAlmostEquals(family_bdeu, model[child].get_counts(data).bdeu_score())
Beispiel #2
0
        print >> f, var,
    print >> f, 'count'
    for inst in fact.insts():
        for i in inst:
            print >> f, i,
        print >> f, fact[inst]
    f.close()


bn0 = BN(domain=Domain(), new_domain_variables={'a': [0, 1], 'b': [0, 1]})
bn0.add_cpts([
    CPT(Factor(variables=['a'], data=[0.5, 0.5]), child='a'),
    CPT(Factor(variables=['a', 'b'], data=[0.3, 0.7, 0.4, 0.6]), child='b')
])
w = CausalWorld(bn0)
samples = w.observe(10000)
disp('two_depend', samples)

bn1 = BN(domain=Domain(), new_domain_variables={'a': [0, 1], 'b': [0, 1]})
bn1.add_cpts([
    CPT(Factor(variables=['a'], data=[0.5, 0.5]), child='a'),
    CPT(Factor(variables=['b'], data=[0.3, 0.7]), child='b')
])
w = CausalWorld(bn1)
samples = w.observe(10000)
disp('two_independ', samples)

bn2 = BN(domain=Domain(),
         new_domain_variables={
             'a': [0, 1, 2],
             'b': [0, 1, 2]
Beispiel #3
0
out=sys.stdout
sys.stdout=sys.stderr
print 'run correlation sampler: num runs =',num_runs,', instances =',num_observations
print 'run correlation sampler: burnin =',burnin,', sample every =',sample_every,
print                'num_samples =',num_samples
sys.stdout=out

# observe (gibbs sample) the model and generate the sampler
# over orders of those observations
w = CausalWorld(model)
out=sys.stdout
sys.stdout=sys.stderr
print 'observing...',
sys.stdout.flush()
w.observe(num_observations)
print 'done.'
print 'scoring...',
sys.stdout.flush()
optimal_family = FamilyScoreCache(w,model.variables(), max_potential_parents = None, best_family_scale = None)
optimal_family.from_adg(model.adg())
optimal_score = Order(optimal_family, model.topological_order()).score()
print 'optimal:',optimal_score
sys.stdout=out

# settings from FK (Mixing rate test on alarm)
ensemble = OrderEnsemble(w,model.variables(), num_runs,
        burnin = burnin,
        max_potential_parents=20, max_parents_family=3,
        max_best_families=4000, best_family_scale=log(10))
Beispiel #4
0
out = sys.stdout
sys.stdout = sys.stderr
print 'run correlation sampler: num runs =', num_runs, ', instances =', num_observations
print 'run correlation sampler: burnin =', burnin, ', sample every =', sample_every,
print 'num_samples =', num_samples
sys.stdout = out

# observe (gibbs sample) the model and generate the sampler
# over orders of those observations
w = CausalWorld(model)
out = sys.stdout
sys.stdout = sys.stderr
print 'observing...',
sys.stdout.flush()
w.observe(num_observations)
print 'done.'
print 'scoring...',
sys.stdout.flush()
optimal_family = FamilyScoreCache(w,
                                  model.variables(),
                                  max_potential_parents=None,
                                  best_family_scale=None)
optimal_family.from_adg(model.adg())
optimal_score = Order(optimal_family, model.topological_order()).score()
print 'optimal:', optimal_score
sys.stdout = out

# settings from FK (Mixing rate test on alarm)
ensemble = OrderEnsemble(w,
                         model.variables(),
Beispiel #5
0
    fact = samples.makeFactor(samples.variables())
    for var in fact.variables():
        print >>f, var,
    print >>f, 'count'
    for inst in fact.insts():
        for i in inst:
            print >>f, i,
        print >>f, fact[inst]
    f.close()

bn0 = BN(domain=Domain(), new_domain_variables={'a': [0,1], 'b':[0,1]})
bn0.add_cpts([CPT(Factor(variables=['a'], data=[0.5, 0.5]),child='a')
             ,CPT(Factor(variables=['a','b'], data=[0.3, 0.7, 0.4, 0.6]),child='b')
             ])
w = CausalWorld(bn0)
samples = w.observe(10000)
disp('two_depend', samples)

bn1 = BN(domain=Domain(), new_domain_variables={'a': [0,1], 'b':[0,1]})
bn1.add_cpts([CPT(Factor(variables=['a'], data=[0.5, 0.5]),child='a')
             ,CPT(Factor(variables=['b'], data=[0.3, 0.7]),child='b')
             ])
w = CausalWorld(bn1)
samples = w.observe(10000)
disp('two_independ', samples)

bn2 = BN(domain=Domain(), new_domain_variables={'a': [0,1,2], 'b':[0,1,2]})
bn2.add_cpts([CPT(Factor(variables=['a'], data=[1.0/3.0, 1.0/3.0, 1.0/3.0]),child='a')
             ,CPT(Factor(variables=['a','b'], data=[0.2, 0.6, 0.2, 0.5, 0.4, 0.1, 0.0, 1.0, 0.0]),child='b')
             ])
w = CausalWorld(bn2)