Use this file to discover all available pages before exploring further.
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.
;; Bernoulli: accepts a tensor of probabilities(dist/bernoulli (t/tensor [0.2 0.8]));; Categorical: one-hot selection from probability vector(dist/categorical (t/tensor [0.1 0.2 0.7]));; Binomial: n trials, probability p(dist/binomial 10.0 0.3);; Geometric: number of trials until first success(dist/geometric 0.4);; Negative binomial(dist/negative-binomial 5.0 0.3)
;; Uniform: continuous on [low, high](dist/uniform -1.0 2.0);; Poisson: count distribution with given rate(dist/poisson 3.0);; Exponential: waiting time with given rate(dist/exponential 2.0)
dist/sample accepts a distribution map and an optional sample-shape vector. The returned value is always a Clorch tensor.
(def d (dist/normal 0.0 1.0));; Single sample (shape [1])(dist/sample d);; Batch of 4 rows × 3 columns(dist/sample d [4 3]);; Large sample for Monte Carlo estimates(dist/sample d [10000])
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.
dist/log-prob evaluates the analytical log-density or log-mass at a given value. It accepts both scalar numbers and tensors.
(def d (dist/normal 0.0 1.0));; Scalar input(dist/log-prob d 0.0) ;; → tensor(-0.9189...);; Tensor input — returns element-wise log-prob(dist/log-prob d (t/tensor [-1.0 0.0 1.0]));; Log-likelihood over a batch(def data (dist/sample d [100]))(t/sum (dist/log-prob d data))
;; Evaluate how well a Gaussian fits some observed data(defn gaussian-log-likelihood [data mu sigma] (let [d (dist/normal mu sigma)] (t/item-float (t/sum (dist/log-prob d data)))))(def observations (t/tensor [1.2 0.8 1.5 1.1 0.9]))(gaussian-log-likelihood observations 1.1 0.3)