Beginner tutorial · built-in distributions

Answer four questions with one distribution

A density describes local plausibility, a CDF answers “at most?”, a quantile finds a cutoff, and a random method simulates what could happen. JDistlib uses these four operations consistently.

The mental model

D, P, Q, and R

In Java these are named density, cumulative, quantile, and random. They mirror R's d/p/q/r convention.

First code

A standard-normal probability and cutoff

Import the distribution class, then state the value, parameters, lower-tail choice, and whether the probability is logarithmic.

import jdistlib.Normal;

double probability = Normal.cumulative(
    1.96, 0.0, 1.0, true, false);
double cutoff = Normal.quantile(
    0.975, 0.0, 1.0, true, false);

// probability and cutoff are both about 0.975 and 1.96.

An instance remembers its parameters and is convenient when you reuse a law:

Normal measurement = new Normal(100.0, 15.0);
double below130 = measurement.cumulative(130.0, true, false);
double[] densities = measurement.density(
    new double[] {85.0, 100.0, 115.0});

Two switches

Lower tail and logarithmic probability

lowerTail

true asks for P(X ≤ x); false asks for P(X > x). Use the upper-tail calculation directly for rare exceedances.

logP

true returns the logarithm of a probability. This avoids underflow and is useful when likelihoods multiply many small probabilities.

double failureRisk = Normal.cumulative(
    180.0, 100.0, 15.0, false, false);
double logFailureRisk = Normal.cumulative(
    180.0, 100.0, 15.0, false, true);

Reproducible simulation

Own the random stream

Give each independent task its own RandomEngine. A fixed seed makes a tutorial, test, or analysis repeatable.

import jdistlib.Normal;
import jdistlib.rng.MersenneTwister;
import jdistlib.rng.RandomEngine;

RandomEngine random = new MersenneTwister(20260826L);
double[] simulated = Normal.random(
    1_000, 100.0, 15.0, random);

Do not share one mutable random engine between threads. Give each worker a separate, explicitly seeded stream.

Avoid surprises

Three common mistakes

Density is not interval probability

For a continuous law, use cumulative(b)-cumulative(a) for P(a < X ≤ b). A density value can exceed one.

Check the parameterization

Scale, rate, shape, and degrees of freedom are not interchangeable. Confirm them in the catalog.

Discrete density means mass

For a binomial or Poisson law, density(k,...) is the exact probability of k.

Estimate, then test carefully

A goodness-of-fit reference distribution should normally be fully specified. If parameters came from the same data, account for that estimation in the resampling procedure.

Next

Work through a complete analysis

Continue with the response-time vignette, browse the full catalog, build a custom distribution, or learn how to adjust a collection of p-values in 0.7.0+.