gonum.org/v1/gonum@v0.14.0/stat/distuv/bernoulli.go (about) 1 // Copyright ©2016 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package distuv 6 7 import ( 8 "math" 9 10 "golang.org/x/exp/rand" 11 ) 12 13 // Bernoulli represents a random variable whose value is 1 with probability p and 14 // value of zero with probability 1-P. The value of P must be between 0 and 1. 15 // More information at https://en.wikipedia.org/wiki/Bernoulli_distribution. 16 type Bernoulli struct { 17 P float64 18 Src rand.Source 19 } 20 21 // CDF computes the value of the cumulative density function at x. 22 func (b Bernoulli) CDF(x float64) float64 { 23 if x < 0 { 24 return 0 25 } 26 if x < 1 { 27 return 1 - b.P 28 } 29 return 1 30 } 31 32 // Entropy returns the entropy of the distribution. 33 func (b Bernoulli) Entropy() float64 { 34 if b.P == 0 || b.P == 1 { 35 return 0 36 } 37 q := 1 - b.P 38 return -b.P*math.Log(b.P) - q*math.Log(q) 39 } 40 41 // ExKurtosis returns the excess kurtosis of the distribution. 42 func (b Bernoulli) ExKurtosis() float64 { 43 pq := b.P * (1 - b.P) 44 return (1 - 6*pq) / pq 45 } 46 47 // LogProb computes the natural logarithm of the value of the probability density function at x. 48 func (b Bernoulli) LogProb(x float64) float64 { 49 if x == 0 { 50 return math.Log(1 - b.P) 51 } 52 if x == 1 { 53 return math.Log(b.P) 54 } 55 return math.Inf(-1) 56 } 57 58 // Mean returns the mean of the probability distribution. 59 func (b Bernoulli) Mean() float64 { 60 return b.P 61 } 62 63 // Median returns the median of the probability distribution. 64 func (b Bernoulli) Median() float64 { 65 p := b.P 66 switch { 67 case p < 0.5: 68 return 0 69 case p > 0.5: 70 return 1 71 default: 72 return 0.5 73 } 74 } 75 76 // NumParameters returns the number of parameters in the distribution. 77 func (Bernoulli) NumParameters() int { 78 return 1 79 } 80 81 // Prob computes the value of the probability distribution at x. 82 func (b Bernoulli) Prob(x float64) float64 { 83 if x == 0 { 84 return 1 - b.P 85 } 86 if x == 1 { 87 return b.P 88 } 89 return 0 90 } 91 92 // Quantile returns the minimum value of x from amongst all those values whose CDF value exceeds or equals p. 93 func (b Bernoulli) Quantile(p float64) float64 { 94 if p < 0 || 1 < p { 95 panic(badPercentile) 96 } 97 if p <= 1-b.P { 98 return 0 99 } 100 return 1 101 } 102 103 // Rand returns a random sample drawn from the distribution. 104 func (b Bernoulli) Rand() float64 { 105 var rnd float64 106 if b.Src == nil { 107 rnd = rand.Float64() 108 } else { 109 rnd = rand.New(b.Src).Float64() 110 } 111 if rnd < b.P { 112 return 1 113 } 114 return 0 115 } 116 117 // Skewness returns the skewness of the distribution. 118 func (b Bernoulli) Skewness() float64 { 119 return (1 - 2*b.P) / math.Sqrt(b.P*(1-b.P)) 120 } 121 122 // StdDev returns the standard deviation of the probability distribution. 123 func (b Bernoulli) StdDev() float64 { 124 return math.Sqrt(b.Variance()) 125 } 126 127 // Survival returns the survival function (complementary CDF) at x. 128 func (b Bernoulli) Survival(x float64) float64 { 129 if x < 0 { 130 return 1 131 } 132 if x < 1 { 133 return b.P 134 } 135 return 0 136 } 137 138 // Variance returns the variance of the probability distribution. 139 func (b Bernoulli) Variance() float64 { 140 return b.P * (1 - b.P) 141 }