コード例 #1
0
def generate_MAP_Ind_Simplification(graph, exp_var, explanadum, threshold):
    """A method that outputs MAP(Simplified MPE) using Independence-based simplification rule."""

    MPE = generate_MPE(graph, exp_var, explanadum)

    best_simplified_exp = []
    key_set = {}

    for (explanation, prob) in MPE:
        ori_metric = graph.prob_given(explanadum, explanation)
        lower_bound = ori_metric * (1 - threshold)
        upper_bound = ori_metric * (1 + threshold)

        simplified_space = []
        keys = explanation.keys()
        # Loop over all possible abductions of an explanation to find all simplifications
        for i in range(len(explanation)):
            for abduction in combination(i, keys):
                # retrieve assignments from the explanation
                abducted_assignment = {}
                for var in abduction:
                    abducted_assignment[var] = explanation[var]
                # test equivalence
                if graph.prob_given(explanadum, abducted_assignment) >= lower_bound \
                 and graph.prob_given(explanadum, abducted_assignment) <= upper_bound:
                    simplified_space.append(abducted_assignment)

        # find out the best simplification and its posterior probability
        if not len(simplified_space) == 0:
            cur_min = len(simplified_space[0])
            candidates = []
            for simplification in simplified_space:
                if len(simplification) == cur_min:
                    candidates.append(simplification)
                elif len(simplification) < cur_min:
                    candidates = [simplification]
                    cur_min = len(simplification)

            min_imprecision = graph.prob_given(explanadum,
                                               candidates[0]) - ori_metric
            cur_best = candidates[0]
            for candidate in candidates:
                if graph.prob_given(explanadum,
                                    candidate) - ori_metric < min_imprecision:
                    min_imprecision = graph.prob_given(explanadum,
                                                       candidate) - ori_metric
                    cur_best = candidate

            if cur_best.__str__() not in key_set:
                best_simplified_exp.append(
                    (cur_best, graph.prob_given(cur_best, explanadum)))
                key_set[cur_best.__str__()] = 0

    return sorted(best_simplified_exp, key=lambda x: -x[1])
コード例 #2
0
def generate_MAP_Ind_Simplification(graph, exp_var, explanadum, threshold):
    """A method that outputs MAP(Simplified MPE) using Independence-based simplification rule."""

    MPE = generate_MPE(graph, exp_var, explanadum)

    best_simplified_exp = []
    key_set = {}

    for (explanation, prob) in MPE:
        ori_metric = graph.prob_given(explanadum, explanation)
        lower_bound = ori_metric * (1 - threshold)
        upper_bound = ori_metric * (1 + threshold)

        simplified_space = []
        keys = explanation.keys()
        # Loop over all possible abductions of an explanation to find all simplifications
        for i in range(len(explanation)):
            for abduction in combination(i, keys):
                # retrieve assignments from the explanation
                abducted_assignment = {}
                for var in abduction:
                    abducted_assignment[var] = explanation[var]
                    # test equivalence
                if (
                    graph.prob_given(explanadum, abducted_assignment) >= lower_bound
                    and graph.prob_given(explanadum, abducted_assignment) <= upper_bound
                ):
                    simplified_space.append(abducted_assignment)

                    # find out the best simplification and its posterior probability
        if not len(simplified_space) == 0:
            cur_min = len(simplified_space[0])
            candidates = []
            for simplification in simplified_space:
                if len(simplification) == cur_min:
                    candidates.append(simplification)
                elif len(simplification) < cur_min:
                    candidates = [simplification]
                    cur_min = len(simplification)

            min_imprecision = graph.prob_given(explanadum, candidates[0]) - ori_metric
            cur_best = candidates[0]
            for candidate in candidates:
                if graph.prob_given(explanadum, candidate) - ori_metric < min_imprecision:
                    min_imprecision = graph.prob_given(explanadum, candidate) - ori_metric
                    cur_best = candidate

            if cur_best.__str__() not in key_set:
                best_simplified_exp.append((cur_best, graph.prob_given(cur_best, explanadum)))
                key_set[cur_best.__str__()] = 0

    return sorted(best_simplified_exp, key=lambda x: -x[1])
