然ranjs

Parameter estimation

What is parameter estimation?

Parameter estimation is the process of finding the distribution parameters that best explain an observed dataset. ranjs uses maximum likelihood estimation (MLE): it finds the parameter values that maximise the probability of seeing the data under the assumed model. This is done by a derivative-free Nelder-Mead simplex optimizer, which makes it robust to distributions with non-smooth or discontinuous likelihoods.

Once parameters are estimated you can inspect them, compare models by AIC/BIC, or run a built-in goodness-of-fit test — all with the same distribution instance returned by fit().

The full workflow

fit() closes the full statistical cycle: define a model, generate (or collect) data, estimate parameters from that data, then test whether the fit is adequate.

import { dist } from 'ranjs'

// 1. Define and sample
const model = new dist.Normal(3, 1).seed(42)
const data  = model.sample(500)

// 2. Fit parameters from data via MLE
const fitted = dist.Normal.fit(data)
console.log(fitted.p)           // { mu: 3.000, sigma: 1.000 }

// 3. Test goodness of fit
console.log(fitted.test(data))  // { statistics: 0.031, passed: true }

The same pattern works for any distribution in the library. Here is a discrete example fitting a Poisson model to count data:

import { dist } from 'ranjs'

const data   = new dist.Poisson(4.2).seed(42).sample(200)
const fitted = dist.Poisson.fit(data)
console.log(fitted.p)           // { lambda: 4.225 }
console.log(fitted.test(data))  // { statistics: 2.471, passed: true }

Static method

Note: fit() is a static method called on the class, not on an instance. Use dist.Normal.fit(data), not model.fit(data).

CorrectWrong
dist.Normal.fit(data)model.fit(data) // TypeError

All distributions

All 140 exported distributions support fit(). Most have a data-aware static _fitInit(data) override that seeds the Nelder-Mead optimizer from a method-of-moments estimate, improving convergence reliability. The base-class fallback uses random positive-value retries when no override is present.

Zero-parameter distributions (e.g. Gilbrat, HalfLogistic) skip optimisation entirely: Cls.fit(data) simply returns new Cls().

Goodness of fit

The instance returned by fit() is a fully-featured distribution object. You can chain any distribution method on it:

const fitted = dist.Gamma.fit(data)

fitted.test(data)        // KS goodness-of-fit test
fitted.aic(data)         // Akaike information criterion
fitted.bic(data)         // Bayesian information criterion
fitted.pdf(x)            // density at any point
fitted.sample(100)       // generate new variates from the fitted model

See the API reference for the full list of methods available on every distribution instance.