erand

package
v0.0.0-...-531e5d3 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2023 License: BSD-3-Clause Imports: 5 Imported by: 1

Documentation

Overview

Package erand provides randomization functionality built on top of standard math/rand random number generation functions. Includes:

  • RndParams: specifies parameters for random number generation according to various distributions used e.g., for initializing random weights and generating random noise in neurons
  • Permute*: basic convenience methods calling rand.Shuffle on e.g., []int slice

Index

Constants

This section is empty.

Variables

View Source
var KiT_RndDists = kit.Enums.AddEnum(RndDistsN, kit.NotBitFlag, nil)

Functions

func Bet

func Bet(a, b float64, thr int) float64

Beta returns beta random number with two shape parameters a > 0 and b > 0

func Binom

func Binom(n, p float64, thr int) float64

Binom returns binomial with n trials (par) each of probability p (var)

func BoolP

func BoolP(p float32) bool

BoolP is a simple method to generate a true value with given probability (else false). is just rand.Float32() < p but this is more readable and explicit

func BoolProb

func BoolProb(p float64, thr int) bool

BoolProp returns boolean true/false with given probability.

func Discrete

func Discrete(dist []float64, thr int) int

Discrete samples from a discrete distribution with probabilities given (automatically renormalizes the values). Returns the index of the element of dist.

func Gam

func Gam(v, k float64, thr int) float64

Gam represents maximum entropy distribution with two parameters: scaling parameter (Var, Beta) and shape parameter k (Par, Alpha)

func Gauss

func Gauss(stdev float64, thr int) float64

Gauss returns gaussian (normal) random number with given standard deviation

func IntMeanRange

func IntMeanRange(mean, rnge int64, thr int) int64

IntMeanRange returns uniform random integer with given range on either side of the mean: [mean - range, mean + range]

func IntMinMax

func IntMinMax(min, max int64, thr int) int64

IntMinMax returns uniform random integer in range between min and max, exclusive of max: [min,max).

func IntZeroN

func IntZeroN(n int64, thr int) int64

IntZeroN returns uniform random integer in the range between 0 and n, exclusive of n: [0,n).

func PChoose32

func PChoose32(ps []float32) int

PChoose32 chooses an index in given slice of float32's at random according to the probilities of each item (must be normalized to sum to 1)

func PChoose64

func PChoose64(ps []float64) int

PChoose64 chooses an index in given slice of float64's at random according to the probilities of each item (must be normalized to sum to 1)

func PermuteInts

func PermuteInts(ins []int)

PermuteInts permutes (shuffles) the order of elements in the given int slice using the standard Fisher-Yates shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle So you don't have to remember how to call rand.Shuffle

func PermuteStrings

func PermuteStrings(ins []string)

PermuteStrings permutes (shuffles) the order of elements in the given string slice using the standard Fisher-Yates shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle So you don't have to remember how to call rand.Shuffle

func Permutefloat32s

func Permutefloat32s(ins []float32)

PermuteFloat32s permutes (shuffles) the order of elements in the given float32 slice using the standard Fisher-Yates shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle So you don't have to remember how to call rand.Shuffle

func Permutefloat64s

func Permutefloat64s(ins []float64)

PermuteFloat64s permutes (shuffles) the order of elements in the given float64 slice using the standard Fisher-Yates shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle So you don't have to remember how to call rand.Shuffle

func Poiss

func Poiss(lmb float64, thr int) float64

Poiss returns poisson variable, as number of events in interval, with event rate (lmb = Var) plus mean

func UniformMeanRange

func UniformMeanRange(mean, rnge float64, thr int) float64

UniformMeanRange returns uniform random number with given range on either size of the mean: [mean - range, mean + range]

func UniformMinMax

func UniformMinMax(min, max float64, thr int) float64

UniformMinMax returns uniform random number between min and max values inclusive (Do not use for generating integers - will not include max!)

func ZeroOne

func ZeroOne(thr int) float64

ZeroOne returns a uniform random number between zero and one (exclusive of 1)

Types

type RndDists

type RndDists int

RndDists are different random number distributions

const (
	// Uniform has a uniform probability distribution over var = range on either side of the mean
	Uniform RndDists = iota

	// Binomial represents number of 1's in n (Par) random (Bernouli) trials of probability p (Var)
	Binomial

	// Poisson represents number of events in interval, with event rate (lambda = Var) plus mean
	Poisson

	// Gamma represents maximum entropy distribution with two parameters: scaling parameter (Var)
	// and shape parameter k (Par) plus mean
	Gamma

	// Gaussian normal with Var = stddev plus mean
	Gaussian

	// Beta with var = a and par = b shape parameters
	Beta

	// Mean is just the constant mean, no randomness
	Mean

	RndDistsN
)

The random number distributions

func (*RndDists) FromString

func (i *RndDists) FromString(s string) error

func (RndDists) MarshalJSON

func (ev RndDists) MarshalJSON() ([]byte, error)

func (RndDists) String

func (i RndDists) String() string

func (*RndDists) UnmarshalJSON

func (ev *RndDists) UnmarshalJSON(b []byte) error

type RndParams

type RndParams struct {
	Dist RndDists `desc:"distribution to generate random numbers from"`
	Mean float64  `desc:"mean of random distribution -- typically added to generated random variants"`
	Var  float64  `` /* 145-byte string literal not displayed */
	Par  float64  `view:"if Dist=Gamma,Binomial,Beta" desc:"extra parameter for distribution (depends on each one)"`
}

RndParams provides parameterized random number generation according to different distributions and variance, mean params

func (*RndParams) Density

func (rp *RndParams) Density(s float64) float64

Density returns density of random variable according to current params, at given x value

func (*RndParams) Gen

func (rp *RndParams) Gen(thr int) float64

Gen generates a random variable according to current parameters. (0 <= thr < 100) specifies thread or dmem proc number for parallel safe random sequences (-1 = taMisc::dmem_proc for auto-safe dmem)

Jump to

Keyboard shortcuts

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