def __init__(self): self.samples = np.array( [[0.82, 0.56, 0.08, 0.81],[0.34, 0.22, 0.37, 0.99], [0.55, 0.61, 0.31, 0.66], [0.28, 1.00, 0.95, 0.71], [0.14, 0.10, 1.00, 0.71], [0.10, 0.60, 0.64, 0.73], [0.39, 0.03, 0.99, 1.00], [0.97, 0.54, 0.80, 0.97], [0.07, 0.69, 0.43, 0.29], [0.61, 0.03, 0.13, 0.14], [0.13, 0.40, 0.94, 0.19], [0.60, 0.68, 0.36, 0.67], [0.12, 0.38, 0.42, 0.81], [0.00, 0.20, 0.85, 0.01], [0.55, 0.30, 0.30, 0.11], [0.83, 0.96, 0.41, 0.65], [0.29, 0.40, 0.54, 0.23], [0.74, 0.65, 0.38, 0.41], [0.82, 0.08, 0.39, 0.97], [0.95, 0.01, 0.62, 0.32], [0.56, 0.68, 0.32, 0.27], [0.77, 0.74, 0.79, 0.11], [0.29, 0.69, 0.99, 0.79], [0.21, 0.20, 0.43, 0.81], [0.90, 0.00, 0.91, 0.01]]); # check self.samples is stored correctly for i in self.samples: printV(["given self.samples: ", i],0); # Im not sure if this is how im supposed to use the given self.samples # There are 4 nodes and 100 self.samples so there will be 25 complete iterations self.cpt = {"cloudy":[0.5],"sprinkler":[0.1,0.5],"rain":[0.8,0.2],"wet":[0.99,0.90,0.90,0.00]} self.dict_array = []; # set the dictionary for each complete iteration and append # to dict_array for sample_temp in self.samples: self.set_sample_dict(sample_temp) self.dict_array = np.array(self.dict_array); for i in range(0,25): printV([i,": ",self.dict_array[i]],1);
def check_mark(self, check, mark): printV(["check", check], 1) printV(["mark", mark], 1) if check <= mark: return True else: return False
def setPrior(self,key,value): if (key == 'P')is True: self.marginal['p'] = value; self.prior['p'] = value; return ['p',self.marginal['p']]; elif(key == 'S')is True: self.marginal['s'] = value; self.prior['s'] = value; return ['s',value]; else: printV("ERROR: cant set prior to given key");
def prior_marginal_prob(self,key): # calculate the number of entries where key is true # divide by numnber of complete iterations (25) true_count = 0; false_count = 0; for i in range(0,25): if (self.dict_array[i][key])is True: true_count = true_count + 1; else: false_count = false_count + 1; printV(true_count,1); printV(false_count,1); return np.array([float(true_count)/25,float(false_count)/25]);
def main(): ps = prior_sampling(); # ready to start computing prob printV("Problem 1: Prior sampling",2); cloudy_marg = ps.prior_marginal_prob("cloudy"); printV(["a. P(c = True): ",cloudy_marg[0]],2); cond_returned = ps.prior_conditional_prob({'cloudy':True,},[{'rain':True}]); printV(["b. P(c = True | rain = True): ",cond_returned[0]],2); cond_returned = ps.prior_conditional_prob({'sprinkler':True,},[{'wet':True}]); printV(["c. P(s = True | w = True): ",cond_returned[0]],2); cond_returned = ps.prior_conditional_prob({'sprinkler':True,},[{'cloudy':True},{'wet':True}]); printV(["d. P(s = True | c = True, w = True): ",cond_returned[0]],2);
def calcJoint(self,key_list): arg_list=self.get_arg_list(key_list); printV(["arg_list: ",arg_list],1); joint = 1; for arg in arg_list: printV(["arg: ",arg[0]],1); if('~' is not arg[0]): self.calcMarginal(arg[0]); joint = joint * self.marginal[arg[0]]; else: # case when there is ~ self.calcMarginal(arg[1]); joint = joint * (1-self.marginal[arg[1]]); return joint;
def add_temp_sample(self, sample, given_dict, name, cpt_val): printV(["sample: ", sample, "given_dict: ", given_dict], 1) printV(["self.cpt[name][cpt_val]", self.cpt[name][cpt_val]], 1) name_val = self.check_mark(sample, self.cpt[name][cpt_val]) if (name in given_dict) is True: # cloudy is in given if (given_dict[name] == name_val) is True: # no contradition return name_val else: # cloudy is given and sample contradicts return None else: # cloudy is not in given return name_val
def isCausal(self,key,E_list): E_list=self.get_arg_list(E_list); r_val = 1; # recursive check for ch_key in self.children[key]: self.isCausal(ch_key,self.children[ch_key]); for E in E_list: if '~' is not E[0]: for ch_key in self.children[key]: if E[0] is ch_key: printV(["found evidence",E[0],"is not a parent of",key],1); r_val=0; else: for ch_key in self.children[key]: if E[1] is ch_key: printV(["found evidence",E[1],"is not a parent of",key],1); r_val=0; return r_val;
def calcProb_given_E(self,key): if (key == 'p')is True or (key == 's')is True: return self.prior[key]; # update_pi with no evidence is same as marginal parent = self.parents[key]; key_cpt = self.cpt[key]; printV(["inside calcMarginal key: ",key],1); printV(["key_cpt:",key_cpt[0]],1); for index_parent in parent: self.calcProb_given_E(index_parent); # FIXME: matt # change this to work for any node with two parents # p --> pollution = low # given p, prior(p)--> 1 # if prior[parent]_0 (pollution) = 0 then pollution = h if (key == 'c')is True: self.prior[key] = ( (1-self.prior[parent[0]]) * (self.prior[parent[1]]) * (key_cpt[0]) + (1-self.prior[parent[0]]) * (1-self.prior[parent[1]]) * (key_cpt[1]) + (self.prior[parent[0]]) * (self.prior[parent[1]]) * (key_cpt[2]) + (self.prior[parent[0]]) * (1-self.prior[parent[1]]) * (key_cpt[3]) ); printV(self.prior[key],1); return self.prior[key]; else : self.prior[key] = ( (self.prior[parent[0]]) * (key_cpt[0]) + (1-self.prior[parent[0]]) * (key_cpt[1]) ); printV(self.prior[key],1); return self.prior[key];
def calcMarginal(self,key): if (key == 'p')is True or (key == 's')is True: return self.marginal[key]; # update_pi with no evidence is same as marginal parent = self.parents[key]; key_cpt = self.cpt[key]; printV(["inside calcMarginal key: ",key],1); printV(["key_cpt:",key_cpt[0]],1); # recursinve call update_pi for parents for index_parent in parent: self.calcMarginal(index_parent); # FIXME: matt # change this to work for any node with two parents if (key == 'c')is True: self.marginal[key] = ( (1-self.marginal[parent[0]]) * (self.marginal[parent[1]]) * (key_cpt[0]) + (1-self.marginal[parent[0]]) * (1-self.marginal[parent[1]]) * (key_cpt[1]) + (self.marginal[parent[0]]) * (self.marginal[parent[1]]) * (key_cpt[2]) + (self.marginal[parent[0]]) * (1-self.marginal[parent[1]]) * (key_cpt[3]) ); printV(self.marginal[key],1); return self.marginal[key]; else : self.marginal[key] = ( (self.marginal[parent[0]]) * (key_cpt[0]) + (1-self.marginal[parent[0]]) * (key_cpt[1]) ); printV(self.marginal[key],1); return self.marginal[key];
def make_valid_array(self, given_dict): return_val = 1 while return_val: printV("********************************************", 1) return_val = self.set_sample_dict(given_dict) printV(self.valid_array, 1) for i in range(len(self.valid_array)): printV([i, ": ", self.valid_array[i]], 1)
def main(): rs = rejection_sampling() printV("Problem 2: Rejection sampling", 2) return_val = rs.rejection_prob("cloudy", {}) printV(["a. P(c = True): ", return_val[0]], 2) rs.sample_cnt = 0 rs.valid_array = [] return_val = rs.rejection_prob("cloudy", {"rain": True}) printV(["b. P(c = True | rain = True): ", return_val[0]], 2) rs.sample_cnt = 0 rs.valid_array = [] return_val = rs.rejection_prob("sprinkler", {"wet": True}) printV(["c. P(s = True | wet = True): ", return_val[0]], 2) rs.sample_cnt = 0 rs.valid_array = [] return_val = rs.rejection_prob("sprinkler", {"wet": True, "cloudy": True}) printV(["d. P(s = True | wet = True, cloudy = True): ", return_val[0]], 2) rs.sample_cnt = 0 rs.valid_array = []
def set_sample_dict(self,sample_array): temp_dict = {}; #check cloudy if (self.check_mark(sample_array[0],self.cpt['cloudy'][0]))is True: printV("it is cloudy",1); # set the cloudy value in temp_dict['cloudy'] = True; else: printV("it is not cloudy",1); # set the cloudy value in temp_dict['cloudy'] = False; # check sprinkler if (temp_dict['cloudy']) is True: # cloudy is true so look at sprinkler[0] if (self.check_mark(sample_array[1],self.cpt['sprinkler'][0]))is True: printV("cloudy is true and sprinkler is true",1); temp_dict['sprinkler'] = True; else: printV("cloudy is true and sprinkler is false",1); temp_dict['sprinkler'] = False; else: # cloudy is false so look at sprinkler[1] if (self.check_mark(sample_array[1],self.cpt['sprinkler'][1]))is True: printV("cloudy is false and sprinkler is true",1); temp_dict['sprinkler'] = True; else: printV("cloudy is false and sprinkler is false",1); temp_dict['sprinkler'] = False; # check rain if (temp_dict['cloudy']) is True: # cloudy is true so look at rain[0] if (self.check_mark(sample_array[2],self.cpt['rain'][0]))is True: printV("cloudy is true and rain is true",1); temp_dict['rain'] = True; else: printV("cloudy is true and rain is false",1); temp_dict['rain'] = False; else: # cloudy is false so look at rain[1] if (self.check_mark(sample_array[2],self.cpt['rain'][1]))is True: printV("cloudy is false and rain is true",1); temp_dict['rain'] = True; else: printV("cloudy is false and rain is false",1); temp_dict['rain'] = False; #check wet grass if ((temp_dict['sprinkler'])is True and (temp_dict['rain'])is True): printV("true true",1); # use wet[0] if (self.check_mark(sample_array[3],self.cpt['wet'][0]))is True: printV("cloudy is false and wet is true",1); temp_dict['wet'] = True; else: printV("cloudy is false and wet is false",1); temp_dict['wet'] = False; elif ((temp_dict['sprinkler'])is True and (temp_dict['rain'])is False): printV("true false",1); # use wet[1] if (self.check_mark(sample_array[3],self.cpt['wet'][1]))is True: printV("cloudy is false and wet is true",1); temp_dict['wet'] = True; else: printV("cloudy is false and wet is false",1); temp_dict['wet'] = False; elif ((temp_dict['sprinkler'])is False and (temp_dict['rain'])is True): printV("false true",1); # use wet[2] if (self.check_mark(sample_array[3],self.cpt['wet'][2]))is True: printV("cloudy is false and wet is true",1); temp_dict['wet'] = True; else: printV("cloudy is false and wet is false",1); temp_dict['wet'] = False; elif ((temp_dict['sprinkler'])is False and (temp_dict['rain'])is False): printV("false false",1); # use wet[3] if (self.check_mark(sample_array[3],self.cpt['wet'][3]))is True: printV("cloudy is false and wet is true",1); temp_dict['wet'] = True; else: printV("cloudy is false and wet is false",1); temp_dict['wet'] = False; self.dict_array.append(temp_dict);
def check_mark(self,check,mark): printV(["check",check],1); printV(["mark",mark],1); if (check <= mark): return True; else: return False;
def set_sample_dict(self, given_dict): # This is where you will call each node # Each node will return true to keep going # will return false to start a new iteration temp_dict = {} # cloudy if (self.sample_cnt < len(self.samples)) is True: cloudy_sample = self.get_sample() printV(["cloudy sample: ", cloudy_sample], 1) cloudy_ret = self.add_temp_sample(cloudy_sample, given_dict, "cloudy", 0) # if false start a new iteration if (cloudy_ret == None) is True: return True else: temp_dict["cloudy"] = cloudy_ret else: return False printV(["temp_dict after cloudy check", temp_dict], 1) # sprinkler # set cpt_val for sprinkler based on cloudy_val if (temp_dict["cloudy"] == False) is True: cpt_s = 1 else: cpt_s = 0 if (self.sample_cnt < len(self.samples)) is True: sprinkler_sample = self.get_sample() printV(["sprinkler sample: ", sprinkler_sample], 1) sprinkler_ret = self.add_temp_sample(sprinkler_sample, given_dict, "sprinkler", cpt_s) # if false start a new iteration if (sprinkler_ret == None) is True: return True else: temp_dict["sprinkler"] = sprinkler_ret else: return False printV(["temp_dict after sprinkler check", temp_dict], 1) # rain # set cpt_val for rain based on cloudy_val if (self.sample_cnt < len(self.samples)) is True: rain_sample = self.get_sample() printV(["rain sample: ", rain_sample], 1) rain_ret = self.add_temp_sample(rain_sample, given_dict, "rain", cpt_s) # if false start a new iteration if (rain_ret == None) is True: return True else: temp_dict["rain"] = rain_ret else: return False printV(["temp_dict after rain check", temp_dict], 1) # wet # set cpt_val for wet based on sprinkler_val and rain_val if (temp_dict["sprinkler"] == True) is True and (temp_dict["rain"] == True) is True: cpt_w = 0 elif (temp_dict["sprinkler"] == True) is True and (temp_dict["rain"] == False) is True: cpt_w = 1 elif (temp_dict["sprinkler"] == False) is True and (temp_dict["rain"] == True) is True: cpt_w = 2 elif (temp_dict["sprinkler"] == False) is True and (temp_dict["rain"] == False) is True: cpt_w = 3 printV(["cpt_w value:", cpt_w], 1) if (self.sample_cnt < len(self.samples)) is True: wet_sample = self.get_sample() printV(["wet sample: ", wet_sample], 1) wet_ret = self.add_temp_sample(wet_sample, given_dict, "wet", cpt_w) # if false start a new iteration if (wet_ret == None) is True: return True else: temp_dict["wet"] = wet_ret else: return False printV(["temp_dict after wet check", temp_dict], 1) printV("----- Iteration satisfies all given values -----", 1) self.valid_array.append(temp_dict) return True
def prior_conditional_prob(self,find_val,given_list): printV(["find value",find_val],1); printV(["given list",given_list],1); # need to calculate the total number of eqations that satisfy the # given list cond_array = []; [(find_key,find_value)] = find_val.items(); printV(["find_key: ",find_key," find_value: ",find_value],1); # valid array is the array of currently valid complete samples valid_array = self.dict_array; # find all complete samples which satisfy all of the givens for given_temp in given_list: given_sample_number = 0; [(given_key, given_value)] = given_temp.items(); # temp_array stores complete samples from valid array that # satisfy the new given temp_array = []; printV(["given_key: ",given_key," given_value: ",given_value],1); for i in range(len(valid_array)): if (valid_array[i][given_key] == given_value)is True: given_sample_number = given_sample_number + 1; temp_array.append(valid_array[i]); valid_array = temp_array; cond_array = np.array(valid_array); printV(["given_sample_number: ",given_sample_number],1); for i in range(0,given_sample_number): printV([i," :",cond_array[i]],1); # calculate the number of equations where find_value is <true,false> from within the # above list and divide by the total number of eq in the above list find_value_cnt = 0; not_find_value_cnt = 0; for i in range(0,given_sample_number): if (cond_array[i][find_key] == find_value)is True: find_value_cnt = find_value_cnt + 1; else: not_find_value_cnt = not_find_value_cnt +1; return np.array([float(find_value_cnt)/given_sample_number, float(not_find_value_cnt)/given_sample_number]);
def __init__(self): self.samples = np.array( [ 0.82, 0.56, 0.08, 0.81, 0.34, 0.22, 0.37, 0.99, 0.55, 0.61, 0.31, 0.66, 0.28, 1.00, 0.95, 0.71, 0.14, 0.10, 1.00, 0.71, 0.10, 0.60, 0.64, 0.73, 0.39, 0.03, 0.99, 1.00, 0.97, 0.54, 0.80, 0.97, 0.07, 0.69, 0.43, 0.29, 0.61, 0.03, 0.13, 0.14, 0.13, 0.40, 0.94, 0.19, 0.60, 0.68, 0.36, 0.67, 0.12, 0.38, 0.42, 0.81, 0.00, 0.20, 0.85, 0.01, 0.55, 0.30, 0.30, 0.11, 0.83, 0.96, 0.41, 0.65, 0.29, 0.40, 0.54, 0.23, 0.74, 0.65, 0.38, 0.41, 0.82, 0.08, 0.39, 0.97, 0.95, 0.01, 0.62, 0.32, 0.56, 0.68, 0.32, 0.27, 0.77, 0.74, 0.79, 0.11, 0.29, 0.69, 0.99, 0.79, 0.21, 0.20, 0.43, 0.81, 0.90, 0.00, 0.91, 0.01, ] ) printV(["samples: ", self.samples], 1) self.cpt = {"cloudy": [0.5], "sprinkler": [0.1, 0.5], "rain": [0.8, 0.2], "wet": [0.99, 0.90, 0.90, 0.00]} self.valid_array = [] self.sample_cnt = 0
def calcConditional(self,key,E_list): E_list=self.get_arg_list(E_list); printV("in calcConditional",1); printV(["key: ",key," evidence: ",E_list],1); #compute marginal for key and all E nodes self.calcMarginal(key); self.marg_E(E_list); #check if the evidence is causal if (self.isCausal(key,E_list) == 1): printV("the evidence is purely causal",1); # set priors according to evidence self.prior_given_E(E_list); return_val = self.calcProb_given_E(key) printV(["probability of", key,"given",E_list,"is:",return_val],2); return return_val; #check if the evidence is diagnostic if (self.isDiagnostic(key,E_list) == 1): printV("the evidence is purely diagnostic",1); # use bayes to calculate by finding causal conditionals and marginals # only works for 1 evidence for E in E_list: if '~' is not E[0]: cond_flip = self.calcConditional(E[0],key); return_val= ((cond_flip * self.marginal[key])/self.marginal[E[0]]); printV(["probability of", key,"given",E_list,"is:",return_val],2); return return_val; else: cond_flip = (1 - self.calcConditional(E[1],key)); return_val ((cond_flip * self.marginal[key])/self.marginal[E[1]]); printV(["probability of", key,"given",E_list,"is:",return_val],2); return return_val;
def main(): BN = BN_dict(); # set parents and children #printV(BN.pollution_dict['prob_dist'],1); #printV(BN.smoker_dict['prob_dist'],1); #printV(BN.cancer_dict['prob_dist'],1); #printV(BN.x_dict['prob_dist'],1); #printV(BN.d_dict['prob_dist'],1); ## next organize possible inputs try: opts, args = getopt.getopt(sys.argv[1:], "m:g:j:p:") except getopt.GetoptError as err: # print help information and exit: print str(err) # will print something like "option -a not recognized" sys.exit(2) for o, a in opts: if o in ("-p"): printV(["flag", o],1); printV(["args", a],1); printV(a[0],1); printV(float(a[1:]),1); printV("command to set prior",1); #setting the prior here works if the Bayes net is already built prior_out=BN.setPrior(a[0], float(a[1:])); printV(["prior for ",prior_out[0],"is now",prior_out[1]],2); elif o in ("-m"): printV(["flag", o],1); printV(["args", a],1); printV(type(a),1); printV("command to compute marginal prob",1); # FIXME: matt # calculate marginal for a a_list=BN.get_arg_list(a); printV(["marginal list",a_list],2); for a_index in a_list: if ('~' is not a_index[0]): printV(['Marginal of',a, "is: ",BN.calcMarginal(a[0])],2); else: printV(['Marginal of',a, "is: ",(1-BN.calcMarginal(a[1]))],2); elif o in ("-g"): printV(["flag", o],1); printV(["args", a],1); printV(type(a),1); printV("command to compute conditional prob",1); '''you may want to parse a here and pass the left of | and right of | as arguments to calcConditional ''' p = a.find("|") printV(a[:p],1); printV(a[p+1:],1); BN.calcConditional(a[:p], a[p+1:]); elif o in ("-j"): printV(["flag", o],1); printV(["args", a],1); printV("command to compute joint prob",1) printV(["J= ",BN.calcJoint(a)],2); else: assert False, "unhandled option"