89 lines
2.2 KiB
Python
89 lines
2.2 KiB
Python
#Project 1 for the University of Tulsa's CS-7313 Adv. AI Course
|
|
#Approximate Inference Methods for Bayesian Networks
|
|
#Professor: Dr. Sen, Fall 2021
|
|
#Noah Schrick - 1492657
|
|
|
|
import json
|
|
import random
|
|
|
|
def main():
|
|
bayes_net = import_bayes()
|
|
print(len(bayes_net))
|
|
print(bayes_net["0"]["prob"][0][1])
|
|
#print(is_root("3", bayes_net))
|
|
|
|
#Import the BN from the json
|
|
def import_bayes():
|
|
with open ("gen-bn/bn.json") as json_file:
|
|
bayes_json = json.load(json_file)
|
|
json_file.close()
|
|
return bayes_json
|
|
|
|
|
|
|
|
#Checks if node has parents
|
|
def is_root(node, BN):
|
|
return (BN[node]["parents"]) == []
|
|
|
|
#Return a list of the root nodes
|
|
def get_root(node, BN):
|
|
roots = []
|
|
for i in range(len(BN)):
|
|
if ((BN[node]["parents"]) == []):
|
|
roots.append(node)
|
|
return roots
|
|
|
|
|
|
#print(bayes_json["x"]): prints the information about node x (an int)
|
|
#print(bayes_json["x"]["parents"] prints the information about node x's parents
|
|
#class BayesianNetwork:
|
|
|
|
|
|
#Compute the estimate of P(X|e), where X is the query variable, and e is the observed value for variables E
|
|
def likelihood_weighting(X, e, bayes_net, samples):
|
|
#Vector of weighted counts for each value of X, initialized to zero
|
|
W = {}
|
|
#Init True and False probabilities
|
|
T = 0
|
|
F = 0
|
|
|
|
for i in range(samples):
|
|
x,w = weighted_sample(bayes_net, e)
|
|
print("Hello")
|
|
|
|
#Returns an event and a weight
|
|
def weighted_sample(bayes_net, e):
|
|
w = 1
|
|
sample = {}
|
|
#Elements in event x
|
|
for node in bayes_net:
|
|
#if bayes_net[e]["prob"][node] != None:
|
|
if e[node] !=None:
|
|
w*= bayes_net[node]["prob"][sample][1]
|
|
sample[node] = e[node]
|
|
else:
|
|
#Random sample
|
|
random_sample = random()
|
|
sample[node] = #True or False
|
|
|
|
#v
|
|
|
|
#for i in range(len(n)):
|
|
|
|
#Given a node in the bayes net and a random sample, determine if it should be
|
|
#classified as True or False
|
|
def get_bool(node, rand_sample, sample_table, BN):
|
|
table = {}
|
|
if is_root(node, BN):
|
|
table
|
|
|
|
# for
|
|
|
|
def gibbs_sampling():
|
|
print("Hello")
|
|
|
|
def metropolis_hastings():
|
|
print("Hello")
|
|
|
|
if __name__ == '__main__':
|
|
main() |