コード例 #3
0
test_tree = generate_causal_explanation_tree(lake_graph, lake_graph, ['Bird', 'Island'], {}, {'Pox':'T'}, [], 0.0001) 
print test_tree
print "========================="


print "Testing Explanation Forest:"
forest = generate_CET_forest(lake_graph, lake_graph, ['Bird', 'Island'], {}, {'Pox':'T'}, []) 
for tree in forest:
    print tree
print "========================="


print "Testing scores calculations of different methods:"
print "BGF of [Island being true]: ", calculate_GBF( lake_graph, {"Island" : "T" }, {"Pox" : "T"} )
print "BGF of [Bird being true, Island being false]: ", calculate_GBF( lake_graph, {"Island" : "T", "Bird" : "T" }, {"Pox" : "T"} )
print "ET score of [Island being true], which is essentially posterior probability of the explanation given explanadum : ", calculate_ET_score( lake_graph, {"Island" : "T"}, {"Pox" : "T"})
print "CET score of [Island being true]", calculate_CET_score( lake_graph, {"Island" : "T"}, {}, {"Pox" : "T"}) #The empty hash is for Observation

print "========================="
print "Testing MPE:"
MRE = generate_MPE(lake_graph, ['Bird', 'Island'], {'Pox':'T'})
for x in MRE:
    print x
print "========================="

print "Testing MAP:"
MRE = generate_MAP_Ind_Simplification(lake_graph, ['Bird', 'Island'], {'Pox':'T'}, 0.1)
for x in MRE:
    print x
print "========================="
コード例 #4
0
print "========================="

print "Testing scores calculations of different methods:"
print "BGF of [Island being true]: ", calculate_GBF(lake_graph,
                                                    {"Island": "T"},
                                                    {"Pox": "T"})
print "BGF of [Bird being true, Island being false]: ", calculate_GBF(
    lake_graph, {
        "Island": "T",
        "Bird": "T"
    }, {"Pox": "T"})
print "ET score of [Island being true], which is essentially posterior probability of the explanation given explanadum : ", calculate_ET_score(
    lake_graph, {"Island": "T"}, {"Pox": "T"})
print "CET score of [Island being true]", calculate_CET_score(
    lake_graph, {"Island": "T"}, {},
    {"Pox": "T"})  #The empty hash is for Observation

print "========================="
print "Testing MPE:"
MRE = generate_MPE(lake_graph, ['Bird', 'Island'], {'Pox': 'T'})
for x in MRE:
    print x
print "========================="

print "Testing MAP:"
MRE = generate_MAP_Ind_Simplification(lake_graph, ['Bird', 'Island'],
                                      {'Pox': 'T'}, 0.1)
for x in MRE:
    print x
