-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneralized_extreme_value_function.R
More file actions
executable file
·71 lines (69 loc) · 2.21 KB
/
generalized_extreme_value_function.R
File metadata and controls
executable file
·71 lines (69 loc) · 2.21 KB
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
# generalized_extreme_value_function.R - Bill White - 2/27/19
#
# https://www.wikiwand.com/en/Generalized_extreme_value_distribution
#
# Parameters:
# x_s - vector of numerics to evaluate (x values)
# m_s - vector of numerics -location - real
# s_s - vector of numerics - scale > 0
# xi_s - vector of numerics - shape - real
# * a family of continuous probability distributions developed within
# extreme value theory to combine the Gumbel, Fréchet and Weibull families
# also known as type I, II and III extreme value distributions
# * often used as an approximation to model the maxima of long (finite)
# sequences of random variables
# * known as the Fisher–Tippett distribution
# t(x) function needed by amstat_generalized_extreme_value
t_f <- function(x, m, s, xi) {
result <- 0
std_x <- (x - m) / s
if (xi == 0) {
result <- exp(-std_x)
} else {
result <- (1 + ((xi * std_x))) ^ (-1 / xi)
}
result
}
amstat_generalized_extreme_value <- function(x_s, m_s, s_s, xi_s) {
x_results <- lapply(x_s, function(x) {
m_results <- lapply(m_s, function(m) {
s_results <- lapply(s_s, function(s) {
xi_results <- lapply(xi_s, function(xi) {
valid_x <- TRUE
cutoff <- (m - s) / xi
valid_x_value <- x
if (xi > 0) {
if (x <= cutoff) {
valid_x <- FALSE
valid_x_value <- cutoff
}
} else {
if (xi < 0) {
if (x >= cutoff) {
valid_x <- FALSE
valid_x_value <- cutoff
}
}
}
# was a new x needed to deal with the constraints?
if (valid_x) {
y <- (1 / s) *
(t_f(valid_x_value, m, s, xi) ^ (xi + 1)) *
exp(-t_f(valid_x_value, m, s, xi))
} else {
valid_x_value <- cutoff
y <- 0
}
data.frame(x = valid_x_value, y = y,
Parameters =
sprintf("m=%4.2f s=%4.2f xi=%4.2f",
m, s, xi))
})
do.call(rbind, xi_results)
})
do.call(rbind, s_results)
})
do.call(rbind, m_results)
})
do.call(rbind, x_results)
}