Skip to main content
The clorch.distributions namespace implements a probability distribution library with a data-first API. Each distribution is a plain Clojure map, which means it can be stored in atoms, serialized with EDN, and passed through any Clojure data-manipulation function without special wrappers. The four core operations — sample, log-prob, mean, and variance — accept these maps and dispatch on the :dist key.

Distribution Constructors

Every constructor returns a map. Parameters can be Clojure numbers or Clorch tensors.

Location–Scale Distributions

Discrete Distributions

Rate and Count Distributions

Shape Distributions

Statistical Test Distributions


Core Operations

Sampling

dist/sample accepts a distribution map and an optional sample-shape vector. The returned value is always a Clorch tensor.
sample-shape is supported for all numeric-parameter distributions. Distributions parameterized by tensors (e.g., bernoulli with a tensor probability) currently support single samples; multi-dimensional sample-shape is available for the common cases where parameters are scalars.
Categorical sampling delegates to torch/multinomial internally. The integer sample-shape specifies the number of draws; results are returned as an integer tensor of category indices.

Log-Probability

dist/log-prob evaluates the analytical log-density or log-mass at a given value. It accepts both scalar numbers and tensors.

Mean and Variance

Closed-form moments are computed analytically when they exist. Distributions whose moments are undefined (e.g., Cauchy, categorical) return nil.

Example Use Cases

Sampling from a Posterior

Computing Log-Likelihood of Data

Reparameterized Sampling for Variational Inference

Categorical Sampling for Token Generation


Implementation Notes