forked from grothered/Flood_freq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gev.R
115 lines (101 loc) · 3.27 KB
/
gev.R
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
## All this code is slightly adapted from fExtremes, to remove the
## log=FALSE bug in dgev, and so I can distribute gev code without the
## many dependencies of fExtremes
dgev<-function(x, location=0, scale=1, shape = 1, log = FALSE){
# Adapted from .devd in fExtremes
stopifnot(min(scale) > 0)
stopifnot(length(shape) == 1)
x = (x - location)/scale
if (shape == 0) {
d = log(1/scale) - x - exp(-x)
}
else {
nn = length(x)
xx = 1 + shape * x
xxpos = xx[xx > 0 | is.na(xx)]
scale = rep(scale, length.out = nn)[xx > 0 | is.na(xx)]
d = numeric(nn)
d[xx > 0 | is.na(xx)] = log(1/scale) - xxpos^(-1/shape) -
(1/shape + 1) * log(xxpos)
d[xx <= 0 & !is.na(xx)] = -Inf
}
if (!log) {
d = exp(d)
}
#attr(d, "control") = data.frame(location = location[1], scale = scale[1],
# shape = shape[1], log = log, row.names = "")
return(d)
}
pgev<-function (q, location = 0, scale = 1, shape = 0, lower.tail = TRUE){
# Adapted from .pevd in fExtremes
stopifnot(min(scale) > 0)
stopifnot(length(shape) == 1)
q = (q - location)/scale
if (shape == 0) {
p = exp(-exp(-q))
}
else {
p = exp(-pmax(1 + shape * q, 0)^(-1/shape))
}
if (!lower.tail) {
p = 1 - p
}
return(p)
}
qgev<-function (p, location = 0, scale = 1, shape = 0, lower.tail = TRUE){
# Adapted directly from .qevd in fExtremes
stopifnot(min(scale) > 0)
stopifnot(length(shape) == 1)
stopifnot(min(p, na.rm = TRUE) >= 0)
stopifnot(max(p, na.rm = TRUE) <= 1)
if (!lower.tail)
p = 1 - p
if (shape == 0) {
q = location - scale * log(-log(p))
}
else {
q = location + scale * ((-log(p))^(-shape) - 1)/shape
}
#attr(q, "control") = data.frame(location = location[1], scale = scale[1],
# shape = shape[1], lower.tail, row.names = "")
return(q)
}
rgev<-function (n, location = 0, scale = 1, shape = 0){
# Adapted from .revd in fExtremes package
stopifnot(min(scale) > 0)
stopifnot(length(shape) == 1)
if (shape == 0) {
r = location - scale * log(rexp(n))
}
else {
r = location + scale * (rexp(n)^(-shape) - 1)/shape
}
#attr(r, "control") = data.frame(location = location[1], scale = scale[1],
# shape = shape[1], row.names = "")
return(r)
}
#############################
test_gev_code<-function(){
x=seq(0,1000,len=100)
p=seq(0,1,len=101)
#library(fExtremes) # Should be identical to this
xd=dgev(x, shape=0.2, location=200,scale=100)
xq=qgev(p, shape=0.2, location=200,scale=100)
xp=pgev(x, shape=0.2, location=200,scale=100)
set.seed(1)
xr=rgev(100, shape=0.2, location=200,scale=100)
library(fExtremes)
yd=fExtremes::dgev(x,xi=0.2,mu=200,beta=100)
stopifnot(all(xd==yd))
print('PASS - same as dgev fExtremes')
yq=fExtremes::qgev(p,xi=0.2,mu=200,beta=100)
stopifnot(all(xq==yq))
print('PASS - same as qgev fExtremes')
yp=fExtremes::pgev(x,xi=0.2,mu=200,beta=100)
stopifnot(all(xp==yp))
print('PASS - same as pgev fExtremes')
set.seed(1)
yr=fExtremes::rgev(100,xi=0.2,mu=200,beta=100)
stopifnot(all(xr==yr))
print('PASS - same as rgev fExtremes')
}