Having a probability distribution \(\pi(K)\), and a subset of variables \(L \subseteq K\), \(\pi^{\downarrow L}\) denotes a marginal distribution of \(\pi\) defined for each \(x \in X_L\) by the formula $$\pi^{\downarrow L}(x) = \sum_{y \in X_K; y^{\downarrow L} = x}\pi(y).$$ Note that we do not exclude situations when \(L = \emptyset\), for which we get \(\pi^{\downarrow \emptyset} = 1\).

In case of a compositional model, the marginalization is a complicated process.

marginalize(x, variables = NULL, keep = TRUE, new = TRUE, ...)

# S3 method for Distribution
marginalize(x, variables = NULL, keep = TRUE,
  new = TRUE, ...)

# S3 method for Model
marginalize(x, variables = NULL, keep = TRUE,
  perfect = FALSE, new = TRUE, ...)

Arguments

x

Distribution, Compositional model

variables

vector of variables

keep

logical, if TRUE, respective variables are kept, if FALSE the variables are marginalized out

new

logical, if TRUE a new distribution/compositional model is created. Otherwise the current one is changed.

perfect

logical, if TRUE the algorithm expects a perfect sequence in model and its behavior is different

Value

If input is:

  • Distribution return marginalized Distribution

  • Model return marginalized Model

Details

To obtain the marginal distribution over a subset of random variables, one only needs to drop the irrelevant variables (the variables that one wants to marginalize out).

In case of having a compositional model, the situation is more complex and there are many approaches how to speed up the process.

Variables in variables will be marginalized out if the keep parameter will be FALSE. In the opposite case, variables in variables will stay in Distribution. If the new parameter is TRUE then a new Distribution is created and the original one stays unchanged. In the other case, the original distribution referenced in parameter x is changed and marginalized.

Methods (by class)

  • Distribution: Marginalize probability distribution

  • Model: Marginalize Compositional model

See also

Examples

# Distribution class data(Pi) variables(Pi);
#> [1] "A" "B"
PiMarginal <- marginalize(Pi, variables = c("A"), keep = TRUE, new = TRUE); dTable(PiMarginal);
#> A MUDIM.frequency #> 1: 0 0.3 #> 2: 1 0.7
dTable(Pi)
#> A B MUDIM.frequency #> 1: 0 0 0.1 #> 2: 0 1 0.2 #> 3: 1 0 0.3 #> 4: 1 1 0.4
marginalize(Pi, variables = "A", new = FALSE)
#> Probability distribution #> * Name:pi #> * Info: #> * Variables:A #> * Non-empty items:2 #> NULL
dTable(Pi)
#> A MUDIM.frequency #> 1: 0 0.3 #> 2: 1 0.7
# Model class data(m) m
#> Compositional model #> * Name:demo model #> * Info:created for demonstration of the mudim package functionality #> * Variables:D, N, R, T, W, U, B #> * Length:7 #> NULL
#> [1] "D" "N" "R" "T" "W" "U" "B"
m2 <- marginalize(m, variables = variables(m)[1:3], keep = FALSE)