dist

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

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

Examples

Constants

View Source
const (

	// DefaultPERTShape is the t of the modified PERT when none is given.
	DefaultPERTShape = 4
)

Variables

This section is empty.

Functions

func PoissonCount

func PoissonCount(r *rand.Rand, lambda float64) uint64

PoissonCount draws one count for a rate that changes on every call, such as a mixed-Poisson rate drawn per period. The PTRS constants are built inline: a square root and a logarithm per call.

Types

type Bernoulli

type Bernoulli struct {
	// contains filtered or unexported fields
}

Bernoulli is a single trial with success probability p.

func NewBernoulli

func NewBernoulli(p float64) (Bernoulli, error)

NewBernoulli validates 0 ≤ p ≤ 1.

func (Bernoulli) CDF

func (d Bernoulli) CDF(x float64) float64

func (Bernoulli) Fill

func (d Bernoulli) Fill(r *rand.Rand, dst []float64)

func (Bernoulli) Laplace

func (Bernoulli) Laplace(complex128) (complex128, bool)

func (Bernoulli) Mean

func (d Bernoulli) Mean() float64

func (Bernoulli) Nodes

func (Bernoulli) Nodes(_, _ []float64) bool

func (Bernoulli) PMF

func (d Bernoulli) PMF(x float64) float64

func (Bernoulli) Quantile

func (d Bernoulli) Quantile(p float64) float64

func (Bernoulli) QuantileUpper

func (d Bernoulli) QuantileUpper(q float64) float64

func (Bernoulli) Support

func (d Bernoulli) Support() (lo, hi float64)

func (Bernoulli) Survival

func (d Bernoulli) Survival(x float64) float64

func (Bernoulli) Var

func (d Bernoulli) Var() float64

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 NewBeta

func NewBeta(alpha, beta float64) (Beta, error)

NewBeta validates alpha > 0 and beta > 0.

func NewBetaFromMeanSD

func NewBetaFromMeanSD(mean, sd float64) (Beta, error)

NewBetaFromMeanSD converts a mean and standard deviation; it fails when no Beta has them, that is when sd² ≥ mean·(1 − mean).

func (Beta) CDF

func (d Beta) CDF(x float64) float64

func (Beta) Fill

func (d Beta) Fill(r *rand.Rand, dst []float64)

func (Beta) Laplace

func (Beta) Laplace(complex128) (complex128, bool)

func (Beta) Mean

func (d Beta) Mean() float64

func (Beta) Nodes

func (d Beta) Nodes(x, w []float64) bool

func (Beta) Quantile

func (d Beta) Quantile(p float64) float64

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

func (d Beta) QuantileUpper(q float64) float64

QuantileUpper inverts the survival function the same way; the seed uses the mirrored inverse.

func (Beta) Support

func (d Beta) Support() (lo, hi float64)

func (Beta) Survival

func (d Beta) Survival(x float64) float64

func (Beta) Var

func (d Beta) Var() float64

type Binomial

type Binomial struct {
	// contains filtered or unexported fields
}

Binomial counts successes in n trials with probability p.

func NewBinomial

func NewBinomial(n, p float64) (Binomial, error)

NewBinomial validates n ≥ 0 integer and 0 ≤ p ≤ 1.

func (Binomial) CDF

func (d Binomial) CDF(x float64) float64

func (Binomial) Fill

func (d Binomial) Fill(r *rand.Rand, dst []float64)

func (Binomial) Laplace

func (Binomial) Laplace(complex128) (complex128, bool)

func (Binomial) Mean

func (d Binomial) Mean() float64

func (Binomial) Nodes

func (Binomial) Nodes(_, _ []float64) bool

func (Binomial) PMF

func (d Binomial) PMF(x float64) float64

func (Binomial) Quantile

func (d Binomial) Quantile(p float64) float64

func (Binomial) QuantileUpper

func (d Binomial) QuantileUpper(q float64) float64

QuantileUpper is the smallest k with Survival(k) ≤ q.

func (Binomial) Support

func (d Binomial) Support() (lo, hi float64)

func (Binomial) Survival

func (d Binomial) Survival(x float64) float64

func (Binomial) Var

func (d Binomial) Var() float64

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) Atom

func (d Capped) Atom() float64

