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
|
#############################################################################
## Copyright (c) 2010-2022 Rune Haubo Bojesen Christensen
##
## This file is part of the ordinal package for R (*ordinal*)
##
## *ordinal* is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
##
## *ordinal* is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## A copy of the GNU General Public License is available at
## <https://www.r-project.org/Licenses/> and/or
## <http://www.gnu.org/licenses/>.
#############################################################################
## This file contains:
## [pdqrg]gumbel functions for the gumbel distribution.
## Here ggumbel is the gradient of the density function, dgumbel.
pgumbel <-
function(q, location = 0, scale = 1, lower.tail = TRUE, max = TRUE)
### CDF for Gumbel max and min distributions
### Currently only unit length location and scale are supported.
{
if(max) ## right skew, loglog link
.C("pgumbel_C",
q = as.double(q),
length(q),
as.double(location)[1],
as.double(scale)[1],
as.integer(lower.tail),
NAOK = TRUE)$q
else ## left skew, cloglog link
.C("pgumbel2_C",
q = as.double(q),
length(q),
as.double(location)[1],
as.double(scale)[1],
as.integer(lower.tail),
NAOK = TRUE)$q
}
pgumbelR <- function(q, location = 0, scale = 1, lower.tail = TRUE)
### R equivalent of pgumbel()
{
q <- (q - location)/scale
p <- exp(-exp(-q))
if (!lower.tail) 1 - p else p
}
pgumbel2R <- function(q, location = 0, scale = 1, lower.tail = TRUE)
{
q <- (-q - location)/scale
p <- exp(-exp(-q))
if (!lower.tail) p else 1 - p
}
dgumbel <-
function(x, location = 0, scale = 1, log = FALSE, max = TRUE)
### PDF for the Gumbel max and mon distributions
{
if(max) ## right skew, loglog link
.C("dgumbel_C",
x = as.double(x),
length(x),
as.double(location)[1],
as.double(scale)[1],
as.integer(log),
NAOK = TRUE)$x
else ## left skew, cloglog link
.C("dgumbel2_C",
x = as.double(x),
length(x),
as.double(location)[1],
as.double(scale)[1],
as.integer(log),
NAOK = TRUE)$x
}
dgumbelR <- function(x, location = 0, scale = 1, log = FALSE)
### dgumbel in R
{
q <- (x - location)/scale
log.d <- -exp(-q) - q - log(scale)
if (!log) exp(log.d) else log.d
}
dgumbel2R <- function(x, location = 0, scale = 1, log = FALSE)
{
q <- (-x - location)/scale
log.d <- -exp(-q) - q - log(scale)
if (!log) exp(log.d) else log.d
}
ggumbel <- function(x, max = TRUE) {
### gradient of dgumbel(x) wrt. x
if(max) ## right skew, loglog link
.C("ggumbel_C",
x = as.double(x),
length(x),
NAOK = TRUE)$x
else ## left skew, cloglog link
.C("ggumbel2_C",
x = as.double(x),
length(x),
NAOK = TRUE)$x
}
ggumbelR <- function(x){
### ggumbel in R
q <- exp(-x)
ifelse(q == Inf, 0, {
eq <- exp(-q)
-eq*q + eq*q*q
})
}
ggumbel2R <- function(x) -ggumbelR(-x)
rgumbel <- function(n, location = 0, scale = 1, max = TRUE) {
if(max)
location - scale * log(-log(runif(n)))
else
location + scale * log(-log(runif(n)))
}
qgumbel <- function(p, location = 0, scale = 1, lower.tail = TRUE, max = TRUE) {
if(!lower.tail) p <- 1 - p
if(max) ## right skew, loglog link
location - scale * log(-log(p))
else ## left skew, cloglog link
location + scale * log(-log(1 - p))
}
|