github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/stat/distuv/gumbel.go (about) 1 // Copyright ©2018 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 // GumbelRight implements the right-skewed Gumbel distribution, a two-parameter 14 // continuous distribution with support over the real numbers. The right-skewed 15 // Gumbel distribution is also sometimes known as the Extreme Value distribution. 16 // 17 // The right-skewed Gumbel distribution has density function 18 // 1/beta * exp(-(z + exp(-z))) 19 // z = (x - mu)/beta 20 // Beta must be greater than 0. 21 // 22 // For more information, see https://en.wikipedia.org/wiki/Gumbel_distribution. 23 type GumbelRight struct { 24 Mu float64 25 Beta float64 26 Src rand.Source 27 } 28 29 func (g GumbelRight) z(x float64) float64 { 30 return (x - g.Mu) / g.Beta 31 } 32 33 // CDF computes the value of the cumulative density function at x. 34 func (g GumbelRight) CDF(x float64) float64 { 35 z := g.z(x) 36 return math.Exp(-math.Exp(-z)) 37 } 38 39 // Entropy returns the differential entropy of the distribution. 40 func (g GumbelRight) Entropy() float64 { 41 return math.Log(g.Beta) + eulerMascheroni + 1 42 } 43 44 // ExKurtosis returns the excess kurtosis of the distribution. 45 func (g GumbelRight) ExKurtosis() float64 { 46 return 12.0 / 5 47 } 48 49 // LogProb computes the natural logarithm of the value of the probability density function at x. 50 func (g GumbelRight) LogProb(x float64) float64 { 51 z := g.z(x) 52 return -math.Log(g.Beta) - z - math.Exp(-z) 53 } 54 55 // Mean returns the mean of the probability distribution. 56 func (g GumbelRight) Mean() float64 { 57 return g.Mu + g.Beta*eulerMascheroni 58 } 59 60 // Median returns the median of the Gumbel distribution. 61 func (g GumbelRight) Median() float64 { 62 return g.Mu - g.Beta*math.Log(math.Ln2) 63 } 64 65 // Mode returns the mode of the normal distribution. 66 func (g GumbelRight) Mode() float64 { 67 return g.Mu 68 } 69 70 // NumParameters returns the number of parameters in the distribution. 71 func (GumbelRight) NumParameters() int { 72 return 2 73 } 74 75 // Prob computes the value of the probability density function at x. 76 func (g GumbelRight) Prob(x float64) float64 { 77 return math.Exp(g.LogProb(x)) 78 } 79 80 // Quantile returns the inverse of the cumulative probability distribution. 81 func (g GumbelRight) Quantile(p float64) float64 { 82 if p < 0 || 1 < p { 83 panic(badPercentile) 84 } 85 return g.Mu - g.Beta*math.Log(-math.Log(p)) 86 } 87 88 // Rand returns a random sample drawn from the distribution. 89 func (g GumbelRight) Rand() float64 { 90 var rnd float64 91 if g.Src == nil { 92 rnd = rand.ExpFloat64() 93 } else { 94 rnd = rand.New(g.Src).ExpFloat64() 95 } 96 return g.Mu - g.Beta*math.Log(rnd) 97 } 98 99 // Skewness returns the skewness of the distribution. 100 func (GumbelRight) Skewness() float64 { 101 return 12 * math.Sqrt(6) * apery / (math.Pi * math.Pi * math.Pi) 102 } 103 104 // StdDev returns the standard deviation of the probability distribution. 105 func (g GumbelRight) StdDev() float64 { 106 return (math.Pi / math.Sqrt(6)) * g.Beta 107 } 108 109 // Survival returns the survival function (complementary CDF) at x. 110 func (g GumbelRight) Survival(x float64) float64 { 111 return 1 - g.CDF(x) 112 } 113 114 // Variance returns the variance of the probability distribution. 115 func (g GumbelRight) Variance() float64 { 116 return math.Pi * math.Pi * g.Beta * g.Beta / 6 117 }