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