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 (*Normal) CDF ¶
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 ¶
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