Learn R Programming

bnlearn (version 3.4)

cpquery: Perform conditional probability queries

Description

Perform conditional probability queries (CPQs).

Usage

cpquery(fitted, event, evidence, cluster = NULL, method = "ls", ...,
  debug = FALSE)
cpdist(fitted, nodes, evidence, cluster = NULL, method = "ls", ...,
  debug = FALSE)

mutilated(x, evidence)

Arguments

fitted
an object of class bn.fit.
x
an object of class bn or bn.fit.
event, evidence
see below.
nodes
a vector of character strings, the labels of the nodes whose conditional distribution we are interested in.
cluster
an optional cluster object from package snow. See snow integration for details and a simple example.
method
a character string, the method used to perform the conditional probability query. Currently only logic sampling (ls, the default) and likelihood weighting (lw) are implemented.
...
additional tuning parameters.
debug
a boolean value. If TRUE a lot of debugging output is printed; otherwise the function is completely silent.

Value

  • cpquery returns a numeric value, the conditional probability of event conditional on evidence.

    cpudist returns a data frame containing the observations generated from the conditional distribution of the nodes conditional on evidence.

    mutilated returns a bn or bn.fit object, depending on the class of x.

Logic Sampling

The event and evidence arguments must be two expressions describing the event of interest and the conditioning evidence in a format such that, if we denote with data the data set the network was learned from, data[evidence, ] and data[event, ] return the correct observations. If either event or evidence is set to TRUE an unconditional probability query is performed with respect to that argument.

Three tuning parameters are available:

  • n: a positive integer number, the number of random observations to generate fromfitted. Defaults to5000 * nparams(fitted).
  • batch: a positive integer number, the size of each batch of random observations. Defaults to10^4.
  • query.nodes: a a vector of character strings, the labels of the nodes involved ineventandevidence. Simple queries do not require to generate observations from all the nodes in the network, socpqueryandcpdisttry to identify which nodes are used ineventandevidenceand reduce the network to their upper closure.query.nodesmay be used to manually specify these nodes when automatic identification fails.

Note that the number of observations returned by cpdist is always smaller than n, because logic sampling is a form of rejection sampling. Therefore, only the obervations matching evidence (out of the n that are generated) are returned, and their number depends on the probability of evidence.

Likelihood Weighting

The event argument must be an expression describing the event of interest, as in logic sampling. The evidence argument must be a named list; each element corresponds to one node in the network and must contain the value that node will be set to when sampling. If either event or evidence is set to TRUE an unconditional probability query is performed with respect to that argument.

Tuning parameters are the same as for logic sampling: n, batch and query.nodes.

Details

cpquery estimates the conditional probability of event given evidence using the method specified in the method argument.

cpdist generates random observations conditional on the evidence using the method specified in the method argument.

mutilated constructs the mutilated network used for sampling in likelihood weighting.

References

Koller D, Friedman N (2009). Probabilistic Graphical Models: Principles and Techniques. MIT Press.

Korb K, Nicholson AE (2010). Bayesian Artificial Intelligence. Chapman & Hall/CRC, 2nd edition.

Examples

Run this code
## discrete Bayesian network (it's the same with ordinal nodes).
data(learning.test)
fitted = bn.fit(hc(learning.test), learning.test)
# the result should be around 0.025.
cpquery(fitted, (B == "b"), (A == "a"))
# for a single observation, predict the value of a single
# variable conditional on the others.
var = names(learning.test)
obs = 2
str = paste("(", names(learning.test)[-3], "=='",
        sapply(learning.test[obs,-3], as.character), "')", 
        sep = "", collapse = "& ")
str
str2 = paste("(", names(learning.test)[3], "=='",
         as.character(learning.test[obs, 3]), "')", sep = "")
str2
cpquery(fitted, eval(parse(text = str2)), eval(parse(text = str)))
# do the same with likelihood weighting
cpquery(fitted, event = eval(parse(text = str2)), 
  evidence = as.list(learning.test[2, -3]), method = "lw")
# conditional distribution of A given C == "c". 
table(cpdist(fitted, "A", (C == "c")))

## Gaussian Bayesian network.
data(gaussian.test)
fitted = bn.fit(hc(gaussian.test), gaussian.test)
# the result should be around 0.04.
cpquery(fitted, 
  event = ((A >= 0) & (A <= 1)) & ((B >= 0) & (B <= 3)),
  evidence = (C + D < 10))

Run the code above in your browser using DataLab