package
1.2.12
Repository: https://github.com/cpmech/gosl.git
Documentation: pkg.go.dev

# README

Gosl. rnd. Random numbers and probability distributions

Go Reference

The rnd package assists on computations involving stochastic processes. The package has many functions to generate pseudo-random numbers, probability distributions, and sampling techniques such as the Latin hypercube algorithm.

Pseudo random numbers

In this package, some standard Go functions from package rand are wrapped for convenience. Most random generation functions have an equivalent function wrapping the Mersenne Twister code from Mutsuo Saito and Makoto Matsumoto.

Some useful functions are:

  1. Init initialize the system with a seed
  2. Int, Ints, Float64, Float64s to generate integers and floats
  3. Shuffle and GetUnique functions to shuffle slices and filter slices with unique values, respectively.

Probability distributions

The probability distributions in the rnd package are initialized with the help of the VarData structure that contains the following main fields:

// input
D DistType // type of distribution
M float64  // mean
S float64  // standard deviation

// input: Frechet
L float64 // location
C float64 // scale
A float64 // shape

// input: uniform
Min float64 // min value
Max float64 // max value

The currently available distributions are:

  1. NormalKind Normal distribution
  2. LognormalKind Lognormal distribution
  3. GumbelKind Type I Extreme Value distribution
  4. FrechetKind Type II Extreme Value distribution
  5. UniformKind Uniform distribution

Sampling algorithms: Halton and Latin Hypercube methods

The HaltonPoints function is a simple way to generate combinations of point coordinates in a hypercube.

The LatinIHS function implements the Latin improved distributed hypercube sampling method. The results are the indices of points. The point coordinates can be computed with the HypercubeCoords function.

Examples

Generate 100,000 integers and draw Histogram

Go code:

	// initialize seed with fixed number; use 0 to use current time
	rnd.Init(1234)

	// allocate slice for integers
	nsamples := 100000
	nints := 10
	vals := make([]int, nsamples)

	// using the rnd.Int function
	t0 := time.Now()
	for i := 0; i < nsamples; i++ {
		vals[i] = rnd.Int(0, nints-1)
	}
	io.Pf("time elapsed = %v\n", time.Now().Sub(t0))

	// text histogram
	hist := rnd.IntHistogram{Stations: utl.IntRange(nints + 1)}
	hist.Count(vals, true)
	io.Pf(rnd.TextHist(hist.GenLabels("%d"), hist.Counts, 60))

	// using the rnd.Ints function
	t0 = time.Now()
	rnd.Ints(vals, 0, nints-1)
	io.Pf("time elapsed = %v\n", time.Now().Sub(t0))

	// text histogram
	hist.Count(vals, true)
	io.Pf(rnd.TextHist(hist.GenLabels("%d"), hist.Counts, 60))

Output:

time elapsed = 3.506121ms
  [0,1) |  10085 ############################################################
  [1,2) |   9874 ###########################################################
  [2,3) |  10078 ############################################################
  [3,4) |   9998 ############################################################
  [4,5) |   9937 ###########################################################
  [5,6) |  10003 ############################################################
  [6,7) |  10119 #############################################################
  [7,8) |   9795 ###########################################################
  [8,9) |  10026 ############################################################
 [9,10) |  10085 ############################################################
  count = 100000
time elapsed = 3.259988ms
  [0,1) |  10077 ############################################################
  [1,2) |  10017 ############################################################
  [2,3) |   9910 ###########################################################
  [3,4) |  10092 ############################################################
  [4,5) |   9853 ###########################################################
  [5,6) |   9976 ############################################################
  [6,7) |  10096 #############################################################
  [7,8) |  10058 ############################################################
  [8,9) |   9905 ###########################################################
 [9,10) |  10016 ############################################################
  count = 100000

Generate samples based on the Lognormal distribution

