BayesBase.jl
BayesBase is a package that serves as an umbrella, defining, exporting, and re-exporting methods essential for Bayesian statistics specifically for the RxInfer ecosystem.
Related projects:
Index
BayesBase.ArrowheadMatrixBayesBase.ClosedProdBayesBase.ContingencyBayesBase.ContinuousMultivariateLogPdfBayesBase.ContinuousUnivariateLogPdfBayesBase.CountingRealBayesBase.DiscreteUnivariateLogPdfBayesBase.FactorizedJointBayesBase.GenericProdBayesBase.InplaceLogpdfBayesBase.InvArrowheadMatrixBayesBase.LinearizedProductOfBayesBase.MixtureDistributionBayesBase.PointMassBayesBase.PreserveTypeLeftProdBayesBase.PreserveTypeProdBayesBase.PreserveTypeRightProdBayesBase.ProductOfBayesBase.SampleListBayesBase.TerminalProdArgumentBayesBase.UnspecifiedDimensionBayesBase.UnspecifiedDomainBayesBase.UnspecifiedProdBase.prodBayesBase.InfinityBayesBase.MinusInfinityBayesBase.clamplogBayesBase.convert_paramfloattypeBayesBase.deep_eltypeBayesBase.default_prod_ruleBayesBase.distribution_typewrapperBayesBase.dtanhBayesBase.fuse_supportsBayesBase.isequal_typeofBayesBase.logmvbetaBayesBase.logpdf_optimizedBayesBase.logpdf_sampling_optimizedBayesBase.mcov!BayesBase.mean_covBayesBase.mean_invcovBayesBase.mean_precisionBayesBase.mean_stdBayesBase.mean_varBayesBase.mirrorlogBayesBase.mvtrigammaBayesBase.paramfloattypeBayesBase.probvecBayesBase.promote_paramfloattypeBayesBase.promote_samplefloattypeBayesBase.promote_sampletypeBayesBase.promote_variate_typeBayesBase.resolve_prod_strategyBayesBase.samplefloattypeBayesBase.sampletypeBayesBase.sampling_optimizedBayesBase.vagueBayesBase.weightedmeanBayesBase.weightedmean_covBayesBase.weightedmean_invcovBayesBase.weightedmean_stdBayesBase.weightedmean_varBayesBase.xtlog
Library API
Generic densities
BayesBase.PointMass — Type
PointMass(point)A PointMass structure represents a delta distribution, a discrete probability distribution where all probability mass is concentrated at a single point. This point is specified by the provided point.
BayesBase.ContinuousUnivariateLogPdf — Type
ContinuousUnivariateLogPdf{ D <: DomainSets.Domain, F } <: AbstractContinuousGenericLogPdfGeneric continuous univariate distribution in a form of domain specification and logpdf function. Can be used in cases where no known analytical distribution available.
Arguments
domain: domain specificatiom fromDomainSets.jlpackage, by default thedomainis set toDomainSets.FullSpace(). UseBayesBase.UnspecifiedDomain()to bypass domain checks.logpdf: callable object that represents the logdensity. Can be un-normalised.
BayesBase.ContinuousMultivariateLogPdf — Type
ContinuousMultivariateLogPdf{ D <: DomainSets.Domain, F } <: AbstractContinuousGenericLogPdfGeneric continuous multivariate distribution in a form of domain specification and logpdf function. Can be used in cases where no known analytical distribution available.
Arguments
domain: multidimensional domain specification fromDomainSets.jlpackage. UseBayesBase.UnspecifiedDomain()to bypass domain checks.logpdf: callable object that accepts anAbstractVectoras an input and represents the logdensity. Can be un-normalised.
BayesBase.DiscreteUnivariateLogPdf — Type
DiscreteUnivariateLogPdf{ D <: DomainSets.Domain, F } <: AbstractDiscreteGenericLogPdfGeneric discrete univariate distribution in a form of domain specification and logpdf function. Can be used in cases where no known analytical distribution available.
Arguments
domain: domain specificatiom fromDomainSets.jlpackage, by default thedomainis set toDomainSets.Integers(). UseBayesBase.UnspecifiedDomain()to bypass domain checks.logpdf: callable object that represents the logdensity. Can be un-normalised.
BayesBase.SampleList — Type
SampleListGeneric distribution represented as a list of weighted samples.
Arguments
samples::Sweights::W: optional, equivalent tofill(1 / N, N)by default, whereNis the length ofsamplescontainer
BayesBase.FactorizedJoint — Type
FactorizedJoint(components)FactorizedJoint represents a joint distribution of independent random variables. Use component() function or square-brackets indexing to access the marginal distribution for individual variables. Use components() function to get a tuple of multipliers.
BayesBase.MixtureDistribution — Type
MixtureDistribution(components, weights)A custom mixture distribution implementation, parameterized by:
Ctype family of the mixtureCTthe type for the weights
This implementation solves:
BayesBase.Contingency — Type
Contingency(P, renormalize = Val(true))The contingency distribution is a multivariate generalization of the categorical distribution. As a bivariate distribution, the contingency distribution defines the joint probability over two unit vectors v1 and v2. The parameter P encodes a contingency matrix that specifies the probability of co-occurrence.
v1 ∈ {0, 1}^d1 where Σ_j v1_j = 1
v2 ∈ {0, 1}^d2 where Σ_k v2_k = 1
P ∈ [0, 1]^{d1 × d2}, where Σ_jk P_jk = 1
f(v1, v2, P) = Contingency(out1, out2 | P) = Π_jk P_jk^{v1_j * v2_k}A Contingency distribution over more than two variables requires higher-order tensors as parameters; these are not implemented in ReactiveMP.
Arguments:
P, required, contingency matrixrenormalize, optional, supports eitherVal(true)orVal(false), specifies whether matrixPmust be automatically renormalized. Does not modify the originalPand allocates a new one for the renormalized version. If set tofalsethe contingency matrixPmust be normalized by hand, otherwise the result of related calculations might be wrong
Product API
The prod function defines an interface to compute a product between two probability distributions over the same variable. It accepts a strategy as its first argument, which defines how the prod function should behave and what results you can expect.
Base.prod — Method
prod(strategy, left, right)prod function is used to find a product of two probability distributions (or any other objects) over same variable (e.g. 𝓝(x|μ1, σ1) × 𝓝(x|μ2, σ2)). There are multiple strategies for prod function, e.g. ClosedProd, GenericProd or PreserveTypeProd.
See also: default_prod_rule, ClosedProd, PreserveTypeProd, GenericProd
BayesBase.default_prod_rule — Function
default_prod_rule(::Type, ::Type)Returns the most suitable prod rule for two given distribution types. Returns UnspecifiedProd by default.
See also: prod, ClosedProd, GenericProd
Product strategies
For certain distributions, it's possible to compute the product using a straightforward mathematical equation, yielding a closed-form solution. However, for some distributions, finding a closed-form solution might not be feasible. Various strategies ensure consistent behavior in these situations. These strategies can either guarantee a fast and closed-form solution or, when necessary, fall back to a slower but more generic method.
BayesBase.UnspecifiedProd — Type
UnspecifiedProdA strategy for the prod function, which does not compute the prod, but instead fails in run-time and prints a descriptive error message.
See also: prod, ClosedProd, GenericProd
BayesBase.ClosedProd — Type
ClosedProdClosedProd is one of the strategies for prod function. For example, if both inputs are of type Distribution, then ClosedProd would fallback to PreserveTypeProd(Distribution).
See also: prod, PreserveTypeProd, GenericProd
BayesBase.PreserveTypeProd — Type
PreserveTypeProd{T}PreserveTypeProd is one of the strategies for prod function. This strategy constraint an output of a prod to be in some specific form. By default it uses the strategy from default_prod_rule and converts the output to the prespecified type but can be overwritten for some distributions for better performance.
See also: prod, ClosedProd, PreserveTypeLeftProd, PreserveTypeRightProd, GenericProd
BayesBase.PreserveTypeLeftProd — Type
PreserveTypeLeftProdAn alias for the PreserveTypeProd(L) where L is the type of the left argument of the prod function.
See also: prod, PreserveTypeProd, PreserveTypeRightProd, GenericProd
BayesBase.PreserveTypeRightProd — Type
PreserveTypeRightProdAn alias for the PreserveTypeProd(R) where R is the type of the right argument of the prod function.
See also: prod, PreserveTypeProd, PreserveTypeLeftProd, GenericProd
BayesBase.GenericProd — Type
GenericProdGenericProd is one of the strategies for prod function. This strategy does always produces a result, even if the closed form product is not availble, in which case simply returns the ProductOf object. GenericProd sometimes fallbacks to the default_prod_rule which it may or may not use under some circumstances. For example if the default_prod_rule is ClosedProd - GenericProd will try to optimize the tree with analytical closed solutions (if possible).
See also: prod, ProductOf, ClosedProd, PreserveTypeProd, default_prod_rule
BayesBase.ProductOf — Type
ProductOfA generic structure representing a product of two distributions. Can be viewed as a tuple of (left, right). Does not check nor supports neither variate forms during the creation stage. Uses the fuse_support function to fuse supports of two different distributions.
This object does not define any statistical properties (such as mean or var etc) and cannot be used as a distribution explicitly. Instead, it must be further approximated as a member of some other distribution.
See also: prod, GenericProd, fuse_supports
BayesBase.LinearizedProductOf — Type
LinearizedProductOfAn efficient linearized implementation of product of multiple distributions. This structure prevents ProductOf tree from growing too much in case of identical objects. This trick significantly reduces Julia compilation times when closed product rules are not available but distributions are of the same type. Essentially this structure linearizes leaves of the ProductOf tree in case if it sees objects of the same type (via dispatch).
See also: ProductOf, [GenericProd]
BayesBase.TerminalProdArgument — Type
TerminalProdArgument(argument)TerminalProdArgument is a specialized wrapper structure. When used as an argument to the prod function, it returns itself without considering any product strategy and does not perform any safety checks (e.g. variate_form or support). Attempting to calculate the product of two instances of TerminalProdArgument will raise an error. Use .argument field to get the underlying wrapped argument.
BayesBase.resolve_prod_strategy — Function
resolve_prod_strategy(left, right)Given two strategies, this function returns the one with higher priority, if possible.
These strategies offer flexibility and reliability when working with different types of distributions, ensuring that the package can handle a wide range of cases effectively.
Promotion type utilities
BayesBase.deep_eltype — Function
deep_eltype(T)Returns:
deep_eltypeofTifTis anAbstractArraycontainerTotherwise
julia> deep_eltype(Float64)
Float64
julia> deep_eltype(Vector{Float64})
Float64
julia> deep_eltype(Vector{Matrix{Vector{Float64}}})
Float64BayesBase.isequal_typeof — Function
isequal_typeof(left, right)Alias for typeof(left) === typeof(right), but can be specialized.
BayesBase.paramfloattype — Function
paramfloattype(distribution)Returns the underlying float type of distribution's parameters.
See also: promote_paramfloattype, convert_paramfloattype
BayesBase.sampletype — Function
sampletype(distribution)Returns a type of the distribution. By default fallbacks to the eltype.
See also: samplefloattype, promote_sampletype, promote_samplefloattype
BayesBase.samplefloattype — Function
samplefloattype(distribution)Returns a type of the distribution or the underlying float type in case if sample is Multivariate or Matrixvariate. By default fallbacks to the deep_eltype(sampletype(distribution)).
See also: sampletype, promote_sampletype, promote_samplefloattype
BayesBase.promote_variate_type — Function
promote_variate_type(::Type{ <: VariateForm }, distribution_type)Promotes (if possible) a distribution_type to be of the specified variate form.
BayesBase.promote_paramfloattype — Function
promote_paramfloattype(distributions...)Promotes paramfloattype of the distributions to a single type. See also promote_type.
See also: paramfloattype, convert_paramfloattype
BayesBase.promote_sampletype — Function
promote_sampletype(distributions...)Promotes sampletype of the distributions to a single type. See also promote_type.
See also: sampletype, samplefloattype, promote_samplefloattype
BayesBase.promote_samplefloattype — Function
promote_samplefloattype(distributions...)Promotes samplefloattype of the distributions to a single type. See also promote_type.
See also: sampletype, samplefloattype, promote_sampletype
BayesBase.convert_paramfloattype — Function
convert_paramfloattype(::Type{T}, distribution)Converts (if possible) the params float type of the distribution to be of type T.
See also: paramfloattype, promote_paramfloattype
convert_paramfloattype(::Type{T}, container)Converts (if possible) the elements of the container to be of type T.
Extra stats functions
BayesBase.mirrorlog — Function
mirrorlog(x)Returns log(1 - x).
BayesBase.xtlog — Function
xtlog(x)Returns x * log(x).
BayesBase.logmvbeta — Function
logmvbeta(x)Uses the numerically stable algorithm to compute the logarithm of the multivariate beta distribution over with the parameter vector x.
BayesBase.clamplog — Function
clamplog(x)Same as log but clamps the input argument x to be in the range tiny <= x <= typemax(x) such that log(0) does not explode.
BayesBase.mvtrigamma — Function
mvtrigamma(p, x)Computes multivariate trigamma function .
BayesBase.dtanh — Function
dtanh(x)Alias for 1 - tanh(x) ^ 2
BayesBase.probvec — Function
probvec(d)Returns the probability vector of the given distribution.
BayesBase.mcov! — Function
mcov!(Z, X::AbstractMatrix, Y::AbstractMatrix; tmp1 = zeros(eltype(Z), size(X, 2)), tmp2 = zeros(eltype(Z), size(Y, 2)), tmp3 = similar(X), tmp4 = similar(Y))Same as Statistics.cov(X, Y), but does not allocate the result. Instead uses a buffer Z to store the result in. Additionally, it allows for passing temporary buffers tmp1, tmp2, tmp3, tmp4 to avoid any allocations. Always computes corrected = true covariance matrix.
BayesBase.mean_std — Function
Alias for (mean(d), std(d)), but can be specialized.
BayesBase.mean_var — Function
Alias for (mean(d), var(d)), but can be specialized.
BayesBase.mean_cov — Function
Alias for (mean(d), cov(d)), but can be specialized.
BayesBase.mean_invcov — Function
Alias for (mean(d), invcov(d)), but can be specialized.
BayesBase.mean_precision — Function
Alias for mean_invcov(d), but can be specialized.
BayesBase.weightedmean — Function
weightedmean(d)Returns the weighted mean of the given distribution. Alias to invcov(d) * mean(d), but can be specialized
BayesBase.weightedmean_std — Function
Alias for (weightedmean(d), std(d)), but can be specialized.
BayesBase.weightedmean_var — Function
Alias for (weightedmean(d), var(d)), but can be specialized.
BayesBase.weightedmean_cov — Function
Alias for (weightedmean(d), cov(d)), but can be specialized.
BayesBase.weightedmean_invcov — Function
Alias for weightedmean_invcov(d), but can be specialized.
Extra matrix structures
BayesBase.ArrowheadMatrix — Type
ArrowheadMatrix{O, T, Z, P} <: AbstractMatrix{O}A structure representing an arrowhead matrix, which is a special type of sparse matrix.
Fields
α::T: The scalar value at the bottom-right corner of the matrix.z::Z: A vector representing the last row/column (excluding the corner element).D::P: A vector representing the diagonal elements (excluding the corner element).
Constructors
ArrowheadMatrix(a::T, z::Z, d::D) where {T,Z,D}Constructs an ArrowheadMatrix with the given α, z, and D values. The output type O is automatically determined as the promoted type of all input elements.
Operations
- Matrix-vector multiplication:
A * xormul!(y, A, x) - Linear system solving:
A \ borldiv!(x, A, b) - Conversion to dense matrix:
convert(Matrix, A) - Inversion:
inv(A)(returns anInvArrowheadMatrix)
Examples
α = 2.0
z = [1.0, 2.0, 3.0]
D = [4.0, 5.0, 6.0]
A = ArrowheadMatrix(α, z, D)
# Matrix-vector multiplication
x = [1.0, 2.0, 3.0, 4.0]
y = A * x
# Solving linear system
b = [7.0, 8.0, 9.0, 10.0]
x = A \ b
# Convert to dense matrix
dense_A = convert(Matrix, A)Notes
- The matrix is singular if α - dot(z ./ D, z) = 0 or if any element of D is zero.
- For best performance, use
ldiv!for solving linear systems when possible.
BayesBase.InvArrowheadMatrix — Type
InvArrowheadMatrix{O, T, Z, P} <: AbstractMatrix{O}A wrapper structure representing the inverse of an ArrowheadMatrix.
This structure doesn't explicitly compute or store the inverse matrix. Instead, it stores a reference to the original ArrowheadMatrix and implements efficient operations that leverage the special structure of the arrowhead matrix.
Fields
A::ArrowheadMatrix{O, T, Z, P}: The originalArrowheadMatrixbeing inverted.
Constructors
InvArrowheadMatrix(A::ArrowheadMatrix{O, T, Z, P})Constructs an InvArrowheadMatrix by wrapping the given ArrowheadMatrix.
Operations
- Matrix-vector multiplication:
A_inv * xormul!(y, A_inv, x)(Equivalent to solving the system A * y = x) - Linear system solving:
A_inv \ x(Equivalent to multiplication by the original matrix: A * x) - Conversion to dense matrix:
convert(Matrix, A_inv)(Computes and returns the actual inverse as a dense matrix)
Examples
α = 2.0
z = [1.0, 2.0, 3.0]
D = [4.0, 5.0, 6.0]
A = ArrowheadMatrix(α, z, D)
A_inv = inv(A) # Returns an InvArrowheadMatrix
# Multiplication (equivalent to solving A * y = x)
x = [1.0, 2.0, 3.0, 4.0]
y = A_inv * x
# Division (equivalent to multiplying by A)
b = [5.0, 6.0, 7.0, 8.0]
x = A_inv \ b
# Convert to dense inverse matrix
dense_inv_A = convert(Matrix, A_inv)Notes
- The inverse exists only if the original
ArrowheadMatrixis non-singular. - Operations with
InvArrowheadMatrixdo not explicitly compute the inverse, but instead solve the corresponding system with the original matrix.
See Also
ArrowheadMatrix: The original arrowhead matrix structure.
Helper utilities
BayesBase.vague — Function
vague(distribution_type, [ dims... ])Returns uninformative probability distribution of the given type.
BayesBase.logpdf_sampling_optimized — Function
logpdf_sampling_optimized(d)logpdf_sample_optimized function takes as an input a distribution d and returns corresponding optimized two versions for taking logpdf() and sampling with rand! respectively. Alias for (logpdf_optimized(d), sampling_optimized(d)), but can be specialized.
BayesBase.logpdf_optimized — Function
logpdf_optimized(d)Returns a version of d specifically optimized to call logpdf(d, x). By default returns the same d, but can be specialized.
BayesBase.sampling_optimized — Function
sampling_optimized(d)Returns a version of d specifically optimized to call rand and rand!. By default returns the same d, but can be specialized.
BayesBase.fuse_supports — Function
fuse_supports(left, right)Fuses supports left and right. By default, checks that the inputs are identical and throws an error otherwise. Can implement specific fusions for specific supports.
BayesBase.UnspecifiedDomain — Type
Unknown domain that is used as a placeholder when exact domain knowledge is unavailable
BayesBase.UnspecifiedDimension — Type
Unknown dimension is equal and not equal to any number
BayesBase.distribution_typewrapper — Function
Strips type parameters from the type of the distribution.
BayesBase.CountingReal — Type
CountingRealCountingReal implements a real "number" that counts 'infinities' in a separate field. See also BayesBase.Infinity and BayesBase.MinusInfinity.
Arguments
value::T: value of type<: Realinfinities::Int: number of added/subtracted infinities
julia> r = BayesBase.CountingReal(0.0, 0)
CountingReal{Float64}(0.0, 0)
julia> float(r)
0.0
julia> r = r + BayesBase.Infinity(Float64)
CountingReal{Float64}(0.0, 1)
julia> float(r)
Inf
julia> r = r + BayesBase.MinusInfinity(Float64)
CountingReal{Float64}(0.0, 0)
julia> float(r)
0.0BayesBase.Infinity — Function
An object representing infinity.
BayesBase.MinusInfinity — Function
An object representing minus infinity.
BayesBase.InplaceLogpdf — Type
InplaceLogpdf(logpdf!)
Wraps a logpdf! function in a type that can later on be used for dispatch. The sole purpose of this wrapper type is to allow for in-place logpdf operation on a batch of samples. Accepts a function logpdf! that takes two arguments: out and sample and writes the logpdf of the sample to the out array. A regular logpdf function can be converted to logpdf! by using convert(InplaceLogpdf, logpdf).
julia> using Distributions, BayesBase
julia> d = Beta(2, 3);
julia> inplace = convert(BayesBase.InplaceLogpdf, (sample) -> logpdf(d, sample));
julia> out = zeros(9);
julia> inplace(out, 0.1:0.1:0.9)
9-element Vector{Float64}:
-0.028399474521697776
0.42918163472548043
0.5675839575845996
0.5469646703818638
0.4054651081081646
0.14149956227369964
-0.2797139028026039
-0.9571127263944104
-2.2256240518579173julia> using Distributions, BayesBase
julia> d = Beta(2, 3);
julia> inplace = BayesBase.InplaceLogpdf((out, sample) -> logpdf!(out, d, sample));
julia> out = zeros(9);
julia> inplace(out, 0.1:0.1:0.9)
9-element Vector{Float64}:
-0.028399474521697776
0.42918163472548043
0.5675839575845996
0.5469646703818638
0.4054651081081646
0.14149956227369964
-0.2797139028026039
-0.9571127263944104
-2.2256240518579173