def inferenceByVariableElimination(bayesNet, queryVariables, evidenceDict, eliminationOrder): """ Question 6: Your inference by variable elimination implementation This function should perform a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) It should perform inference by interleaving joining on a variable and eliminating that variable, in the order of variables according to eliminationOrder. See inferenceByEnumeration for an example on how to use these functions. You need to use joinFactorsByVariable to join all of the factors that contain a variable in order for the autograder to recognize that you performed the correct interleaving of joins and eliminates. If a factor that you are about to eliminate a variable from has only one unconditioned variable, you should not eliminate it and instead just discard the factor. This is since the result of the eliminate would be 1 (you marginalize all of the unconditioned variables), but it is not a valid factor. So this simplifies using the result of eliminate. The sum of the probabilities should sum to one (so that it is a true conditional probability, conditioned on the evidence). bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. eliminationOrder: The order to eliminate the variables in. Hint: BayesNet.getAllCPTsWithEvidence will return all the Conditional Probability Tables even if an empty dict (or None) is passed in for evidenceDict. In this case it will not specialize any variable domains in the CPTs. Useful functions: BayesNet.getAllCPTsWithEvidence normalize eliminate joinFactorsByVariable joinFactors """ # this is for autograding -- don't modify joinFactorsByVariable = joinFactorsByVariableWithCallTracking(callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) if eliminationOrder is None: # set an arbitrary elimination order if None given eliminationVariables = bayesNet.variablesSet() - set(queryVariables) -\ set(evidenceDict.keys()) eliminationOrder = sorted(list(eliminationVariables)) "*** YOUR CODE HERE ***" # grab all factors where we know the evidence variables (to reduce the size of the tables) currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) # join all factors by variable for joinVariable in eliminationOrder: currentFactorsList, joinedFactor = joinFactorsByVariable(currentFactorsList, joinVariable) if len(joinedFactor.unconditionedVariables()) > 1: eliminateFactor = eliminate(joinedFactor, joinVariable) currentFactorsList.append(eliminateFactor) fullJoint = joinFactors(currentFactorsList) return normalize(fullJoint)
def inferenceByEnumeration(bayesNet, queryVariables, evidenceDict): """ An inference by enumeration implementation provided as reference. This function performs a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. """ callTrackingList = [] joinFactorsByVariable = joinFactorsByVariableWithCallTracking( callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) # initialize return variables and the variables to eliminate evidenceVariablesSet = set(evidenceDict.keys()) queryVariablesSet = set(queryVariables) eliminationVariables = (bayesNet.variablesSet() - evidenceVariablesSet) - queryVariablesSet # grab all factors where we know the evidence variables (to reduce the size of the tables) currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) # join all factors by variable for joinVariable in bayesNet.variablesSet(): currentFactorsList, joinedFactor = joinFactorsByVariable( currentFactorsList, joinVariable) currentFactorsList.append(joinedFactor) # currentFactorsList should contain the connected components of the graph now as factors, must join the connected components fullJoint = joinFactors(currentFactorsList) # marginalize all variables that aren't query or evidence incrementallyMarginalizedJoint = fullJoint for eliminationVariable in eliminationVariables: incrementallyMarginalizedJoint = eliminate( incrementallyMarginalizedJoint, eliminationVariable) fullJointOverQueryAndEvidence = incrementallyMarginalizedJoint # normalize so that the probability sums to one # the input factor contains only the query variables and the evidence variables, # both as unconditioned variables queryConditionedOnEvidence = normalize(fullJointOverQueryAndEvidence) # now the factor is conditioned on the evidence variables # the order is join on all variables, then eliminate on all elimination variables #print "callTrackingList: ", callTrackingList return queryConditionedOnEvidence
def inferenceByEnumeration(bayesNet, queryVariables, evidenceDict): """ An inference by enumeration implementation provided as reference. This function performs a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. """ callTrackingList = [] joinFactorsByVariable = joinFactorsByVariableWithCallTracking(callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) # initialize return variables and the variables to eliminate evidenceVariablesSet = set(evidenceDict.keys()) queryVariablesSet = set(queryVariables) eliminationVariables = (bayesNet.variablesSet() - evidenceVariablesSet) - queryVariablesSet # grab all factors where we know the evidence variables (to reduce the size of the tables) currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) # join all factors by variable for joinVariable in bayesNet.variablesSet(): currentFactorsList, joinedFactor = joinFactorsByVariable(currentFactorsList, joinVariable) currentFactorsList.append(joinedFactor) # currentFactorsList should contain the connected components of the graph now as factors, must join the connected components fullJoint = joinFactors(currentFactorsList) # marginalize all variables that aren't query or evidence incrementallyMarginalizedJoint = fullJoint for eliminationVariable in eliminationVariables: incrementallyMarginalizedJoint = eliminate(incrementallyMarginalizedJoint, eliminationVariable) fullJointOverQueryAndEvidence = incrementallyMarginalizedJoint # normalize so that the probability sums to one # the input factor contains only the query variables and the evidence variables, # both as unconditioned variables queryConditionedOnEvidence = normalize(fullJointOverQueryAndEvidence) # now the factor is conditioned on the evidence variables # the order is join on all variables, then eliminate on all elimination variables #print "callTrackingList: ", callTrackingList return queryConditionedOnEvidence
def inferenceByVariableElimination(bayesNet, queryVariables, evidenceDict, eliminationOrder): """ Question 6: Your inference by variable elimination implementation This function should perform a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) It should perform inference by interleaving joining on a variable and eliminating that variable, in the order of variables according to eliminationOrder. See inferenceByEnumeration for an example on how to use these functions. You need to use joinFactorsByVariable to join all of the factors that contain a variable in order for the autograder to recognize that you performed the correct interleaving of joins and eliminates. If a factor that you are about to eliminate a variable from has only one unconditioned variable, you should not eliminate it and instead just discard the factor. This is since the result of the eliminate would be 1 (you marginalize all of the unconditioned variables), but it is not a valid factor. So this simplifies using the result of eliminate. The sum of the probabilities should sum to one (so that it is a true conditional probability, conditioned on the evidence). bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. eliminationOrder: The order to eliminate the variables in. Hint: BayesNet.getAllCPTsWithEvidence will return all the Conditional Probability Tables even if an empty dict (or None) is passed in for evidenceDict. In this case it will not specialize any variable domains in the CPTs. Useful functions: BayesNet.getAllCPTsWithEvidence normalize eliminate joinFactorsByVariable joinFactors """ # this is for autograding -- don't modify joinFactorsByVariable = joinFactorsByVariableWithCallTracking( callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) if eliminationOrder is None: # set an arbitrary elimination order if None given eliminationVariables = bayesNet.variablesSet() - set(queryVariables) -\ set(evidenceDict.keys()) eliminationOrder = sorted(list(eliminationVariables)) "*** YOUR CODE HERE ***" evidenceVariablesSet = set(evidenceDict.keys()) queryVariablesSet = set(queryVariables) eliminationVariables = (bayesNet.variablesSet() - evidenceVariablesSet) - queryVariablesSet # grab all factors where we know the evidence variables (to reduce the size of the tables) newfactor = None currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) for var in eliminationOrder: currentFactorsList, newfactor = joinFactorsByVariable( currentFactorsList, var) if len(newfactor.unconditionedVariables()) == 1: pass else: newfactor = eliminate(newfactor, var) currentFactorsList.append(newfactor) retfactor = joinFactors(currentFactorsList) retfactor = normalize(retfactor) return retfactor
def inferenceByVariableElimination(bayesNet, queryVariables, evidenceDict, eliminationOrder): """ Question 6: Your inference by variable elimination implementation This function should perform a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) It should perform inference by interleaving joining on a variable and eliminating that variable, in the order of variables according to eliminationOrder. See inferenceByEnumeration for an example on how to use these functions. You need to use joinFactorsByVariable to join all of the factors that contain a variable in order for the autograder to recognize that you performed the correct interleaving of joins and eliminates. If a factor that you are about to eliminate a variable from has only one unconditioned variable, you should not eliminate it and instead just discard the factor. This is since the result of the eliminate would be 1 (you marginalize all of the unconditioned variables), but it is not a valid factor. So this simplifies using the result of eliminate. The sum of the probabilities should sum to one (so that it is a true conditional probability, conditioned on the evidence). bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. eliminationOrder: The order to eliminate the variables in. Hint: BayesNet.getAllCPTsWithEvidence will return all the Conditional Probability Tables even if an empty dict (or None) is passed in for evidenceDict. In this case it will not specialize any variable domains in the CPTs. Useful functions: BayesNet.getAllCPTsWithEvidence normalize eliminate joinFactorsByVariable joinFactors Query: ['D'] eliminateOrder: ['T'] evidenceDict : W {'W': 'sun'} Variables: D, W, T Edges: W -> D, W -> T cpt [Factor(set(['D']), set(['W']), {'D': ['dry', 'wet'], 'W': ['sun'], 'T': ['hot', 'cold']}), Factor(set(['W']), set([]), {'D': ['dry', 'wet'], 'W': ['sun'], 'T': ['hot', 'cold']}), Factor(set(['T']), set(['W']), {'D': ['dry', 'wet'], 'W': ['sun'], 'T': ['hot', 'cold']})] join -> eliminate -> join -> eliminate ### """ # this is for autograding -- don't modify joinFactorsByVariable = joinFactorsByVariableWithCallTracking( callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) if eliminationOrder is None: # set an arbitrary elimination order if None given eliminationVariables = bayesNet.variablesSet() - set(queryVariables) -\ set(evidenceDict.keys()) eliminationOrder = sorted(list(eliminationVariables)) "*** YOUR CODE HERE ***" cpts = bayesNet.getAllCPTsWithEvidence(evidenceDict) for join in eliminationOrder: cpts, joinedFactor = joinFactorsByVariable(cpts, join) if (len(joinedFactor.unconditionedVariables()) == 1): continue else: #eliminate eliminateVariable = join eliminatedFactor = eliminate(joinedFactor, eliminateVariable) cpts.append(eliminatedFactor) factor = joinFactors(cpts) normalizeFactor = normalize(factor) return normalizeFactor
def inferenceByVariableElimination(bayesNet, queryVariables, evidenceDict, eliminationOrder): """ Question 4: Your inference by variable elimination implementation This function should perform a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) It should perform inference by interleaving joining on a variable and eliminating that variable, in the order of variables according to eliminationOrder. See inferenceByEnumeration for an example on how to use these functions. You need to use joinFactorsByVariable to join all of the factors that contain a variable in order for the autograder to recognize that you performed the correct interleaving of joins and eliminates. If a factor that you are about to eliminate a variable from has only one unconditioned variable, you should not eliminate it and instead just discard the factor. This is since the result of the eliminate would be 1 (you marginalize all of the unconditioned variables), but it is not a valid factor. So this simplifies using the result of eliminate. The sum of the probabilities should sum to one (so that it is a true conditional probability, conditioned on the evidence). bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. eliminationOrder: The order to eliminate the variables in. Hint: BayesNet.getAllCPTsWithEvidence will return all the Conditional Probability Tables even if an empty dict (or None) is passed in for evidenceDict. In this case it will not specialize any variable domains in the CPTs. Useful functions: BayesNet.getAllCPTsWithEvidence normalize eliminate joinFactorsByVariable joinFactors """ # this is for autograding -- don't modify joinFactorsByVariable = joinFactorsByVariableWithCallTracking(callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) if eliminationOrder is None: # set an arbitrary elimination order if None given eliminationVariables = bayesNet.variablesSet() - set(queryVariables) -\ set(evidenceDict.keys()) eliminationOrder = sorted(list(eliminationVariables)) "*** YOUR CODE HERE ***" evidenceVariablesSet = set(evidenceDict.keys()) queryVariablesSet = set(queryVariables) eliminationVariables = set(eliminationOrder) currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) for i in range(len(eliminationOrder)): '''eviDict = {eliminationOrder[i]:evidenceDict[eliminationOrder[i]]} currentFactorsList = bayesNet.getAllCPTsWithEvidence(eviDict)''' # join factors by variable joinVariable = eliminationOrder[i] currentFactorsList, joinedFactor = joinFactorsByVariable(currentFactorsList, joinVariable) # marginalize the variable if joinedFactor.unconditionedVariables() != set(joinVariable): joinedFactor = eliminate(joinedFactor, joinVariable) currentFactorsList.append(joinedFactor) # currentFactorsList should contain the connected components of the graph now as factors, must join the connected components fullJointOverQueryAndEvidence = joinFactors(currentFactorsList) queryConditionedOnEvidence = normalize(fullJointOverQueryAndEvidence) return queryConditionedOnEvidence 'pdb.set_trace()' util.raiseNotDefined()
def inferenceByVariableElimination(bayesNet, queryVariables, evidenceDict, eliminationOrder): """ Question 6: Your inference by variable elimination implementation This function should perform a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) It should perform inference by interleaving joining on a variable and eliminating that variable, in the order of variables according to eliminationOrder. See inferenceByEnumeration for an example on how to use these functions. You need to use joinFactorsByVariable to join all of the factors that contain a variable in order for the autograder to recognize that you performed the correct interleaving of joins and eliminates. If a factor that you are about to eliminate a variable from has only one unconditioned variable, you should not eliminate it and instead just discard the factor. This is since the result of the eliminate would be 1 (you marginalize all of the unconditioned variables), but it is not a valid factor. So this simplifies using the result of eliminate. The sum of the probabilities should sum to one (so that it is a true conditional probability, conditioned on the evidence). bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. eliminationOrder: The order to eliminate the variables in. Hint: BayesNet.getAllCPTsWithEvidence will return all the Conditional Probability Tables even if an empty dict (or None) is passed in for evidenceDict. In this case it will not specialize any variable domains in the CPTs. Useful functions: BayesNet.getAllCPTsWithEvidence normalize eliminate joinFactorsByVariable joinFactors """ # this is for autograding -- don't modify joinFactorsByVariable = joinFactorsByVariableWithCallTracking( callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) if eliminationOrder is None: # set an arbitrary elimination order if None given eliminationVariables = bayesNet.variablesSet() - set(queryVariables) -\ set(evidenceDict.keys()) eliminationOrder = sorted(list(eliminationVariables)) "*** YOUR CODE HERE ***" #util.raiseNotDefined() """ # print "@@@@@queryVariables ", queryVariables # print "@@@@@evidenceDict ", evidenceDict # print "@@@@@eliminationOrder ", eliminationOrder # print "@@@@@bayesNet ", bayesNet # currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) # print "@@@@@currentFactorsList ", currentFactorsList for joinVariable in eliminationOrder: # print "@@@@@joinVariable ", joinVariable currentFactorsList, joinedFactor = joinFactorsByVariable(currentFactorsList, joinVariable) # print "@@@@@currentFactorsList ", currentFactorsList # print "@@@@@joinedFactor ", joinedFactor if len(joinedFactor.unconditionedVariables()) > 1: eliminatedFactor = eliminate(joinedFactor, joinVariable) currentFactorsList.append(eliminatedFactor) # print "@@@@@currentFactorsList ", currentFactorsList joinedFactor = joinFactors(currentFactorsList) normalizedFactor = normalize(joinedFactor) return normalizedFactor """ # grab all factors where we know the evidence variables (to reduce the size of the tables) # pass by evidenceDict through BayesNet.getAllCPTsWithEvidence factors = bayesNet.getAllCPTsWithEvidence(evidenceDict) # join all factors by eliminationOrder for join_Factor in eliminationOrder: not_joined_Factor, joined = joinFactorsByVariable( factors, join_Factor) factors = not_joined_Factor # currentFactorsList should contain the connected components of the graph now as factors, must join the connected components joined_unconditioned = joined.unconditionedVariables() if len(joined_unconditioned) > 1: temp_factor = eliminate(joined, join_Factor) # eliminate not_joined_Factor.append(temp_factor) joinedFactors = joinFactors(not_joined_Factor) return normalize(joinedFactors) # normalize the joinedFactors
def inferenceByVariableElimination(bayesNet, queryVariables, evidenceDict, eliminationOrder): """ Question 6: Your inference by variable elimination implementation This function should perform a probabilistic inference query that returns the factor: P(queryVariables | evidenceDict) It should perform inference by interleaving joining on a variable and eliminating that variable, in the order of variables according to eliminationOrder. See inferenceByEnumeration for an example on how to use these functions. You need to use joinFactorsByVariable to join all of the factors that contain a variable in order for the autograder to recognize that you performed the correct interleaving of joins and eliminates. If a factor that you are about to eliminate a variable from has only one unconditioned variable, you should not eliminate it and instead just discard the factor. This is since the result of the eliminate would be 1 (you marginalize all of the unconditioned variables), but it is not a valid factor. So this simplifies using the result of eliminate. The sum of the probabilities should sum to one (so that it is a true conditional probability, conditioned on the evidence). bayesNet: The Bayes Net on which we are making a query. queryVariables: A list of the variables which are unconditioned in the inference query. evidenceDict: An assignment dict {variable : value} for the variables which are presented as evidence (conditioned) in the inference query. eliminationOrder: The order to eliminate the variables in. Hint: BayesNet.getAllCPTsWithEvidence will return all the Conditional Probability Tables even if an empty dict (or None) is passed in for evidenceDict. In this case it will not specialize any variable domains in the CPTs. Useful functions: BayesNet.getAllCPTsWithEvidence normalize eliminate joinFactorsByVariable joinFactors """ # this is for autograding -- don't modify joinFactorsByVariable = joinFactorsByVariableWithCallTracking( callTrackingList) eliminate = eliminateWithCallTracking(callTrackingList) if eliminationOrder is None: # set an arbitrary elimination order if None given eliminationVariables = bayesNet.variablesSet() - set(queryVariables) -\ set(evidenceDict.keys()) eliminationOrder = sorted(list(eliminationVariables)) "*** YOUR CODE HERE ***" """ this section is heavily based on the "inferenceByEnumeration" section. In "inferenceByEnumeration" section we would join all "joinVariable" to the "currentFactorsList" according to: for joinVariable in bayesNet.variablesSet(): currentFactorsList, joinedFactor = joinFactorsByVariable(currentFactorsList, joinVariable) currentFactorsList.append(joinedFactor) and then eliminate by: for eliminationVariable in eliminationVariables: incrementallyMarginalizedJoint = eliminate(incrementallyMarginalizedJoint, eliminationVariable) fullJointOverQueryAndEvidence = incrementallyMarginalizedJoint and then normalize so that the probability sums to one queryConditionedOnEvidence = normalize(fullJointOverQueryAndEvidence) and then return "queryConditionedOnEvidence" NOW, for "inferenceByVariableElimination" we would heavily use the same method but instead of adding all of them together and then removing all together, we would add one and then remove the same: to achieve this task we would loop through all the "eliminationOrder" and form a "currentFactorsList" with the "joinedFactor" and "elimVariable" currentFactorsList, joinedFactor = joinFactorsByVariable(currentFactorsList, elimVariable) then I would eliminate "elimVariable" from "joinedFactor" as long as there is at least more than 1 variable in the unconditioned in the "joinedFactor". I am not fully sure why I have to do it but the comments above say: If a factor that you are about to eliminate a variable from has only one unconditioned variable, you should not eliminate it and instead just discard the factor. This is since the result of the eliminate would be 1 (you marginalize all of the unconditioned variables), but it is not a valid factor. So this simplifies using the result of eliminate. I guess the reason is that if there is only one unconditioned in the joint and we remove them there would be no unconditioned left in joint. if there is only 1 unconditioned this is what happens: P(A|B).P(B) = P(A,B) --> end of it --> cant eliminate B from P(A,B) once the currentfactorList is updated a new factors list is generated and the normalized result is returned. finalFactorsList = joinFactors(currentFactorsList) finalFactorsListNormalized = normalize(finalFactorsList) """ #util.raiseNotDefined() "generate the current factor list. this list will be updated on the road" currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict) "loop through the elimination parameters and thenjoin and remove" for elimVariable in eliminationOrder: #print currentFactorsList "add the elimVariable and generate join" currentFactorsList, joinedFactor = joinFactorsByVariable( currentFactorsList, elimVariable) #print joinedFactor #print currentFactorsList "if unconditioned are larger or equal to 2 then remove from joinedFactor" numUncontiotioned = len(joinedFactor.unconditionedVariables()) if numUncontiotioned >= 2: incrementallyMarginalizedJoint = eliminate( joinedFactor, elimVariable) currentFactorsList.append(incrementallyMarginalizedJoint) #print "incrementallyMarginalizedJoint", incrementallyMarginalizedJoint #print currentFactorsList "otherwise this is the end of the add and remove --> do nothing" "generate final factor list join out of the updated currentFactorsList" finalFactorsList = joinFactors(currentFactorsList) #print finalFactorsList "normalize" finalFactorsListNormalized = normalize(finalFactorsList) return finalFactorsListNormalized