Go code:

	// initialize generator
	rnd.Init(1234)

	// parameters
	μ := 1.0
	σ := 0.25

	// generate samples
	nsamples := 1000
	X := make([]float64, nsamples)
	for i := 0; i < nsamples; i++ {
		X[i] = rnd.Lognormal(μ, σ)
	}

	// constants
	nstations := 41        // number of bins + 1
	xmin, xmax := 0.0, 3.0 // limits for histogram

	// build histogram: count number of samples within each bin
	var hist rnd.Histogram
	hist.Stations = utl.LinSpace(xmin, xmax, nstations)
	hist.Count(X, true)

	// compute area of density diagram
	area := hist.DensityArea(nsamples)
	io.Pf("area = %v\n", area)

	// lognormal distribution
	var dist rnd.DistLogNormal
	dist.Init(&rnd.Variable{M: μ, S: σ})
  
	// compute lognormal points
	n := 101
	x := utl.LinSpace(0, 3, n)
	y := make([]float64, n)
	Y := make([]float64, n)
	for i := 0; i < n; i++ {
		y[i] = dist.Pdf(x[i])
		Y[i] = dist.Cdf(x[i])
	}

Example: sampling algorithms

	// initialize generator
	rnd.Init(1234)

	// Halton points
	ndim := 2
	npts := 100
	Xhps := rnd.HaltonPoints(ndim, npts)

	// Latin Hypercube
	dupfactor := 5
	lhs := rnd.LatinIHS(ndim, npts, dupfactor)
	xmin := []float64{0, 0}
	xmax := []float64{1, 1}
	Xlhs := rnd.HypercubeCoords(lhs, xmin, xmax)

API

Please see the documentation here

# Functions

BuildTextHist builds a text histogram Input: xmin -- station xmin xmax -- station xmax nstations -- number of stations values -- values to be counted numfmt -- number format barlen -- max length of bar.
FlipCoin generates a Bernoulli variable; throw a coin with probability p.
Float64 generates a pseudo random real number between low and high; i.e.
Float64s generates pseudo random real numbers between low and high; i.e.
GetDistrib returns a distribution from factory.
HaltonPoints generates randomly spaced points x -- [dim][n] points.
HypercubeCoords computes the coordinates in the hypercube Input: sample -- the hypercube sampling indices; e.g.
Init initializes random numbers generators Input: seed -- seed value; use seed <= 0 to use current time.
Int generates pseudo random integer between low and high.
IntGetGroups randomly selects indices from pool separating them in groups Input: pool -- all ints.
IntGetShuffled returns a shufled slice of integers.
IntGetUnique randomly selects n items in a list avoiding duplicates Note: using the 'reservoir sampling' method; see Wikipedia: https://en.wikipedia.org/wiki/Reservoir_sampling.
IntGetUniqueN randomly selects n items from start to endp1-1 avoiding duplicates Note: using the 'reservoir sampling' method; see Wikipedia: https://en.wikipedia.org/wiki/Reservoir_sampling.
Ints generates pseudo random integers between low and high.
IntShuffle shuffles a slice of integers.
LatinIHS implements the improved distributed hypercube sampling algorithm.
Lognormal returns a random number belonging to a lognormal distribution.
MTfloat64 generates pseudo random real numbers between low and high; i.e.
MTfloat64s generates pseudo random real numbers between low and high; i.e.
MTinit initializes random numbers generators (Mersenne Twister code) Input: seed -- seed value; use seed <= 0 to use current time.
MTint generates pseudo random integer between low and high using the Mersenne Twister method.
MTints generates pseudo random integers between low and high using the Mersenne Twister method.
MTintShuffle shuffles a slice of integers using Mersenne Twister algorithm.
Normal returns a random number belonging to a normal distribution.
Shuffle shuffles a slice of float point numbers.
StatAve computes the average of x values Input: x -- sample Output: xave -- average.
StatAveDev computes the average of x and the average deviation or standard deviation (σ) Input: x -- sample std -- compute standard deviation (σ) instead of average deviation (adev) Output: xdev -- average deviation; if std==true, computes standard deviation (σ) instead.
StatBasic performs some basic statistics Input: x -- sample std -- compute standard deviation (σ) instead of average deviation (adev) Output: xmin -- minimum value xave -- mean average (first moment) xmax -- maximum value xdev -- average deviation; if std==true, computes standard deviation (σ) instead.
StatDev computes the average deviation or standard deviation (σ) Input: x -- sample std -- compute standard deviation (σ) instead of average deviation (adev) Output: xdev -- average deviation; if std==true, computes standard deviation (σ) instead.
StatDevFirst computes the average deviation or standard deviation (σ) for given value of average/mean/first moment Input: x -- sample xave -- bar(x) == average/mean/first moment std -- compute standard deviation (σ) instead of average deviation (adev) Output: xdev -- average deviation; if std==true, computes standard deviation (σ) instead.
StatDur generates stat about duration.
StatMoments computes the 4th moments of a data set Input: x -- sample Output: sum -- sum of values mean -- mean average (first moment) adev -- average deviation sdev -- standrad deviation vari -- variance (second moment) skew -- skewness (third moment) kurt -- kurtosis (fourth moment) Based on: Press WH, Teukolsky SA, Vetterling WT and Flannery BP (2007) Numerical Recipes in C++ 2007 (3rd Edition), page 725.
StatTable computes the min, ave, max, and dev of values organised in a table Input: x -- sample std -- compute standard deviation (σ) instead of average deviation (adev) withZ -- computes z-matrix as well Convention of indices: 0=min 1=ave 2=max 3=dev Output: min ave max dev ↓ ↓ ↓ ↓ x00 x01 x02 x03 x04 x05 → y00=min(x0?) y10=ave(x0?) y20=max(x0?) y30=dev(x0?) x10 x11 x12 x13 x14 x15 → y01=min(x1?) y11=ave(x1?) y21=max(x1?) y31=dev(x1?) x20 x21 x22 x23 x24 x25 → y02=min(x2?) y12=ave(x2?) y22=max(x2?) y32=dev(x2?) ↓ ↓ ↓ ↓ min → z00=min(y0?) z01=min(y1?) z02=min(y2?) z03=min(y3?) ave → z10=ave(y0?) z11=ave(y1?) z12=ave(y2?) z13=ave(y3?) max → z20=max(y0?) z21=max(y1?) z22=max(y2?) z23=max(y3?) dev → z30=dev(y0?) z31=dev(y1?) z32=dev(y2?) z33=dev(y3?) = = = = min → z00=min(min) z01=min(ave) z02=min(max) z03=min(dev) ave → z10=ave(min) z11=ave(ave) z12=ave(max) z13=ave(dev) max → z20=max(min) z21=max(ave) z22=max(max) z23=max(dev) dev → z30=dev(min) z31=dev(ave) z32=dev(max) z33=dev(dev).
StdInvPhi implements Φ⁻¹(x), the inverse standard cumulative distribution function.
Stdphi implements φ(x), the standard probability density function.
StdPhi implements Φ(x), the standard cumulative distribution function.
TextHist prints a text histogram Input: labels -- labels counts -- frequencies.
Uniform returns a random number belonging to a uniform distribution.
UnitVectors generates random unit vectors in 3D.