Atom is the probability mass at the cap.

func (Capped) CDF

func (d Capped) CDF(x float64) float64

func (Capped) Fill

func (d Capped) Fill(r *rand.Rand, dst []float64)

func (Capped) Laplace

func (Capped) Laplace(complex128) (complex128, bool)

func (Capped) Mean

func (d Capped) Mean() float64

func (Capped) Nodes

func (Capped) Nodes(_, _ []float64) bool

func (Capped) Quantile

func (d Capped) Quantile(p float64) float64

func (Capped) QuantileUpper

func (d Capped) QuantileUpper(q float64) float64

func (Capped) Support

func (d Capped) Support() (lo, hi float64)

func (Capped) Survival

func (d Capped) Survival(x float64) float64

func (Capped) Var

func (d Capped) Var() float64

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) Fill

func (d Categorical) Fill(r *rand.Rand, dst []float64)

func (Categorical) Laplace

func (d Categorical) Laplace(s complex128) (complex128, bool)

Categorical: the exact finite mixture.

func (Categorical) Len

func (d Categorical) Len() int

Len returns the number of entries.

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

func NewConstant(c float64) (Constant, error)

NewConstant validates that the value is finite.

func (Constant) CDF

func (d Constant) CDF(x float64) float64

func (Constant) Fill

func (d Constant) Fill(_ *rand.Rand, dst []float64)

func (Constant) Laplace

func (d Constant) Laplace(s complex128) (complex128, bool)

func (Constant) Mean

func (d Constant) Mean() float64

func (Constant) Nodes

func (d Constant) Nodes(x, w []float64) bool

func (Constant) Quantile

func (d Constant) Quantile(float64) float64

func (Constant) QuantileUpper

func (d Constant) QuantileUpper(float64) float64

func (Constant) Support

func (d Constant) Support() (lo, hi float64)

func (Constant) Survival

func (d Constant) Survival(x float64) float64

func (Constant) Var

func (d Constant) Var() float64

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) Fill

func (d Exponential) Fill(r *rand.Rand, dst []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 NewGPD

func NewGPD(mu, sigma, xi float64) (GPD, error)

NewGPD validates sigma > 0 and finite mu and xi.

func (GPD) CDF

func (d GPD) CDF(x float64) float64

func (GPD) Fill

func (d GPD) Fill(r *rand.Rand, dst []float64)

func (GPD) Laplace

func (GPD) Laplace(complex128) (complex128, bool)

func (GPD) Mean

func (d GPD) Mean() float64

Mean is +Inf for xi ≥ 1.

func (GPD) Nodes

func (GPD) Nodes(_, _ []float64) bool

func (GPD) Quantile

func (d GPD) Quantile(p float64) float64

Quantile works from p directly, so the lower tail keeps its precision.

func (GPD) QuantileUpper

func (d GPD) QuantileUpper(q float64) float64

func (GPD) Support

func (d GPD) Support() (lo, hi float64)

func (GPD) Survival

func (d GPD) Survival(x float64) float64

func (GPD) Var

func (d GPD) Var() float64

Var is +Inf for xi ≥ ½.

type Gamma

type Gamma struct {
	// contains filtered or unexported fields
}

Gamma is the Gamma distribution with shape k and scale θ.

func NewGamma

func NewGamma(shape, scale float64) (Gamma, error)

NewGamma validates shape > 0 and scale > 0.

func NewGammaFromMeanSD

func NewGammaFromMeanSD(mean, sd float64) (Gamma, error)

NewGammaFromMeanSD converts a mean and standard deviation.

func NewGammaFromShapeRate

func NewGammaFromShapeRate(shape, rate float64) (Gamma, error)

NewGammaFromShapeRate converts a shape and rate.

func (Gamma) CDF

func (d Gamma) CDF(x float64) float64

func (Gamma) Fill

func (d Gamma) Fill(r *rand.Rand, dst []float64)

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) Mean

func (d Gamma) Mean() float64

func (Gamma) Nodes

func (Gamma) Nodes(_, _ []float64) bool

func (Gamma) Quantile

func (d Gamma) Quantile(p float64) float64

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

func (d Gamma) QuantileUpper(q float64) float64

QuantileUpper inverts the survival function the same way.

func (Gamma) Support

