Learn R Programming

exams.forge (version 1.0.11)

prob_solve: Total or Conditional Probability Computation

Description

The following functions are available:

  • prob_solve given a set of events it computes the total or conditional probability of the given event or NA if no solution could be found. For the naming of the events upper case letters must be used and the available operators are ! (complementary event), | (conditional event), and ^ (intersection of events). The attribute latex of the return value contains the necessary computation steps for computation of the given event. If getprob is TRUE then additionally the attribute prob, a vector with all computed probabilities, and compute, which includes all computational steps, are generated.

  • print shows the solution way in ASCII.

  • toLatex shows the solution way in LaTeX/MathJax with an align environment.

  • lprob converts !A to \\bar{A} and A^B to A \\cap B.

Usage

prob_solve(target, ...)

# S3 method for default prob_solve(target, ..., partition = NULL, getprob = FALSE, quiet = TRUE)

lprob(txt)

# S3 method for prob_solve toLatex(object, ...)

# S3 method for prob_solve print(x, type = c("numeric", "latex", "prob", "compute"), ...)

latex_prob(txt)

probability_solution(target, ...)

sprob(target, ...)

Value

An object of the class prob_solve with the resulting probability, including the steps for computing. If NA is returned then no solution could be found.

Arguments

target

character: target event

...

numeric: named events with given probabilities

partition

character or list: set of events which form a partition

getprob

logical: return all computed probabilities and used computation steps (default: FALSE)

quiet

logical: show all computation steps (default: FALSE)

txt

character: vector to convert ! to \\bar and ^ to `\cap'

object, x

prob_solve object

type

character: what to print, either numeric (solution, default), latex (solution steps in ASCII format), prob (optional: all probabilities computed), or compute (optional: all rules used)

Details

The program applies iteratively the following rules to find a solution:

  • \(P(A) = 1-P(!A)\),

  • \(P(A|B) = 1-P(!A|B)\),

  • \(P(A^B) = P(B^A)\),

  • \(P(B) = P(A^B)+P(!A^B)\),

  • \(P(A|B) = P(A^B)/P(B)\), and

  • \(P(A) = P(A|P1)+P(A|P2)+...+ P(A|Pn)\) for a partition \(P1, P2, ..., Pn\).

Examples

Run this code
prob_solve("!A", "A"=0.3)
prob_solve("!A|B", "A|B"=0.3)
prob_solve("B^A", "A^B"=0.3)
# P(B)   = P(A^B)+P(!A^B)
prob_solve("B", "A^B"=0.3, "!A^B"= 0.4)
prob_solve("A^B", "B"=0.7, "!A^B"= 0.4)
prob_solve("!A^B", "B"=0.7, "A^B"= 0.3)
# P(A|B) = P(A^B)/P(B)
prob_solve("A|B", "A^B"=0.3, "B"= 0.6)
prob_solve("A^B", "B"=0.6, "A|B"= 0.5)
prob_solve("B", "A|B"=0.5, "A^B"= 0.3)
#' latex, prob and compute attributes
pmt <- prob_solve("M|T", "M"=0.6, "T|M"=0.75, "T|!M"=0.39, quiet=FALSE, getprob=TRUE)
toLatex(pmt)
attr(pmt, "latex")
pmt <- prob_solve("M|T", "M"=0.6, "T|M"=0.75, "T|!M"=0.39, quiet=FALSE, getprob=TRUE)
attr(pmt, "prob")
print(pmt, "latex") 
print(pmt, "prob")    # only if getprob=TRUE   
print(pmt, "compute") # only if getprob=TRUE   
# bayes theorem and total probability
prob_solve("Z", "Z|A"=0.1, "Z|B"=0.2, "Z|C"=0.3, partition=c("A", "B", "C"))
prob_solve("Z|A", "Z"=0.6, "Z|B"=0.2, "Z|C"=0.3, partition=c("A", "B", "C"))
prob_solve('A|K', "A"=0.55, "B"=0.35, "C"=0.1, "K|A"=0.4, "K|B"=0.1, "K|C"=0.1, 
           partition=c("A", "B", "C"))
prob_solve('K', "A"=0.55, "B"=0.35, "C"=0.1, "K|A"=0.4, "K|B"=0.1, "K|C"=0.1, 
           partition=c("A", "B", "C"))

Run the code above in your browser using DataLab