Skip to main content
kstats-distributions provides a unified API for continuous and discrete probability models. Every distribution supports the same workflow: construct with parameters, inspect statistical properties, evaluate probabilities, compute quantiles, and draw random samples.

Working with a Distribution

val normal = NormalDistribution(mu = 0.0, sigma = 1.0)

// Statistical properties
normal.mean                  // 0.0
normal.variance              // 1.0
normal.standardDeviation     // 1.0
normal.skewness              // 0.0
normal.kurtosis              // 0.0
normal.entropy               // 1.4189

// Evaluate
normal.pdf(0.0)              // 0.3989 — density at x = 0
normal.cdf(1.96)             // 0.9750 — P(X ≤ 1.96)
normal.sf(1.96)              // 0.0250 — P(X > 1.96) = 1 - cdf

// Invert
normal.quantile(0.975)       // 1.9600 — value at the 97.5th percentile

// Sample
normal.sample(Random(42))           // single random draw
normal.sample(5, Random(42))        // 5 random draws

Shared API

Every distribution implements common statistical properties: mean, variance, standardDeviation, skewness, kurtosis, and entropy. The evaluation methods differ between continuous and discrete distributions:
OperationContinuousDiscrete
Point valuepdf(x) — densitypmf(k) — exact probability
Log point valuelogPdf(x)logPmf(k)
Cumulativecdf(x)P(Xx)P(X \le x)cdf(k)P(Xk)P(X \le k)
Upper tailsf(x)P(X>x)P(X > x)sf(k)P(X>k)P(X > k)
Quantilequantile(p)DoublequantileInt(p)Int
Samplesample(random)Doublesample(random)Int
Batch samplesample(n, random)DoubleArraysample(n, random)IntArray
Constructors validate parameters eagerly. Invalid values (negative standard deviation, probability outside [0, 1], non-positive degrees of freedom) throw InvalidParameterException at construction time, not at evaluation.

Continuous Distributions

The Gaussian distribution. Models data that clusters symmetrically around a mean with a characteristic bell-shaped curve.Parameters: mu — mean, sigma — standard deviation (must be positive)
val d = NormalDistribution(mu = 100.0, sigma = 15.0)
d.mean       // 100.0
d.cdf(115.0) // 0.8413
d.quantile(0.975) // 129.3994
Use when data is approximately symmetric and unbounded.
Heavier tails than the normal distribution. Approaches the normal as degrees of freedom increase.Parameters: df — degrees of freedom (must be positive)
val d = StudentTDistribution(degreesOfFreedom = 10.0)
d.mean       // 0.0
d.cdf(2.228) // ≈ 0.975
d.quantile(0.975) // 2.2281
Use for confidence intervals and t-tests when the sample size is small.
Similar shape to the normal but with heavier tails. The CDF has a closed-form logistic function.Parameters: location — center, scale — spread parameter (must be positive)
val d = LogisticDistribution(mu = 0.0, scale = 1.0)
d.mean       // 0.0
d.cdf(0.0)   // 0.5
d.pdf(0.0)   // 0.25
Use when a closed-form CDF is needed or data has slightly heavier tails than normal.
Extremely heavy tails. The mean and variance are undefined.Parameters: location — center (median), scale — half-width at half-maximum (must be positive)
val d = CauchyDistribution(location = 0.0, scale = 1.0)
d.pdf(0.0)        // 0.3183
d.cdf(0.0)        // 0.5
d.quantile(0.75)  // 1.0
Use for data with extreme outliers where the mean is not a meaningful summary.
Double-exponential distribution. Sharper peak and heavier tails than the normal.Parameters: location — center (mean and median), scale — spread parameter (must be positive)
val d = LaplaceDistribution(mu = 0.0, scale = 1.0)
d.mean       // 0.0
d.variance   // 2.0
d.pdf(0.0)   // 0.5
Use for data with a sharp peak at the center and exponential tails.

Discrete Distributions

Models the number of events in a fixed interval when events occur independently at a constant rate.Parameters: lambda — expected number of events (must be positive)
val d = PoissonDistribution(rate = 3.0)
d.mean          // 3.0
d.pmf(5)        // 0.1008
d.cdf(5)        // 0.9161
d.quantileInt(0.95) // 6
Use for count data: defects per batch, arrivals per hour, events per day.
Models the number of successes in a fixed number of independent Bernoulli trials.Parameters: trials — number of trials (must be non-negative), probability — success probability per trial (must be in [0, 1])
val d = BinomialDistribution(trials = 10, probability = 0.3)
d.mean          // 3.0
d.pmf(3)        // 0.2668
d.cdf(3)        // 0.6496
d.quantileInt(0.5) // 3
Use for yes/no experiments repeated a known number of times.
Models the number of failures before achieving a specified number of successes.Parameters: r — number of successes (must be positive), p — success probability (must be in (0, 1])
val d = NegativeBinomialDistribution(successes = 5, probability = 0.5)
d.mean          // 5.0
d.variance      // 10.0
d.pmf(3)        // probability of exactly 3 failures before 5 successes
Use for over-dispersed count data or modeling the number of trials until a target is reached.
Models the number of trials until the first success. A special case of the negative binomial.Parameters: probability — success probability per trial (must be in (0, 1])
val d = GeometricDistribution(probability = 0.3)
d.mean          // 3.3333
d.pmf(1)        // 0.3
d.cdf(3)        // 0.657
Use for “how many tries until it works” questions.

Choosing a Distribution

When unsure, start with NormalDistribution for continuous data and PoissonDistribution for counts. These are the most common defaults and serve as reasonable baselines.
Data shapeStart with
Symmetric real-valued dataNormalDistribution, StudentTDistribution, LogisticDistribution
Positive real-valued dataExponentialDistribution, GammaDistribution, WeibullDistribution, LogNormalDistribution
Data bounded on [0, 1]BetaDistribution
Bounded on a known intervalUniformDistribution, TriangularDistribution
Counts and event totalsPoissonDistribution, BinomialDistribution, NegativeBinomialDistribution
Counts without replacementHypergeometricDistribution
Heavy-tailed dataCauchyDistribution, ParetoDistribution, LevyDistribution
Rank-frequency dataZipfDistribution, LogarithmicDistribution

API Reference

Full API Reference

Browse all distribution constructors, methods, and properties in the Dokka-generated reference.
Last modified on April 18, 2026