Distributions.jl

repository·master·Indexed 22 days ago

https://github.com/juliastats/distributions.jl

A core Julia package providing a comprehensive suite of tools for probability distributions. It includes a unified interface for calculating moments (mean, variance, skewness, kurtosis), entropy, probability density/mass functions (pdf, logpdf), and sampling. The package supports Maximum Likelihood Estimation (MLE) via fit() and fit_mle(), distribution convolution, and the creation of censored distributions. It also provides frameworks for implementing custom univariate, multivariate, and matrix-variate distributions and samplers, and integrates with the DensityInterface.jl ecosystem.

Tokens
10K
Snippets
26
Records
56
Agent score
79%

What's inside Distributions.jl

  1. Overview of the Distributions.jl package

    master

    The Distributions.jl package provides a comprehensive collection of probabilistic distributions and statistical functions for Julia. It is designed to handle common tasks in probabilistic modeling and statistical analysis, including:

    • Sampling: Generating random samples from various distributions.
    • Statistical Properties: Calculating moments (mean, variance, skewness, kurtosis), entropy, and other properties.
    • Probability Functions: Computing probability density/mass functions (pdf) and their logarithms (logpdf).
    • Generating Functions: Working with moment-generating and characteristic functions.
    • Estimation: Performing Maximum Likelihood Estimation (MLE).
    • Distribution Composition: Creating complex distributions via Cartesian products, truncation, or censoring.
  2. Overview of Distributions.jl

    master

    Distributions.jl is a Julia package for probability distributions and associated statistical functions. It provides a unified interface for:

    • Moments: Calculating properties like mean, variance, skewness, and kurtosis.
    • Entropy: Computing entropy and other statistical properties.
    • Probability Functions: Accessing probability density/mass functions (pdf) and their logarithms (logpdf).
    • Generating Functions: Working with moment generating functions and characteristic functions.
    • Sampling: Drawing samples from a population or directly from a distribution.
    • Estimation: Performing Maximum Likelihood Estimation (MLE).

    Note: Functionalities related to conjugate priors are no longer in this package and have been moved to the ConjugatePriors.jl package.

  3. Understand Univariate Distributions

    master

    In Distributions.jl, univariate distributions are distributions where each sample is a scalar (Univariate).

    There are two primary abstract types for univariate distributions:

    • DiscreteUnivariateDistribution: Distributions with discrete outcomes.
    • ContinuousUnivariateDistribution: Distributions with continuous outcomes.

    All univariate distributions follow the UnivariateDistribution{S<:ValueSupport} structure.

  4. Use DensityInterface with Distributions.jl

    master

    The Distributions.jl package provides support for the DensityInterface.jl ecosystem. This allows you to use standard DensityInterface functions on any distribution object.

    For any distribution d and single variate value x:

    • DensityInterface.densityof(d, x) is equivalent to pdf(d, x).
    • DensityInterface.logdensityof(d, x) is equivalent to logpdf(d, x).

    All distributions in this package are recognized by DensityInterface as having a density via the check: DensityInterface.DensityKind(::Distribution) === HasDensity().

    using Distributions
    using DensityInterface
    
    d = Normal(0, 1)
    x = 0.5
    
    # These are equivalent
    density = densityof(d, x) # same as pdf(d, x)
    log_density = logdensityof(d, x) # same as logpdf(d, x)
  5. Common interface for multivariate distributions

    master

    Multivariate distributions in Distributions.jl are distributions where each sample is a vector (Multivariate). They follow a consistent interface for statistical computation, probability evaluation, and sampling.

    Statistical Computation

    You can extract the following properties and statistics from any MultivariateDistribution:

    • length(d): The dimension of the distribution.
    • size(d): The size of the distribution.
    • eltype(Type{d}): The element type.
    • mean(d): The mean vector.
    • var(d): The variance.
    • std(d): The standard deviation.
    • cov(d): The covariance matrix.
    • cor(d): The correlation matrix.
    • entropy(d) or entropy(d, real_val): The entropy.

    Probability Evaluation

    Note: Because PDF values for multivariate distributions can be extremely small or large, it is highly recommended to use log-scale computations to avoid numerical instability.

    • insupport(d, x): Checks if vector x is in the support of the distribution.
    • pdf(d, x): Probability density function at x.
    • logpdf(d, x): Log-probability density function at x.
    • loglikelihood(d, vector): Log-likelihood for a vector of observations.

    Sampling

    • rand(rng, d): Generates a random sample from the distribution using the provided RNG.
    • rand!(rng, d, x): In-place sampling into the existing array x.
  6. Understand the Sampleable type hierarchy

    master

    The core of Distributions.jl is the Sampleable abstract type. It represents any object from which samples can be drawn, including both samplers and distributions.

    Sampleable is parameterized by two types:

    1. VariateForm: Defines the shape of the samples (e.g., scalar, vector, or matrix).
    2. ValueSupport: Defines the nature of the values (e.g., discrete or continuous).

    All functions applicable to Sampleable are also applicable to Distribution objects.

    abstract type Sampleable{F<:VariateForm,S<:ValueSupport} end
  7. Understand Matrix-variate Distributions

    master

    Matrix-variate distributions are distributions where each sample is a matrix (represented by the Matrixvariate type). In Distributions.jl, these are categorized under the abstract type MatrixDistribution{S<:ValueSupport}.

    There are two primary sub-types:

    • DiscreteMatrixDistribution: For discrete matrix-valued distributions.
    • ContinuousMatrixDistribution: For continuous matrix-valued distributions.

    For advanced random matrix functionalities, consider using the RandomMatrices.jl package.

  8. Use sufficient statistics for distribution estimation

    master

    For many distributions, estimation can be performed using sufficient statistics rather than the raw dataset. This is useful for efficiency or when working with pre-aggregated data.

    1. Compute sufficient statistics using suffstats(D, x) or suffstats(D, x, w) for weighted data.
    2. Pass those statistics to fit_mle(D, ss) to obtain the fitted distribution.

    Note: When calling fit_mle(D, x) directly, the package often internally calls suffstats as a fallback unless a more efficient specialized implementation exists.

    # 1. Capture sufficient statistics
    ss = suffstats(Normal, x)
    
    # 2. Estimate distribution from statistics
    d = fit_mle(Normal, ss)
  9. Compute statistics for truncated distributions

    master

    Functions to compute descriptive statistics such as mean, mode, var, std, and entropy are not available for generic truncated distributions because these quantities are often difficult to compute due to truncation complications.

    Exception: These methods are supported specifically for truncated normal distributions, which can be constructed using truncated(::Normal, ...). For these distributions, Truncated{<:Normal} will support standard statistical methods.

  10. Explore different distribution types

    master

    The package supports three main categories of distributions, each of which can be Discrete or Continuous:

    1. Univariate (ArrayLikeVariate{0}): e.g., Binomial(n, p) (Discrete) or Cauchy(u, b) (Continuous).
    2. Multivariate (ArrayLikeVariate{1}): e.g., Multinomial(n, p).
    3. Matrixvariate (ArrayLikeVariate{2}): e.g., Wishart(nu, S).

    You can also create truncated distributions from existing univariate distributions using the truncated(dist, l, u) function, where l and u are the lower and upper bounds.

    # Example of different types
    b = Binomial(n, p)
    c = Cauchy(u, b)
    m = Multinomial(n, p)
    w = Wishart(nu, S)
    
    # Truncated distribution
    t = truncated(Normal(0, 1), -1, 1)