# Variables

Primes1000 contains 1000 prime numbers.

# Structs

DistFrechet implements the Frechet / Type II Extreme Value Distribution (largest value).
DistGumbel implements the Gumbel / Type I Extreme Value Distribution (largest value).
DistLogNormal implements the lognormal distribution.
DistNormal implements the normal distribution.
DistUniform implements the normal distribution.
Histogram holds data for computing/plotting histograms bin[i] corresponds to station[i] <= x < station[i+1] [ bin[0] )[ bin[1] )[ bin[2] )[ bin[3] )[ bin[4] ) ---|---------|---------|---------|---------|---------|--- x s[0] s[1] s[2] s[3] s[4] s[5] .
IntHistogram holds data for computing/plotting histograms with integers bin[i] corresponds to station[i] <= x < station[i+1] [ bin[0] )[ bin[1] )[ bin[2] )[ bin[3] )[ bin[4] ) ---|---------|---------|---------|---------|---------|--- x s[0] s[1] s[2] s[3] s[4] s[5] .
Variable holds all data defining a single random variable including information about a probability distribution that bests represents this variable Some distributions: "N" : Normal "L" : Lognormal "G" : Gumbel (Type I Extreme Value) "F" : Frechet (Type II Extreme Value) "U" : Uniform .

# Interfaces

Distribution defines a probability distribution.

# Type aliases

Variables implements a set of random variables.