本文整理汇总了Python中bayesNet.Factor.specializeVariableDomains方法的典型用法代码示例。如果您正苦于以下问题:Python Factor.specializeVariableDomains方法的具体用法?Python Factor.specializeVariableDomains怎么用?Python Factor.specializeVariableDomains使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bayesNet.Factor
的用法示例。
在下文中一共展示了Factor.specializeVariableDomains方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: inferenceByLikelihoodWeightingSampling
# 需要导入模块: from bayesNet import Factor [as 别名]
# 或者: from bayesNet.Factor import specializeVariableDomains [as 别名]
def inferenceByLikelihoodWeightingSampling(bayesNet, queryVariables, evidenceDict, numSamples):
"""
Question 6: Inference by likelihood weighted sampling
This function should perform a probabilistic inference query that
returns the factor:
P(queryVariables | evidenceDict)
It should perform inference by performing likelihood weighting
sampling. It should sample numSamples times.
In order for the autograder's solution to match yours,
your outer loop needs to iterate over the number of samples,
with the inner loop sampling from each variable's factor.
Use the ordering of variables provided by BayesNet.linearizeVariables in
your inner loop so that the order of samples matches the autograder's.
There are typically many linearization orders of a directed acyclic
graph (DAG), however we just use a particular one.
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.
numSamples: The number of samples that should be taken.
Useful functions:
sampleFromFactor
normalize
BayesNet.getCPT
BayesNet.linearizeVariables
"""
sampleFromFactor = sampleFromFactorRandomSource(randomSource)
"*** YOUR CODE HERE ***"
# create return factor
newFactor = Factor(queryVariables, evidenceDict, bayesNet.variableDomainsDict())
reducedVariableDomains = bayesNet.getReducedVariableDomains(evidenceDict)
newFactor = newFactor.specializeVariableDomains(reducedVariableDomains)
for i in range(numSamples):
weight = 1.0
allAssignments = {}
allAssignments.update(evidenceDict)
for var in bayesNet.linearizeVariables():
tmpCPT = bayesNet.getCPT(var)
if var in evidenceDict:
# accumulate weight
weight *= tmpCPT.getProbability(allAssignments)
else:
newAssignment = sampleFromFactor(tmpCPT, allAssignments)
allAssignments.update(newAssignment)
# accumulate sample
p = newFactor.getProbability(allAssignments)
newFactor.setProbability(allAssignments, p + weight)
return normalize(newFactor)
util.raiseNotDefined()