func (d Gamma) Support() (lo, hi float64)

func (Gamma) Survival

func (d Gamma) Survival(x float64) float64

func (Gamma) Var

func (d Gamma) Var() float64

type Geometric

type Geometric struct {
	// contains filtered or unexported fields
}

Geometric counts failures before the first success; support {0, 1, …}.

func NewGeometric

func NewGeometric(p float64) (Geometric, error)

NewGeometric validates 0 < p ≤ 1.

func (Geometric) CDF

func (d Geometric) CDF(x float64) float64

CDF is 1 − (1−p)^(⌊k⌋+1).

func (Geometric) Fill

func (d Geometric) Fill(r *rand.Rand, dst []float64)

func (Geometric) Laplace

func (Geometric) Laplace(complex128) (complex128, bool)

func (Geometric) Mean

func (d Geometric) Mean() float64

func (Geometric) Nodes

func (Geometric) Nodes(_, _ []float64) bool

func (Geometric) PMF

func (d Geometric) PMF(x float64) float64

func (Geometric) Quantile

func (d Geometric) Quantile(p float64) float64

Quantile is the smallest k with CDF(k) ≥ p.

func (Geometric) QuantileUpper

func (d Geometric) QuantileUpper(q float64) float64

QuantileUpper is the smallest k with Survival(k) ≤ q.

func (Geometric) Support

func (d Geometric) Support() (lo, hi float64)

func (Geometric) Survival

func (d Geometric) Survival(x float64) float64

func (Geometric) Var

func (d Geometric) Var() float64

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

func NewLogPERT(low, peak, high, t float64) (LogPERT, error)

NewLogPERT validates like NewPERT and additionally requires low > 0. Its moments are computed once by quadrature.

func (LogPERT) CDF

func (d LogPERT) CDF(x float64) float64

func (LogPERT) Fill

func (d LogPERT) Fill(r *rand.Rand, dst []float64)

func (LogPERT) Laplace

func (LogPERT) Laplace(complex128) (complex128, bool)

func (LogPERT) Mean

func (d LogPERT) Mean() float64

func (LogPERT) Nodes

func (LogPERT) Nodes(_, _ []float64) bool

func (LogPERT) Quantile

func (d LogPERT) Quantile(p float64) float64

func (LogPERT) QuantileUpper

func (d LogPERT) QuantileUpper(q float64) float64

func (LogPERT) Support

func (d LogPERT) Support() (lo, hi float64)

func (LogPERT) Survival

func (d LogPERT) Survival(x float64) float64

func (LogPERT) Var

func (d LogPERT) Var() float64

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) Fill

func (d LogUniform) Fill(r *rand.Rand, dst []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

func NewLognormal(mu, sigma float64) (Lognormal, error)

NewLognormal validates sigma > 0 and a finite mu.

func NewLognormalFromMeanSD

func NewLognormalFromMeanSD(mean, sd float64) (Lognormal, error)

NewLognormalFromMeanSD converts a mean and standard deviation.

func NewLognormalFromMedianP90

func NewLognormalFromMedianP90(median, p90 float64) (Lognormal, error)

NewLognormalFromMedianP90 converts a median and a 90th percentile.

func (Lognormal) CDF

func (d Lognormal) CDF(x float64) float64

func (Lognormal) Fill

func (d Lognormal) Fill(r *rand.Rand, dst []float64)

func (Lognormal) Laplace

func (Lognormal) Laplace(complex128) (complex128, bool)

func (Lognormal) Mean

func (d Lognormal) Mean() float64

func (Lognormal) Nodes

func (Lognormal) Nodes(_, _ []float64) bool

func (Lognormal) Quantile

func (d Lognormal) Quantile(p float64) float64

func (Lognormal) QuantileUpper

func (d Lognormal) QuantileUpper(q float64) float64

func (Lognormal) Support

func (d Lognormal) Support() (lo, hi float64)

func (Lognormal) Survival

func (d Lognormal) Survival(x float64) float64

func (Lognormal) Var

func (d Lognormal) Var() float64

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) Fill

func (d NegativeBinomial) Fill(r *rand.Rand, dst []float64)

func (NegativeBinomial) Laplace

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 NewNormal

func NewNormal(mu, sigma float64) (Normal, error)

