Supply a nonnegative formula and its support. JDistlib computes
the normalizing constant, then provides density or mass, CDF, quantile, and
random-generation operations—with diagnostics for the numerical assumptions
that no finite test can prove.
Available from 0.6.0
Beginner path
Your formula does not need to be normalized
Start with a shape you want and the values it is allowed to take. For a
continuous distribution, the shape is a nonnegative kernel. For a
discrete distribution, it is a nonnegative weight for each outcome. JDistlib
finds the missing constant that makes the total probability equal one.
Write the shapeIt may omit any positive constant that is shared by every value.
Declare the supportGive the smallest and largest possible value, or the exact integer outcomes.
Build and inspectConstruction normalizes the formula; diagnostics help catch numerical trouble.
Your first continuous law
The kernel x * (1 - x) is nonnegative between zero and one and
zero at both endpoints. The result is a smooth, symmetric distribution on
that interval:
The basic class requires a finite, unique support. Infinite integer
support uses the certified truncation API described below.
Multiplying every kernel value by the same positive constant does not
change the distribution. This is useful when algebraic rescaling makes a
formula safer to evaluate.
JDistlib 0.6.0 workflow
Build, compose, summarize, and evaluate in batches
A complete compilable example
accompanies these focused snippets and is checked against the packaged
JAR by the release build.
Keep related choices in one builder
The fluent builders gather the formula, support, singularities,
numerical controls, diagnostic strength, cache, and sampling strategy.
FAST, STANDARD, and THOROUGH are starting
points; their resulting options remain fully editable.
Builders analyze ordinary formulas directly and log formulas in log
space. A log-kernel may return negative infinity to represent zero mass;
NaN and positive infinity are diagnostic errors. Use
withoutAnalysis() only when this advisory pass is intentionally
disabled.
At a censoring bound, density returns atom probability rather
than an ordinary continuous density, matching the mixed-support API.
For a complete beginner walkthrough of mixtures, truncation, censoring,
affine changes of units, and general monotone transformations, see
Compose and transform distributions.
Finite discrete distributions automatically use a Walker alias table.
Adaptive continuous rejection is conditional on the caller's global
log-concavity promise; decreasing derivatives and all encountered tangent
bounds are checked.
Evaluate without per-call allocation
double[] x = {0.1, 0.5, 0.9};
double[] probabilities = new double[x.length];
custom.cumulativeInto(
x, 0, probabilities, 0, x.length, true, false);
double[] draws = new double[10_000];
custom.randomInto(draws, 0, draws.length);
Ordinary continuous CDF batches reuse one monotone CDF table. Logged and
extreme tails continue through direct integration.
Continuous distributions
Choose support and numerical strategy deliberately
Integration controls
Use IntegrationOptions when the defaults are not appropriate. Declare
known discontinuities or interior singularities as breakpoints, set a finite
callback budget, and select double-exponential quadrature for difficult
endpoints or infinite domains.
If \(g(x)=e^{\ell(x)}\) would overflow or underflow, provide
\(\ell(x)=\log g(x)\) directly. Automatic construction searches for multiple
modes, scales regions independently, and combines their normalizers with
log-sum-exp.
NumericalContinuousDistribution shiftedNormal =
NumericalContinuousDistribution.fromLogKernel(
x -> 1000.0 - 0.5 * x * x,
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY
);
Disconnected support, holes, singularities, and atoms
NumericalSupport and NumericalPiecewiseDistribution
represent unions of intervals and mixed laws. At a declared atom,
density returns its probability
mass; elsewhere it returns the continuous density.
cumulative uses direct integration.
cumulativeCached opts into an
adaptive monotone interpolation table. Central quantiles use that table as
an initial estimate and then apply direct corrections; logged and extreme
tails remain directly integrated.
Optional rejection sampling
Inverse-CDF sampling is general but can be expensive. On finite support,
install a UniformRejectionEnvelope when you know a global upper bound
\(L\ge\sup_x\log f(x)\). The bound is a mathematical promise from the caller;
JDistlib detects encountered violations but cannot prove the bound globally.
custom.configureUniformRejectionSampling(
logDensityUpperBound,
10_000 // maximum proposals per draw
);
Discrete distributions
Finite, irregular, log-scale, and certified infinite support
Irregular finite outcomes
Supply a double[] when outcomes are not consecutive integers. Outcomes are
copied, sorted, and required to be finite and unique.
Use fromLogWeights when ordinary weights cannot be represented safely:
NumericalDiscreteDistribution logScaled =
NumericalDiscreteDistribution.fromLogWeights(
k -> 1000.0 + k * Math.log(2.0),
0, 20
);
Infinite integer support requires a certificate
CertifiedInfiniteDiscreteDistribution truncates only when a
caller-supplied DiscreteTailBound proves the remaining
unnormalized mass is small enough.
For geometric weights \(w(k)=r^k\), \(0<r<1\), the exact remainder from
the first omitted integer \(m\) is
\[
\sum_{k=m}^{\infty}r^k=\frac{r^m}{1-r}.
\]
double r = 0.7;
CertifiedInfiniteDiscreteDistribution geometric =
CertifiedInfiniteDiscreteDistribution.rightInfinite(
k -> Math.pow(r, k),
0,
DiscreteTailBounds.geometricRatio(r),
CertifiedDiscreteOptions.defaults()
);
Before and after construction
Use diagnostics as evidence, not proof
Analyze the kernel before constructing
ProbabilityFunctionAnalyzer combines a transformed deterministic grid with
reproducible randomized probes. It looks for negative or non-finite values,
callback exceptions, non-repeatability, sharp changes, oscillation, large
dynamic range, suspicious tails, and unstable normalization.
Every hardened integration result includes a callback cost profile.
For benchmark or service workloads, set total and per-call wall-clock
limits. Opt-in daemon isolation lets the integrating thread return even
when one callback blocks:
Reports expose versioned, dependency-free JSON. Non-finite numeric
measurements are represented as null, because JSON has no NaN or
infinity number literals.
Warnings or errors block construction. Useful for
controlled production pipelines.
WARNING
Errors block construction; warnings remain visible.
This is the default.
PERMISSIVE
Advisory findings do not block an attempt. Hard
kernel and integration failures still do.
Analyze the constructed distribution
analyzeDistribution checks normalization, CDF endpoints and tails, quantile
monotonicity and round trips, and absolute moments. Select moment orders and a
left/right split explicitly: