Documentation
¶
Overview ¶
Package dist holds the distribution families and their exact samplers. Every family implements Distribution; continuous families also implement Quantiler and discrete families PMFer. Simulation code never calls Fill directly: it draws through a Sampler.
Index ¶
- Constants
- func PoissonCount(r *rand.Rand, lambda float64) uint64
- type Bernoulli
- func (d Bernoulli) CDF(x float64) float64
- func (d Bernoulli) Fill(r *rand.Rand, dst []float64)
- func (Bernoulli) Laplace(complex128) (complex128, bool)
- func (d Bernoulli) Mean() float64
- func (Bernoulli) Nodes(_, _ []float64) bool
- func (d Bernoulli) PMF(x float64) float64
- func (d Bernoulli) Quantile(p float64) float64
- func (d Bernoulli) QuantileUpper(q float64) float64
- func (d Bernoulli) Support() (lo, hi float64)
- func (d Bernoulli) Survival(x float64) float64
- func (d Bernoulli) Var() float64
- type Beta
- func (d Beta) CDF(x float64) float64
- func (d Beta) Fill(r *rand.Rand, dst []float64)
- func (Beta) Laplace(complex128) (complex128, bool)
- func (d Beta) Mean() float64
- func (d Beta) Nodes(x, w []float64) bool
- func (d Beta) Quantile(p float64) float64
- func (d Beta) QuantileUpper(q float64) float64
- func (d Beta) Support() (lo, hi float64)
- func (d Beta) Survival(x float64) float64
- func (d Beta) Var() float64
- type Binomial
- func (d Binomial) CDF(x float64) float64
- func (d Binomial) Fill(r *rand.Rand, dst []float64)
- func (Binomial) Laplace(complex128) (complex128, bool)
- func (d Binomial) Mean() float64
- func (Binomial) Nodes(_, _ []float64) bool
- func (d Binomial) PMF(x float64) float64
- func (d Binomial) Quantile(p float64) float64
- func (d Binomial) QuantileUpper(q float64) float64
- func (d Binomial) Support() (lo, hi float64)
- func (d Binomial) Survival(x float64) float64
- func (d Binomial) Var() float64
- type Capped
- func (d Capped) Atom() float64
- func (d Capped) CDF(x float64) float64
- func (d Capped) Fill(r *rand.Rand, dst []float64)
- func (Capped) Laplace(complex128) (complex128, bool)
- func (d Capped) Mean() float64
- func (Capped) Nodes(_, _ []float64) bool
- func (d Capped) Quantile(p float64) float64
- func (d Capped) QuantileUpper(q float64) float64
- func (d Capped) Support() (lo, hi float64)
- func (d Capped) Survival(x float64) float64
- func (d Capped) Var() float64
- type Categorical
- func (d Categorical) CDF(x float64) float64
- func (d Categorical) Fill(r *rand.Rand, dst []float64)
- func (d Categorical) Laplace(s complex128) (complex128, bool)
- func (d Categorical) Len() int
- func (d Categorical) Mean() float64
- func (d Categorical) Nodes(x, w []float64) bool
- func (d Categorical) PMF(x float64) float64
- func (d Categorical) Quantile(p float64) float64
- func (d Categorical) QuantileUpper(q float64) float64
- func (d Categorical) Support() (lo, hi float64)
- func (d Categorical) Survival(x float64) float64
- func (d Categorical) Var() float64
- type Constant
- func (d Constant) CDF(x float64) float64
- func (d Constant) Fill(_ *rand.Rand, dst []float64)
- func (d Constant) Laplace(s complex128) (complex128, bool)
- func (d Constant) Mean() float64
- func (d Constant) Nodes(x, w []float64) bool
- func (d Constant) Quantile(float64) float64
- func (d Constant) QuantileUpper(float64) float64
- func (d Constant) Support() (lo, hi float64)
- func (d Constant) Survival(x float64) float64
- func (d Constant) Var() float64
- type Continuous
- type Distribution
- type Exponential
- func (d Exponential) CDF(x float64) float64
- func (d Exponential) Fill(r *rand.Rand, dst []float64)
- func (d Exponential) Laplace(s complex128) (complex128, bool)
- func (d Exponential) Mean() float64
- func (Exponential) Nodes(_, _ []float64) bool
- func (d Exponential) Quantile(p float64) float64
- func (d Exponential) QuantileUpper(q float64) float64
- func (d Exponential) Support() (lo, hi float64)
- func (d Exponential) Survival(x float64) float64
- func (d Exponential) Var() float64
- type GPD
- func (d GPD) CDF(x float64) float64
- func (d GPD) Fill(r *rand.Rand, dst []float64)
- func (GPD) Laplace(complex128) (complex128, bool)
- func (d GPD) Mean() float64
- func (GPD) Nodes(_, _ []float64) bool
- func (d GPD) Quantile(p float64) float64
- func (d GPD) QuantileUpper(q float64) float64
- func (d GPD) Support() (lo, hi float64)
- func (d GPD) Survival(x float64) float64
- func (d GPD) Var() float64
- type Gamma
- func (d Gamma) CDF(x float64) float64
- func (d Gamma) Fill(r *rand.Rand, dst []float64)
- func (d Gamma) Laplace(s complex128) (complex128, bool)
- func (d Gamma) Mean() float64
- func (Gamma) Nodes(_, _ []float64) bool
- func (d Gamma) Quantile(p float64) float64
- func (d Gamma) QuantileUpper(q float64) float64
- func (d Gamma) Support() (lo, hi float64)
- func (d Gamma) Survival(x float64) float64
- func (d Gamma) Var() float64
- type Geometric
- func (d Geometric) CDF(x float64) float64
- func (d Geometric) Fill(r *rand.Rand, dst []float64)
- func (Geometric) Laplace(complex128) (complex128, bool)
- func (d Geometric) Mean() float64
- func (Geometric) Nodes(_, _ []float64) bool
- func (d Geometric) PMF(x float64) float64
- func (d Geometric) Quantile(p float64) float64
- func (d Geometric) QuantileUpper(q float64) float64
- func (d Geometric) Support() (lo, hi float64)
- func (d Geometric) Survival(x float64) float64
- func (d Geometric) Var() float64
- type LogPERT
- func (d LogPERT) CDF(x float64) float64
- func (d LogPERT) Fill(r *rand.Rand, dst []float64)
- func (LogPERT) Laplace(complex128) (complex128, bool)
- func (d LogPERT) Mean() float64
- func (LogPERT) Nodes(_, _ []float64) bool
- func (d LogPERT) Quantile(p float64) float64
- func (d LogPERT) QuantileUpper(q float64) float64
- func (d LogPERT) Support() (lo, hi float64)
- func (d LogPERT) Survival(x float64) float64
- func (d LogPERT) Var() float64
- type LogUniform
- func (d LogUniform) CDF(x float64) float64
- func (d LogUniform) Fill(r *rand.Rand, dst []float64)
- func (LogUniform) Laplace(complex128) (complex128, bool)
- func (d LogUniform) Mean() float64
- func (LogUniform) Nodes(_, _ []float64) bool
- func (d LogUniform) Quantile(p float64) float64
- func (d LogUniform) QuantileUpper(q float64) float64
- func (d LogUniform) Support() (lo, hi float64)
- func (d LogUniform) Survival(x float64) float64
- func (d LogUniform) Var() float64
- type Lognormal
- func (d Lognormal) CDF(x float64) float64
- func (d Lognormal) Fill(r *rand.Rand, dst []float64)
- func (Lognormal) Laplace(complex128) (complex128, bool)
- func (d Lognormal) Mean() float64
- func (Lognormal) Nodes(_, _ []float64) bool
- func (d Lognormal) Quantile(p float64) float64
- func (d Lognormal) QuantileUpper(q float64) float64
- func (d Lognormal) Support() (lo, hi float64)
- func (d Lognormal) Survival(x float64) float64
- func (d Lognormal) Var() float64
- type NegativeBinomial
- func (d NegativeBinomial) CDF(x float64) float64
- func (d NegativeBinomial) Fill(r *rand.Rand, dst []float64)
- func (NegativeBinomial) Laplace(complex128) (complex128, bool)
- func (d NegativeBinomial) Mean() float64
- func (NegativeBinomial) Nodes(_, _ []float64) bool
- func (d NegativeBinomial) PMF(x float64) float64
- func (d NegativeBinomial) Quantile(p float64) float64
- func (d NegativeBinomial) QuantileUpper(q float64) float64
- func (d NegativeBinomial) Support() (lo, hi float64)
- func (d NegativeBinomial) Survival(x float64) float64
- func (d NegativeBinomial) Var() float64
- type Normal
- func (d Normal) CDF(x float64) float64
- func (d Normal) Fill(r *rand.Rand, dst []float64)
- func (Normal) Laplace(complex128) (complex128, bool)
- func (d Normal) Mean() float64
- func (Normal) Nodes(_, _ []float64) bool
- func (d Normal) Quantile(p float64) float64
- func (d Normal) QuantileUpper(q float64) float64
- func (d Normal) Support() (lo, hi float64)
- func (d Normal) Survival(x float64) float64
- func (d Normal) Var() float64
- type PERT
- func (d PERT) CDF(x float64) float64
- func (d PERT) Fill(r *rand.Rand, dst []float64)
- func (PERT) Laplace(complex128) (complex128, bool)
- func (d PERT) Mean() float64
- func (d PERT) Nodes(x, w []float64) bool
- func (d PERT) Quantile(p float64) float64
- func (d PERT) QuantileUpper(q float64) float64
- func (d PERT) Shape() (alpha, beta float64)
- func (d PERT) Support() (lo, hi float64)
- func (d PERT) Survival(x float64) float64
- func (d PERT) Var() float64
- type PMFer
- type Pareto
- func (d Pareto) CDF(x float64) float64
- func (d Pareto) Fill(r *rand.Rand, dst []float64)
- func (Pareto) Laplace(complex128) (complex128, bool)
- func (d Pareto) Mean() float64
- func (Pareto) Nodes(_, _ []float64) bool
- func (d Pareto) Quantile(p float64) float64
- func (d Pareto) QuantileUpper(q float64) float64
- func (d Pareto) Support() (lo, hi float64)
- func (d Pareto) Survival(x float64) float64
- func (d Pareto) Var() float64
- type Poisson
- func (d Poisson) CDF(x float64) float64
- func (d Poisson) Fill(r *rand.Rand, dst []float64)
- func (Poisson) Laplace(complex128) (complex128, bool)
- func (d Poisson) Mean() float64
- func (Poisson) Nodes(_, _ []float64) bool
- func (d Poisson) PMF(x float64) float64
- func (d Poisson) Quantile(p float64) float64
- func (d Poisson) QuantileUpper(q float64) float64
- func (d Poisson) Support() (lo, hi float64)
- func (d Poisson) Survival(x float64) float64
- func (d Poisson) Var() float64
- type Quantiler
- type Sampler
- type SamplerSet
- type Scaled
- func (d Scaled) Base() Distribution
- func (d Scaled) CDF(x float64) float64
- func (d Scaled) Factor() float64
- func (d Scaled) Fill(r *rand.Rand, dst []float64)
- func (d Scaled) Laplace(s complex128) (complex128, bool)
- func (d Scaled) Mean() float64
- func (d Scaled) Nodes(x, w []float64) bool
- func (d Scaled) Quantile(p float64) float64
- func (d Scaled) QuantileUpper(q float64) float64
- func (d Scaled) Support() (lo, hi float64)
- func (d Scaled) Survival(x float64) float64
- func (d Scaled) Var() float64
- type Triangular
- func (d Triangular) CDF(x float64) float64
- func (d Triangular) Fill(r *rand.Rand, dst []float64)
- func (Triangular) Laplace(complex128) (complex128, bool)
- func (d Triangular) Mean() float64
- func (Triangular) Nodes(_, _ []float64) bool
- func (d Triangular) Quantile(p float64) float64
- func (d Triangular) QuantileUpper(q float64) float64
- func (d Triangular) Support() (lo, hi float64)
- func (d Triangular) Survival(x float64) float64
- func (d Triangular) Var() float64
- type Truncated
- func (d Truncated) CDF(x float64) float64
- func (d Truncated) Fill(r *rand.Rand, dst []float64)
- func (Truncated) Laplace(complex128) (complex128, bool)
- func (d Truncated) Mean() float64
- func (Truncated) Nodes(_, _ []float64) bool
- func (d Truncated) Quantile(p float64) float64
- func (d Truncated) QuantileUpper(q float64) float64
- func (d Truncated) Support() (lo, hi float64)
- func (d Truncated) Survival(x float64) float64
- func (d Truncated) Var() float64
- type TruncatedNormal
- func (d TruncatedNormal) CDF(x float64) float64
- func (d TruncatedNormal) Fill(r *rand.Rand, dst []float64)
- func (TruncatedNormal) Laplace(complex128) (complex128, bool)
- func (d TruncatedNormal) Mean() float64
- func (TruncatedNormal) Nodes(_, _ []float64) bool
- func (d TruncatedNormal) Quantile(p float64) float64
- func (d TruncatedNormal) QuantileUpper(q float64) float64
- func (d TruncatedNormal) Support() (lo, hi float64)
- func (d TruncatedNormal) Survival(x float64) float64
- func (d TruncatedNormal) Var() float64
- type Uniform
- func (d Uniform) CDF(x float64) float64
- func (d Uniform) Fill(r *rand.Rand, dst []float64)
- func (d Uniform) Laplace(s complex128) (complex128, bool)
- func (d Uniform) Mean() float64
- func (d Uniform) Nodes(x, w []float64) bool
- func (d Uniform) Quantile(p float64) float64
- func (d Uniform) QuantileUpper(q float64) float64
- func (d Uniform) Support() (lo, hi float64)
- func (d Uniform) Survival(x float64) float64
- func (d Uniform) Var() float64
- type Weibull
- func (d Weibull) CDF(x float64) float64
- func (d Weibull) Fill(r *rand.Rand, dst []float64)
- func (Weibull) Laplace(complex128) (complex128, bool)
- func (d Weibull) Mean() float64
- func (Weibull) Nodes(_, _ []float64) bool
- func (d Weibull) Quantile(p float64) float64
- func (d Weibull) QuantileUpper(q float64) float64
- func (d Weibull) Support() (lo, hi float64)
- func (d Weibull) Survival(x float64) float64
- func (d Weibull) Var() float64
Examples ¶
Constants ¶
const (
// DefaultPERTShape is the t of the modified PERT when none is given.
DefaultPERTShape = 4
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bernoulli ¶
type Bernoulli struct {
// contains filtered or unexported fields
}
Bernoulli is a single trial with success probability p.
func NewBernoulli ¶
NewBernoulli validates 0 ≤ p ≤ 1.
func (Bernoulli) Laplace ¶
func (Bernoulli) Laplace(complex128) (complex128, bool)
func (Bernoulli) QuantileUpper ¶
type Beta ¶
type Beta struct {
// contains filtered or unexported fields
}
Beta is the Beta distribution on [0, 1], drawn as X/(X+Y) from two Gammas.
func NewBetaFromMeanSD ¶
NewBetaFromMeanSD converts a mean and standard deviation; it fails when no Beta has them, that is when sd² ≥ mean·(1 − mean).
func (Beta) Laplace ¶
func (Beta) Laplace(complex128) (complex128, bool)
func (Beta) Quantile ¶
Quantile inverts the regularised incomplete beta function to full precision, seeded by gonum's inverse or by the small-x series I_x(a, b) ≈ x^a / (a·B(a, b)) when that seed is 0.
func (Beta) QuantileUpper ¶
QuantileUpper inverts the survival function the same way; the seed uses the mirrored inverse.
type Binomial ¶
type Binomial struct {
// contains filtered or unexported fields
}
Binomial counts successes in n trials with probability p.
func NewBinomial ¶
NewBinomial validates n ≥ 0 integer and 0 ≤ p ≤ 1.
func (Binomial) Laplace ¶
func (Binomial) Laplace(complex128) (complex128, bool)
func (Binomial) QuantileUpper ¶
QuantileUpper is the smallest k with Survival(k) ≤ q.
type Capped ¶
type Capped struct {
// contains filtered or unexported fields
}
Capped is min(X, c): the distribution of X with its upper tail collapsed into an atom at c. Draws cost the same as X.
func NewCapped ¶
func NewCapped(d Continuous, c float64) (Capped, error)
NewCapped validates that c lies inside the support and computes the moments from the partial expectations below c and the atom at c.
func (Capped) Laplace ¶
func (Capped) Laplace(complex128) (complex128, bool)
func (Capped) QuantileUpper ¶
type Categorical ¶
type Categorical struct {
// contains filtered or unexported fields
}
Categorical draws from a finite set of values with given probabilities, by Vose's alias method in O(1) per draw. Values may repeat.
func NewCategorical ¶
func NewCategorical(values, probs []float64) (Categorical, error)
NewCategorical validates the lengths match, values are finite, probabilities are non-negative and sum to 1 within 1e-12.
func NewEmpirical ¶
func NewEmpirical(values []float64) (Categorical, error)
NewEmpirical is a Categorical with equal weights: resampling of observed data.
func (Categorical) CDF ¶
func (d Categorical) CDF(x float64) float64
func (Categorical) Laplace ¶
func (d Categorical) Laplace(s complex128) (complex128, bool)
Categorical: the exact finite mixture.
func (Categorical) Mean ¶
func (d Categorical) Mean() float64
func (Categorical) Nodes ¶
func (d Categorical) Nodes(x, w []float64) bool
Nodes of a Categorical are its atoms, exact for every g, when they fit.
func (Categorical) PMF ¶
func (d Categorical) PMF(x float64) float64
func (Categorical) Quantile ¶
func (d Categorical) Quantile(p float64) float64
Quantile is the smallest value with at least a fraction p of the mass at or below it.
func (Categorical) QuantileUpper ¶
func (d Categorical) QuantileUpper(q float64) float64
QuantileUpper is the smallest value with at most q of the mass above it.
func (Categorical) Support ¶
func (d Categorical) Support() (lo, hi float64)
func (Categorical) Survival ¶
func (d Categorical) Survival(x float64) float64
Survival is P(X > x).
func (Categorical) Var ¶
func (d Categorical) Var() float64
type Constant ¶
type Constant struct {
// contains filtered or unexported fields
}
Constant is a point mass.
func NewConstant ¶
NewConstant validates that the value is finite.
func (Constant) Laplace ¶
func (d Constant) Laplace(s complex128) (complex128, bool)
func (Constant) QuantileUpper ¶
type Continuous ¶
type Continuous interface {
Distribution
Quantiler
}
Continuous is a distribution with the tail-safe inverses the wrappers need.
type Distribution ¶
type Distribution interface {
Fill(r *rand.Rand, dst []float64)
Mean() float64
Var() float64
Support() (lo, hi float64)
CDF(x float64) float64
Laplace(s complex128) (value complex128, ok bool)
Nodes(x, w []float64) bool
Quantiler
}
Distribution is one family with fixed parameters. Fill writes len(dst) independent draws and never allocates. Laplace is E[exp(−s·X)] for complex s with Re(s) ≥ 0 in closed form; ok is false for a family without one, and the caller integrates numerically instead. Nodes fills a Gauss rule for the family's own density (see nodes.go); false for a family without one.
Example ¶
Fill draws straight from a distribution when no Sampler is wanted.
package main
import (
"fmt"
"math/rand/v2"
"code.eitanity.com/variate/dist"
)
func main() {
d, _ := dist.NewExponential(2)
r := rand.New(rand.NewPCG(1, 2))
buf := make([]float64, 4)
d.Fill(r, buf)
fmt.Println(len(buf), buf[0] > 0)
}
Output: 4 true
type Exponential ¶
type Exponential struct {
// contains filtered or unexported fields
}
Exponential has the given mean.
func NewExponential ¶
func NewExponential(mean float64) (Exponential, error)
NewExponential validates mean > 0.
func NewExponentialFromRate ¶
func NewExponentialFromRate(rate float64) (Exponential, error)
NewExponentialFromRate converts a rate.
func (Exponential) CDF ¶
func (d Exponential) CDF(x float64) float64
func (Exponential) Laplace ¶
func (d Exponential) Laplace(s complex128) (complex128, bool)
func (Exponential) Mean ¶
func (d Exponential) Mean() float64
func (Exponential) Nodes ¶
func (Exponential) Nodes(_, _ []float64) bool
func (Exponential) Quantile ¶
func (d Exponential) Quantile(p float64) float64
func (Exponential) QuantileUpper ¶
func (d Exponential) QuantileUpper(q float64) float64
func (Exponential) Support ¶
func (d Exponential) Support() (lo, hi float64)
func (Exponential) Survival ¶
func (d Exponential) Survival(x float64) float64
func (Exponential) Var ¶
func (d Exponential) Var() float64
type GPD ¶
type GPD struct {
// contains filtered or unexported fields
}
GPD is the generalised Pareto with location mu, scale sigma and shape xi. Shape 0 is the exponential; a negative shape bounds the support above.
func (GPD) Laplace ¶
func (GPD) Laplace(complex128) (complex128, bool)
func (GPD) QuantileUpper ¶
type Gamma ¶
type Gamma struct {
// contains filtered or unexported fields
}
Gamma is the Gamma distribution with shape k and scale θ.
func NewGammaFromMeanSD ¶
NewGammaFromMeanSD converts a mean and standard deviation.
func NewGammaFromShapeRate ¶
NewGammaFromShapeRate converts a shape and rate.
func (Gamma) Laplace ¶
func (d Gamma) Laplace(s complex128) (complex128, bool)
Gamma: (1 + θs)^(−k) through the principal logarithm; with Re(s) ≥ 0 the argument has real part at least 1, away from the branch cut.
func (Gamma) Quantile ¶
Quantile inverts the regularised incomplete gamma function to full precision: gonum's inverse seeds a safeguarded Newton iteration, with a small-x series seed where gonum returns 0.
func (Gamma) QuantileUpper ¶
QuantileUpper inverts the survival function the same way.
type Geometric ¶
type Geometric struct {
// contains filtered or unexported fields
}
Geometric counts failures before the first success; support {0, 1, …}.
func NewGeometric ¶
NewGeometric validates 0 < p ≤ 1.
func (Geometric) Laplace ¶
func (Geometric) Laplace(complex128) (complex128, bool)
func (Geometric) QuantileUpper ¶
QuantileUpper is the smallest k with Survival(k) ≤ q.
type LogPERT ¶
type LogPERT struct {
// contains filtered or unexported fields
}
LogPERT is a PERT on log10 of the value: the draw is 10 to a PERT draw on [log10 low, log10 high] with mode log10 peak.
func NewLogPERT ¶
NewLogPERT validates like NewPERT and additionally requires low > 0. Its moments are computed once by quadrature.
func (LogPERT) Laplace ¶
func (LogPERT) Laplace(complex128) (complex128, bool)
func (LogPERT) QuantileUpper ¶
type LogUniform ¶
type LogUniform struct {
// contains filtered or unexported fields
}
LogUniform is uniform in the logarithm on [a, b].
func NewLogUniform ¶
func NewLogUniform(a, b float64) (LogUniform, error)
NewLogUniform validates 0 < a < b.
func (LogUniform) CDF ¶
func (d LogUniform) CDF(x float64) float64
func (LogUniform) Laplace ¶
func (LogUniform) Laplace(complex128) (complex128, bool)
func (LogUniform) Mean ¶
func (d LogUniform) Mean() float64
func (LogUniform) Nodes ¶
func (LogUniform) Nodes(_, _ []float64) bool
func (LogUniform) Quantile ¶
func (d LogUniform) Quantile(p float64) float64
func (LogUniform) QuantileUpper ¶
func (d LogUniform) QuantileUpper(q float64) float64
func (LogUniform) Support ¶
func (d LogUniform) Support() (lo, hi float64)
func (LogUniform) Survival ¶
func (d LogUniform) Survival(x float64) float64
func (LogUniform) Var ¶
func (d LogUniform) Var() float64
type Lognormal ¶
type Lognormal struct {
// contains filtered or unexported fields
}
Lognormal is exp of a Normal(mu, sigma).
func NewLognormal ¶
NewLognormal validates sigma > 0 and a finite mu.
func NewLognormalFromMeanSD ¶
NewLognormalFromMeanSD converts a mean and standard deviation.
func NewLognormalFromMedianP90 ¶
NewLognormalFromMedianP90 converts a median and a 90th percentile.
func (Lognormal) Laplace ¶
func (Lognormal) Laplace(complex128) (complex128, bool)
func (Lognormal) QuantileUpper ¶
type NegativeBinomial ¶
type NegativeBinomial struct {
// contains filtered or unexported fields
}
NegativeBinomial counts failures before the r-th success with success probability p; r is real. It is drawn exactly as Poisson(Gamma(r, (1−p)/p)).
func NewNegativeBinomial ¶
func NewNegativeBinomial(r, p float64) (NegativeBinomial, error)
NewNegativeBinomial validates r > 0 and 0 < p < 1.
func NewNegativeBinomialFromMeanSize ¶
func NewNegativeBinomialFromMeanSize(mean, size float64) (NegativeBinomial, error)
NewNegativeBinomialFromMeanSize converts a mean and size: r = size, p = size/(size + mean), so Var = mean + mean²/size.
func (NegativeBinomial) CDF ¶
func (d NegativeBinomial) CDF(x float64) float64
CDF is P(K ≤ k) = I_p(r, ⌊k⌋ + 1).
func (NegativeBinomial) Laplace ¶
func (NegativeBinomial) Laplace(complex128) (complex128, bool)
func (NegativeBinomial) Mean ¶
func (d NegativeBinomial) Mean() float64
func (NegativeBinomial) Nodes ¶
func (NegativeBinomial) Nodes(_, _ []float64) bool
func (NegativeBinomial) PMF ¶
func (d NegativeBinomial) PMF(x float64) float64
func (NegativeBinomial) Quantile ¶
func (d NegativeBinomial) Quantile(p float64) float64
Quantile is the smallest k with CDF(k) ≥ p, by bracketing and bisection.
func (NegativeBinomial) QuantileUpper ¶
func (d NegativeBinomial) QuantileUpper(q float64) float64
QuantileUpper is the smallest k with Survival(k) ≤ q.
func (NegativeBinomial) Support ¶
func (d NegativeBinomial) Support() (lo, hi float64)
func (NegativeBinomial) Survival ¶
func (d NegativeBinomial) Survival(x float64) float64
func (NegativeBinomial) Var ¶
func (d NegativeBinomial) Var() float64
type Normal ¶
type Normal struct {
// contains filtered or unexported fields
}
Normal is the normal distribution; its support is the whole line, so it needs truncation before it can sit in a model slot.
func (Normal) Laplace ¶
func (Normal) Laplace(complex128) (complex128, bool)
func (Normal) QuantileUpper ¶
type PERT ¶
type PERT struct {
// contains filtered or unexported fields
}
PERT is the modified PERT distribution on [low, high] with mode peak and shape parameter t.
func NewPERT ¶
NewPERT validates low ≤ peak ≤ high, all finite with a positive finite span, and t ≥ 0 (t = 0 is the uniform).
Example ¶
A PERT is built from its low, most likely and high values; the fourth parameter is the shape, usually dist.DefaultPERTShape.
package main
import (
"fmt"
"math"
"code.eitanity.com/variate/dist"
)
func main() {
d, err := dist.NewPERT(1000, 2500, 9000, dist.DefaultPERTShape)
if err != nil {
fmt.Println(err)
return
}
lo, hi := d.Support()
fmt.Printf("mean %.1f sd %.1f support [%g, %g]\n", d.Mean(), math.Sqrt(d.Var()), lo, hi)
fmt.Printf("P(X <= 2500) = %.4f\n", d.CDF(2500))
}
Output: mean 3333.3 sd 1374.4 support [1000, 9000] P(X <= 2500) = 0.3216
func (PERT) Laplace ¶
func (PERT) Laplace(complex128) (complex128, bool)
func (PERT) QuantileUpper ¶
type PMFer ¶
PMFer is implemented by discrete distributions. Values are integers stored as float64, except for Categorical and Empirical.
type Pareto ¶
type Pareto struct {
// contains filtered or unexported fields
}
Pareto has minimum xm and tail index alpha.
func (Pareto) Laplace ¶
func (Pareto) Laplace(complex128) (complex128, bool)
func (Pareto) QuantileUpper ¶
type Poisson ¶
type Poisson struct {
// contains filtered or unexported fields
}
Poisson is the Poisson distribution with a fixed rate.
func NewPoisson ¶
NewPoisson validates lambda ≥ 0 and finite.
func (Poisson) Laplace ¶
func (Poisson) Laplace(complex128) (complex128, bool)
func (Poisson) QuantileUpper ¶
type Quantiler ¶
type Quantiler interface {
Quantile(p float64) float64
Survival(x float64) float64
QuantileUpper(q float64) float64
}
Quantiler is implemented by every continuous distribution. Survival and QuantileUpper work on the upper tail directly so that no precision is lost to 1 − CDF.
type Sampler ¶
type Sampler struct {
// contains filtered or unexported fields
}
Sampler is a pool of pre-drawn values for one distribution. Every call to Next consumes one value; when the pool is used up it is refilled with fresh draws, never reused.
type SamplerSet ¶
type SamplerSet struct {
// contains filtered or unexported fields
}
SamplerSet holds one Sampler per compiled distribution plus the control stream, all backed by one slab allocated once. It is reused across tasks by Reset.
Example ¶
Draws come through a Sampler, which refills a buffer from the distribution's Fill and hands out one value per Next. A SamplerSet gives every distribution its own stream for a seed and task.
package main
import (
"fmt"
"code.eitanity.com/variate/dist"
)
func main() {
d, _ := dist.NewPERT(1, 2, 4, dist.DefaultPERTShape)
set := dist.NewSamplerSet([]dist.Distribution{d}, 1024)
set.Reset(7, 0)
s := set.Sampler(0)
sum := 0.0
for i := 0; i < 100000; i++ {
sum += s.Next()
}
fmt.Printf("sample mean %.2f, exact %.2f\n", sum/100000, d.Mean())
}
Output: sample mean 2.17, exact 2.17
func NewSamplerSet ¶
func NewSamplerSet(dists []Distribution, poolSize int) *SamplerSet
NewSamplerSet allocates the pools: poolSize values per distribution.
func (*SamplerSet) Control ¶
func (ss *SamplerSet) Control() *rand.Rand
Control returns the task's control stream.
func (*SamplerSet) Reset ¶
func (ss *SamplerSet) Reset(seed uint64, task uint32)
Reset re-seeds every stream for task j under seed and empties the pools. Distribution k uses the key splitmix64(j<<32 | k); the control stream uses k = len(dists), one past the last distribution. It allocates nothing.
func (*SamplerSet) Sampler ¶
func (ss *SamplerSet) Sampler(k int) *Sampler
Sampler returns the pool of distribution k.
type Scaled ¶ added in v0.1.3
type Scaled struct {
// contains filtered or unexported fields
}
Scaled is f·X for a factor f > 0: every draw of X multiplied by f, from the same stream, so a scaled model draws what the unscaled one does, value for value, times f.
func NewScaled ¶ added in v0.1.3
func NewScaled(d Distribution, f float64) (Scaled, error)
NewScaled validates that f is finite and positive.
func (Scaled) Laplace ¶ added in v0.1.3
func (d Scaled) Laplace(s complex128) (complex128, bool)
Laplace is E[e^{−s·f·X}], X's transform at f·s.
func (Scaled) QuantileUpper ¶ added in v0.1.3
type Triangular ¶
type Triangular struct {
// contains filtered or unexported fields
}
Triangular is the triangular distribution on [low, high] with the given mode. The closed forms are written so that nothing cancels when the mode sits at an end of the range.
func NewTriangular ¶
func NewTriangular(low, mode, high float64) (Triangular, error)
NewTriangular validates low ≤ mode ≤ high with low < high, all finite.
func (Triangular) CDF ¶
func (d Triangular) CDF(x float64) float64
func (Triangular) Laplace ¶
func (Triangular) Laplace(complex128) (complex128, bool)
func (Triangular) Mean ¶
func (d Triangular) Mean() float64
func (Triangular) Nodes ¶
func (Triangular) Nodes(_, _ []float64) bool
func (Triangular) Quantile ¶
func (d Triangular) Quantile(p float64) float64
func (Triangular) QuantileUpper ¶
func (d Triangular) QuantileUpper(q float64) float64
func (Triangular) Support ¶
func (d Triangular) Support() (lo, hi float64)
func (Triangular) Survival ¶
func (d Triangular) Survival(x float64) float64
func (Triangular) Var ¶
func (d Triangular) Var() float64
type Truncated ¶
type Truncated struct {
// contains filtered or unexported fields
}
Truncated restricts a continuous distribution to [lo, hi] and renormalises. Draws are by inversion on the side of the range that keeps its precision, so it costs one quantile evaluation per draw.
func NewSplit ¶ added in v0.2.0
NewSplit returns the half of d that a threshold at t keeps: X given X > t when above is set, else X given X ≤ t, so an atom at t goes below. mass is the half's probability, P(X > t) or P(X ≤ t); when it is 0 the half is empty and the Truncated is the zero value. The moments are summed for a finite or an integer family and taken by quadrature in probability units otherwise, which an atom does not disturb: the probability range holds it on the side it belongs to.
func NewTruncated ¶
func NewTruncated(d Continuous, lo, hi float64) (Truncated, error)
NewTruncated validates lo < hi, that the range keeps at least 1e-9 of the mass, and computes the moments by quadrature.
Example ¶
Truncation restricts a distribution to an interval and renormalises; capping clamps draws at a ceiling, leaving an atom there.
package main
import (
"fmt"
"code.eitanity.com/variate/dist"
)
func main() {
base, _ := dist.NewLognormalFromMedianP90(4, 24)
t, err := dist.NewTruncated(base, 1, 48)
if err != nil {
fmt.Println(err)
return
}
c, _ := dist.NewCapped(base, 48)
fmt.Printf("lognormal mean %.2f\n", base.Mean())
fmt.Printf("truncated to [1, 48]: mean %.2f, P(X < 1) = %.0f\n", t.Mean(), t.CDF(1))
fmt.Printf("capped at 48: mean %.2f, P(X = 48) = %.3f\n", c.Mean(), 1-c.CDF(47.999999))
}
Output: lognormal mean 10.63 truncated to [1, 48]: mean 8.48, P(X < 1) = 0 capped at 48: mean 8.70, P(X = 48) = 0.038
func (Truncated) Laplace ¶
func (Truncated) Laplace(complex128) (complex128, bool)
func (Truncated) QuantileUpper ¶
type TruncatedNormal ¶
type TruncatedNormal struct {
// contains filtered or unexported fields
}
TruncatedNormal is Normal(mu, sigma) restricted to [lo, hi]; either bound may be infinite.
func NewTruncatedNormal ¶
func NewTruncatedNormal(mu, sigma, lo, hi float64) (TruncatedNormal, error)
NewTruncatedNormal validates sigma > 0, lo < hi, and that the interval keeps a representable mass: both standardised bounds within ±37 unless infinite.
func (TruncatedNormal) CDF ¶
func (d TruncatedNormal) CDF(x float64) float64
func (TruncatedNormal) Laplace ¶
func (TruncatedNormal) Laplace(complex128) (complex128, bool)
func (TruncatedNormal) Mean ¶
func (d TruncatedNormal) Mean() float64
func (TruncatedNormal) Nodes ¶
func (TruncatedNormal) Nodes(_, _ []float64) bool
func (TruncatedNormal) Quantile ¶
func (d TruncatedNormal) Quantile(p float64) float64
func (TruncatedNormal) QuantileUpper ¶
func (d TruncatedNormal) QuantileUpper(q float64) float64
func (TruncatedNormal) Support ¶
func (d TruncatedNormal) Support() (lo, hi float64)
func (TruncatedNormal) Survival ¶
func (d TruncatedNormal) Survival(x float64) float64
func (TruncatedNormal) Var ¶
func (d TruncatedNormal) Var() float64
type Uniform ¶
type Uniform struct {
// contains filtered or unexported fields
}
Uniform is the continuous uniform on [a, b].
func NewUniform ¶
NewUniform validates a < b, both finite, with a finite span.
func (Uniform) Laplace ¶
func (d Uniform) Laplace(s complex128) (complex128, bool)
func (Uniform) QuantileUpper ¶
type Weibull ¶
type Weibull struct {
// contains filtered or unexported fields
}
Weibull has shape k and scale lambda.
func NewWeibull ¶
NewWeibull validates shape > 0 and scale > 0.
func (Weibull) Laplace ¶
func (Weibull) Laplace(complex128) (complex128, bool)