NewNormal validates sigma > 0 and a finite mu.

func (Normal) CDF

func (d Normal) CDF(x float64) float64

func (Normal) Fill

func (d Normal) Fill(r *rand.Rand, dst []float64)

func (Normal) Laplace

func (Normal) Laplace(complex128) (complex128, bool)

func (Normal) Mean

func (d Normal) Mean() float64

func (Normal) Nodes

func (Normal) Nodes(_, _ []float64) bool

func (Normal) Quantile

func (d Normal) Quantile(p float64) float64

func (Normal) QuantileUpper

func (d Normal) QuantileUpper(q float64) float64

func (Normal) Support

func (d Normal) Support() (lo, hi float64)

func (Normal) Survival

func (d Normal) Survival(x float64) float64

func (Normal) Var

func (d Normal) Var() float64

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

func NewPERT(low, peak, high, t float64) (PERT, error)

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) CDF

func (d PERT) CDF(x float64) float64

func (PERT) Fill

func (d PERT) Fill(r *rand.Rand, dst []float64)

func (PERT) Laplace

func (PERT) Laplace(complex128) (complex128, bool)

func (PERT) Mean

func (d PERT) Mean() float64

func (PERT) Nodes

func (d PERT) Nodes(x, w []float64) bool

func (PERT) Quantile

func (d PERT) Quantile(p float64) float64

func (PERT) QuantileUpper

func (d PERT) QuantileUpper(q float64) float64

func (PERT) Shape

func (d PERT) Shape() (alpha, beta float64)

Shape returns the Beta shapes behind the PERT.

func (PERT) Support

func (d PERT) Support() (lo, hi float64)

func (PERT) Survival

func (d PERT) Survival(x float64) float64

func (PERT) Var

func (d PERT) Var() float64

type PMFer

type PMFer interface {
	PMF(x float64) float64
}

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 NewPareto

func NewPareto(xm, alpha float64) (Pareto, error)

NewPareto validates xm > 0 and alpha > 0.

func (Pareto) CDF

func (d Pareto) CDF(x float64) float64

func (Pareto) Fill

func (d Pareto) Fill(r *rand.Rand, dst []float64)

func (Pareto) Laplace

func (Pareto) Laplace(complex128) (complex128, bool)

func (Pareto) Mean

func (d Pareto) Mean() float64

Mean is +Inf for alpha ≤ 1.

func (Pareto) Nodes

func (Pareto) Nodes(_, _ []float64) bool

func (Pareto) Quantile

func (d Pareto) Quantile(p float64) float64

func (Pareto) QuantileUpper

func (d Pareto) QuantileUpper(q float64) float64

func (Pareto) Support

func (d Pareto) Support() (lo, hi float64)

func (Pareto) Survival

func (d Pareto) Survival(x float64) float64

func (Pareto) Var

func (d Pareto) Var() float64

Var is +Inf for alpha ≤ 2.

type Poisson

type Poisson struct {
	// contains filtered or unexported fields
}

Poisson is the Poisson distribution with a fixed rate.

func NewPoisson

func NewPoisson(lambda float64) (Poisson, error)

NewPoisson validates lambda ≥ 0 and finite.

func (Poisson) CDF

func (d Poisson) CDF(x float64) float64

func (Poisson) Fill

func (d Poisson) Fill(r *rand.Rand, dst []float64)

func (Poisson) Laplace

func (Poisson) Laplace(complex128) (complex128, bool)

func (Poisson) Mean

func (d Poisson) Mean() float64

func (Poisson) Nodes

func (Poisson) Nodes(_, _ []float64) bool

func (Poisson) PMF

func (d Poisson) PMF(x float64) float64

PMF returns P(K = x); zero off the integers.

func (Poisson) Quantile

func (d Poisson) Quantile(p float64) float64

func (Poisson) QuantileUpper

func (d Poisson) QuantileUpper(q float64) float64

func (Poisson) Support

func (d Poisson) Support() (lo, hi float64)

func (Poisson) Survival

func (d Poisson) Survival(x float64) float64

func (Poisson) Var

func (d Poisson) Var() float64

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.

func (*Sampler) Next

func (s *Sampler) Next() float64

Next returns the next pre-drawn value, refilling the pool when empty.

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) Len

