normal

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: 4 Imported by: 0

Documentation

Overview

Package normal implements a normal continuous random variable

normal distribution is a probability distribution parameterized by two parameters, mean (also called location) and standard deviation (also called scale)

References:

https://en.wikipedia.org/wiki/Normal_distribution http://mathworld.wolfram.com/NormalDistribution.html

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Normal

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

Normal is a continuous random variable for a normal probability distribution parameterized by mean and standard deviation.

Example

Example - Normal Distribution

package main

import (
	"fmt"
	"strings"

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

func main() {
	// Example based on
	// http://www.cplusplus.com/reference/random/normal_distribution/
	r := xorshift128plus.New(20170611)
	d := normal.New(r, 5.0, 2.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("Normal Distribution: mu=5.0, sigma=2.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:

Normal Distribution: mu=5.0, sigma=2.0
 0- 1: * (1)
 1- 2: **** (4)
 2- 3: ******** (8)
 3- 4: *************** (15)
 4- 5: ******************* (19)
 5- 6: ******************* (19)
 6- 7: ************** (14)
 7- 8: ******** (8)
 8- 9: **** (4)
 9-10: * (1)

func New

func New(r prng.Engine, m float64, s float64) *Normal

New returns a new Normal Distribution

func (*Normal) CDF

func (n *Normal) 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 normal distribution: CDF(x) = 0.5 * [ 1 + erf((x-mu)/sqrt(2)*sigma) ]

func (*Normal) Float64

func (n *Normal) Float64() float64

Float64 returns the next random number satisfying the underlying probability distribution

For normal distribution, Float64 return a float64 in [-Inf, Inf] Implementation here uses polynomial approximation of the inverse of the Normal distribution CDF

n = N()*sigma + mu

func (*Normal) GetParams

func (n *Normal) GetParams() []float64

GetParams returns the current parameters of the Distribution

func (*Normal) Init

func (n *Normal) Init(r prng.Engine, params ...float64)

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

Two float64 parameters are required for Normal distribution, namely, mu and sigma

where:

-inf < mu < inf and
sigma > 0

func (*Normal) PDF

func (n *Normal) PDF(x float64) float64

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

For normal distribution:

PDF(x) = 1/(sqrt(2*pi*sigma^2)) * e^(-(x-mu)^2/2*sigma^2)

Jump to

Keyboard shortcuts

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