print "========================="
コード例 #5
0
def fill_in_csv(graph, exp_var, explanadum, path = "temp.csv", lake = 0):
	# Assume for now that the codes in csv use the some scheme as implied
    # by this script
    assignments = []
    code_hash = encode_nodes_to_hash([graph.variables[name] for name in exp_var])
    MPE_space = generate_MPE(graph, exp_var, explanadum)
    MAP_threshold = 0.1
    MAP_space = generate_MAP_Ind_Simplification(graph, exp_var, explanadum, MAP_threshold)
    MRE_space = generate_MRE(graph, exp_var, explanadum)

    alpha = 0.01
    beta = 0.2
    ET = generate_explanation_tree(graph, exp_var, explanadum, [], alpha, beta)
    ET_space = sorted(ET.assignment_space(), key = lambda x: -x[1])
    ET_score_space = sorted(
    		[(code_hash[key], calculate_ET_score(graph, code_hash[key], explanadum)) for key in code_hash.keys() if len(code_hash[key])],
    		key = lambda x: -x[1])
    # print ET_score_space

    alpha_CET = 0.0001
    CET = generate_causal_explanation_tree(graph, graph, exp_var, {}, explanadum, [], alpha_CET)
    # print "Printing cet", CET
    CET_space = sorted(CET.assignment_space(), key = lambda x: -x[1])
    CET_score_space = sorted(
    		[(code_hash[key], calculate_CET_score(graph, code_hash[key], {}, explanadum)) for key in code_hash.keys() if len(code_hash[key])],
    		key = lambda x: -x[1] if not math.isnan(x[1]) else 9999999999999)
    # print CET_score_space

    with open(path, 'r') as csvfile:
    	reader = csv.reader(csvfile, dialect = 'excel')
    	for row in reader:
    		processed_code = join([row[0][i] for i in range(len(exp_var))], "")
    		assignments.append((row[0], code_hash[processed_code]))

    # print assignments
    with open(path, 'w') as csvfile:
   		writer = csv.writer(csvfile, dialect = 'excel') 
   		writer.writerow(["CondensedString","MPE_rank","MPE_score","MAP_I_rank","MAP_I_score","MAP_I_para_theta","MRE_rank","MRE_score","ET_rank_for_tree","ET_rank_for_score","ET_score","ET_leaf","ET_ALPHA","ET_BETA","CET_rank_for_tree","CET_rank_for_score","CET_score","CET_leaf","CET_ALPHA"])
   		for code, assignment in assignments:
   			row = ['"' + code + '"']
   			# escape all empty explanations
   			# and all invalid assignemnts for lake.py
   			print lake, code[2] != '9', code[3] != '9'
   			if (not len(assignment)) or (lake and (code[2] != '9' or code[3] != '9')):
   				print "escaping"
   				row += ["NaN" for _ in range(18)]
   				writer.writerow(row)
   				continue

   			rank = space_rank(MPE_space, assignment)
   			if rank:
	   			row.append(rank)# MPE rank
	   			row.append(MPE_space[int(rank) - 1][1]) # MPE score
	   		else:
	   			row.append("NaN")
	   			row.append("NaN")

	   		rank = space_rank(MAP_space, assignment)
	   		if rank:
	   			row.append(rank)# map rank
	   			row.append(MAP_space[int(rank) - 1][1]) # MAP score
	   		else:
	   			row.append("NaN")
	   			row.append("NaN")

	   		row.append(MAP_threshold)

	   		rank = space_rank(MRE_space, assignment)
	   		if rank:
	   			row.append(rank)# mre rank
	   			row.append(MRE_space[int(rank) - 1][1]) # MRE score
	   		else:
	   			row.append("NaN")
	   			row.append("NaN")

	   		rank = space_rank(ET_space, assignment)
	   		if rank:
	   			row.append(rank)# et tree rank
	   		else:
	   			row.append("NaN")

	   		rank = space_rank(ET_score_space, assignment)
	   		# print code, assignment, rank, ET_score_space[int(rank) - 1][1]
	   		if rank:
	   			row.append(rank) # et score rank
	   			row.append(ET_score_space[int(rank) - 1][1]) # ET score
	   		else:
	   			row.append("ERROR ! should not happen") #should not happen

	   		row += [1] if ET.is_leaf(assignment) else [0]
	   		row.append(alpha)
	   		row.append(beta)

	   		rank = space_rank(CET_space, assignment)
	   		if rank:
	   			row.append(rank)# et tree rank
	   		else:
	   			row.append("NaN")

	   		rank = space_rank(CET_score_space, assignment)
	   		if rank:
	   			row.append(rank) # et score rank
	   			row.append(CET_score_space[int(rank) - 1][1]) # ET score
	   		else:
	   			row.append("ERROR ! should not happen") #should not happen

	   		# print "determining leaf for", code
	   		row += [1] if CET.is_leaf(assignment) else [0]
	   		row.append(alpha_CET)

   			writer.writerow(row)
