Multivariate counts and probabilities API

For counting and probabilities, Associations.jl extends the single-variable machinery in ComplexityMeasures.jl to multiple variables. See the following types:

ComplexityMeasures.countsMethod
counts(o::UniqueElements, x₁, x₂, ..., xₙ) → Counts{N}
counts(encoding::CodifyPoints, x₁, x₂, ..., xₙ) → Counts{N}
counts(encoding::CodifyVariables, x₁, x₂, ..., xₙ) → Counts{N}

Construct an N-dimensional contingency table from the input iterables x₁, x₂, ..., xₙ which are such that length(x₁) == length(x₂) == ⋯ == length(xₙ).

If x₁, x₂, ..., xₙ are already discrete, then use UniqueElements as the first argument to directly construct the joint contingency table.

If x₁, x₂, ..., xₙ need to be discretized, provide as the first argument

  • CodifyPoints (encodes every point in each of the input variables xᵢs individually)
  • CodifyVariables (encodes every xᵢ individually using a sliding window encoding). NB: If using different OutcomeSpaces for the different xᵢ, then total_outcomes must be the same for every outcome space.

Examples

# Discretizing some non-discrete data using a sliding-window encoding for each variable
x, y = rand(100), rand(100)
c = CodifyVariables(OrdinalPatterns(m = 4))
counts(c, x, y)

# Discretizing the data by binning each individual data point
binning = RectangularBinning(3)
encoding = RectangularBinEncoding(binning, [x; y]) # give input values to ensure binning covers all data
c = CodifyPoints(encoding)
counts(c, x, y)

# Counts table for already discrete data
n = 50 # all variables must have the same number of elements
x = rand(["dog", "cat", "mouse"], n)
y = rand(1:3, n)
z = rand([(1, 2), (2, 1)], n)

counts(UniqueElements(), x, y, z)

See also: CodifyPoints, CodifyVariables, UniqueElements, OutcomeSpace, probabilities.

source
ComplexityMeasures.probabilitiesMethod
probabilities(o::UniqueElements, x₁, x₂, ..., xₙ) → Counts{N}
probabilities(encoding::CodifyPoints, x₁, x₂, ..., xₙ) → Counts{N}
probabilities(encoding::CodifyVariables, x₁, x₂, ..., xₙ) → Counts{N}

Construct an N-dimensional Probabilities array from the input iterables x₁, x₂, ..., xₙ which are such that length(x₁) == length(x₂) == ⋯ == length(xₙ).

Description

Probabilities are computed by first constructing a joint contingency matrix in the form of a Counts instance.

If x₁, x₂, ..., xₙ are already discrete, then use UniqueElements as the first argument to directly construct the joint contingency table.

If x₁, x₂, ..., xₙ need to be discretized, provide as the first argument

  • CodifyPoints (encodes every point in each of the input variables xᵢs individually)
  • CodifyVariables (encodes every xᵢ individually using a sliding window encoding).

Examples

# Discretizing some non-discrete data using a sliding-window encoding for each variable
x, y = rand(100), rand(100)
c = CodifyVariables(OrdinalPatterns(m = 4))
probabilities(c, x, y)

# Discretizing the data by binning each individual data point
binning = RectangularBinning(3)
encoding = RectangularBinEncoding(binning, [x; y]) # give input values to ensure binning covers all data
c = CodifyPoints(encoding)
probabilities(c, x, y)

# Joint probabilities for already discretized data
n = 50 # all variables must have the same number of elements
x = rand(["dog", "cat", "mouse"], n)
y = rand(1:3, n)
z = rand([(1, 2), (2, 1)], n)

probabilities(UniqueElements(), x, y, z)

See also: CodifyPoints, CodifyVariables, UniqueElements, OutcomeSpace.

source

The utility function marginal is also useful.

Associations.marginalFunction
marginal(p::Probabilities; dims = 1:ndims(p))
marginal(c::Counts; dims = 1:ndims(p))

Given a set of counts c (a contingency table), or a multivariate probability mass function p, return the marginal counts/probabilities along the given dims.

source

Example: estimating Counts and Probabilities

Estimating multivariate counts (contingency matrices) and PMFs is simple. If the data are pre-discretized, then we can use UniqueElements to simply count the number of occurrences.

using Associations
n = 50 # the number of samples must be the same for each input variable
x = rand(["dog", "cat", "snake"], n)
y = rand(1:4, n)
z = rand([(2, 1), (0, 0), (1, 1)], n)
discretization = CodifyVariables(UniqueElements())
counts(discretization, x, y, z)
 3×4×3 Counts{Int64,3}
[:, :, 1]
          2  4  3  1
 "snake"  3  1  3  2
 "cat"    4  0  3  0
 "dog"    4  2  2  1
[and 2 more slices...]

Probabilities are computed analogously, except counts are normalized to sum to 1.

discretization = CodifyVariables(UniqueElements())
probabilities(discretization, x, y, z)
 3×4×3 Probabilities{Float64,3}
[:, :, 1]
          2     4     3     1
 "snake"  0.06  0.02  0.06  0.04
 "cat"    0.08  0.0   0.06  0.0
 "dog"    0.08  0.04  0.04  0.02
[and 2 more slices...]

For numerical data, we can estimate both counts and probabilities using CodifyVariables with any count-based OutcomeSpace.

using Associations
x, y = rand(100), rand(100)
discretization = CodifyVariables(BubbleSortSwaps(m = 4))
probabilities(discretization, x, y)
 7×7 Probabilities{Float64,2}
       2                     50
 1     0.02061855670103093   0.0                      0.010309278350515465
 3     0.04123711340206186   0.051546391752577324     0.04123711340206186
 2     0.030927835051546396  0.051546391752577324     0.010309278350515465
 4     0.04123711340206186   0.02061855670103093      0.010309278350515465
 5     0.02061855670103093   0.02061855670103093   …  0.0
 0     0.010309278350515465  0.0                      0.0
 6     0.0                   0.0                      0.0

For more fine-grained control, we can use CodifyPoints with one or several Encodings.

using Associations
x, y = StateSpaceSet(rand(1000, 2)), StateSpaceSet(rand(1000, 3))

 # min/max of the `rand` call is 0 and 1
precise = true # precise bin edges
r = range(0, 1; length = 3)
binning = FixedRectangularBinning(r, dimension(x), precise)
encoding_x = RectangularBinEncoding(binning, x)
encoding_y = CombinationEncoding(RelativeMeanEncoding(0.0, 1, n = 2), OrdinalPatternEncoding(3))
discretization = CodifyPoints(encoding_x, encoding_y)

# now estimate probabilities
probabilities(discretization, x, y)
 4×12 Probabilities{Float64,2}
       5      4      12      62      11      8      1      3
 1     0.023  0.021   0.015  0.022     0.025   0.028  0.027  0.023  0.019
 3     0.019  0.017   0.025  0.022     0.024   0.017  0.022  0.023  0.017
 2     0.015  0.019   0.02   0.022     0.013   0.015  0.015  0.025  0.022
 4     0.025  0.024   0.019  0.013     0.019   0.028  0.02   0.021  0.023