aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gonum.org/v1/gonum/stat/distuv/gumbel.go
blob: 1c25f4a1059dff8856fa4c6b9e35d2e1e1368dbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright ©2018 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package distuv

import (
	"math"

	"golang.org/x/exp/rand"
)

// GumbelRight implements the right-skewed Gumbel distribution, a two-parameter
// continuous distribution with support over the real numbers. The right-skewed
// Gumbel distribution is also sometimes known as the Extreme Value distribution.
//
// The right-skewed Gumbel distribution has density function
//
//	1/beta * exp(-(z + exp(-z)))
//	z = (x - mu)/beta
//
// Beta must be greater than 0.
//
// For more information, see https://en.wikipedia.org/wiki/Gumbel_distribution.
type GumbelRight struct {
	Mu   float64
	Beta float64
	Src  rand.Source
}

func (g GumbelRight) z(x float64) float64 {
	return (x - g.Mu) / g.Beta
}

// CDF computes the value of the cumulative density function at x.
func (g GumbelRight) CDF(x float64) float64 {
	z := g.z(x)
	return math.Exp(-math.Exp(-z))
}

// Entropy returns the differential entropy of the distribution.
func (g GumbelRight) Entropy() float64 {
	return math.Log(g.Beta) + eulerMascheroni + 1
}

// ExKurtosis returns the excess kurtosis of the distribution.
func (g GumbelRight) ExKurtosis() float64 {
	return 12.0 / 5
}

// LogProb computes the natural logarithm of the value of the probability density function at x.
func (g GumbelRight) LogProb(x float64) float64 {
	z := g.z(x)
	return -math.Log(g.Beta) - z - math.Exp(-z)
}

// Mean returns the mean of the probability distribution.
func (g GumbelRight) Mean() float64 {
	return g.Mu + g.Beta*eulerMascheroni
}

// Median returns the median of the Gumbel distribution.
func (g GumbelRight) Median() float64 {
	return g.Mu - g.Beta*math.Log(math.Ln2)
}

// Mode returns the mode of the normal distribution.
func (g GumbelRight) Mode() float64 {
	return g.Mu
}

// NumParameters returns the number of parameters in the distribution.
func (GumbelRight) NumParameters() int {
	return 2
}

// Prob computes the value of the probability density function at x.
func (g GumbelRight) Prob(x float64) float64 {
	return math.Exp(g.LogProb(x))
}

// Quantile returns the inverse of the cumulative probability distribution.
func (g GumbelRight) Quantile(p float64) float64 {
	if p < 0 || 1 < p {
		panic(badPercentile)
	}
	return g.Mu - g.Beta*math.Log(-math.Log(p))
}

// Rand returns a random sample drawn from the distribution.
func (g GumbelRight) Rand() float64 {
	var rnd float64
	if g.Src == nil {
		rnd = rand.ExpFloat64()
	} else {
		rnd = rand.New(g.Src).ExpFloat64()
	}
	return g.Mu - g.Beta*math.Log(rnd)
}

// Skewness returns the skewness of the distribution.
func (GumbelRight) Skewness() float64 {
	return 12 * math.Sqrt(6) * apery / (math.Pi * math.Pi * math.Pi)
}

// StdDev returns the standard deviation of the probability distribution.
func (g GumbelRight) StdDev() float64 {
	return (math.Pi / math.Sqrt(6)) * g.Beta
}

// Survival returns the survival function (complementary CDF) at x.
func (g GumbelRight) Survival(x float64) float64 {
	return 1 - g.CDF(x)
}

// Variance returns the variance of the probability distribution.
func (g GumbelRight) Variance() float64 {
	return math.Pi * math.Pi * g.Beta * g.Beta / 6
}