cpquery: Perform conditional probability queries

Description Usage Arguments Details Value Logic Sampling Likelihood Weighting Author(s) References Examples

Description

Perform conditional probability queries (CPQs).

Usage

1
2
3
4
5
6
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

a list, a data.frame, an unevaluated expression or TRUE. See Details and Examples 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.

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.

When event or evidence are of class lists, names should correspond to nodes in the fitted object and each component will be treated as or expression. For example an event like list(A=c("a1", "a2"), B="c") is equivalent to (A=="a1"||A=="a2") & (B=="c"). If either event or evidence is a data.frames, it will be treated as a list.

If event and evidence arguments are unevaluated expressions then they should describe the event of interest and the conditioning evidence such that, if we denote with data the data set the network was learned from, data[eval(evidence, data), ] and data[eval(event, data), ] return the correct observations.

If either event or evidence is set to TRUE an unconditional probability query is performed with respect to that argument.

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

Three tuning parameters are available:

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

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

Author(s)

Marco Scutari

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## discrete Bayesian network (it's the same with ordinal nodes).
data(learning.test)
bn <- hc(learning.test)
plot(bn)
fitted <- bn.fit(bn, learning.test)

## the result should be around 0.025.
cpquery(fitted, expression((B == "b")), expression((A == "a")))
## for a single observation, predict the value of a single
## variable conditional on the others:
ev <- learning.test[2, -4]
cpquery(fitted, list(D = "b"), ev)
cpquery(fitted, list(D = "c"), ev)
cpquery(fitted, list(D = c("b", "c")), ev)
cpquery(fitted, list(D = "b", D = "c"), ev) # 0

## do the same with logical sampling:
cpquery(fitted, event = quote(C=='c'),  evidence = learning.test[2, -3], method = "ls")
## do the same with likelihood weighting:
cpquery(fitted, event = quote(C=='c'),  evidence = learning.test[2, -3], method = "lw")

## conditional distribution of A given C == "c".
prop.table(table(cpdist(fitted, "A", list(C = "c"), method = "lw")))

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

vspinu/bnlearn documentation built on May 3, 2019, 7:08 p.m.