func (ss *SamplerSet) Len() int

Len returns the number of distributions.

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) Base added in v0.1.3

func (d Scaled) Base() Distribution

Base is X.

func (Scaled) CDF added in v0.1.3

func (d Scaled) CDF(x float64) float64

func (Scaled) Factor added in v0.1.3

func (d Scaled) Factor() float64

Factor is f.

func (Scaled) Fill added in v0.1.3

func (d Scaled) Fill(r *rand.Rand, dst []float64)

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) Mean added in v0.1.3

func (d Scaled) Mean() float64

func (Scaled) Nodes added in v0.1.3

func (d Scaled) Nodes(x, w []float64) bool

Nodes are X's nodes times f, with X's weights.

func (Scaled) Quantile added in v0.1.3

func (d Scaled) Quantile(p float64) float64

func (Scaled) QuantileUpper added in v0.1.3

func (d Scaled) QuantileUpper(q float64) float64

func (Scaled) Support added in v0.1.3

func (d Scaled) Support() (lo, hi float64)

func (Scaled) Survival added in v0.1.3

func (d Scaled) Survival(x float64) float64

func (Scaled) Var added in v0.1.3

func (d Scaled) Var() float64

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) Fill

func (d Triangular) Fill(r *rand.Rand, dst []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 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) CDF

func (d Truncated) CDF(x float64) float64

func (Truncated) Fill

func (d Truncated) Fill(r *rand.Rand, dst []float64)

func (Truncated) Laplace

func (Truncated) Laplace(complex128) (complex128, bool)

func (Truncated) Mean

func (d Truncated) Mean() float64

func (Truncated) Nodes

func (Truncated) Nodes(_, _ []float64) bool

func (Truncated) Quantile

func (d Truncated) Quantile(p float64) float64

func (Truncated) QuantileUpper

func (d Truncated) QuantileUpper(q float64) float64

func (Truncated) Support

func (d Truncated) Support() (lo, hi float64)

func (Truncated) Survival

func (d Truncated) Survival(x float64) float64

func (Truncated) Var

func (d Truncated) Var() float64

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) Fill

func (d TruncatedNormal) Fill(r *rand.Rand, dst []float64)

func (TruncatedNormal) Laplace

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

func NewUniform(a, b float64) (Uniform, error)

NewUniform validates a < b, both finite, with a finite span.

func (Uniform) CDF

func (d Uniform) CDF(x float64) float64

func (Uniform) Fill

func (d Uniform) Fill(r *rand.Rand, dst []float64)

func (Uniform) Laplace

func (d Uniform) Laplace(s complex128) (complex128, bool)

func (Uniform) Mean

func (d Uniform) Mean() float64

func (Uniform) Nodes

func (d Uniform) Nodes(x, w []float64) bool

func (Uniform) Quantile

func (d Uniform) Quantile(p float64) float64

func (Uniform) QuantileUpper

func (d Uniform) QuantileUpper(q float64) float64

func (Uniform) Support

func (d Uniform) Support() (lo, hi float64)

func (Uniform) Survival

func (d Uniform) Survival(x float64) float64

func (Uniform) Var

func (d Uniform) Var() float64

type Weibull

type Weibull struct {
	// contains filtered or unexported fields
}

Weibull has shape k and scale lambda.

func NewWeibull

func NewWeibull(shape, scale float64) (Weibull, error)

NewWeibull validates shape > 0 and scale > 0.

func (Weibull) CDF

func (d Weibull) CDF(x float64) float64

func (Weibull) Fill

func (d Weibull) Fill(r *rand.Rand, dst []float64)

func (Weibull) Laplace

func (Weibull) Laplace(complex128) (complex128, bool)

func (Weibull) Mean

func (d Weibull) Mean() float64

func (Weibull) Nodes

func (Weibull) Nodes(_, _ []float64) bool

func (Weibull) Quantile

func (d Weibull) Quantile(p float64) float64

func (Weibull) QuantileUpper

func (d Weibull) QuantileUpper(q float64) float64

func (Weibull) Support

func (d Weibull) Support() (lo, hi float64)

func (Weibull) Survival

func (d Weibull) Survival(x float64) float64

func (Weibull) Var

func (d Weibull) Var() float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL