/
generalizedextremevalue.jl
275 lines (222 loc) · 7.65 KB
/
generalizedextremevalue.jl
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
GeneralizedExtremeValue(μ, σ, ξ)
The *Generalized extreme value distribution* with shape parameter `ξ`, scale `σ` and location `μ` has probability density function
```math
f(x; \\xi, \\sigma, \\mu) = \\begin{cases}
\\frac{1}{\\sigma} \\left[ 1+\\left(\\frac{x-\\mu}{\\sigma}\\right)\\xi\\right]^{-1/\\xi-1} \\exp\\left\\{-\\left[ 1+ \\left(\\frac{x-\\mu}{\\sigma}\\right)\\xi\\right]^{-1/\\xi} \\right\\} & \\text{for } \\xi \\neq 0 \\\\\\
\\frac{1}{\\sigma} \\exp\\left\\{-\\frac{x-\\mu}{\\sigma}\\right\\} \\exp\\left\\{-\\exp\\left[-\\frac{x-\\mu}{\\sigma}\\right]\\right\\} & \\text{for } \\xi = 0 \\\\
\\end{cases}
```
for
```math
x \\in \\begin{cases}
\\left[ \\mu - \\frac{\\sigma}{\\xi}, + \\infty \\right) & \\text{for } \\xi > 0 \\\\
\\left( - \\infty, + \\infty \\right) & \\text{for } \\xi = 0 \\\\
\\left( - \\infty, \\mu - \\frac{\\sigma}{\\xi} \\right] & \\text{for } \\xi < 0
\\end{cases}
```
```julia
GeneralizedExtremeValue(m, s, k) # Generalized Pareto distribution with shape k, scale s and location m.
params(d) # Get the parameters, i.e. (m, s, k)
location(d) # Get the location parameter, i.e. m
scale(d) # Get the scale parameter, i.e. s
shape(d) # Get the shape parameter, i.e. k (sometimes called c)
```
External links
* [Generalized extreme value distribution on Wikipedia](https://en.wikipedia.org/wiki/Generalized_extreme_value_distribution)
"""
struct GeneralizedExtremeValue{T<:Real} <: ContinuousUnivariateDistribution
μ::T
σ::T
ξ::T
function GeneralizedExtremeValue{T}(μ::T, σ::T, ξ::T) where T
σ > zero(σ) || error("Scale must be positive")
new{T}(μ, σ, ξ)
end
end
GeneralizedExtremeValue(μ::T, σ::T, ξ::T) where {T<:Real} = GeneralizedExtremeValue{T}(μ, σ, ξ)
GeneralizedExtremeValue(μ::Real, σ::Real, ξ::Real) = GeneralizedExtremeValue(promote(μ, σ, ξ)...)
function GeneralizedExtremeValue(μ::Integer, σ::Integer, ξ::Integer)
return GeneralizedExtremeValue(float(μ), float(σ), float(ξ))
end
#### Conversions
function convert(::Type{GeneralizedExtremeValue{T}}, μ::Real, σ::Real, ξ::Real) where T<:Real
GeneralizedExtremeValue(T(μ), T(σ), T(ξ))
end
function convert(::Type{GeneralizedExtremeValue{T}}, d::GeneralizedExtremeValue{S}) where {T <: Real, S <: Real}
GeneralizedExtremeValue(T(d.μ), T(d.σ), T(d.ξ))
end
minimum(d::GeneralizedExtremeValue{T}) where {T<:Real} =
d.ξ > 0 ? d.μ - d.σ / d.ξ : -T(Inf)
maximum(d::GeneralizedExtremeValue{T}) where {T<:Real} =
d.ξ < 0 ? d.μ - d.σ / d.ξ : T(Inf)
#### Parameters
shape(d::GeneralizedExtremeValue) = d.ξ
scale(d::GeneralizedExtremeValue) = d.σ
location(d::GeneralizedExtremeValue) = d.μ
params(d::GeneralizedExtremeValue) = (d.μ, d.σ, d.ξ)
@inline partype(d::GeneralizedExtremeValue{T}) where {T<:Real} = T
#### Statistics
testfd(d::GeneralizedExtremeValue) = d.ξ^3
g(d::GeneralizedExtremeValue, k::Real) = gamma(1 - k * d.ξ) # This should not be exported.
function median(d::GeneralizedExtremeValue)
(μ, σ, ξ) = params(d)
if abs(ξ) < eps() # ξ == 0
return μ - σ * log(log(2))
else
return μ + σ * (log(2) ^ (- ξ) - 1) / ξ
end
end
function mean(d::GeneralizedExtremeValue{T}) where T<:Real
(μ, σ, ξ) = params(d)
if abs(ξ) < eps(one(ξ)) # ξ == 0
return μ + σ * MathConstants.γ
elseif ξ < 1
return μ + σ * (gamma(1 - ξ) - 1) / ξ
else
return T(Inf)
end
end
function mode(d::GeneralizedExtremeValue)
(μ, σ, ξ) = params(d)
if abs(ξ) < eps(one(ξ)) # ξ == 0
return μ
else
return μ + σ * ((1 + ξ) ^ (-ξ) - 1) / ξ
end
end
function var(d::GeneralizedExtremeValue{T}) where T<:Real
(μ, σ, ξ) = params(d)
if abs(ξ) < eps(one(ξ)) # ξ == 0
return σ ^2 * π^2 / 6
elseif ξ < 1/2
return σ^2 * (g(d, 2) - g(d, 1) ^ 2) / ξ^2
else
return T(Inf)
end
end
function skewness(d::GeneralizedExtremeValue{T}) where T<:Real
(μ, σ, ξ) = params(d)
if abs(ξ) < eps(one(ξ)) # ξ == 0
return 12sqrt(6) * zeta(3) / pi ^ 3 * one(T)
elseif ξ < 1/3
g1 = g(d, 1)
g2 = g(d, 2)
g3 = g(d, 3)
return sign(ξ) * (g3 - 3g1 * g2 + 2g1^3) / (g2 - g1^2) ^ (3/2)
else
return T(Inf)
end
end
function kurtosis(d::GeneralizedExtremeValue{T}) where T<:Real
(μ, σ, ξ) = params(d)
if abs(ξ) < eps(one(ξ)) # ξ == 0
return T(12)/5
elseif ξ < 1 / 4
g1 = g(d, 1)
g2 = g(d, 2)
g3 = g(d, 3)
g4 = g(d, 4)
return (g4 - 4g1 * g3 + 6g2 * g1^2 - 3 * g1^4) / (g2 - g1^2)^2 - 3
else
return T(Inf)
end
end
function entropy(d::GeneralizedExtremeValue)
(μ, σ, ξ) = params(d)
return log(σ) + MathConstants.γ * ξ + (1 + MathConstants.γ)
end
function quantile(d::GeneralizedExtremeValue, p::Real)
(μ, σ, ξ) = params(d)
if abs(ξ) < eps(one(ξ)) # ξ == 0
return μ + σ * (-log(-log(p)))
else
return μ + σ * ((-log(p))^(-ξ) - 1) / ξ
end
end
#### Support
insupport(d::GeneralizedExtremeValue, x::Real) = minimum(d) <= x <= maximum(d)
#### Evaluation
function logpdf(d::GeneralizedExtremeValue{T}, x::Real) where T<:Real
if x == -Inf || x == Inf || ! insupport(d, x)
return -T(Inf)
else
(μ, σ, ξ) = params(d)
z = (x - μ) / σ # Normalise x.
if abs(ξ) < eps(one(ξ)) # ξ == 0
t = z
return -log(σ) - t - exp(-t)
else
if z * ξ == -1 # Otherwise, would compute zero to the power something.
return -T(Inf)
else
t = (1 + z * ξ) ^ (-1/ξ)
return - log(σ) + (ξ + 1) * log(t) - t
end
end
end
end
function pdf(d::GeneralizedExtremeValue{T}, x::Real) where T<:Real
if x == -Inf || x == Inf || ! insupport(d, x)
return zero(T)
else
(μ, σ, ξ) = params(d)
z = (x - μ) / σ # Normalise x.
if abs(ξ) < eps(one(ξ)) # ξ == 0
t = exp(-z)
return (t * exp(-t)) / σ
else
if z * ξ == -1 # In this case: zero to the power something.
return zero(T)
else
t = (1 + z*ξ)^(- 1 / ξ)
return (t^(ξ + 1) * exp(- t)) / σ
end
end
end
end
function logcdf(d::GeneralizedExtremeValue{T}, x::Real) where T<:Real
if insupport(d, x)
(μ, σ, ξ) = params(d)
z = (x - μ) / σ # Normalise x.
if abs(ξ) < eps(one(ξ)) # ξ == 0
return -exp(- z)
else
return - (1 + z * ξ) ^ ( -1/ξ)
end
elseif x <= minimum(d)
return -T(Inf)
else
return zero(T)
end
end
function cdf(d::GeneralizedExtremeValue{T}, x::Real) where T<:Real
if insupport(d, x)
(μ, σ, ξ) = params(d)
z = (x - μ) / σ # Normalise x.
if abs(ξ) < eps(one(ξ)) # ξ == 0
t = exp(-z)
else
t = (1 + z * ξ) ^ (-1/ξ)
end
return exp(- t)
elseif x <= minimum(d)
return zero(T)
else
return one(T)
end
end
logccdf(d::GeneralizedExtremeValue, x::Real) = log1p(- cdf(d, x))
ccdf(d::GeneralizedExtremeValue, x::Real) = - expm1(logcdf(d, x))
#### Sampling
function rand(rng::AbstractRNG, d::GeneralizedExtremeValue)
(μ, σ, ξ) = params(d)
# Generate a Float64 random number uniformly in (0,1].
u = 1 - rand(rng)
if abs(ξ) < eps(one(ξ)) # ξ == 0
rd = - log(- log(u))
else
rd = expm1(- ξ * log(- log(u))) / ξ
end
return μ + σ * rd
end