forked from grothered/Flood_freq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpd.R
115 lines (99 loc) · 3.17 KB
/
gpd.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
## so I can distribute gpd code without the
## many dependencies of fExtremes
dgpd<-function (x, location = 0, scale = 1, shape = 0, log = FALSE)
{
stopifnot(min(scale) > 0)
stopifnot(length(shape) == 1)
d <- (x - location)/scale
nn <- length(d)
scale <- rep(scale, length.out = nn)
index <- (d > 0 & ((1 + shape * d) > 0)) | is.na(d)
if (shape == 0) {
d[index] <- log(1/scale[index]) - d[index]
d[!index] <- -Inf
}
else {
d[index] <- log(1/scale[index]) - (1/shape + 1) * log(1 +
shape * d[index])
d[!index] <- -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)
}
qgpd<-function (p, location = 0, scale = 1, shape = 0, lower.tail = TRUE){
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(p)
}
else {
q = location + scale * (p^(-shape) - 1)/shape
}
#attr(q, "control") = data.frame(location = location[1], scale = scale[1],
# shape = shape[1], lower.tail = lower.tail, row.names = "")
return(q)
}
pgpd<-function (q, location = 0, scale = 1, shape = 0, lower.tail = TRUE)
{
stopifnot(min(scale) > 0)
stopifnot(length(shape) == 1)
q <- pmax(q - location, 0)/scale
if (shape == 0)
p <- 1 - exp(-q)
else {
p <- pmax(1 + shape * q, 0)
p <- 1 - p^(-1/shape)
}
if (!lower.tail)
p <- 1 - p
#attr(p, "control") = data.frame(location = location[1], scale = scale[1],
# shape = shape[1], lower.tail = lower.tail, row.names = "")
return(p)
}
rgpd<-function (n, location = 0, scale = 1, shape = 0)
{
stopifnot(min(scale) > 0)
stopifnot(length(shape) == 1)
if (shape == 0) {
r = location + scale * rexp(n)
}
else {
r = location + scale * (runif(n)^(-shape) - 1)/shape
}
attr(r, "control") = data.frame(location = location[1], scale = scale[1],
shape = shape[1], row.names = "")
return(r)
}
#############################
test_gpd_code<-function(){
x=seq(0,1000,len=100)
p=seq(0,1,len=101)
#library(fExtremes) # Should be identical to this
xd=dgpd(x, shape=0.2, location=200,scale=100)
xq=qgpd(p, shape=0.2, location=200,scale=100)
xp=pgpd(x, shape=0.2, location=200,scale=100)
set.seed(1)
xr=rgpd(100, shape=0.2, location=200,scale=100)
library(fExtremes)
yd=fExtremes::dgpd(x,xi=0.2,mu=200,beta=100)
stopifnot(all(xd==yd))
print('PASS - same as dgpd fExtremes')
yq=fExtremes::qgpd(p,xi=0.2,mu=200,beta=100)
stopifnot(all(xq==yq))
print('PASS - same as qgpd fExtremes')
yp=fExtremes::pgpd(x,xi=0.2,mu=200,beta=100)
stopifnot(all(xp==yp))
print('PASS - same as pgpd fExtremes')
set.seed(1)
yr=fExtremes::rgpd(100,xi=0.2,mu=200,beta=100)
stopifnot(all(xr==yr))
print('PASS - same as rgpd fExtremes')
}