Class VariableEliminationInference
- java.lang.Object
-
- com.bayesserver.inference.VariableEliminationInference
-
- All Implemented Interfaces:
Inference
public final class VariableEliminationInference extends Object implements Inference
An exact inference algorithm for Bayesian networks and Dynamic Bayesian networks, loosely based on the Variable Elimination algorithm.- See Also:
RelevanceTreeInference
,Inference
-
-
Constructor Summary
Constructors Constructor Description VariableEliminationInference(Network network)
Initializes a new instance of theVariableEliminationInference
class, with the target Bayesian network.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Evidence
getBaseEvidence()
Optional evidence which can be used to calculate the lift of queries.Evidence
getEvidence()
Gets the evidence (case data, e.g.Network
getNetwork()
The target Bayesian network.QueryDistributionCollection
getQueryDistributions()
The collection of distributions required from aInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
.QueryFunctionCollection
getQueryFunctions()
Gets the collection of functions to evaluate, after QueryDistributions have been calculated.QueryLifecycle
getQueryLifecycle()
Optional, allowing callers to hook into query lifecycle events, such as begin query and end query.void
query(QueryOptions queryOptions, QueryOutput queryOutput)
Calculates a number of distributions, e.g.void
setBaseEvidence(Evidence value)
Optional evidence which can be used to calculate the lift of queries.void
setEvidence(Evidence value)
Sets the evidence (case data, e.g.void
setQueryDistributions(QueryDistributionCollection value)
The collection of distributions required from aInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
.void
setQueryFunctions(QueryFunctionCollection value)
Sets the collection of functions to evaluate, after QueryDistributions have been calculated.void
setQueryLifecycle(QueryLifecycle value)
Optional, allowing callers to hook into query lifecycle events, such as begin query and end query.
-
-
-
Constructor Detail
-
VariableEliminationInference
public VariableEliminationInference(Network network)
Initializes a new instance of theVariableEliminationInference
class, with the target Bayesian network.- Parameters:
network
- The targetNetwork
.- Throws:
NullPointerException
- Raised if [network] is null.
-
-
Method Detail
-
getEvidence
public Evidence getEvidence()
Gets the evidence (case data, e.g. row in a database) used in aquery
. Thedistributions
are only recalculated whenInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
is called, not each time evidence is changed.- Specified by:
getEvidence
in interfaceInference
-
setEvidence
public void setEvidence(Evidence value)
Sets the evidence (case data, e.g. row in a database) used in aquery
. Thedistributions
are only recalculated whenInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
is called, not each time evidence is changed.- Specified by:
setEvidence
in interfaceInference
-
getBaseEvidence
public Evidence getBaseEvidence()
Optional evidence which can be used to calculate the lift of queries. Defaults to null. WhenQueryDistribution.getComparison()
istrue
queries are adjusted based on marginals calculated with this evidence or no evidence if this value is null.- Specified by:
getBaseEvidence
in interfaceInference
- See Also:
QueryDistribution.getComparison()
-
setBaseEvidence
public void setBaseEvidence(Evidence value)
Optional evidence which can be used to calculate the lift of queries. Defaults to null. WhenQueryDistribution.getComparison()
istrue
queries are adjusted based on marginals calculated with this evidence or no evidence if this value is null.- Specified by:
setBaseEvidence
in interfaceInference
- See Also:
QueryDistribution.getComparison()
-
getQueryDistributions
public QueryDistributionCollection getQueryDistributions()
The collection of distributions required from aInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
. Only request those that you need.Each distribution added, such as a
Table
, acts like a buffer. I.e. the values are populated by theInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
method. This means that the same distributions can be resused across multiple calls toInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
, without the need to recreate the distribution each time.It is important not to request distributions you do not need, because the computations will take longer. For example, it is common to request all marginal probabilities, P(A), P(B), P(C), ... , P(Z) given the
evidence
, however if you only need to know P(A) and P(B) given theevidence
, then limit the query to just these distributions.Typically the distributions requested are a number of marginal propabilities, such as P(A), P(B), P(C) etc... given the
evidence
, however it is also possible to request distributions over more than one variable, so we might query P(A), P(B,C) given theevidence
.If you are performing a batch of queries, there is no need to remove those distributions that you require, however happen to have evidence set for a particular query.
- Specified by:
getQueryDistributions
in interfaceInference
-
setQueryDistributions
public void setQueryDistributions(QueryDistributionCollection value)
The collection of distributions required from aInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
. Only request those that you need.Each distribution added, such as a
Table
, acts like a buffer. I.e. the values are populated by theInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
method. This means that the same distributions can be resused across multiple calls toInference.query(com.bayesserver.inference.QueryOptions, com.bayesserver.inference.QueryOutput)
, without the need to recreate the distribution each time.It is important not to request distributions you do not need, because the computations will take longer. For example, it is common to request all marginal probabilities, P(A), P(B), P(C), ... , P(Z) given the
evidence
, however if you only need to know P(A) and P(B) given theevidence
, then limit the query to just these distributions.Typically the distributions requested are a number of marginal propabilities, such as P(A), P(B), P(C) etc... given the
evidence
, however it is also possible to request distributions over more than one variable, so we might query P(A), P(B,C) given theevidence
.If you are performing a batch of queries, there is no need to remove those distributions that you require, however happen to have evidence set for a particular query.
- Specified by:
setQueryDistributions
in interfaceInference
-
getQueryFunctions
public QueryFunctionCollection getQueryFunctions()
Gets the collection of functions to evaluate, after QueryDistributions have been calculated. If required, a query function collection can be dynamically attached or detached. Also note that individual elements can be enabled or disabled on a per query basis.- Specified by:
getQueryFunctions
in interfaceInference
-
setQueryFunctions
public void setQueryFunctions(QueryFunctionCollection value)
Sets the collection of functions to evaluate, after QueryDistributions have been calculated. If required, a query function collection can be dynamically attached or detached. Also note that individual elements can be enabled or disabled on a per query basis.- Specified by:
setQueryFunctions
in interfaceInference
-
getQueryLifecycle
public QueryLifecycle getQueryLifecycle()
Optional, allowing callers to hook into query lifecycle events, such as begin query and end query.- Specified by:
getQueryLifecycle
in interfaceInference
-
setQueryLifecycle
public void setQueryLifecycle(QueryLifecycle value)
Optional, allowing callers to hook into query lifecycle events, such as begin query and end query.- Specified by:
setQueryLifecycle
in interfaceInference
-
getNetwork
public Network getNetwork()
The target Bayesian network.- Specified by:
getNetwork
in interfaceInference
-
query
public void query(QueryOptions queryOptions, QueryOutput queryOutput) throws InconsistentEvidenceException
Calculates a number of distributions, e.g. P(A) and P(B) given theevidence
(case data, e.g. row in a database), and if requested the log-likelihood of the data. Each time this method is called thedistributions
have their values replaced, acting like buffers.As well as requesting distributions of the form P(A), P(B) it is also possible to request distributions over a number of variables such as P(A,B).
If
CLGaussian
distributions are requested that include instantiated discrete variables, an algorithm may legitimately return mean and covariance values equal to NaN for entries corresponding to inconsistent discrete combinations. For example, if a CLGaussian is requested that includes a discrete variable 'Gender' with states 'Male' and 'Female' and evidence is set to 'Female' then any mean and covariance entries that correspond to 'Male' may be set to NaN. NaN values will always be accompanied by a corresponding zero value in theCLGaussian.getTable()
.- Specified by:
query
in interfaceInference
- Parameters:
queryOptions
- Options governing which calculations are performed and how.queryOutput
- Returns any information, in addition to thedistributions
, that is requested. For example thelog-likelihood
.- Throws:
InconsistentEvidenceException
- Raised if inconsistent evidence is detected, or underflow/overflow occurs when calculating queries or the conflict measure. Note that log-likelihood calculations do not raise this exception, but instead report -Infinity or +Infinity.
-
-