コード例 #6
0
def fill_in_csv(graph, exp_var, explanadum, path="temp.csv", lake=0):
    # Assume for now that the codes in csv use the some scheme as implied
    # by this script
    assignments = []
    code_hash = encode_nodes_to_hash(
        [graph.variables[name] for name in exp_var])
    MPE_space = generate_MPE(graph, exp_var, explanadum)
    MAP_threshold = 0.1
    MAP_space = generate_MAP_Ind_Simplification(graph, exp_var, explanadum,
                                                MAP_threshold)
    MRE_space = generate_MRE(graph, exp_var, explanadum)

    alpha = 0.01
    beta = 0.2
    ET = generate_explanation_tree(graph, exp_var, explanadum, [], alpha, beta)
    ET_space = sorted(ET.assignment_space(), key=lambda x: -x[1])
    ET_score_space = sorted([
        (code_hash[key], calculate_ET_score(graph, code_hash[key], explanadum))
        for key in code_hash.keys() if len(code_hash[key])
    ],
                            key=lambda x: -x[1])
    # print ET_score_space

    alpha_CET = 0.0001
    CET = generate_causal_explanation_tree(graph, graph, exp_var, {},
                                           explanadum, [], alpha_CET)
    # print "Printing cet", CET
    CET_space = sorted(CET.assignment_space(), key=lambda x: -x[1])
    CET_score_space = sorted(
        [(code_hash[key],
          calculate_CET_score(graph, code_hash[key], {}, explanadum))
         for key in code_hash.keys() if len(code_hash[key])],
        key=lambda x: -x[1] if not math.isnan(x[1]) else 9999999999999)
    # print CET_score_space

    with open(path, 'r') as csvfile:
        reader = csv.reader(csvfile, dialect='excel')
        for row in reader:
            processed_code = join([row[0][i] for i in range(len(exp_var))], "")
            assignments.append((row[0], code_hash[processed_code]))

    # print assignments
    with open(path, 'w') as csvfile:
        writer = csv.writer(csvfile, dialect='excel')
        writer.writerow([
            "CondensedString", "MPE_rank", "MPE_score", "MAP_I_rank",
            "MAP_I_score", "MAP_I_para_theta", "MRE_rank", "MRE_score",
            "ET_rank_for_tree", "ET_rank_for_score", "ET_score", "ET_leaf",
            "ET_ALPHA", "ET_BETA", "CET_rank_for_tree", "CET_rank_for_score",
            "CET_score", "CET_leaf", "CET_ALPHA"
        ])
        for code, assignment in assignments:
            row = ['"' + code + '"']
            # escape all empty explanations
            # and all invalid assignemnts for lake.py
            print lake, code[2] != '9', code[3] != '9'
            if (not len(assignment)) or (lake and
                                         (code[2] != '9' or code[3] != '9')):
                print "escaping"
                row += ["NaN" for _ in range(18)]
                writer.writerow(row)
                continue

            rank = space_rank(MPE_space, assignment)
            if rank:
                row.append(rank)  # MPE rank
                row.append(MPE_space[int(rank) - 1][1])  # MPE score
            else:
                row.append("NaN")
                row.append("NaN")

            rank = space_rank(MAP_space, assignment)
            if rank:
                row.append(rank)  # map rank
                row.append(MAP_space[int(rank) - 1][1])  # MAP score
            else:
                row.append("NaN")
                row.append("NaN")

            row.append(MAP_threshold)

            rank = space_rank(MRE_space, assignment)
            if rank:
                row.append(rank)  # mre rank
                row.append(MRE_space[int(rank) - 1][1])  # MRE score
            else:
                row.append("NaN")
                row.append("NaN")

            rank = space_rank(ET_space, assignment)
            if rank:
                row.append(rank)  # et tree rank
            else:
                row.append("NaN")

            rank = space_rank(ET_score_space, assignment)
            # print code, assignment, rank, ET_score_space[int(rank) - 1][1]
            if rank:
                row.append(rank)  # et score rank
                row.append(ET_score_space[int(rank) - 1][1])  # ET score
            else:
                row.append("ERROR ! should not happen")  #should not happen

            row += [1] if ET.is_leaf(assignment) else [0]
            row.append(alpha)
            row.append(beta)

            rank = space_rank(CET_space, assignment)
            if rank:
                row.append(rank)  # et tree rank
            else:
                row.append("NaN")

            rank = space_rank(CET_score_space, assignment)
            if rank:
                row.append(rank)  # et score rank
                row.append(CET_score_space[int(rank) - 1][1])  # ET score
            else:
                row.append("ERROR ! should not happen")  #should not happen

# print "determining leaf for", code
            row += [1] if CET.is_leaf(assignment) else [0]
            row.append(alpha_CET)

            writer.writerow(row)