Skip to content

Commit

Permalink
run enumeration as the rest of them
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed Nov 17, 2019
1 parent f9425fe commit 29a4c94
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 59 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ testcase = {
'samples': 10000,
}

bayesian_networks.Enumeration().run(testcase)
bayesian_networks.RejectionSampling().run(testcase)
bayesian_networks.LikelihoodWeighting().run(testcase)
enum = bayesian_networks.Enumeration()
results = enum.run(testcase)
bayesian_networks.print_result(results, showcolors=True)

rejection = bayesian_networks.RejectionSampling()
results = rejection.run(testcase)
bayesian_networks.print_result(results, showcolors=True)

weighting = bayesian_networks.LikelihoodWeighting()
results = weighting.run(testcase)
bayesian_networks.print_result(results, showcolors=True)
```
25 changes: 25 additions & 0 deletions bayesian_networks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,28 @@
from bayesian_networks.enumeration import Enumeration
from bayesian_networks.approximate_inference import LikelihoodWeighting
from bayesian_networks.approximate_inference import RejectionSampling

colors = {
'HEADER': '\033[95m',
'OKBLUE': '\033[94m',
'OKGREEN': '\033[92m',
'WARNING': '\033[93m',
'FAIL': '\033[91m',
'ENDC': '\033[0m',
'BOLD': '\033[1m',
'UNDERLINE': '\033[4m',
}


def print_result(run_result, showcolors=False):
unnormalized = {key: round(value, 5) for key, value in run_result['unnormalized'].items()}
normalized = {key: round(value, 5) for key, value in run_result['normalized'].items()}
print(' unnormalized: {}'.format(unnormalized))
print(' Normalized: {}'.format(normalized))
if run_result['too_much_wrong']:
print('{} ! percent Wrong: {}{}'.format(
colors['FAIL'],
run_result['percWrong'],
colors['ENDC'])
)
print(' time: {}'.format(run_result['timediff']))
29 changes: 15 additions & 14 deletions bayesian_networks/approximate_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ def run(self, testcase):

# Test with my data graph
timer_start = datetime.now()
inference = self.start_inference(node, query)
normalized = self.normalize(inference)
print(' Inference: {}'.format(inference))
print(' Normalized: {}'.format(normalized))
unnormalized = self.start_inference(node, query)
normalized = self.normalize(unnormalized)
timediff = datetime.now() - timer_start

percWrong = {}
too_much_wrong = False
for key in normalized.keys():
percWrong[key] = round(abs(normalized[key] - testcase['result'][key]), 3)
if percWrong[key] >= 0.1:
too_much_wrong = True
if too_much_wrong:
print('{} ! percent Wrong: {}{}'.format(
colors['FAIL'],
percWrong,
colors['ENDC'])
)
print(' time: {}'.format(datetime.now() - timer_start))
run_result = {
'unnormalized': unnormalized,
'normalized': normalized,
'timediff': timediff,
'too_much_wrong': too_much_wrong,
'percWrong': percWrong,
}
return run_result

def get_probability(self, node, conditions):
prob = self.graph[node]['prob']
Expand Down Expand Up @@ -86,7 +87,7 @@ def normalize(self, prob_results):
mysum = float(sum(prob_results.values()))
for cond, value in prob_results.items():
if mysum != 0:
normalized[cond] = round(value / mysum, 3)
normalized[cond] = value / mysum
else:
normalized[cond] = 0
return normalized
Expand Down Expand Up @@ -176,8 +177,8 @@ def start_inference(self, node, query):
event, weight = self.weighted_sample(query)
likelihood[event[node]['value']] += weight

likelihood[False] = round(likelihood[False], 3)
likelihood[True] = round(likelihood[True], 3)
likelihood[False] = likelihood[False]
likelihood[True] = likelihood[True]
return likelihood

def weighted_sample(self, query):
Expand Down
36 changes: 29 additions & 7 deletions bayesian_networks/enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

import copy
from bayesian_networks.bayesian import ParseInputs
from datetime import datetime

colors = {
'HEADER': '\033[95m',
'OKBLUE': '\033[94m',
'OKGREEN': '\033[92m',
'WARNING': '\033[93m',
'FAIL': '\033[91m',
'ENDC': '\033[0m',
'BOLD': '\033[1m',
'UNDERLINE': '\033[4m',
}


class BayesianClass():
Expand Down Expand Up @@ -44,18 +56,28 @@ def run(self, testcase):
parser = ParseInputs()
known_data = parser.get_test_data(testcase['netid'])
self.graph = parser.get_graph(known_data)

node, query = parser.get_query('|'.join(testcase['query']))
for key, value in self.graph.items():
print(key, value)
print('---------------')

# enum = Enumeration(graph)
timer_start = datetime.now()
enum_results = self.enum_ask(node, query)
parser.print_results(node, query, enum_results)
timediff = datetime.now() - timer_start

normalized = {}
unnormalized = {}
for enum_result in enum_results:
unnormalized[enum_result['condition']] = enum_result['prob']
normalized[enum_result['condition']] = enum_result['norm']
run_result = {
'unnormalized': unnormalized,
'normalized': normalized,
'timediff': timediff,
'too_much_wrong': False,
'percWrong': False,
}
return run_result

def enum_ask(self, node, query):
sorted_nodes = self.graph.keys()
sorted_nodes = list(self.graph.keys())
prob_results = []
for cond in [False, True]:
query_copy = copy.deepcopy(query)
Expand Down
File renamed without changes.
58 changes: 25 additions & 33 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@

import bayesian_networks

colors = {
'HEADER': '\033[95m',
'OKBLUE': '\033[94m',
'OKGREEN': '\033[92m',
'WARNING': '\033[93m',
'FAIL': '\033[91m',
'ENDC': '\033[0m',
'BOLD': '\033[1m',
'UNDERLINE': '\033[4m',
}


if __name__ == "__main__":
testcases = [
{
'netid': "burglary",
'query': ('B', 'j,m'),
'result': {True: 0.28, False: 0.72},
'samples': 10000,
},
# {
# 'netid': "burglary",
# 'query': ('B', 'j,m'),
# 'result': {True: 0.28, False: 0.72},
# 'samples': 10000,
# },
# {
# 'netid': "sprinkler",
# 'query': ('W', 's,-r'),
Expand All @@ -46,33 +35,36 @@
# 'result': {True: 0.7, False: 0.3},
# 'samples': 10000,
# },
# {
# 'netid': "cancer",
# 'query': ('S', '+co,+f'),
# 'result': {True: 0.8, False: 0.2},
# 'samples': 10000,
# },
{
'netid': "cancer",
'query': ('S', '+co,+f'),
'result': {True: 0.8, False: 0.2},
'samples': 10000,
},
]
showcolors = False

if not showcolors:
for color in colors.keys():
colors[color] = ''
for color in bayesian_networks.colors.keys():
bayesian_networks.colors[color] = ''

for testcase in testcases:
myquery = 'P({}|{})'.format(testcase['query'][0], testcase['query'][1])
print('{} {} - {} {}'.format(
colors['WARNING'],
bayesian_networks.colors['WARNING'],
testcase['netid'],
myquery,
colors['ENDC'])
bayesian_networks.colors['ENDC'])
)

# print(colors['HEADER'] + '--> Enumeration' + colors['ENDC'])
# bayesian_networks.Enumeration().run(testcase)
print(bayesian_networks.colors['HEADER'] + '--> Enumeration' + bayesian_networks.colors['ENDC'])
enum = bayesian_networks.Enumeration()
bayesian_networks.print_result(enum.run(testcase), showcolors=showcolors)

print(colors['HEADER'] + '--> RejectionSampling' + colors['ENDC'])
bayesian_networks.RejectionSampling(showcolors=showcolors).run(testcase)
print(bayesian_networks.colors['HEADER'] + '--> RejectionSampling' + bayesian_networks.colors['ENDC'])
rejection = bayesian_networks.RejectionSampling()
bayesian_networks.print_result(rejection.run(testcase), showcolors=showcolors)

print(colors['HEADER'] + '--> LikelihoodWeighting' + colors['ENDC'])
bayesian_networks.LikelihoodWeighting(showcolors=showcolors).run(testcase)
print(bayesian_networks.colors['HEADER'] + '--> LikelihoodWeighting' + bayesian_networks.colors['ENDC'])
weighting = bayesian_networks.LikelihoodWeighting()
bayesian_networks.print_result(weighting.run(testcase), showcolors=showcolors)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='bayesian_networks',
version='0.6',
version='0.8',
author="bkbilly",
author_email="[email protected]",
description="Implementation for bayesian network with Enumeration, Rejection Sampling and Likelihood Weighting",
Expand All @@ -15,7 +15,7 @@
packages=setuptools.find_packages(),
install_requires=[
'pytz>=2019.2',
'matplotlib==3.1.1',
'matplotlib>=3.0.3',
'networkx>=2.4',
],
classifiers=[
Expand Down

0 comments on commit 29a4c94

Please sign in to comment.