Class NormalInt
- All Implemented Interfaces:
DiscreteDistribution
This distribution is constructed by sampling a continuous normal X ~ Normal(mean,
sigma^2) and returning the integer Y = round(X) (nearest-even rounding via Math.rint(double)). It therefore places probability mass on all integers (unless truncated).
Probability functions
For the untruncated rounded-normal, the probability mass function (PMF) and cumulative distribution function (CDF) are exact:
pmf(k) = Φ((k + 0.5 - mean)/sigma) - Φ((k - 0.5 - mean)/sigma)
cdf(k) = Φ((k + 0.5 - mean)/sigma)
where Φ is the standard normal CDF.
For the truncated variant on [lower, upper] (inclusive), mass is
re-normalized to that interval:
Z = Φ((upper + 0.5 - mean)/sigma) - Φ((lower - 0.5 - mean)/sigma)
pmf_trunc(k) = pmf(k) / Z for k in [lower, upper], else 0
cdf_trunc(k) = (Φ((k + 0.5 - mean)/sigma) - Φ((lower - 0.5 - mean)/sigma)) / Z
Sampling
Sampling uses the Marsaglia polar (Box–Muller) method to draw a standard normal, then scales/shifts and applies nearest-even rounding. When truncated, out-of-range draws are rejected and re-sampled (efficient unless the window is deep in the tails).
Numerics
Φis computed via anerfapproximation (Abramowitz & Stegun 7.1.26), with max absolute error ≈ 1.5e−7.- Untruncated mean/variance are computed by summing the PMF over a wide
±8σwindow (extended until the remaining tail mass is negligible).
Determinism & threading
Given the same seed, parameters, and JDK RNG algorithm, sequences are repeatable. Instances are not synchronized; prefer one instance per thread or supply thread-local RNGs.
Examples
// Untruncated discrete normal centered near 2, σ = 1.5
var d1 = new NormalInt(2.0, 1.5);
int x = d1.sample();
// Seeded and truncated to [-3, +3]
var d2 = new NormalInt(1234L, 0.0, 2.0, -3, 3);
double p0 = d2.pmf(0); // probability at 0
double F2 = d2.cdf(2); // P(Y <= 2)
- Since:
- 0.1.0
-
Constructor Summary
ConstructorsConstructorDescriptionNormalInt(double mean, double sigma) Creates an untruncated discrete normal using the library's default RNG.NormalInt(double mean, double sigma, int lower, int upper) Creates a truncated discrete normal on the closed interval[lower, upper]using the library's default RNG.NormalInt(long seed, double mean, double sigma) Creates an untruncated discrete normal with a deterministic RNG built fromseed.NormalInt(long seed, double mean, double sigma, int lower, int upper) Creates a truncated discrete normal on[lower, upper]with a deterministic RNG.NormalInt(RandomGenerator rng, double mean, double sigma) Creates an untruncated discrete normal with a caller-supplied RNG.NormalInt(RandomGenerator rng, double mean, double sigma, int lower, int upper) Creates a truncated discrete normal on[lower, upper]with a caller-supplied RNG. -
Method Summary
Modifier and TypeMethodDescriptiondoublecdf(int k) Returns the cumulative distribution function atk, i.e.,P(Y ≤ k).doublemean()Returns the distribution mean (expected value).doublepmf(int k) Returns the probability mass atk, i.e.,P(Y = k).intsample()Draws a single sampleYfrom this distribution.support()Reports the mathematical support (domain) of this distribution.doublevariance()Returns the distribution variance.
-
Constructor Details
-
NormalInt
public NormalInt(double mean, double sigma) Creates an untruncated discrete normal using the library's default RNG.- Parameters:
mean- the meanμof the underlying continuous normal (finite)sigma- the standard deviationσof the underlying normal; must be> 0- Throws:
IllegalArgumentException- ifmeanis not finite orsigma <= 0or not finite
-
NormalInt
public NormalInt(long seed, double mean, double sigma) Creates an untruncated discrete normal with a deterministic RNG built fromseed.- Parameters:
seed- RNG seed for reproducible samplingmean- the meanμof the underlying continuous normal (finite)sigma- the standard deviationσof the underlying normal; must be> 0- Throws:
IllegalArgumentException- ifmeanis not finite orsigma <= 0or not finite
-
NormalInt
Creates an untruncated discrete normal with a caller-supplied RNG.- Parameters:
rng- the random generator (must not benull)mean- the meanμof the underlying continuous normal (finite)sigma- the standard deviationσof the underlying normal; must be> 0- Throws:
NullPointerException- ifrngisnullIllegalArgumentException- ifmeanis not finite orsigma <= 0or not finite
-
NormalInt
public NormalInt(double mean, double sigma, int lower, int upper) Creates a truncated discrete normal on the closed interval[lower, upper]using the library's default RNG. Mass is re-normalized to the interval.- Parameters:
mean- the meanμof the underlying continuous normal (finite)sigma- the standard deviationσof the underlying normal; must be> 0lower- inclusive lower bound for the integer outcomesupper- inclusive upper bound for the integer outcomes; must be>= lower- Throws:
IllegalArgumentException- if parameters are invalid orlower > upper
-
NormalInt
public NormalInt(long seed, double mean, double sigma, int lower, int upper) Creates a truncated discrete normal on[lower, upper]with a deterministic RNG.- Parameters:
seed- RNG seed for reproducible samplingmean- the meanμof the underlying continuous normal (finite)sigma- the standard deviationσof the underlying normal; must be> 0lower- inclusive lower bound for the integer outcomesupper- inclusive upper bound for the integer outcomes; must be>= lower- Throws:
IllegalArgumentException- if parameters are invalid orlower > upper
-
NormalInt
Creates a truncated discrete normal on[lower, upper]with a caller-supplied RNG.- Parameters:
rng- the random generator (must not benull)mean- the meanμof the underlying continuous normal (finite)sigma- the standard deviationσof the underlying normal; must be> 0lower- inclusive lower bound for the integer outcomesupper- inclusive upper bound for the integer outcomes; must be>= lower- Throws:
NullPointerException- ifrngisnullIllegalArgumentException- if parameters are invalid orlower > upper
-
-
Method Details
-
sample
public int sample()Draws a single sampleYfrom this distribution.For untruncated distributions, this value can be any integer (with quickly decaying tails). For truncated distributions, the result is guaranteed to lie in
[lower, upper].- Specified by:
samplein interfaceDiscreteDistribution- Returns:
- a random integer variate
-
pmf
public double pmf(int k) Returns the probability mass atk, i.e.,P(Y = k).- Specified by:
pmfin interfaceDiscreteDistribution- Parameters:
k- integer at which to evaluate the PMF- Returns:
- the probability mass at
k(zero if truncated andkis outside the bounds)
-
cdf
public double cdf(int k) Returns the cumulative distribution function atk, i.e.,P(Y ≤ k).- Specified by:
cdfin interfaceDiscreteDistribution- Parameters:
k- integer at which to evaluate the CDF- Returns:
- the cumulative probability at
k
-
mean
public double mean()Returns the distribution mean (expected value).For the rounded-normal, this is close to
mean, with a tiny quantization effect. For the truncated variant, this is the re-normalized mean over the interval.- Specified by:
meanin interfaceDiscreteDistribution- Returns:
E[Y]
-
variance
public double variance()Returns the distribution variance.For the rounded-normal, this is close to
sigma^2plus a small quantization effect. For the truncated variant, this is the re-normalized variance over the interval.- Specified by:
variancein interfaceDiscreteDistribution- Returns:
Var[Y]
-
support
Reports the mathematical support (domain) of this distribution.For untruncated instances the support is the unbounded integer line. For truncated instances it is the closed interval
[lower, upper].- Returns:
- a
DistributionSupportdescribing the support (discrete)
-