Attributes
- Companion
- class
- Graph
-
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
NegativeBinomial.type
Members list
Type members
Inherited and Abstract types
The names of the product elements.
The names of the product elements.
Attributes
- Inherited from:
- Mirror
The name of the type.
The name of the type.
Attributes
- Inherited from:
- Mirror
Value members
Concrete methods
Maximum likelihood estimation for Negative Binomial parameters.
Maximum likelihood estimation for Negative Binomial parameters.
Use: the result is the distribution of a further observation (e.g. the forecast year), with variability calibrated on history. It is a plug-in estimate: uncertainty in (a, b) itself is not included. For observations at differing volumes, use volweightedMle.
Profiles out b (the MLE of the mean is x̄, so b = x̄/a) and solves the one-dimensional profile score equation S(a) = 0 by safeguarded Newton-Raphson, starting from the method of moments estimate.
Parameterisation p = 1/(1+b), mean = a·b, with b = x̄/a. Counts are integers, so
ψ(a + x) − ψ(a) = Σ_{k k}:
S(a) = Σᵢ [ψ(a + xᵢ) − ψ(a)] − n·log(1 + x̄/a)
= Σₖ cₖ / (a + k) − n·log(1 + x̄/a) (used when x̄/a ≥ 1)
= n·(y − log(1 + y)) − (1/a)·Σₖ cₖ·k / (a + k), y = x̄/a (used when x̄/a < 1)
The second form uses Σₖ cₖ = n·x̄ to cancel the leading n·x̄/a of both terms analytically; near the
Poisson boundary each term of the first form is ≈ n·x̄/a while S itself is O(1/a²).
−S′(a) = Σₖ cₖ / (a + k)² − n·x̄ / (a·(a + x̄))
The finite sums replace differences of digamma / trigamma values, which cancel catastrophically near the Poisson boundary (a → ∞): at a ≈ 2·10⁴ the special-function form loses ~4 significant digits of a, and even the first finite-sum form loses ~1 digit to the cancellation above. The two terms of −S′ still nearly cancel for large a (−S′ is O(1/a³) while each term is O(1/a²)), but each is now computed to full relative precision.
When the population variance exceeds x̄ the profile score has a unique root, with S > 0 below it and S < 0 above it (Levin & Reeds, 1977). Each evaluation of S therefore tightens a bracket [lo, hi] around the root. A Newton step is taken when −S′ > 0 and the step stays inside the bracket; otherwise the iteration bisects (geometrically) or, while the bracket is still open on one side, doubles / halves a.
Convergence requires a Newton step (or the bracket width) smaller than tol·a. The step size is tested before the bracket, so a step that rounds to nothing counts as converged rather than triggering a bisection.
Cost per iteration is O(max xᵢ) after a single O(n) pass to build cₖ.
The NB is returned only if it beats Poisson(x̄) by more than rounding noise in ℓ (see MinRelativeLogLikGain). With integer counts the variance can equal x̄ exactly yet compute a few ulps above it, which would otherwise yield an NB with a ~ 1e15.
Value parameters
- maxIter
-
maximum number of Newton-Raphson iterations
- observations
-
array of non-negative integer observations
- tol
-
relative convergence tolerance for parameter 'a'
Attributes
- Returns
-
Named tuple with
dist: the fitted distribution (Poisson if no overdispersion, otherwise NegativeBinomial), andconverged: whether the optimizer converged within maxIter
Maximum likelihood estimation for the volume-adjusted Negative Binomial.
Maximum likelihood estimation for the volume-adjusted Negative Binomial.
We observe pairs (nᵢ, vᵢ) where nᵢ is the count and vᵢ is the volume ratio (historical volume / modelled volume).
Model: each observation has its own claim intensity Λᵢ per unit of modelled volume, drawn independently from a common Gamma(r, scale β), and nᵢ | Λᵢ ~ Poisson(Λᵢ·vᵢ). Hence the nᵢ are independent with nᵢ ~ NB(r, β·vᵢ), E[nᵢ] = r·β·vᵢ and variance/mean = 1 + β·vᵢ: NB2 with offset log vᵢ, not the "independent units" model NB(r·vᵢ, β) whose variance/mean is constant in volume. (A single Λ shared by all observations would make the nᵢ dependent and needs a different likelihood.) The fitted NegativeBinomial(r, β) is the distribution at the modelled volume (v = 1).
Use: the result is the forecast-year count distribution at the modelled volume, i.e. next year is a fresh draw Λ ~ Gamma(r, β), with year-to-year variability calibrated on history. It is a plug-in estimate: uncertainty in (r, β) itself is not included.
Parameterisation: rate λ = r·β (expected count per unit volume) and dispersion α = 1/r ≥ 0,
so β = λ·α and μᵢ = λ·vᵢ. α = 0 is exactly Poisson(λ).
Log-likelihood, dropping −Σ log nᵢ!:
ℓ(λ, α) = Σᵢ [ Σ_{k 0 ⇔ NB; Levin & Reeds, 1977.)
- ℓₚ and ℓₚ′ are evaluated at α = 0 and on a log-spaced grid α ∈ [1e-8, 1e3] (8 points per decade). λ̂(α) is
found by bracketed Newton on η, warm-started from the previous grid point.
- Every grid interval where ℓₚ′ changes sign from + to − contains a local maximum; its root is refined by
Illinois regula falsi to relative tolerance `tol` in α.
- The best of Poisson (α = 0) and all refined local maxima is returned. A local maximum narrower than one grid
spacing could in principle be missed. An NB is returned only if it beats Poisson by more than rounding noise
(see `MinRelativeLogLikGain`); otherwise an exactly-zero boundary score could yield r ~ 1e15.
- `localMaxima` counts the local maxima of ℓₚ on [0, ∞) seen on the grid: α = 0 if ℓₚ′(0) ≤ 0, each interior + →
− sign change, and the upper edge if ℓₚ is still increasing there. More than one means the chosen mode can
flip on small data changes and may imply a very different rate: such fits deserve a second look.
- No special functions are needed: the k-sums use the counts cₖ = #{i : nᵢ > k}, so one profile evaluation costs
O(number of observations + max nᵢ).
- `converged = false` if any one-dimensional solve hits `maxIter`, or if ℓₚ is still increasing at α = 1e3 (r 1 flags a multimodal fit)
Attributes
Givens
Givens
JSON codec. Only a and b are written; derived state (sampler, RNG) is rebuilt on decode. Constructor validation failures surface as a io.circe.DecodingFailure.
JSON codec. Only a and b are written; derived state (sampler, RNG) is rebuilt on decode. Constructor validation failures surface as a io.circe.DecodingFailure.