uniform

package
v0.0.0-...-d740340 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package uniform implements a uniform continuous random variable

Uniform distribution is a probability distribution that has constant probability.

The distribution is parameterized by two parameters, namely, a and b, which are its minimum and maximum values, respectively.

References:

https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) http://mathworld.wolfram.com/UniformDistribution.html

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Uniform

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

Uniform is a continuous random variable for a uniform probability distribution parameterized by minimum and maximum values, namely, a and b, respectively.

Example

Example - Uniform Distribution

package main

import (
	"fmt"
	"strings"

	"github.com/shivakar/random/distribution/uniform"
	"github.com/shivakar/random/prng/xorshift128plus"
)

func main() {
	// Example based on
	// http://www.cplusplus.com/reference/random/normal_distribution/
	r := xorshift128plus.New(20170611)
	d := uniform.New(r, 3.0, 7.0)

	var p [10]int

	nrolls := 10000
	nstars := 100
	for i := 0; i < nrolls; i++ {
		n := d.Float64()
		if n >= 0.0 && n < 10.0 {
			p[int(n)]++
		}
	}

	fmt.Println("Uniform Distribution: a=3.0, b=7.0")

	for i := 0; i < 10; i++ {
		v := p[i] * nstars / nrolls

		fmt.Printf("%2d-%2d: %s (%d)\n", i, i+1,
			strings.Repeat("*", v), v)
	}

}
Output:
Uniform Distribution: a=3.0, b=7.0
 0- 1:  (0)
 1- 2:  (0)
 2- 3:  (0)
 3- 4: ************************ (24)
 4- 5: ************************* (25)
 5- 6: ************************* (25)
 6- 7: ************************ (24)
 7- 8:  (0)
 8- 9:  (0)
 9-10:  (0)

func New

func New(r prng.Engine, a float64, b float64) *Uniform

New returns a new Uniform Distribution

func (*Uniform) CDF

func (u *Uniform) CDF(x float64) float64

CDF or cumulative distribution function returns the probability that a real-valued random variable X of the probability distribution will be found to have a value less than or equal to x

For uniform distribution:

= 0           for x < a

CDF(x) = (x-a)/(b-a) for a <= x <= b

= 1           for x > b

func (*Uniform) Float64

func (u *Uniform) Float64() float64

Float64 returns the next random number satisfying the underlying probability distribution

For uniform distribution, since the underlying PRNG Engine already produces continuous uniform numbers between [0, 1), a simple linear transformation is sufficient to generate random variates

n = a + (b-a)*U

func (*Uniform) GetParams

func (u *Uniform) GetParams() []float64

GetParams returns the current parameters of the Distribution

func (*Uniform) Init

func (u *Uniform) Init(r prng.Engine, params ...float64)

Init initializes the uniform random variable with the supplied PRNG Engine and distribution parameters

Two float64 parameters are required for Uniform distribution, namely, a and b

where:

-inf < a < b < inf

func (*Uniform) PDF

func (u *Uniform) PDF(x float64) float64

PDF or probability distribution function returns the relative likelihood for the random variable to take on the given value

For uniform distribution:

PDF(x) = 1/(b-a) for a <= x <= b

= 0       for x < a or x > b

Jump to

Keyboard shortcuts

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